Search code examples
javascriptnode.jsweb3js

Error: invalid decimal value (argument="value", value="[object Object]", code=INVALID_ARGUMENT, version=bignumber/5.7.0)


I try to create a function to update the price of an NFT. But i'm getting this error

Error: invalid decimal value (argument="value", value="[object Object]", code=INVALID_ARGUMENT, version=bignumber/5.7.0)

This is the code in the 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'));
    }

And this is my 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>

and this is my useStateSnippet

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

This is what i get in my console


Solution

  • The parseUnits method will return a big number, you need to stringify it to get the number with string format. 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.price, 'ether').toString());
        }