Transaction fees are an unfortunate fact of life on the Ethereum mainnet – NFT gas costs even moreso. The success of NFT projects is particularly vulnerable to high fees, as they inflate the cost of the token. A $75+ gas fee is extremely unattractive to someone minting a single 0.05 Ξ NFT, and may even put the user off of the project altogether.
Luckily, developers are starting to implement new smart contracts which promise reduced gas costs. These performance gains come as developers gain enough confidence in Solidity to venture beyond the battle-tested OpenZeppelin implementations and optimize for efficiency.
The new methods were pioneered by @Squeebo, and quickly adopted by a variety of projects, including Alpha Girl Club and Mecha Chaotic Bedlam.
The War on Gas
At a high level, the new wave of smart contracts are innovating in three main areas to reduce NFT gas costs:
- Custom ERC-721 implementations
- Replacing more powerful but expensive libraries with lean, lightweight code
- Cryptographic whitelisting
Custom ERC-721 Implementations
To avoid bugs and risk, and to focus on the unique parts of their projects, developers use existing code libraries to implement the base ERC-721 interface of their NFT projects. (Reminder: ERC-721 is a standard used by most non-fungible tokens).
When it comes to Solidity libraries, there is only one king: OpenZeppelin. This repository of battle-tested smart contracts is an industry standard for fungible and non-fungible tokens alike. The new wave of developers have found gains in the portion of the implementation responsible for assigning a token to an address (state changing), and checking the total supply of NFTs or balance of an address (read only).
OpenZeppelin’s implementation uses equally expensive methods for the state changing – which costs gas – and read only – which does not cost gas – portions of the contract. The emerging variant uses a method which is very cheap and efficient for state changes, and very inefficient to read. However, since reading the state does not cost gas, the user doesn’t care about the efficiency of that operation!
Replacing Libraries
A commonly used OpenZeppelin library, ERC721Enumerable, implements functions to easily track on-chain all tokens owned by an address. In doing so, it introduces extra operations each time the token is minted or transferred. These extra operations cause bloat and cost the user.
Most projects do not require the whole suite of functionality from ERC721Enumerable, only a single function: totalSupply(). Newer smart contracts are using another library, Counters, to implement the totalSupply() functionality, and cut out the unneeded code. This saves the user gas on both minting and transferring.
Cryptographic Whitelisting
Whitelisting is a commonly-used method to avoid gas wars for project mints. The simple way to handle this is to upload all the whitelisted addresses to the smart contract as a list. This results in ballooning gas costs for project teams, as writing to storage on the blockchain is one of the most expensive operations in Ethereum.
Another, cheaper way to allow verification of whitelisting is required. Modern contracts are increasingly using Merkle trees for this function. In a Merkle whitelist, only the root hash of the Merkle tree is uploaded to the contract. Users verify their whitelist by providing a cryptographic proof that their address is part of the Merkle tree.
This method replaces any number of storage operations with a single storage operation, making it an attractive option for large projects with thousands of whitelisted addresses.
Depending on the contract, implementing the above changes can reduce gas costs by 50% or more. The end result of these improvements is to save money – for both the users and the contract deployer. This capital stays available to the ecosystem, increasing the vitality and long term survivability of the project. The innovation ball has been put in OpenZeppelin’s court – time will tell how they respond.