0

Imagine I have deposit function which can receive funds from user and the amount sent will be updated in state based on balanceBefore and balanceAfter of token in the contract:

1:    function deposit(IERC20 token, uint256 amount) public{
2:        uint256 balanceBefore = token.balanceOf(address(this));
3:
4:       token.transferFrom(msg.sender,address(this),amount);
5:
6:        uint256 balanceAfter = token.balanceOf(address(this));
7:        
8:        balance[msg.sender] = balanceAfter - balanceBefore;
9:    }

now lets say user A calls deposit and in same time user B calls deposit but user's B transaction will be mined when user's A transaction is in middle of execution(in proccesing after line 3) so user B has sent token to contract and contract's balance is increased. In such a scenario user's A balance will be updated more than expected.

Is it possible this scenario happen? if yes is it based on luck?

1
  • Not possible as posted answer explains. But you should know about reentrancy, which is a bug in contracts not written securely enough and has led to many hacks.
    – eth
    Aug 16, 2023 at 6:34

1 Answer 1

1

Transactions are executed sequentially in the block, there is no overlap. The scenario you describe can never happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.