Solidity is a language developed with the aim to make writing of smart contracts easy for developers. The language after being compiled to byte code can then be deployed and executed by the Ethereum Virtual Machine (EVM). One key feature of the language is transerring of ether (Ethereum's native currency) from one account to another. Send, transfer anc call can be used to achieve this, but what are the key differences between them and why is th call function unique? Let's see why.
Send:
The Send function came out first, it was used to send ether from one account to another, tho it's deprecated, it served as a simple function enabling a contract to tranfer ether from one account to another. The transfer function returns a boolean of true if successful, false if not successful. It uses 2300 gas.
bool isSent = <address to transfer to>.send(msg.value);
require(sent, "Transfer failed");
Transfer:
The transfer function is still in use, it sends ether from one account to another but throws an error if the transfer is not successful. This is done in case the user does not handle situations that the transfer is not successful. This function also uses 2300 gas and can reduce the possibilities of reentrancy attacks. Example of how a transfer function looks like:
<address to transfer to>.transfer(msg.value);
Call:
The call function is the recommended function to use to transfer ether from one account to another. Once a call for transferring ether is successful, a boolean of true is returned, else a false statement is sent. One thing unique about the call function is that it can be used to call another function in the smart contract it's interacting with and can send a fixed amount of gas to execute the function.
(bool sent, memory data) = <address to transfer to>.call{value: msg.value}("");
// Call with a function to execute
(bool sent, memory data) = <address to transfer to>.call{gas: 1000, value: msg.value}("functionSignature()");
Top comments (0)