Skip to content

Instantly share code, notes, and snippets.

@alexon1234
Last active May 1, 2021
Embed
What would you like to do?
Storage optimizations

In the contract we are storing the following variables:

    mapping(uint256 => address) private idToCreator;
    mapping(uint256 => address) internal idToOwner;
    mapping(uint256 => uint256) public idToCreatorNft;
    mapping(uint256 => uint256) public creatorNftMints;
    mapping(uint256 => address) internal idToApproval;
    mapping(address => mapping(address => bool)) internal ownerToOperators;
    mapping(address => uint256[]) internal ownerToIds;
    mapping(uint256 => uint256) internal idToOwnerIndex;

Most of these variables could be store using a struct instead of multiple mappings. Using stuct over mapping offers better storage optimizations. Here you have more information: https://medium.com/@novablitz/storing-structs-is-costing-you-gas-774da988895e

Also, creatorNftMints is only used in mintWithAlphaOrBeta. In this method, we validate that is only minted or not. For this purpose, we could use mapping(uint256 => bool) instead mapping(uint256 => uint256).

function mintWithAlphaOrBeta(uint256 _createVia)
        external
        reentrancyGuard
        returns (uint256)
    {
        ...
        require(
            creatorNftMints[_createVia] == 0,
            "Already minted with this alpha/beta"
        );
        ...
        creatorNftMints[_createVia]++;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment