I want to use something like the following as a fallback for failed transfers:
if (!recipientAddress.send(amount)) {
ownerBalance += amount;
}
After reading about using transfer
versus send
, it seems that, assuming the send doesn’t fail due to a bad address (if there is such a thing - I’m not entirely sure if transferring to an unclaimed address would fail) or insufficient contract balance, it’s only possible for the transaction to fail if 1) recipientAddress
is a contract address and 2) the contract at that address has a fallback function that uses too much gas for the transaction to succeed.
Assuming that the transaction fails and that #1 and #2 above are correct, the recipient contract would never be able to receive value from the sending contract, so unless there is an alternative way to extract that value, it could potentially be lost in the contract forever.
I’m just trying to confirm my assumptions here in order to know whether I can rightly transfer the value to myself as the owner if the transfer fails or if I need to account for the possibility that an external user may have to attempt the transfer more than once.
Most of what I’ve read suggests that transfer
is the preferred method for sending value, however, I don’t think I’ve seen this question addressed. If my assumptions are correct, I’d think that send
should be the preferred method.
UPDATE:
Here's a more full example of a transaction with multiple transfers:
function transferGameBalances(uint _gameId) external payable {
Game storage game = gamesById[_gameId];
require(game.status == GameStatus.GAME_OVER);
require(msg.sender == owner || msg.sender == game.player1 || game.player2);
if (!game.player1.send(game.p1_balance)) {
ownerBalance += game.p1_balance;
}
if (!game.player2.send(game.p2_balance)) {
ownerBalance += game.p2_balance;
}
}