diff --git a/Languages/en/20_SendETH_en/SendETH.sol b/Languages/en/20_SendETH_en/SendETH.sol index c1d9dab2..8ea7bffa 100644 --- a/Languages/en/20_SendETH_en/SendETH.sol +++ b/Languages/en/20_SendETH_en/SendETH.sol @@ -7,7 +7,7 @@ pragma solidity ^0.8.21; // call: all gas, return (bool, data) error SendFailed(); // error when sending with Send -error CallFailed(); // error when seding with Call +error CallFailed(); // error when sending with Call contract SendETH { // Constructor, make it payable so we can transfer ETH at deployment diff --git a/Languages/en/20_SendETH_en/readme.md b/Languages/en/20_SendETH_en/readme.md index 4d6c666a..ba04e141 100644 --- a/Languages/en/20_SendETH_en/readme.md +++ b/Languages/en/20_SendETH_en/readme.md @@ -9,7 +9,7 @@ tags: # WTF Solidity Tutorial: 20. Sending ETH -Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies. +Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies. Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | [@WTFAcademy_](https://twitter.com/WTFAcademy_) @@ -18,9 +18,10 @@ Community: [Discord](https://discord.gg/5akcruXrsk)|[Wechat](https://docs.goog Codes and tutorials are open source on GitHub: [github.com/AmazingAng/WTF-Solidity](https://github.com/AmazingAng/WTF-Solidity) ----- -There are three ways of sending `ETH` in `Solidity`: `transfer()`, `send()` and `call()`, in which `call()` is recommended. +There are three ways of sending `ETH` in `Solidity`: `transfer()`, `send()` and `call()`, in which `call()` is recommended. ## Contract of Receiving ETH + Let's deploy a contract `ReceiveETH` to receive `ETH`. `ReceiveETH` has an event `Log`, which logs the received `ETH` amount and the remaining `gas`. Along with two other functions, one is the `receive()` function, which is executed when receiving `ETH`, and emits the `Log` event; the other is the `getBalance()` function that is used to get the balance of the contract. ```solidity contract ReceiveETH { @@ -39,12 +40,13 @@ contract ReceiveETH { } ``` -After deploying `ReceiveETH`, call the `getBalance()` function, we can see the balance is `0 Ether`. +After deploying `ReceiveETH`, call the `getBalance()` function, we can see the balance is `0 Ether`. ![20-1](./img/20-1.png) ## Contract of Sending ETH We will implement three ways to send `ETH` to the `ReceiveETH` contract. First thing first, let's make the `constructor` of the `SendETH` contract `payable`, and add the `receive()` function, so we can transfer `ETH` to our contract at deployment and after. + ```solidity contract SendETH { // constructor, make it payable so we can transfer ETH at deployment @@ -53,12 +55,15 @@ contract SendETH { receive() external payable{} } ``` + ### transfer + - Usage: `receiverAddress.transfer(value in Wei)`. - The `gas` limit of `transfer()` is `2300`, which is enough to make the transfer, but not if the receiving contract has a gas-consuming `fallback()` or `receive()`. -- If `transfer()` fails, the transaction will `revert`. +- If `transfer()` fails, the transaction will `revert`. Sample code: note that `_to` is the address of the `ReceiveETH` contract, and `amount` is the value you want to send. + ```solidity // sending ETH with transfer() function transferETH(address payable _to, uint256 amount) external payable{ @@ -66,26 +71,27 @@ function transferETH(address payable _to, uint256 amount) external payable{ } ``` -After deploying the `SendETH` contract, we can send `ETH` to the `ReceiveETH` contract. If `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails and gets `reverted`. +After deploying the `SendETH` contract, we can send `ETH` to the `ReceiveETH` contract. If `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails and gets `reverted`. ![20-2](./img/20-2.png) -If `amount` is 10, and `value` is 10, `amount`<=`value`, then the transaction will go through. +If `amount` is 10, and `value` is 10, `amount`<=`value`, then the transaction will go through. ![20-3](./img/20-3.png) -In the `ReceiveETH` contract, when we call `getBalance()`, we can see the balance of the contract is `10` Wei. +In the `ReceiveETH` contract, when we call `getBalance()`, we can see the balance of the contract is `10` Wei. ![20-4](./img/20-4.png) ### send -- Usage: `receiverAddress.send(value in Wei)`. -- The `gas` limit of `send()` is `2300`, which is enough to make the transfer, but not if the receiving contract has a gas-consuming `fallback()` or `receive()`. -- If `send()` fails, the transaction will not be `reverted`. -- The return value of `send()` is `bool`, which is the status of the transaction, you can choose to act on that. +- Usage: `receiverAddress.send(value in Wei)`. +- The `gas` limit of `send()` is `2300`, which is enough to make the transfer, but not if the receiving contract has a gas-consuming `fallback()` or `receive()`. +- If `send()` fails, the transaction will not be `reverted`. +- The return value of `send()` is `bool`, which is the status of the transaction, you can choose to act on that. Sample Code: + ```solidity // sending ETH with send() function sendETH(address payable _to, uint256 amount) external payable{ @@ -97,22 +103,23 @@ function sendETH(address payable _to, uint256 amount) external payable{ } ``` -Now we send `ETH` to the `ReceiveETH` contract, if `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails, since we handled the return value, the transaction will be `reverted`. +Now we send `ETH` to the `ReceiveETH` contract, if `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails, since we handled the return value, the transaction will be `reverted`. ![20-5](./img/20-5.png) -If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful. +If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful. ![20-6](./img/20-6.png) ### call -- Usage: `receiverAddress.call{value: value in Wei}("")`. -- There is no `gas` limit for `call()`, so it supports more operations in `fallback()` or `receive()` of the receiving contract. -- If `call()` fails, the transaction will not be `reverted`. -- The return value of `call()` is `(bool, data)`, in which `bool` is the status of the transaction, you can choose to act on that. +- Usage: `receiverAddress.call{value: value in Wei}("")`. +- There is no `gas` limit for `call()`, so it supports more operations in `fallback()` or `receive()` of the receiving contract. +- If `call()` fails, the transaction will not be `reverted`. +- The return value of `call()` is `(bool, data)`, in which `bool` is the status of the transaction, you can choose to act on that. Sample Code: + ```solidity // sending ETH with call() function callETH(address payable _to, uint256 amount) external payable{ @@ -124,22 +131,21 @@ function callETH(address payable _to, uint256 amount) external payable{ } ``` -Now we send `ETH` to the `ReceiveETH` contract, if `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails, since we handled the return value, the transaction will be `reverted`. +Now we send `ETH` to the `ReceiveETH` contract, if `amount` is 10, and `value` is 0, `amount`>`value`, the transaction fails, since we handled the return value, the transaction will be `reverted`. ![20-7](./img/20-7.png) -If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful. +If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful. ![20-8](./img/20-8.png) With any of these three methods, we send `ETH` to the `ReceiveETH` contract successfully. ## Summary + In this tutorial, we talked about three ways of sending `ETH` in `solidity`: -`transfer`, `send` and `call`. +`transfer`, `send` and `call`. + - There is no `gas` limit for `call`, which is the most flexible and recommended way; - The `gas` limit of `transfer` is `2300 gas`, transaction will be `reverted` if it fails, which makes it the second choice; -- The `gas` limit of `send` is `2300`, the transaction will not be `reverted` if it fails, which makes it the worst choice. - - - +- The `gas` limit of `send` is `2300`, the transaction will not be `reverted` if it fails, which makes it the worst choice.