0

i was wondering if someone can help me here.

I'm minting an nft by this smart contract, and the problem is that the tokenid count starts with id 0, and that is an error. It must start in id 1.

Someone can help me please

This is my mint loop function where the error is happening.

I already change the for uint256 i = 0 for i = 1 and doesnt work.

 function _mintLoop(address _receiver, uint256 _mintAmount) internal {
    for (uint256 i = 0; i < _mintAmount; i++) {
      uint256 tokenId = _tokenIdCounter.current();
      _tokenIdCounter.increment();
      _safeMint(_receiver, tokenId);
      tokensOfOwner[_receiver].push(tokenId);
    }
  }

1 Answer 1

0

You must to put the expression _tokenIdCounter.increment() into your smart contract constructor and when you'll call _mintLoop() function for the first time, the counter will start from 1.

You can see this smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyToken is ERC721, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIdCounter;

    mapping(address=>uint[]) tokensOfOwner;

    constructor() ERC721("TX", "Test") {
        _tokenIdCounter.increment();
    }

    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        for (uint256 i = 0; i < _mintAmount; i++) {
            uint256 tokenId = _tokenIdCounter.current();
            _tokenIdCounter.increment();
            _safeMint(_receiver, tokenId);
            tokensOfOwner[_receiver].push(tokenId);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.