TExcelTAD
Home › Solidity › Sending Ether

Sending Ether

Contracts can hold ether and send it to other accounts. There are three ways to do it, and choosing correctly matters for safety.

Method On failure Gas forwarded Recommendation
payable(to).transfer(amount) reverts 2,300 (fixed) legacy; can break with contract recipients
payable(to).send(amount) returns false 2,300 (fixed) legacy; easy to forget the check
to.call{value: amount}("") returns (false, data) all remaining (adjustable) recommended, with a check

Receiving first

A contract can only accept plain ether if it has a receive() function (or a payable fallback); the next lesson covers both. For now: wallets (EOAs) always accept ether, and our helper "user" contracts get receive() external payable {}.

transfer and send

transfer and send forward only 2,300 gas, a fixed stipend that is enough to emit an event but not to write storage. They were once recommended because the small stipend prevents reentrancy, but gas costs change between network upgrades, and recipients that are smart-contract wallets often need more than 2,300 gas. Modern code avoids them.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract SimpleUser {
    receive() external payable {}
}

contract Main {
    constructor() payable {}

    function main() external {
        SimpleUser alice = new SimpleUser();

        payable(address(alice)).transfer(1 ether);          // reverts on failure
        bool ok = payable(address(alice)).send(0.5 ether);   // returns a bool
        console.log("send ok?", ok);
        console.log("alice has:", address(alice).balance);
    }
}
send ok? true
alice has: 1500000000000000000

call: the recommended way

call forwards all available gas and returns a success flag plus any returned data. Always check the flag; ignoring it means a failed payment silently counts as done.

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract SimpleUser {
    receive() external payable {}
}

contract NoReceive {}           // has no receive(): rejects plain ether

contract Payer {
    constructor() payable {}

    function pay(address to, uint256 amount) external {
        (bool ok, ) = payable(to).call{value: amount}("");
        require(ok, "payment failed");
    }
}

contract Main {
    constructor() payable {}

    function main() external {
        Payer payer = new Payer{value: 5 ether}();
        SimpleUser bob = new SimpleUser();
        NoReceive vault = new NoReceive();

        payer.pay(address(bob), 2 ether);
        console.log("bob  :", address(bob).balance / 1 ether, "ETH");
        console.log("payer:", address(payer).balance / 1 ether, "ETH");

        payer.pay(address(vault), 1 ether);   // the recipient rejects the ether
    }
}
bob  : 2 ETH
payer: 3 ETH
main() failed: reverted with reason: payment failed

Pull over push

Sending ether to many addresses in a loop is dangerous: one recipient that rejects payments (or uses huge amounts of gas) blocks everyone. The safer withdrawal pattern lets each user pull their own funds:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Prizes {
    mapping(address => uint256) public owed;

    constructor() payable {}

    function award(address winner, uint256 amount) external {
        owed[winner] += amount;                  // just record it
    }

    function withdraw() external {
        uint256 amount = owed[msg.sender];
        require(amount > 0, "nothing to withdraw");
        owed[msg.sender] = 0;                    // update state BEFORE sending
        (bool ok, ) = payable(msg.sender).call{value: amount}("");
        require(ok, "transfer failed");
    }
}

contract Winner {
    receive() external payable {}

    function claim(Prizes p) external {
        p.withdraw();
    }
}

contract Main {
    constructor() payable {}

    function main() external {
        Prizes prizes = new Prizes{value: 10 ether}();
        Winner ann = new Winner();
        Winner ben = new Winner();

        prizes.award(address(ann), 3 ether);
        prizes.award(address(ben), 1 ether);

        ann.claim(prizes);
        console.log("ann  :", address(ann).balance / 1 ether, "ETH");
        console.log("ben owed:", prizes.owed(address(ben)) / 1 ether, "ETH (not claimed yet)");
        console.log("prize pool:", address(prizes).balance / 1 ether, "ETH");
        ann.claim(prizes);                       // second claim fails
    }
}
ann  : 3 ETH
ben owed: 1 ETH (not claimed yet)
prize pool: 7 ETH
main() failed: reverted with reason: nothing to withdraw

Setting owed[msg.sender] = 0 before the external call is essential. The Reentrancy lesson shows what goes wrong otherwise.

Checklist for sending ether

  1. Use call{value: …}("") and check its result.
  2. Update your bookkeeping before sending (checks → effects → interactions).
  3. Prefer letting users withdraw over pushing payments in loops.
  4. Remember that a recipient can be a contract running arbitrary code when it receives ether.

Test yourself

What does payable(to).send(amount) do when the transfer fails?

  1. Reverts the transaction
  2. Returns false
  3. Retries with more gas
  4. Sends the ether to address(0)

Why is call{value: x}("") recommended over transfer?

  1. It's cheaper
  2. It doesn't depend on a fixed 2,300 gas stipend, so contract recipients keep working
  3. It can't fail
  4. It doesn't need a receive function

In the withdrawal pattern, when should the user's balance be set to 0?

  1. After the ether is sent
  2. Before the ether is sent
  3. It doesn't matter
  4. Only if the send fails