'ethers js exits with this error=> invalid BigNumber value (argument="value", value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.5.0)

I tried to send a request to my contract using ethersjs like this:

        const web3Modal = new Web3Modal()
        const connection = await web3Modal.connect()
        const provider = new ethers.providers.Web3Provider(connection)
        const signer = provider.getSigner()
        const contract = new ethers.Contract(Contract, Market.abi, signer)
        const price = ethers.utils.parseUnits(price.toString(), 18)

       //const price = web3.utils.toWei(nft.price);
        //const price = ethers.BigNumber.from(nft.price.toString()).toHexString();
       
        const transaction = await contract.createSale(address, price)
        
        await transaction.wait()

This code shows an error, I have tried all the solutions in this world, but nothing worked. => versions: nodejs 14.15, npm 8, web3 1.6, ethers 5.5

any help?

error : Unhandled Rejection (Error): invalid BigNumber value (argument="value", value=undefined, code=INVALID_ARGUMENT, version=bignumber/5.5.0)



Solution 1:[1]

It looks like your price variable has the value undefined when you call createSale, resulting in an error.

Your issue is similar to: https://ethereum.stackexchange.com/questions/111042/cant-send-ether-to-smart-contract-using-ethers-js

Solution 2:[2]

When I connect to metamask in my frontend, the below error was happened.

invalid BigNumber string (argument="value", value="1.2e+21", code=INVALID_ARGUMENT, version=bignumber/5.5.0)

I have fixed it with bignumber in ethers. If you are using web3.js version : 1.5.1, please try it in this way. And also in other version you can try to use bignumber.js and web3.utils.toBn.

<pre>
import { BigNumber} from 'ethers';

....

const decimals = 18;
const trans_amount = 1500;
const amount= BigNumber.from(trans_amount).mul(BigNumber.from(10).pow(decimals));
</pre>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phista
Solution 2