header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity|Lecture 20: Send ETH transfer, send and call

Read this article in 8 Minutes
This article introduces three methods of sending ETH in solidity: transfer, send and call.
Original title: " A minimalist introduction to Solidity: 20. Send ETH transfer, send and call 》
Original author: 0xAA   


I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist "Getting Started" is for novices (programmers can find other tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


Solidity There are three ways to send ETH to other contracts, they are: transfer(), send() and call(), where call() is the encouraged usage.


Receive ETH contract


We first deploy a receive ETH contract ReceiveETH. There is an event Log in the ReceiveETH contract, which records the amount of ETH received and the remaining gas. There are also two functions, one is receive(), which is triggered when ETH is received, and a Log event is sent; the other is getBalance() for querying the ETH balance of the contract.


< p style="text-align:center;">


After deploying the contract, run the getBalance() function to see the balance of the current contract ETH balance is 0.


Send ETH contract


We will implement three methods to ReceiveETH The contract sends ETH. First, implement the payable constructor and receive() in the sending ETH contract SendETH, so that we can transfer money to the contract during and after deployment.



transfer 


- usage is transfer (send ETH amount).


- The gas limit of transfer() is 2300, enough for Transfer, but the fallback() or receive() function of the other contract cannot implement too complicated logic.


- transfer() If the transfer fails, it will automatically revert (back roll transaction).

Code sample, pay attention to the _to in the parameter to fill in the address of the ReceiveETH contract, and the amount is the ETH transfer amount:


< p style="text-align:center;">


send  ;


- The usage is send (send ETH amount).


- The gas limit of send() is 2300, enough for Transfer, but the fallback() or receive() function of the other contract cannot implement too complicated logic.


- send() If the transfer fails, it will not revert.


- The return value of send() is bool, which represents the transfer Success or failure requires additional code to handle it.


Code sample:



call 


- usage is call{value: send ETH amount}("").


- call() has no gas limit and can support the other party’s contract fallback () or receive() functions to implement complex logic.


- call() If the transfer fails, it will not revert.


- The return value of call() is (bool, data) , where bool represents the success or failure of the transfer, and additional code is required to process it.


Code sample:



Run the three methods, you can see that they can all successfully send ETH to the ReceiveETH contract.


Summary


In this lecture, we introduce three methods of sending ETH in solidity: transfer , send and call.


call has no gas limit, is the most flexible, and is the most advocated method; transfer has a 2300 gas limit, but if the send fails, it will automatically revert the transaction, which is the second best choice; send has a gas limit of 2300, and the send failure will not automatically revert the transaction, almost no one uses it.   


Original text link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit