[Ethereum] Ethers.js – Error when calling the mint function with eth value

etherethers.jsminttokens

I'm starting on Smart Contract and ERC721 Collectible NFT and I'm using Ethers as Provider and Metamask to connect user.

I'm working on the Mint function from a React website, and it's working good when i'm the owner (so no need to send Eth) but when I try to implement it I receive an error:

index.ts:225 Uncaught (in promise) Error: overflow (fault="overflow", operation="BigNumber.from", value=70000000000000000, code=NUMERIC_FAULT, version=bignumber/5.5.0)
    at Logger.makeError (index.ts:225)
    at Logger.throwError (index.ts:237)
    at throwFault (bignumber.ts:358)
    at Function.from (bignumber.ts:249)
    at index.ts:263
    at Generator.next (<anonymous>)
    at fulfilled (index.ts:1)

This is my smart contract mint function:

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

        require(!paused);
        require(_mintAmount > 0);

        if (msg.sender != owner()) {
            require(msg.value >= cost * _mintAmount);
        }
        for (uint256 i = 1; i <= _mintAmount; i++) {
            _safeMint(msg.sender, supply + i);
        }
    }

This is a working code (when I'm the owner of the contract)

await contract.mint(1);

but This one isn't working (even if i'm the owner or not)

await contract.mint(1, {value: cost});

This the documentation I tried to follow.


Metamask and Ethers as been connected using

connect(ethers.providers.Web3Provider, "any");

And the contract as been create (and signed) using:

const signer = (new ethers.providers.Web3Provider(window.ethereum)).getSigner();
const contract = new ethers.Contract(address, ABI, signer);

The cost variable is 0.07Eth (required amount to mint one NFT) and have been retrieved using the contract.cost function

const cost = parseInt((await contract.cost()).toString())


I really not understand how to fix it ? Thank you all for help ! 🙂

Best Answer

The value parameter takes a string as input, so you don't need to convert the cost to an int. When you try to convert it it becomes too big to be a regular integer, and that's why you get the overflow.

So if you remove the parseInt from your cost, and just send it to mint as a string, it should work.