Solidity – Understanding and Resolving uint256 Value Issues

ethers.jssolidity

On my mint contract abi it says that the internal type is uint256 and the token mint price is 0.01 ETH so i'm not sure how to convert this to uint256 in the frontend?

  {
    "inputs": [
      { "internalType": "uint256", "name": "_mintAmount", "type": "uint256" }
    ],
    "name": "mint",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },

I'm using ethers.js to interact with the contract, what is the best way to pass the value as unit256?

Here is the error that i get:

MetaMask - RPC Error: execution reverted: Invalid mint amount!

Here is how i'm calling to the frontent

  const mintContract = async () => {
    if (contract) {
      try {
        const tx = contract.mint(utils.parseUnits("0.01"));
        tx.wait();
      } catch (error) {
        await setError(true);
      }
    }
  };

Best Answer

the problem is that you are not sending 0.01 ether with the transaction. You are telling the contract to mint 0.01 ..

try this and make sure you have ether

  const mintContract = async () => {
    if (contract) {
      try {
        const numberOfTokenToMint = 1;
        const options = {value: utils.parseUnits("0.01")}
        const tx = await contract.mint(numberOfTokenToMint, options);
        await tx.wait();
      } catch (error) {
        await setError(true);
      }
    }
  };
Related Topic