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?
Asked
Active
Viewed 710 times
2
1 Answers
2
There are basically tree ways to send transaction to another address in Solidity:
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.address.send(amount): equivalent toaddress.call.gas(2300).value(amount)("")address.transfer(amount): equivalent torequire (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;
}
Mikhail Vladimirov
- 7,313
- 1
- 23
- 38
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 '19 at 09:28transfer). You can just try and find out. – goodvibration Mar 26 '19 at 09:42