2

I'm asking myself if the call to a function like transfer in solidity is asynchronous.

In other words, I'm working with the following contract

function count_and_pay() public {
    if (lockCon == true)
         return;
    if (positiveVote > negativeVote) {
        candidate.transfer(proprietyContract);
    } else {
        escrow.transfer(proprietyContract);
    }
    lockCon = true
}

I'm a little worried here because I'm not able to find any resource where it saying the call escrow.transfer(proprietyContract); make the transaction and will continue in the execution of the call.

In my knowledge the method is asynchronous and it will create only the transaction of the blockchain and will continue the execution. With this scenario, my code should be technically correct (but without meaning).

macros
  • 23
  • 3

1 Answers1

2

<address>.transfer in Solidity is synchronous.

That means it completes, before the next line lockCon = true runs.

Note: the argument to transfer is an amount in wei, not a contract.

eth
  • 85,679
  • 53
  • 285
  • 406
  • Thanks, in this case, the lockCon have sense? when the transfer will finish, I have not yet the transaction confirmed on the blockchain. This means that I can make a reentry attack without lockCon, right? – macros May 23 '21 at 10:52
  • I suggest the following: 1. use call() per https://ethereum.stackexchange.com/questions/19341/address-send-vs-address-transfer-best-practice-usage/38642 2. instead of your own locking, use ReentrancyGuard and nonReentrant like https://docs.openzeppelin.com/contracts/2.x/api/utils#ReentrancyGuard – eth May 23 '21 at 11:09