You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Codes and tutorials are open source on GitHub: [github.com/AmazingAng/WTF-Solidity](https://github.com/AmazingAng/WTF-Solidity)
19
19
20
20
-----
21
-
There are three ways of sending `ETH` in `Solidity`: `transfer()`, `send()` and `call()`, in which `call()` is recommended.
21
+
There are three ways of sending `ETH` in `Solidity`: `transfer()`, `send()` and `call()`, in which `call()` is recommended.
22
22
23
23
## Contract of Receiving ETH
24
+
24
25
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.
25
26
```solidity
26
27
contract ReceiveETH {
@@ -39,12 +40,13 @@ contract ReceiveETH {
39
40
}
40
41
```
41
42
42
-
After deploying `ReceiveETH`, call the `getBalance()` function, we can see the balance is `0 Ether`.
43
+
After deploying `ReceiveETH`, call the `getBalance()` function, we can see the balance is `0 Ether`.
43
44
44
45

45
46
46
47
## Contract of Sending ETH
47
48
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.
49
+
48
50
```solidity
49
51
contract SendETH {
50
52
// constructor, make it payable so we can transfer ETH at deployment
@@ -53,39 +55,43 @@ contract SendETH {
53
55
receive() external payable{}
54
56
}
55
57
```
58
+
56
59
### transfer
60
+
57
61
- Usage: `receiverAddress.transfer(value in Wei)`.
58
62
- 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()`.
59
-
- If `transfer()` fails, the transaction will `revert`.
63
+
- If `transfer()` fails, the transaction will `revert`.
60
64
61
65
Sample code: note that `_to` is the address of the `ReceiveETH` contract, and `amount` is the value you want to send.
66
+
62
67
```solidity
63
68
// sending ETH with transfer()
64
69
function transferETH(address payable _to, uint256 amount) external payable{
65
70
_to.transfer(amount);
66
71
}
67
72
```
68
73
69
-
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`.
74
+
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`.
70
75
71
76

72
77
73
-
If `amount` is 10, and `value` is 10, `amount`<=`value`, then the transaction will go through.
78
+
If `amount` is 10, and `value` is 10, `amount`<=`value`, then the transaction will go through.
74
79
75
80

76
81
77
-
In the `ReceiveETH` contract, when we call `getBalance()`, we can see the balance of the contract is `10` Wei.
82
+
In the `ReceiveETH` contract, when we call `getBalance()`, we can see the balance of the contract is `10` Wei.
78
83
79
84

80
85
81
86
### send
82
87
83
-
- Usage: `receiverAddress.send(value in Wei)`.
84
-
- 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()`.
85
-
- If `send()` fails, the transaction will not be `reverted`.
86
-
- The return value of `send()` is `bool`, which is the status of the transaction, you can choose to act on that.
88
+
- Usage: `receiverAddress.send(value in Wei)`.
89
+
- 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()`.
90
+
- If `send()` fails, the transaction will not be `reverted`.
91
+
- The return value of `send()` is `bool`, which is the status of the transaction, you can choose to act on that.
87
92
88
93
Sample Code:
94
+
89
95
```solidity
90
96
// sending ETH with send()
91
97
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`.
106
+
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`.
101
107
102
108

103
109
104
-
If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful.
110
+
If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful.
105
111
106
112

107
113
108
114
### call
109
115
110
-
- Usage: `receiverAddress.call{value: value in Wei}("")`.
111
-
- There is no `gas` limit for `call()`, so it supports more operations in `fallback()` or `receive()` of the receiving contract.
112
-
- If `call()` fails, the transaction will not be `reverted`.
113
-
- The return value of `call()` is `(bool, data)`, in which `bool` is the status of the transaction, you can choose to act on that.
116
+
- Usage: `receiverAddress.call{value: value in Wei}("")`.
117
+
- There is no `gas` limit for `call()`, so it supports more operations in `fallback()` or `receive()` of the receiving contract.
118
+
- If `call()` fails, the transaction will not be `reverted`.
119
+
- The return value of `call()` is `(bool, data)`, in which `bool` is the status of the transaction, you can choose to act on that.
114
120
115
121
Sample Code:
122
+
116
123
```solidity
117
124
// sending ETH with call()
118
125
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`.
134
+
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`.
128
135
129
136

130
137
131
-
If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful.
138
+
If `amount` is 10, and `value` is 11, `amount`<=`value`, the transaction is successful.
132
139
133
140

134
141
135
142
With any of these three methods, we send `ETH` to the `ReceiveETH` contract successfully.
136
143
137
144
## Summary
145
+
138
146
In this tutorial, we talked about three ways of sending `ETH` in `solidity`:
139
-
`transfer`, `send` and `call`.
147
+
`transfer`, `send` and `call`.
148
+
140
149
- There is no `gas` limit for `call`, which is the most flexible and recommended way;
141
150
- The `gas` limit of `transfer` is `2300 gas`, transaction will be `reverted` if it fails, which makes it the second choice;
142
-
- The `gas` limit of `send` is `2300`, the transaction will not be `reverted` if it fails, which makes it the worst choice.
143
-
144
-
145
-
151
+
- The `gas` limit of `send` is `2300`, the transaction will not be `reverted` if it fails, which makes it the worst choice.
0 commit comments