[Ethereum] How to actually test a mint function using Truffle and the payable modifier in Solidity

erc-721nftopenzeppelintruffletruffle-test

I'm creating a smart contract and trying to test it using Truffle. I'm a little stuck on how to call the mint function ( how to actually execute the transaction from the test function ). I know Im going about this all wrong in my test just not certain about the syntax when calling a payable function.

function myMint(uint256 numberOfTokens) external payable nonReentrant {
    require(saleActive,"Nope");
    require(numberOfTokens > 0, "You cannot mint 0.");
    require(SafeMath.add(_numMinted.current(), numberOfTokens) <= MAX_PUBLIC_MINT, "Exceeds maximum supply.");
    require(numberOfTokens <= MAX_PURCHASE, "Exceeds maximum number");
    require(getNFTPrice(numberOfTokens) <= msg.value, "The Amount of Ether sent is not correct.");

    for(uint i = 0; i < numberOfTokens; i++){
        uint256 tokenIndex = _tokenIdCounter.current();
        _numMinted.increment();
        _tokenIdCounter.increment();
        _safeMint(msg.sender, tokenIndex);   
    }
}

and in my test function

  describe('minting', async () => {

    it('creates a new token', async () => {
      const open = await contract.startSale();
      const result = await debug(contract.myMint(1));
    })
  })

Best Answer

If you use web3, you need to add .call() after the methods calls, like that : contract.myMint(1).call(). If you use ethersjs, your call is good.

In all cases, method calls are async operations, you need to await for the result : const result = await debug(await contract.myMint(1));

Related Topic