I try to improve my skills in solidity programming, for that i try to ode a dynamic NFT, which evolve according to 2 variables stored in a mapping.

    struct NFTInfo {

        uint32 nbOwners;
        uint32 MintingDate;

    mapping(uint32 => NFTInfo) private obsolescence;

this mapping is filled each time someone mint an NFT as followed :

    function mintNft() external payable{

        s_tokenCounter = s_tokenCounter + 1;
        _safeMint(msg.sender, s_tokenCounter);

        obsolescence[s_tokenCounter] = NFTInfo(1, uint32(block.timestamp));

obsolescence map for each tokenID, a number of historical owners, and a timestamp of the mint.

I'll use these informations to get different token URI according to the number of historical owners (basically, the most historical owners it has, the most basic characteristics the NFT will have, the less it has, the most powered characteristics it will have) the code is the following :

    function tokenURI(uint256 _tokenId) public view override returns (string memory) {
        require(obsolescence[uint32(_tokenId)].MintingDate != 0, "Token doesn't exist !");

        if (block.timestamp - obsolescence[uint32(_tokenId)].MintingDate >= diams_time){

            if (obsolescence[uint32(_tokenId)].nbOwners <= 1) {
                return "ipfs://[...]";
            } else if (obsolescence[uint32(_tokenId)].nbOwners > 1 && obsolescence[uint32(_tokenId)].nbOwners <= 2) {
                return "ipfs://[...]";
            } else if (obsolescence[uint32(_tokenId)].nbOwners > 2 && obsolescence[uint32(_tokenId)].nbOwners <= 3) {
                return "ipfs://[...]";
            } else {
                return "ipfs://[...]";
        } else {

            return "ipfs://[...]";

I don't want to set a MaxSupply, meaning it can be possible to have a infinite amount of NFTs minted and by extension, an infinite amount of key/value pairs in the mapping.

Here is my question: Will I have some high gas cost using a huge mapping object? (By "using", I mean adding a new Key/pair value, or overwrite some existing values) and are these gas cost will increase according to the amount of key/pair values in the mapping object?

1 Answer 1

  1. It really depends on the key/value size. As a general rule, the larger are the keys/values, the more gas it requires to add or update a mapping object.

  2. Since mapping in Ethereum is a type of storage, it's worth mentioning that the cost of reading or writing to storage is proportional to the number of storage slots that are accessed.

    The more storage slots are being accessed, the higher the cost will be.

    So when having a large number of key-value pairs, the overall gas cost will be higher.

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.