GymBros: Optimising a Smart Contract for a High-Traffic Presale
2021·12·15 · 4 min
GYMBROs is an NFT collection centred around the fitness industry, made up of 10,000 randomly generated avatars based on attributes with different rarities.
The project required a range of different skills on our part, including but not limited to:
- Solidity: Smart contract development.
- React: Minting website framework.
- Web3.js: Bridging between Web2 and the Ethereum blockchain.

How We Achieved It
There were two main problems that we needed to solve:
- 1. Smart contract development.
- 2. A website for minting the tokens.
Smart contract:
The contract follows the ERC-721 standard for NFTs on the Ethereum network — however it was necessary to implement more features on top of this in order to achieve the client's requirements. Such included:
- Whitelisted presale: Restrict who can mint an NFT during the presale phase.
- Reserved tokens: Pre-mint a number of tokens for the team.
- Limit on tokens per wallet: Restrict the number of tokens that can be minted by an individual.
- Provenance hash: Calculate and store a provenance hash.
Existing whitelist solutions typically use a large mapping of wallet addresses, and this requires the client to pay large gas fees to add each whitelisted buyer. As GYMBROs required a whitelist of 1000 addresses, this was not optimal. To combat this we made use of a Merkle tree, and were able to reduce gas fees by 82.78%.
Reserved tokens were pre-minted at the time of contract deployment, and the amount was limited to 50 tokens.
In order to restrict the tokens that can be minted by a single wallet, we tracked the number of tokens minted by each buyer's Ethereum address, and checked against this each time the mint function was called.
The sequence and rarity of tokens is preserved using a provenance hash. After all 10,000 tokens have been minted, the hash can be calculated from the metadata and compared to the proof stored within the smart contract. To calculate it, a sha256 hash is taken of each tokens JSON file. These are then concatenated together in order and the resulting string is also hashed in the same way.
Minting website:
By nature, the minting website needed to be dynamic, and this was achieved using React. To supplement, a handful of libraries were used for both styling and functionality, including:
- Web3.js: Making function calls to the smart contract.
- React-Bootstrap: Bootstrap components.
- Web3Modal: Connecting to browser-based Ethereum wallets.
- merkletreejs: Calculating Merkle proofs for whitelist verification.
- keccak256: Hashing with keccak256 standard.

The website allowed for streamlined minting of GYMBROs tokens, with plentiful error checking and a simple yet informative UI.
Conclusion
Both the smart contract and minting website worked without error, notably under the high traffic that was experienced at the time of presale launch. To date, the GYMBROs project has sold out and the smart contract has handled 700 ETH in transactions.