Focus mode

Smart Contract Development with Solidity

Send ETH

Code from video: https://solidity-by-example.org/sending-ether/ 

This video explains 3 ways to send ETH in Solidity: transfer, send and call.

A contract receiving Ether must have at least one of the functions below:

  • receive() external payable
  • fallback() external payable

receive() is called if msg.data is empty, otherwise fallback() is called. If neither a receive Ether nor a payable fallback function is present, the contract cannot receive Ether through regular transactions and throws an exception.

  • transfer: There is a gas limit of 2300 gas and it throws an error if there isn’t any receive and fallback functions. It is hardcoded to prevent reentrancy attacks.
  • send: There is a gas limit of 2300 gas and it returns a bool if there isn’t any receive and fallback functions.
  • call: It is the recommended way of transferring ETH, because we can set the amount of gas to be sent. It also returns a bool if there isn’t any receive and fallback functions. However, you should be extra careful when using “call” in a contract since it allows reentrancy attacks. If the sender contract is improperly coded, it can result in draining larger amounts of funds from it than planned. 

Resources:

You can find detailed further information from Solidity Official documentation (in English):

https://docs.soliditylang.org/en/v0.8.14/contracts.html#receive-ether-function 

https://docs.soliditylang.org/en/v0.8.14/contracts.html#fallback-function 

https://medium.com/coinmonks/solidity-transfer-vs-send-vs-call-function-64c92cfc878a

https://coinsbench.com/solidity-how-to-send-ether-transfer-send-call-and-fallback-%EF%B8%8F-c470c836fdf1


Comments

You need to enroll in the course to be able to comment!