Privacy preferences

Choose separately whether we may use analytics and advertising measurement. Both stay off unless you turn them on. You can change these choices any time.

All challenges

Challenge 13

tx.origin Misuse

HighReentrancy

Challenge

Review the external call in withdraw. Can a recipient contract call back before its balance is cleared?

Starter code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ReentrancyIssue {
  mapping(address => uint) public balances;

  function deposit() public payable {
    balances[msg.sender] += msg.value;
  }

  function withdraw() public {
    uint amount = balances[msg.sender];
    require(amount > 0, "No balance to withdraw");
    (bool success, ) = payable(msg.sender).call{value: amount}("");
    require(success, "Transfer failed");
    balances[msg.sender] -= amount;
  }
}
Show hint

Consider checks-effects-interactions and a reentrancy guard.