How to use the BN function from bn.js

Find comprehensive JavaScript bn.js.BN code examples handpicked from public code repositorys.

BN.js.BN is a JavaScript library that provides a way to work with big numbers in JavaScript.

196
197
198
199
200
201
202
203
204
205
    var _a = action.params, methodName = _a.methodName, args = _a.args, gas_1 = _a.gas, deposit = _a.deposit;
    console.log('deposit: ', deposit);
    console.log('gas: ', gas_1);
    console.log('args: ', args);
    console.log('methodName: ', methodName);
    return nearTransactions.functionCall(methodName, args, new bn_js_1.BN(gas_1), new bn_js_1.BN(deposit));
}
case "Transfer": {
    var deposit = action.params.deposit;
    return nearTransactions.transfer(new bn_js_1.BN(deposit));
fork icon2
star icon7
watch icon1

+ 14 other calls in file

28
29
30
31
32
33
34
35
36
37
it("Should get8BytesAt()", async function () {
  const [value, at] = input.get8BytesAt;
  const expected = ethers.BigNumber.from(
    "0x" +
      toBN(value)
        .and(new BN("FFFFFFFFFFFFFFFF", 16).shln(at * 8)).shrn(at * 8)
        .toString(16)
  );

  expect(await masks.get8BytesAt(value, at)).to.equal(expected);
fork icon0
star icon0
watch icon0

How does bn.js.BN work?

bn.js.BN is a JavaScript library for arbitrary-precision decimal and non-decimal arithmetic operations. It provides a constructor function that creates an instance of a BigNumber object that can be used to perform arithmetic operations on large numbers. The library supports a wide range of arithmetic operations such as addition, subtraction, multiplication, division, modulo, exponentiation, and more. It also provides methods for comparing, shifting, and manipulating the bits of the numbers.

0
1
2
3
4
5
6
7
8
9
10
const { ethers } = require("hardhat");
const { expect } = require("chai");
const { BN } = require("bn.js");


function generateMask(nBytes, at, reversed) {
  let mask = new BN(
    "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
    16
  );

fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
const BN = require("bn.js");

const num1 = new BN("123456789012345678901234567890");
const num2 = new BN("987654321098765432109876543210");

const sum = num1.add(num2);
const product = num1.mul(num2);

console.log(sum.toString()); // "1111111111111111111111111111100"
console.log(product.toString()); // "1219326311370217953949012804133716984268950688920624044961680987107019840"

In this example, we create two BN objects initialized with very large numbers. We then use the add and mul methods to perform addition and multiplication on those numbers, respectively. Finally, we convert the results back to string representations using the toString method and log them to the console.