ethers js 退出并出现此错误=>无效的 BigNumber 值(参数="value",值=未定义,代码=INVALID_ARGUMENT,版本=bignumber/5.5.0)

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

我尝试像这样使用 ethersjs 向我的合约发送请求:

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

此代码显示错误,我已经尝试了世界上所有的解决方案,但没有任何效果。 => 版本:nodejs 14.15、npm 8、web3 1.6、ethers 5.5

有什么帮助吗?

error:未处理的拒绝(错误):无效的 BigNumber 值(argument="value",value=undefined,code=INVALID_ARGUMENT,version=bignumber/5.5.0)

当您调用 createSale 时,您的价格变量似乎具有值 undefined,导致错误。

您的问题类似于:https://ethereum.stackexchange.com/questions/111042/cant-send-ether-to-smart-contract-using-ethers-js

当我在前端连接到 metamask 时,发生了以下错误。

无效的 BigNumber 字符串 (argument="value", value="1.2e+21", code=INVALID_ARGUMENT, version=bignumber/5.5.0)

我已经用 ethers 中的 bignumber 修复了它。 如果您使用的是 web3.js 版本:1.5.1,请按此方式尝试。 在其他版本中,您也可以尝试使用 bignumber.js 和 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>