3

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));
    })
  })
2
  • What's the debug thing that wraps the call?
    – Ismael
    Commented Nov 17, 2021 at 2:55
  • @Ismael if you are using Truffle to test ( mocha ) than you can wrap calls in debug (), if you run "truffle test debug" in the command line it lets you inspect the objects
    – user379468
    Commented Nov 17, 2021 at 17:57

2 Answers 2

2

It is failing because you aren't sending any ether and the function requires some amount

require(getNFTPrice(numberOfTokens) <= msg.value, "The Amount of Ether sent is not correct.");

To send amount use the value paremeter contract.myMint(1, { value: web3.utils.toWei("0.01", "ether") }). For example

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

    it('creates a new token', async () => {
        const open = await contract.startSale();
        const result = await debug(contract.myMint(1, {
            from: "0x12341234..",
            value: web3.utils.toWei("0.01", "ether"),
        }));
    })
})
1

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));

1
  • Right, but my method myMint is payable ( so I think you need to use send() instead of call() ) but the syntax is still throwing an error, I'm trying to do this from within my Truffle test.js file, from what I understand and instance of web3 is available as well, this seams like a common use case, call a payable function with one parameter while sending payment, but I can't seem to find a working example
    – user379468
    Commented Nov 16, 2021 at 17:31

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.