0
0
Blockchain / Solidityprogramming~7 mins

Sending Ether (transfer, send, call) in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Sending Ether means moving money from one account to another on the blockchain. It is important to know how to do this safely and correctly.

You want to pay someone for a service using a smart contract.
You want to send a refund to a user from your contract.
You want to split payments between multiple addresses.
You want to forward received Ether to another contract or wallet.
Syntax
Blockchain / Solidity
address payable recipient = payable(address);  

// Using transfer
recipient.transfer(amount);

// Using send
bool sent = recipient.send(amount);
require(sent, "Send failed");

// Using call
(bool success, ) = recipient.call{value: amount}("");
require(success, "Call failed");

transfer sends Ether and reverts on failure automatically.

send sends Ether but returns false on failure, so you must check it.

call is the recommended way now; it returns success and allows sending gas.

Examples
This sends 1 Ether to the recipient using transfer. It will stop if it fails.
Blockchain / Solidity
address payable recipient = payable(0x123...);
recipient.transfer(1 ether);
This sends 1 Ether using send. You must check if it worked.
Blockchain / Solidity
address payable recipient = payable(0x123...);
bool sent = recipient.send(1 ether);
require(sent, "Send failed");
This sends 1 Ether using call. It is the safest and most flexible way.
Blockchain / Solidity
address payable recipient = payable(0x123...);
(bool success, ) = recipient.call{value: 1 ether}("");
require(success, "Call failed");
Sample Program

This contract shows three ways to send Ether to a recipient address. You can call each function with some Ether to send it.

Blockchain / Solidity
pragma solidity ^0.8.20;

contract SendEtherExample {
    address payable public recipient;

    constructor(address payable _recipient) {
        recipient = _recipient;
    }

    // Function to receive Ether
    receive() external payable {}

    // Send Ether using transfer
    function sendUsingTransfer() public payable {
        require(msg.value > 0, "Send some Ether");
        recipient.transfer(msg.value);
    }

    // Send Ether using send
    function sendUsingSend() public payable {
        require(msg.value > 0, "Send some Ether");
        bool sent = recipient.send(msg.value);
        require(sent, "Send failed");
    }

    // Send Ether using call
    function sendUsingCall() public payable {
        require(msg.value > 0, "Send some Ether");
        (bool success, ) = recipient.call{value: msg.value}("");
        require(success, "Call failed");
    }
}
OutputSuccess
Important Notes

transfer forwards 2300 gas and reverts on failure.

send forwards 2300 gas and returns false on failure; you must handle it.

call forwards all gas by default and returns success status; it is now recommended.

Summary

Use transfer for simple sending with automatic revert on failure.

Use send if you want to handle failure manually.

Use call for the most control and safety in sending Ether.