Skip to content
Business logic Audit finding Intermediate

Wrong minting logic based on total token count across generations

The mintWithBudget function incorrectly uses the total token count across all generations to limit minting, instead of the count for the current generation. This prevents users from minting multiple tokens in later generations once the total exceeds the per-generation limit.

TopStar Follow with dimulski, 0xlemon and 48 others

Originally disclosed on Code4rena

Submitted by TopStar, also found by Rhaydden (1, 2), dimulski, 0xlemon, pep7siup, aldarion, gajiknownnothing, zhaojohnson, Fitro, eta, 0x3b, dontonka, p0wd3r, pipidu83, almurhasan, 0xDazai, ogKapten, samuraii77, 0xAleko, Ruhum, jesjupyter, stanchev, MinhTriet, avoloder, persik228, Abdessamed, Shubham, AvantGard, Akay, FastChecker, 0xHash, federodes, 0xlookman, y4y, peanuts, zhanmingjing, anonymousjoe, erike1, PENGUN, pfapostol (1, 2), onthehunt11, Autosaida, LeFy, yaioxy, hakunamatata, zeroProtocol, MrValioBg, blackVul, PASCAL, VulnViper, cryptomoon, binary, Daniel_eth, 0xcontrol, Bac0nj, sl1, vinica_boy, Kunhah, 3n0ch, SharpPeaks, inzinko, 0x0bserver, jeremie, DigiSafe, dvrkzy, Abhan, Nihavent, Udsen, 0xPwned, mashbust, shikhar229169, smbv-1923, Bob, Xcrypt, LonelyWolfDemon, frodoBaggins, gkrastenov, Shahil_Hussain, dhank, Topmark, rbserver, ArsenLupin, KupiaSec, gesha17, klau5, ilchovski, ke1caM, 0xrex, shaka, nnez, and 0xJoyBoy03

TraitForgeNft::mintWithBudget function is similar to mintToken, but allows users to mint multiple tokens in a single transaction if they have a budget exceeding the minting price for one token. However, _tokenIds tracks the total number of tokens ever minted, not just the tokens in the current generation.

Impact

In the current implementation, _tokenIds is used to control the minting process. The check while (budgetLeft >= mintPrice && _tokenIds < maxTokensPerGen) ensures that minting will stop when current generation minted tokens reaches maxTokensPerGen. Instead of checking the number of tokens minted in the current generation, the function incorrectly checks the total number of tokens minted across all generations (_tokenIds).

Proof of Concept

Here is the current implementation of the mintWithBudget function in the smart contract on line 215:

TraitForgeNft.sol
202:   function mintWithBudget(
203:     bytes32[] calldata proof
204:   )
205:     public
206:     payable
207:     whenNotPaused
208:     nonReentrant
209:     onlyWhitelisted(proof, keccak256(abi.encodePacked(msg.sender)))
210:   {
211:     uint256 mintPrice = calculateMintPrice();
212:     uint256 amountMinted = 0;
213:     uint256 budgetLeft = msg.value;
214:
215:     while (budgetLeft >= mintPrice && _tokenIds < maxTokensPerGen) { // @audit - _tokenIds counts total tokens minted, not just current generation
216:       _mintInternal(msg.sender, mintPrice);
217:       amountMinted++;
218:       budgetLeft -= mintPrice;
219:       mintPrice = calculateMintPrice();
220:     }
221:     if (budgetLeft > 0) {
222:       (bool refundSuccess, ) = msg.sender.call{ value: budgetLeft }('');
223:       require(refundSuccess, 'Refund failed.');
224:     }
225:   }

The function will not allow minting if _tokenIds is greater than 10,000, which will happen after the 1st generation is fully minted.

Recommended Mitigation Steps

Use generationMintCounts[currentGeneration] instead of _tokenIds:

- while (budgetLeft >= mintPrice && _tokenIds < maxTokensPerGen) {
+ while (budgetLeft >= mintPrice && generationMintCounts[currentGeneration] <= maxTokensPerGen) {

Assessed type

Invalid Validation

TForge1 (TraitForge) confirmed


The original report as published on Code4rena, by TopStar. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The loop condition uses _tokenIds (total tokens minted across all generations) instead of generationMintCounts[currentGeneration] to enforce the per-generation limit.

Impact

Users cannot mint multiple tokens in a single transaction once the total token count exceeds the per-generation limit, which happens after the first generation is fully minted.

What the attacker needed

Whitelisted user; budget exceeding mint price

The fix

The loop should check generationMintCounts[currentGeneration] instead of _tokenIds.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This is a smart contract bug where a function that lets people mint multiple tokens at once uses the wrong counter to decide when to stop. It checks the total number of tokens ever created, not just those in the current batch or generation, so after enough tokens exist, the function stops working even though it should still allow minting.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More from TraitForge

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.