Ethers.js: invalid BigNumber value

Created on 4 Feb 2018  路  3Comments  路  Source: ethers-io/ethers.js

Thanks For Assisting before

Hello please still trying to send tokens. The ones I have sent are in decimals that is behind the zeros. link to the token sent Transferred Tokens you see the token transferred are 0.000000000000005. The amount I'm trying to send is 5000, not 0.000000000000005. Thou I know the cause of I but I don't know how to solve it. when sending the token base on the token decimal function which is 18 decimal place, I'm supposed to set the amount as this 5000*10**18 but when I try this it gives an error of invalid BigNumber value. So the code I wrote is found below

var abi = require('./abi.json'), 
            add = 'contract_address', 
            provider = ether.providers.getDefaultProvider();
            signer = new ether.Wallet("Wallet_pvky", provider);
        var contract = new ether.Contract(add, abi, signer);
        var options = {
            gasPrice: 1100000000, // 1.1Gwei
            gasLimit: 250000 // Should be enough for more transfer transactions
        };
        var amount = 5000*10**18;
        //res.send(amount.toString())
        // amount.toString();
        var callPromise = contract.transfer("receiver_address", amount, options);
        callPromise.then(function(obj){
            res.send(obj)
        })

        callPromise.catch(function(error){
            res.send(error)
        });

So please help me fix this error

The Full Error Code Plus the Error Stack

Error: invalid BigNumber value
    at throwError (EtherWallet\node_modules\ethers-utils\throw-error.js:4:17)
    at new BigNumber (EtherWallet\node_modules\ethers-utils\bignumber.js:40:9)
    at Object.bigNumberify (EtherWallet\node_modules\ethers-utils\bignumber.js:142:12)
    at Object.encode (EtherWallet\node_modules\ethers-contracts\interface.js:111:27)
    at EtherWallet\node_modules\ethers-contracts\interface.js:284:59
    at Array.forEach (native)
    at pack (EtherWallet\node_modules\ethers-contracts\interface.js:283:12)
    at Object.encode (EtherWallet\node_modules\ethers-contracts\interface.js:433:20)
    at Function.encodeParams (EtherWallet\node_modules\ethers-contracts\interface.js:823:45)
    at Interface.func (EtherWallet\node_modules\ethers-contracts\interface.js:639:59)

Most helpful comment

The general purpose units library is not exposed in the ethers.utils:

var decimalPlaces = 18;

var amount = ethers.utils.parseUnits('5000.0', decimalPlaces);
// BigNumber { _bn: <BN: 10f0cf064dd59200000> }

var string = ethers.utils.formatUnits(amount, decimalPlaces);
// '5000.0'

Thanks! :)

All 3 comments

Heya!

I am looking at the umbrella package now, and seeing that I never exposed the units methods directly. I will update that today. Since your number of decimals is 18 though, which is the same as wei to ether, you can use:

// From String to BigNumber
var amount = ethers.utils.parseEther('5000.0');
// BigNumber { _bn: <BN: 10f0cf064dd59200000> }

// From BigNumber to String:
var amount = ethers.utils.formatEther(amount);
// "5000.0"

Once I have added the units to the main package, I'll update this issue with how to support arbitrary number of decimal places.

Ok Thanks Again for this good work

The general purpose units library is not exposed in the ethers.utils:

var decimalPlaces = 18;

var amount = ethers.utils.parseUnits('5000.0', decimalPlaces);
// BigNumber { _bn: <BN: 10f0cf064dd59200000> }

var string = ethers.utils.formatUnits(amount, decimalPlaces);
// '5000.0'

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings