# Discrepancy between nfts minted, price of nft when a generation changes & position of _incrementGeneration() inside _mintInternal() and _mintNewEntity()

TraitForgeNft mints the first NFT of a new generation at the previous generation's max price, and misordered _incrementGeneration() lets forging reset price to startPrice.

- Payout: Audit contest
- Severity: medium
- Program: TraitForge
- Found by: Shubham
- Disclosed: 2024-09-13
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-traitforge-findings/issues/564
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-traitforge%3AM-07
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity vuln:accounting-error vuln:business-logic

**Target:** TraitForgeNft.sol minting and generation increment logic
**Root cause:** calculateMintPrice() uses generationMintCounts[currentGeneration] before _incrementGeneration() runs, so the first mint of a new generation is priced at the old generation's max; and _incrementGeneration() is placed before the count increment in _mintInternal but after in _mintNewEntity, causing inconsistent generation rollover.
**Steps:**
1. Mint 10000 NFTs in generation 1; generationMintCounts[currentGeneration] reaches 10000 but _incrementGeneration() only triggers on the next call.
2. Call mintToken(): calculateMintPrice() returns startPrice + priceIncrement*10000 = 0.25 ether instead of 0.005005 ether for gen 2's first NFT.
3. In _mintInternal the >= maxTokensPerGen check fires, incrementing generation, so user overpays ~0.244995 ether.
4. Alternatively, reach generationMintCounts[gen]=9999 and mint via forging _mintNewEntity: count becomes 10000 and _incrementGeneration() fires immediately, resetting gen count to 0.
5. Next mintToken() computes price = startPrice + priceIncrement*0 = 0.005 ether, undercharging the protocol.
**How found:** Code reading of calculateMintPrice() and the two mint paths, noticing the generation increment happens at different points relative to generationMintCounts updates and price calculation.
**Exploitation:** A minter times a mintToken() call at a generation boundary to pay the previous generation's max price (loss to user) or, after a forging mint triggers _incrementGeneration(), pays only startPrice for the first NFT of the new generation (loss to protocol).
**Insight:** When a price/count function reads state that a later internal call mutates (generation rollover), check the ordering of the increment versus the price read across every mint entry point; divergent placement in two mint functions is a strong signal of a boundary bug.

## Proof of concept (copied exactly from the original report)

**calculateMintPrice function showing price calculation**

```
function calculateMintPrice() public view returns (uint256) {
    uint256 currentGenMintCount = generationMintCounts[currentGeneration];
    uint256 priceIncrease = priceIncrement * currentGenMintCount;
    uint256 price = startPrice + priceIncrease;
    return price;
  }
```

**_mintInternal generation increment check before minting**

```
function _mintInternal(address to, uint256 mintPrice) internal {
>   if (generationMintCounts[currentGeneration] >= maxTokensPerGen) {
      _incrementGeneration();
    }

>   _tokenIds++;
    uint256 newItemId = _tokenIds;
    _mint(to, newItemId);
    uint256 entropyValue = entropyGenerator.getNextEntropy();

    tokenCreationTimestamps[newItemId] = block.timestamp;
    tokenEntropy[newItemId] = entropyValue;
>   tokenGenerations[newItemId] = currentGeneration;
    generationMintCounts[currentGeneration]++;
```

**_mintNewEntity generation increment after minting**

```
function _mintNewEntity(
    address newOwner,
    uint256 entropy,
    uint256 gen
  ) private returns (uint256) {
    require(
      generationMintCounts[gen] < maxTokensPerGen,
      'Exceeds maxTokensPerGen'
    );

>   _tokenIds++;
    uint256 newTokenId = _tokenIds;
    _mint(newOwner, newTokenId);

    tokenCreationTimestamps[newTokenId] = block.timestamp;
    tokenEntropy[newTokenId] = entropy;
    tokenGenerations[newTokenId] = gen;
>   generationMintCounts[gen]++;
    initialOwners[newTokenId] = newOwner;

    if (
>     generationMintCounts[gen] >= maxTokensPerGen && gen == currentGeneration
    ) {
      _incrementGeneration();
    }
```

