0

By advance sorry for asking and thanks for your help, I find/try to resolve my problem from a few days.

I try to add on my smart contract a function to count the number of mint on a specific range (to limit the mint possible on a free mint period from a unique wallet).

For example, i open a free mint period from token n°100 to n°150, i need to limit the mint on a unique wallet at 5.

I try to start this from tokensByOwner function, maybe is not a good solution, if you have an idea let me know

function tokensByOwner(address _owner) external view returns(uint256[] memory ) {
        uint256 tokenCount = balanceOf(_owner);
        if (tokenCount == 0) {
            return new uint256[](0);
        } else {
            uint256[] memory result = new uint256[](tokenCount);
            uint256 index;
            for (index = 0; index < tokenCount; index++) {
                result[index] = tokenOfOwnerByIndex(_owner, index);
            }
            return result;
        }
    }

I think i need to add an if in a for loop but maybe its wrong

if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT && index <= RANGE_MAX_TOKEN_FOR_FREE_MINT) {

I have try to out of the loops with this code but my test are not ok ( result after max range is 0 0 0 .....)

                if (index >= RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;

########

Hi,

I have again try with differents test/parameter, is not again good.

     function atokensByOwnerAAAA(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index < RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
                if (index > RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;
                }
            } 
        return count;
        }

   function atokensByOwnerBBBB(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index < RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                /*if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {*/
                    count++;
                /*}*/
                if (index > RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;
                }
            } 
        return count;
        }

    function atokensByOwnerCCCC(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index <= RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
            } 
        return count;
        }

do you have an idea?

#########

Hi Coliga, thanks for the help.

yes it's the objectiv.

I have try to add your code but I have an error on remix

    mapping (address => uint256) addressToNumberOfTokensMinted;
    bool freeMintPeriod = true;

    function mintforfree(uint256 _count) external {
        addressToNumberOfTokensMinted += _count;
        if(freeMintPeriod) {
            require(addressToNumberOfTokensMinted[msg.sender] < 6, "No free mints left!");
    }

        uint256 totalSupply = totalSupply();
        for(uint256 i = 0; i < _count; i++){
            _safeMint(msg.sender, totalSupply + i);
        }
}

error return ==>

from solidity:
TypeError: Types in storage containing (nested) mappings cannot be assigned to.
  --> test2.sol:41:9:
   |
41 |         addressToNumberOfTokensMinted += _count;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

from solidity:
TypeError: Operator += not compatible with types mapping(address => uint256) and uint256
  --> test2.sol:41:9:
   |
41 |         addressToNumberOfTokensMinted += _count;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

solution by myself (tested ok)

    function tokensByOwnerForFreeMint(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 indextwo;
            uint256 count;
            for (index = 0; index < tokenCount; index++) {
                indextwo = tokenOfOwnerByIndex(_owner, index);
                if (indextwo >= RANGE_MIN_TOKEN_FOR_FREE_MINT && indextwo <= RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
            }
            return count;
        }
1
  • My bad, I edited my answer. Because you didn't know what the problem was, I assume you are not familiar with mappings. So, I suggest you to first look into what a mapping exactly is. Dec 26, 2021 at 13:43

1 Answer 1

1

If I understand correctly, your question is: How to let every unique address mint a maximum of 5 NFTs for free during the free mint period?

This can be done by storing the amount of tokens every address minted. Then in the mint function check if msg.sender minted <6 tokens.

Example:

mapping (address => uint256) addressToNumberOfTokensMinted;
bool freeMintPeriod = true;

function mint(uint256 _count) external {
    addressToNumberOfTokensMinted[msg.sender] += _count;
    if(freeMintPeriod) {
        require(addressToNumberOfTokensMinted[msg.sender] < 6, "No free mints left!");
    }

    // Mint tokens
}

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.