0

I am writing an nft smart contract and I needed to set the first tokenId = 1, the second tokenId = 2 etc. I use the library Counters.sol (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Counters.sol). The problem is that by default _value = 0 and there is no way to change it (it gives an error). How can I solve this problem? Any advice is important for me👾

1
  • Did my suggestion work out for you , buddy? Commented Oct 15, 2022 at 18:26

1 Answer 1

1

You can just invoke tokenId.increment() in the contract's constructor when deploying it. Like so:

import "@openzeppelin/contracts/utils/Counters.sol";

pragma solidity ^0.8.15;


contract Example  {
  using Counters for Counters.Counter;
  Counters.Counter  public tokenId;
    constructor () {
      tokenId.increment();
    }

}

Import it in Remix and deploy it. You will see that when you click on the tokenId button, it will return 1, as you want. Hope I helped you buddy.

1
  • 1
    Good answer! Or you can also, in the minting function, call tokenId.increment just before minting the NFT so it starts minting at 1. But tomatoes tomatoes Commented Oct 16, 2022 at 2:45

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.