Solidity Ethers.js – How to Safemint using Ethers.js

erc-721ethers.jsmintsolidity

My safemint of ERC721 code is something like this:

function mint(address _to, uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();

    {some requires}

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(_to, supply + i);
    }
  }

How will I put the same code for ethers js in a react website with which I can interact with the contract code?

I have tried something like:

const SmartContract = new ethers.Contract(
            address,
            abi,
            signer
          )

await SmartContract.mint(
                    to,
                    amount
                    )

where to: the receiver address, amount is the mint amount.

But it is not working. It is giving the error:

RPC Error: execution reverted: ether value is invalid

which is handled by the require method:

require((cost.mul(_mintAmount)) <= msg.value, "ether value is invalid");

Please let me know how to fix the issue.

Best Answer

The problem is that you are not sending ether, to send ether with etherjs you pass an object as the last parameter to the function call with a property value, and the value should be the amount of ether in wei, this is an example await SmartContract.mint(to,amount,{value: cost})

Related Topic