SolidityIn

Sending ETH safely

Lesson 11 of 40 · Ether

Solidity offers three ways to send ETH: transfer, send and call. transfer and send forward only 2300 gas, which can fail with modern contract wallets, so the recommended way today is call with a check on the returned success flag.

Sending ETH hands control to the receiver. If the receiver is a contract, its receive or fallback function runs, and it can call back into your contract before your function has finished. This is called reentrancy.

The 2016 DAO hack drained millions of dollars this way. The attacker's contract called withdraw again from inside its receive function, and the bank had not yet reduced the balance, so each call paid out again.

The fix is the checks-effects-interactions pattern. First check the conditions, then update your state, and only then interact with other addresses. A reentrancy guard, which is a lock that blocks a second entry, adds another layer of protection.

Also avoid relying on address(this).balance for logic, since anyone can force ETH into a contract with selfdestruct. Use your own accounting, and prefer pull payments, where users withdraw their own funds, over pushing ETH to many addresses in one loop. The Attacker below is for learning only, so run it on a test network.

contract VulnerableBank {
    mapping(address => uint256) public balances;

    function deposit() external payable {
        balances[msg.sender] += msg.value;
    }
    function withdraw() external {
        uint256 amount = balances[msg.sender];
        require(amount > 0, "Nothing to withdraw");
        (bool ok, ) = msg.sender.call{value: amount}("");   // interaction first: BAD
        require(ok, "Transfer failed");
        balances[msg.sender] = 0;
    }
}

contract SafeBank {
    mapping(address => uint256) public balances;
    bool private locked;

    modifier nonReentrant() {
        require(!locked, "Reentrant call");
        locked = true;
        _;
        locked = false;
    }
    function deposit() external payable {
        balances[msg.sender] += msg.value;
    }
    function withdraw() external nonReentrant {
        uint256 amount = balances[msg.sender];
        require(amount > 0, "Nothing to withdraw");
        balances[msg.sender] = 0;                            // effect first
        (bool ok, ) = msg.sender.call{value: amount}("");    // interaction last
        require(ok, "Transfer failed");
    }
}

contract Attacker {
    VulnerableBank public bank;
    constructor(VulnerableBank b) { bank = b; }
    function attack() external payable {
        bank.deposit{value: msg.value}();
        bank.withdraw();
    }
    receive() external payable {
        if (address(bank).balance >= msg.value) bank.withdraw();
    }
}

Try it yourself

In Remix on a test network, deposit 3 ETH into VulnerableBank from one account, then run Attacker.attack with 1 ETH and watch the bank drain. Then explain which single line order in SafeBank stops it.

Press Compile to check your code in the browser. Short fragments are wrapped in a contract automatically.

Quick check: What makes reentrancy possible?