0

I have the following function

function mint(uint256[] memory tokens) public payable {
        require(saleIsActive, "Sale is not active");
        require(tokens.length == 0, "No tokens to mint");
    require(
        tokens.length > maxGradisPurchase,
        "number of tokens incorrect"
    );
        for (uint256 i = 0; i < tokens.length; i++) {
            _safeMint(msg.sender, tokens[i]);
            _setTokenURI(tokens[i], "sometokeuri");
            _totalTokens.increment();
        }

}

but the require(tokens.length == 0, "No tokens to mint"); is not working, here is my javascript test:

it('should not allow to mint a gradis if token array is empty', async () => {
    // activate sales before test
    await myContractInstance.flipSaleState({ from: accounts[0] });

    const tokensToMint = [1];
    await myContractInstance.flipSaleState
      .mint(tokensToMint, {
        from: accounts[1],
        value: 1000000000000000,
      })
      .catch((error) => {
        console.log(error);
        assert(
          error.message,
          'Returned error: VM Exception while processing transaction: revert Sale is not active -- Reason given: Token is not correct.'
        );
      });

    const minted = await gradisInstance.totalSupply();
  });

but the require is always triggered and I don't understand why the Code alway have a token.lengthequal to zero even though I am recibing the correct token

1

You say the require is not working and tokens are getting minted, but that is as expected: require(condition, revertString) means that if the condition is false, then the transaction will revert.

In your case, since you pass to the function parameters that satisfy the condition (tokens list of length either 0 or 6), the require does not trigger and the tokens are minted as you described.

If you want to test that it's working, pass only 4 items to it: const tokensToMint = [1, 2, 3, 4]. Then the transaction should revert with your "Token is not correct" message.

2
  • 1
    Thank you so much I Just update my Code, but as you can see my Problem is that the array variable length is always cero even though I have data inside the array
    – Jean
    Oct 22 at 20:24
  • You're welcome, it's the same problem now mate :) You should use require the opposite way - change it to require(tokens.length > 0, "No tokens to mint");. This means if tokens.length is not > 0, then the tx will revert. Oct 23 at 2:30

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.