Ethereum: Solving an error of “incorrect bignumber value” from ethers.js
When working with Ethereum contracts, especially those built using the ethers.js truffle package, encountering the error of “incorrect bignumber value” can be frustrating. This error usually occurs during an attempt to cause a function on the Ethereum contract, which expects Bignumber instance as its argument.
Problem: Incorrect bignumber value error
In this article, we will examine what causes an error of “incorrect bignumber value” and we will provide solutions to solve it in Ethers.js applications.
What is Bignumber?
Bignumber is a JavaScript library used to represent an intercourse total number of precision. It provides classes such as Bignumber, Bigint and decimal, which support operations on large numbers.
cause of the error
When creating the “Bignumber” instance, you basically create a number with infinite precision. However, when transferring this value as an argument to the contract function, this may cause problems if:
- The input is not properly formatted : If the Bignumbber instance is created from a string or other incompatible format, it may not be in the expected state.
- Bignumber instance was modified after the creation : If the Bignumbon instance is changed externally (e.g. using the JavaScript code), its inner condition changes unexpectedly, which leads to incorrect calculations.
Error solving
To solve the “incorrect bignumber value” error, you can try the following:
Option 1: Entrance ownership and create a new Bignumbon instance
Before calling the contract function with the Bignumbber instance:
`Javascript
const mnemonic = "your_mnemonic";
wallet = ethers.wallet.frommnemonic (mnemonic);
signer = wallet.connect (supplier);
// Check that the Bignumber instance has been modified externally
console.log (signer.address); // Make sure the instance remains unchanged
Const Routeraddress = '0x ...'; // replace the contract address
Routerinstance = new ethers.contract (Routeraddress, ...
Option 2: Use a library such as ethers.js-bignumba
or @@ nomiclabs/ethers-abi
To avoid problems caused by Bignumbon instances outside:
Javascript
import bignumber from the "large";
const mnemonic = "your_mnemonic";
wallet = ethers.wallet.frommnemonic (mnemonic);
signer = wallet.connect (supplier);
Const Routeraddress = '0x ...'; // replace the contract address
// Use a bignr bignr to create a new instance that remains unchanged
Const Bignumbber = new bignumber ("1"); // Create Bignumbon instance
Routerinstance = new ethers.contract (Routeraddress, {
Methods: {
MyFunction (Bignumber): promise
}
});
By following these steps or using one of the above -mentioned libraries, you should be able to solve the “incorrect bignumer value” error and continue working with Ethers.js applications.