Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Calculation of Mint Price When Mint Count Equals Max Tokens Per Generation #977

Closed
howlbot-integration bot opened this issue Aug 9, 2024 · 1 comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-564 edited-by-warden 🤖_03_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality

Comments

@howlbot-integration
Copy link

Lines of code

https://github.com/code-423n4/2024-07-traitforge/blob/279b2887e3d38bc219a05d332cbcb0655b2dc644/contracts/TraitForgeNft/TraitForgeNft.sol#L227-L232
https://github.com/code-423n4/2024-07-traitforge/blob/279b2887e3d38bc219a05d332cbcb0655b2dc644/contracts/TraitForgeNft/TraitForgeNft.sol#L280-L309

Vulnerability details

Impact

The _mintInternal function does not correctly handle the case when the number of minted tokens in the current generation equals maxTokensPerGen. This can lead to incorrect mint price calculations when the generation is incremented.
As a result, users might encounter errors such as 'Insufficient ETH send for minting.' due to mismatches between the expected and actual mint price.

Proof of Concept

In the provided test code, the issue manifests as follows:

describe('test', () => {
  it('test calculating mint price', async () => {
    const maxTokensPerGen = await nft.maxTokensPerGen();
    const startPrice = await nft.startPrice();
    for(let i = 0; i < maxTokensPerGen; i++) {
      await nft.connect(owner).mintToken(merkleInfo.whitelist[0].proof, {
        value: ethers.parseEther('1'),
      });
    }

    console.log(await nft.calculateMintPrice());
    await expect(
      nft.connect(user1).mintToken(merkleInfo.whitelist[1].proof, {
        value: startPrice
      })
    ).to.be.revertedWith('Insufficient ETH send for minting.');
  });
});

This test reveals that after minting the maximum number of tokens, the next mint attempt fails due to incorrect price calculation.

Tools Used

Manual code review

Recommended Mitigation Steps

This is the mitigated code.

function _mintInternal(address to, uint256 mintPrice) internal {    // @audit-ok
  _tokenIds++;
  uint256 newItemId = _tokenIds;
  _mint(to, newItemId);
  uint256 entropyValue = entropyGenerator.getNextEntropy();

  tokenCreationTimestamps[newItemId] = block.timestamp;
  tokenEntropy[newItemId] = entropyValue;
  tokenGenerations[newItemId] = currentGeneration;
  generationMintCounts[currentGeneration]++;
  initialOwners[newItemId] = to;

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

  if (!airdropContract.airdropStarted()) {
    airdropContract.addUserAmount(to, entropyValue);
  }

  emit Minted(
    msg.sender,
    newItemId,
    currentGeneration,
    entropyValue,
    mintPrice
  );

  _distributeFunds(mintPrice);
}

Assessed type

Invalid Validation

@howlbot-integration howlbot-integration bot added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value 🤖_03_group AI based duplicate group recommendation bug Something isn't working duplicate-210 edited-by-warden sufficient quality report This report is of sufficient quality labels Aug 9, 2024
howlbot-integration bot added a commit that referenced this issue Aug 9, 2024
@c4-judge c4-judge added satisfactory satisfies C4 submission criteria; eligible for awards 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value downgraded by judge Judge downgraded the risk level of this issue and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value 3 (High Risk) Assets can be stolen/lost/compromised directly upgraded by judge Original issue severity upgraded from QA/Gas by judge labels Aug 20, 2024
@c4-judge
Copy link
Contributor

c4-judge commented Sep 5, 2024

koolexcrypto marked the issue as duplicate of #564

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working downgraded by judge Judge downgraded the risk level of this issue duplicate-564 edited-by-warden 🤖_03_group AI based duplicate group recommendation satisfactory satisfies C4 submission criteria; eligible for awards sufficient quality report This report is of sufficient quality
Projects
None yet
Development

No branches or pull requests

1 participant