Ethers.js – How Does Comparing BigNumbers with Ethers Work?

bignumberethers.js

I am using ethers and writing tests and I don't quite understand the ethers.BigNumber.from('some number') method. Could someone please explain how it works, the documentation is not very helpful to me.
For example:
console.log(ethers.BigNumber.from('30') < ethers.BigNumber.from('120'));

returns "false"

Why is BigNumber from 30 greater than BigNumber from 120?

Best Answer

I exactly ran into the same problem. You should use the built.in methods in bignumber:

let a = ethers.BigNumber.from('30');
let b = ethers.BigNumber.from('120');
 a.lt(b) // returns true  as 30 is lower than (lt) 120

Here you use lt (lower than), you can use:

  • eq: equal
  • gte: greater or equal
  • gt: greater
  • lte: lower or equal
  • lt: lower

Hope it helps

Related Topic