1

Creating my first contract and was taking a look at the Open Zeppelin wizard. I notice that it includes a Counters.Counter private _tokenIdCounter;, which tracks the amount of tokens minted. When I add the enumerable library it still seems to include that snippet within the code when there is now a totalSupply variable that seems to track the same value.

Question: Why is _tokenIdCounter included, and is there any difference between that and the totalSupply variable? Seems as though totalSupply would work fine and I should remove the _tokenIdCounter.

0

On the surface they may sound the same.

Each NFT typically has an unique ID, and the Counters are used to assign the next available ID to a new NFT. This goes hand in hand with totalSupply as long as NFTs are minted.

However, if there is functionality to burn an NFT, the Counter doesn't decrease (since the IDs shouldn't typically be reused), but the totalSupply goes down.

Furthermore, I'm unsure if such functionality is ever desired, but at least in theory you can have two NFTs with the same ID - in which case the Counter is not in sync with totalSupply also.

2
  • Okay that makes sense. So in the case that your NFT is not burnable, then the counter wouldn't be needed correct? Oct 8 '21 at 6:06
  • I guess. You need to analyze the contract and make sure Oct 8 '21 at 8:44
0

There should be no difference as totalSupply simply returns the length of the enumerated tokens array, which is decremented on burn and incremented on mint.

New contributor
tunadip is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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