[ad_1]
- The transfer function
transfer()
is a method used for sending Ether and is available in Solidity for transferring funds between contracts or to external addresses.
It’s a safer way to send Ether as it automatically reverts the transaction if it runs out of gas or encounters any other error during the transfer. The gas used for transfer()
is maxed to 23000
Read more on this here
Use this method whenever you want to send Ether with a fixed gas limit. Let’s see a withdraw()
utilising transfer()
function withdraw() public {
require(msg.sender == owner, "Only callable by owner");
payable(msg.sender.transfer(address(this).balance);
}
2. The send function
Similarly to transfer()
, send()
has a max gas limit of 23000
, but returns a boolean value indicating success or failure.send()
does not revert by itself if an error happens, instead, it returns false!
This method is less commonly used due to the lack of automatic error handling, which could result in unexpected behavior if not handled properly. Here is how you would use send()
inside withdraw()
function withdraw() public {
require(msg.sender == owner, "Only callable by owner");
bool sendSuccess = payable(msg.sender).send(address(this).balance);
require(sendSuccess, "Send failed!");
}
Notice the require()
statement checking for sendSuccess
. Without checking back, a failed transaction would not revert!
3. The call function — The recommended way!
call()
is a low-level method used to send Ether and interact with external contracts.
Its main advantage is that it allows to specify gas! By default, it will “…forward all gas”. It also allows sending additional data along with the value of the transaction when, f.e. interacting with another contract!
Similar to send()
, it returns a boolean value to indicate the success or failure of the call, as well as any data that comes back as a possible return value of any function call. Read more about call()
here
Here is our withdraw()
using call()
function withdraw() public {
require(msg.sender == owner, "Only callable by owner");(bool callSuccess, data returnedData) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed!");
}
Notice (bool callSuccess, data returnedData) = ...
, here we destructure the 2 return values of call()
The first return value is the boolean indicating the success status, and the second one is optional callback data. You could skip returnedData
and end up with
function withdraw() public {
require(msg.sender == owner, "Only callable by owner");(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed!");
}
Note though, that you have to put the comma, to let the compiler know, that you are aware of the second return value, but intentionally omit it.
Further, take a close look at ...call{value: address(this).balance}("")
Inside {}
we can pass along f.e. the value
of the call, and here you would also specify the amount of gas
you want to send along. Inside ()
we specify ""
because we do not want to call any specific function here when sending funds only, but inside here, you would specify the function you want to call when interacting with a contract!
This ability makes call()
a low-level
way of doing things, as it gives you more control over the specifics of the execution!
Don’t hesitate to head over to Remix IDE and give this a try! Also, I highly recommend following Patrick Collin’s YouTube course if you want to level up your blockchain development skills, his course is insanely good and 100% FREE as well!!
I hope this could shed some light on how to send Ether in Solidity. For further deepening of the topic, I highly recommend reading up on the Solidity by Example website.
Let me know in the comments how you liked the article and what other topics you are interested in.
[ad_2]
Source link