Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why web3 utils BN not work correct with math?

I am trying do this:

import { BN } from 'web3-utils'

const AmountBN = new BN('1000000000000000000')
const res = AmountBN.mul(99).div(100)
console.log(res)  

And get this

Uncaught (in promise) RangeError: Invalid array length at BN.mul (bn.js:1862)

like image 621
Руслан Миров Avatar asked Mar 14 '26 18:03

Руслан Миров


1 Answers

it is somewhat inflexible, but mul() and div() also expect BN as parameters, not numbers. And the result is a BN again, use toString()

So you need to write something like this:

const res = AmountBN.mul(new BN('99')).div(new BN('100'))
console.log(res.toString())
like image 121
Ossip Avatar answered Mar 17 '26 08:03

Ossip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!