Infinite ERC‑20 Allowances
With ERC‑20 tokens, allowances are often set to “infinite” values so that they only need to be set once for a given contract. This is safe under the assumption that the target contract is written in a way that it can only make use of the funds in a pre-determined manner. For example, uniswap sets the allowance by default to (2^256 - 1), which is the maximum integer value supported by the Ethereum. Then, whenever uniswap is used to sell some of the approved tokens, this very high value is adjusted and an event emitted. This works, but uses more gas than necessary given the intended behaviour.
Since gas fees are higher than ever, we adjusted our default ERC‑20 contract to support “infinite” allowances. Whenever the allowance is set to 2^255 or higher, the allowance is considered infinite and is not decreased when used. This saves us a small, but not negligible amount of gas on every transaction that makes use of the allowance.
First, we already save a little bit of gas when setting the value. Initiating an allowance to (2^256 - 1) or “0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff” in hex costs 47′872 gas in the current version of Ethereum. Initiating an allowance with (2^255) or “0x8000000000000000000000000000000000000000000000000000000000000000” in hex costs 45′888 gas because zeroes are cheaper than non-zero values in the hex representation of the transaction payload. So we already save about 4% when initiating the allowance just by choosing a better value.
Second, with the smart contract recognizing “infinite” allowances, the allowance does not need to be adjusted anymore when spent. Usually, a transferFrom transaction costs about 48′024 gas, whereof the internal execution costs are 23′744 gas. When skipping the allowance update in case of an infinite allowance, this cost is reduced to about 40′677 gas, whereof the internal execution costs are 16′397 gas. So this little trick saves us 7′347 gas on most “transferFrom” method calls. At today’s Ether and gas prices, this is about 1 CHF per invocation.
Interestingly, the USDT already has this feature built-in (with the value used by uniswap) and also never emits an Approval event to save a little more gas, bringing down the costs of setting the allowance to about 43′935 gas.