SIP-26: Max Gas on token fallback
A bug was reported https://github.com/Synthetixio/synthetix/issues/243 which the position of emit would reduce the gas estimation of trading SNX via uniswap down from 900K to 100K
Solidity passes 63/64 of gas in subcalls and it is burning all this on a failed subcall.
Move the emitTransfer to before the callTokenFallbackIfNeeded.
// Emit a standard ERC20 transfer event
emitTransfer(from, to, value);
// If the recipient is a contract, we need to call tokenFallback on it so they can do ERC223 // actions when receiving our tokens. Unlike the standard, however, we don't revert if the // recipient contract doesn't implement tokenFallback. callTokenFallbackIfNeeded(from, to, value, data);
Limit the gas to sub calls to either 200K or the amount of gas thats left if it is less than 200K
uint gasLimit = gasleft() < MAX_GAS_SUB_CALL ? gasleft() : MAX_GAS_SUB_CALL;
recipient.call.gas(gasLimit)(abi.encodeWithSignature("tokenFallback(address,uint256,bytes)", sender, amount, data));
Copyright and related rights waived via CC0.