2017-07-06
Solidity - .send(...) vs. .transfer(...)
A short notice on the two functions
address.send(amount);
vs.
address.transfer(amount);
Since version 0.4.13 (actually since this issue is resolved) Solidity has one new important function, the transfer
function. Here is the difference between .send and .transfer:
How send works (or worked)
With address.send(amount)
it was possible to send an amount to a specific address. There are two problems:
- Only a small amount of gas is sent along (21,000 gas). So the receiver can only emit one single event at max, it can't be another contract that changes state variables or call again other contracts. So it's pretty "safe by accident".
- If an error throw
.send
will return false and not propagate the error.
.send(...)
is now deprecated.
Workaround with call()()
Man developers therefore defaulted back to recipient.call.value(x)()
. Which was merely a workaround and still returns a boolean on success or error. Now exactly this workaround received a name and fully propagates exceptions.
The function .transfer
With the new function transfer, it fully propagates errors (on throw)
address.transfer(amountEther)
Also the Solidity by Example Documentation got updated and now uses the .transfer()
function.
Further readings
Of course this doesn't mean your contracts have to become "no-brainers". Security is still one of the most important factors. Read on about reentrancy and the DAO hack.
Did I miss something? Do you have opinions? Let me hear in the comments!