【问题标题】:Error: invalid decimal value (argument="value", value="[object Object]", code=INVALID_ARGUMENT, version=bignumber/5.7.0)错误:无效的十进制值(argument=\"value\", value=\"[object Object]\", code=INVALID_ARGUMENT, version=bignumber/5.7.0)
【发布时间】:2023-02-15 22:56:44
【问题描述】:

我尝试创建一个函数来更新 NFT 的价格。但是我收到了这个错误

错误:无效的十进制值 (argument="value", value="[object Object]", code=INVALID_ARGUMENT, version=bignumber/5.7.0)

这是 Dapp.config.js 中的代码

    const updatePrice = async (tokenId, newPrice) => {
        const web3Modal = new Web3Modal();
        const connection = await web3Modal.connect();
        const provider = new ethers.providers.Web3Provider(connection);
        const signer = provider.getSigner();
        const contract = signerOrProvider(signer);
        console.log('New price', newPrice);
        console.log('Token ID', tokenId);
        await contract.updatePrice(tokenId, ethers.utils.parseUnits(newPrice.toString(), 'ether'));
    }

这是我的 HTML

<div className='flex flex-col w-full'>
    <Input 
       inputType='number'
       title='Update listing price'
       placeholder='Enter new listing price here ...'
       handleClick={(e) => {setFormInput({...formInput, price: e.target.value})}}
     />
     <Button 
         buttonType='primary'
         innerText='Update price'
         parentStyles='mt-5 rounded-full'
         handleClick={() => handleUpdatePrice(NFT.tokenId, formInput)}
      />
</div>

这是我的 useStateSnippet

const [ formInput, setFormInput ] = useState({price : ''});

【问题讨论】:

    标签: javascript node.js web3 decentralized-applications


    【解决方案1】:

    parseUnits 方法会返回一个很大的数字,需要将其字符串化才能得到字符串格式的数字。 ethers.utils.parseUnits(newPrice.toString(), 'ether').toString()

        const updatePrice = async (tokenId, newPrice) => {
            const web3Modal = new Web3Modal();
            const connection = await web3Modal.connect();
            const provider = new ethers.providers.Web3Provider(connection);
            const signer = provider.getSigner();
            const contract = signerOrProvider(signer);
            console.log('New price', newPrice);
            console.log('Token ID', tokenId);
            await contract.updatePrice(tokenId, ethers.utils.parseUnits(newPrice.toString(), 'ether').toString());
        }
    

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 2022-08-14
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多