0

My dApp allows user to save their URL when purchasing each NFT token. The obvious design would define --

struct NFT {
    ...
    string url;
}

However, URLs can be long. To save gas for users, I modify the design to save only the hash of URL in each NFT and the actual URL in another mapping, which basically means --

struct NFT {
    ...
    uint urlKeccak256;
}
mapping(uint => string) private urlMap;

function saveUrl(string calldata _url) {
    ...
    uint _urlKeccak256 = uint(keccak256(abi.encode(_url)));
    urlMap[_urlKeccak256] = _url;

    NFT memory nft;
    nft.urlKeccak256 = _urlKeccak256;
}

While this design does save gas on average, my question is:

Since there is no (efficient or cheap) way to delete unused entries from urlMap, would there be a future concern if the onchain storage of urlMap keeps growing (to hundreds of MB and beyond potentially)?

0

Ethereum does not charge rent for storing data, so in many cases it is more gas-efficient to keep in storage data, that is not needed anymore, rather than to delete it.

Self destruct (a.k.a. suicide) opcode was supposed to address this, by deleting storage of the contracts not needed anymore, but it caused more problems than solved, so now its usage is discouraged.

So for now it is normal to leave junk data in storage in case clearing is unreasonable from gas cost point of view.

|improve this answer|||||

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.