Transferring Ether from a Smart Contract to a Payable Address

Photo by Kanchanara on Unsplash

Transferring Ether from a Smart Contract to a Payable Address

A Guide to Different Methods

ยท

3 min read

Solidity is a programming language for writing smart contracts on the Ethereum blockchain. If you're new to solidity and smart contracts, I wrote an article to get you started, you can check it out here

One of the most common tasks in a smart contract is to transfer Ether (ETH) from the contract to an external address. We will go over various ways to move ether from a contract to an external address in this article.

Transfer Method

The first method is to use the built-in functiontransfer(). This function is part of theaddress type and can be used to send Ether from the contract to an external address. The amount of ether to be sent is the only argument the function accepts. For example:

address _reveiver = 0xf07373bb489AB0957E932515261fbCE433EF988c;
payable(_receiver).transfer(1 ether);

Send Method

The second method is to use the built-in functionsend(). This function is also part of theaddress type and can be used to send Ether from the contract to an external address. The function takes a single argument, the amount of ether to be transferred, and outputs a boolean indicating whether or not the transaction was successful. For example:

address _receiver = 0xf07373bb489AB0957E932515261fbCE433EF988c;
bool status =  payable(_receiver).send(1 ether);
require(status, "Transaction Failed");

Call Method

The third method is to use the built-in functioncall(). This function is part of theaddress type and can be used to call a function on the external address and also transfer Ether to it. The function takes a single argument, the amount of ether to be transferred, and returns a boolean indicating whether the transaction was successful or not and a bytes data type as well. For example:

address _receiver = 0xf07373bb489AB0957E932515261fbCE433EF988c;
(bool status, bytes memory data) =  payable(_receiver).call{ value: 1 ether }("");
require(status, "Transaction Failed");

Just a note here: the returnedbytes variable above might be useful or not; it all depends on the use case, but if you do not need it, it should be ignored because you'd get some sort of warning from your compiler indicating an unused variable. To ignore it, we can rewrite the code snippet like this:

 address _receiver = 0xf07373bb489AB0957E932515261fbCE433EF988c;
(bool status,) =  payable(_receiver).call{ value: 1 ether }("");
require(status

A couple of things to take note of:

  • We marked the address payable before calling the different functions, this is just to ensure the receiving address can receive ether. You will not encounter this issue when sending to a wallet address but it might come up when sending ether to a contract address that does not have a provision to receive ether.

  • Even though there are 3 ways to send ether, the third method call is the

    recommended one to use when sending ether.

Wrapping Up

I hope you were able to learn something from this article that you could use in your next smart contract project.

Watch out for more helpful content on solidity from me. Don't forget to like and follow to get more articles on solidity.

Cheers ๐ŸŽ‰.

ย