SIP 26: Max Gas on token fallback Source

AuthorClinton Ennis
Discussions-Tohttps://discord.gg/3uJ5rAy
StatusImplemented
Created2019-10-29

Simple Summary

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

Motivation

Solidity passes 63/64 of gas in subcalls and it is burning all this on a failed subcall.

Specification

Move the emitTransfer to before the callTokenFallbackIfNeeded.

In ExternStateToken.sol: // 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

In TokenFallbackCaller.sol: 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));

Implementation

https://github.com/Synthetixio/synthetix/blob/v2.12.2/contracts/ExternStateToken.sol#L134 https://github.com/Synthetixio/synthetix/blob/v2.12.2/contracts/TokenFallbackCaller.sol#L52

Copyright and related rights waived via CC0.