estarriolvetch / ERC721Psi

MIT License
116 stars 28 forks source link

In Burnable extension, the gas cost of totalSupply() increases as the number of tokens increases. #37

Closed cryptedy closed 1 year ago

cryptedy commented 1 year ago

Hello. I am currently using PsiBurnable for some NFT contracts, but I am facing a problem with high mint cost. Looking at the code, I noticed that Burnable's _burned() counts up the _burnedToken Bitmap and as the number of tokens increases, it accesses a lot of storage, which increases the gas cost.

Checking the latest ERC721A, we see that the gas cost of totalSupply() is constant regardless of the number of tokens because _burnCounter is incremented during the burn.

estarriolvetch commented 1 year ago

Hi @cryptedy

Yes, you are correct. This is a deliberate choice for gas saving on not maintaining a _burnCounter.

Depending on you application, you can probably use _nextTokenId() instead of totalSupply() in the mint function.

e.g.

require(_nextTokenId() + amount < SOME_CAP)
estarriolvetch commented 1 year ago

If your application heavily uses burn a lot, you might want to check another implementation I developed.

https://github.com/ctor-lab/ERC1155Delta