1

Does this discussion imply that we cannot call transfer() to some contract and call its fallback function and can only do the same by using call() and setting gas?

  • No, you can call a contract's fallback via transfer, but there is a gas stipend of 2300 units, so if this fallback requires more than that to complete, then the transaction will revert. – goodvibration Mar 26 at 9:28
  • @goodvibration Thanks for your answer! If the fallback function will transfer it again to another contract or to the first one - there will be revert? And in this case, I should use call() ? – Aleksandr Mar 26 at 9:34
  • Depends how much gas is required in the first one and in the second one (and in the function which initially called transfer). You can just try and find out. – goodvibration Mar 26 at 9:42
1

There are basically tree ways to send transaction to another address in Solidity:

  1. address.call.gas(gasLimit).value(amount)(data): sends transaction to another account with given gas limit, ether amount and data, though .gas(gasLimit) and .value(amount) parts are optional.
  2. address.send(amount): equivalent to address.call.gas(2300).value(amount)("")
  3. address.transfer(amount): equivalent to require (address.send(amount))

In case you are not sure that 2300 will be enough for target account to process the transfer, you have to use call. 2300 gas is not enough to perform transaction to another contract or to write anything into storage, so basically, it is not enough to modify blockchain state.

Though, you should use call with care, because it allows so called recursive attacks. This is when contract you are calling, calls your contract back, but once your contract is currently in the middle of function execution, its storage may be in inconsistent state. Here is an example:

function withdraw (uint _amount) public {
    require (_amount <= balances [msg.sender]);
    // Balance is not yet reduced, so msg.sender may withdraw the same
    // amount again inside the call
    msg.sender.call.value (_amount)();
    balances [msg.sender] -= amount;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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