Have you ever tried to code a smart contract that can hold CET?also it can send CET to another!
In this tutorial we will talk about Send, Transfer and Call in solidiy. stay with me 😉
Transferring Methods
Solidity supports several method to sending native coin to another contract/wallet.
You can send native coin to other contracts by
transfer
(2300 gas, throws error)send
(2300 gas, returns bool)call
(forward all gas or set gas, returns bool)
also a contract receiving native coin 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.
call
In combination with re-entrancy guard is the recommended method to use after December 2019.
Guard against re-entrancy by
- making all state changes before calling other contracts
- using re-entrancy guard modifier
transfer vs send vs call
transfer:the receiving smart contract should have a fallback function defined or else the transfer call will throw an error. There is a gas limit of 2300 gas, which is enough to complete the transfer operation. It is hard coded to prevent re-entrance attacks.
send :It works in a similar way as to transfer call and has a gas limit of 2300 gas as well. It returns the status as a boolean.
call :It is the recommended way of sending native coin to a smart contract. The empty argument triggers the fallback function of the receiving address.
(bool sent,memory data) = _to.call{value: msg.value}("");
using callone can also trigger other functions defined in the contract and send a fixed amount of gas to execute the function. The transaction status is sent as a boolean and the return value is sent in the data variable.
(bool sent, bytes memory data) = _to.call{gas :10000, value: msg.value}("func_signature(uint256 args)");
Also there are some methods ( depending on the situation ) for transferring coin to other contract/wallet.
address.send(amount)
First and the most important characteristic is providing 2300 gas limit for execution the fallback function of a contract receiving ether. By the way, this quantity will be sufficient only to create one event.
contract Sender {
function send(address _receiver) payable {
_receiver.send(msg.value);
}
}contract Receiver {
uint public balance = 0;
event Receive(uint value);function () payable {
Receive(msg.value);
}
}
unsuccessful execution of send()
for example out of gas error returns falsebut does not throw an exception. Hence, each usage of send()
should be inside of require. Otherwise you will pay for gas to proceed a transaction submission in the blockchain, but all the state changes will be undone.
For example, it only takes to change a bit the payable function in the Receiver
contract:
function () payable {
Receive(msg.value);
balance += msg.value;
}
address.transfer(amount)
Let us consider a method that appeared in the later versions of solidity. It has two details worth mentioning as well.
First, this method has the same 2300 gas limit. However during the development process of these features the developers discussed an opportunity to add .gas()
modifier, which redefines the limit of the provided gas.
Second, unlike send()
method, transfer()
throws exception when performed unsuccessfully.
address.call.value(amount)( )
The last and most customized method.
The given function still returns false in case an error occurs, that is why keep the usage of require()
in mind.
Its principal difference from the two previous functions is an opportunity to set gas limit via .gas(gasLimit)
modifier. It is necessary in case the payable function of the contract receiving ether performs a complex logic, that requires plenty of gas.
Let us take a look at the following example:
contract Sender {
function send(address _receiver) payable {
_receiver.call.value(msg.value).gas(20317)();
}
}contract Receiver {
uint public balance = 0;function () payable {
balance += msg.value;
}
}
Execution of the payable function in the Receiver contract costs more than 20 thousand gas. That is why, usage of send()
or transfer()
would cause an out of gas error.
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing