indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Cannot create a Big Number #274

Open aress31 opened 3 years ago

aress31 commented 3 years ago

I am trying to create a BN object from the value 10**32, see the following test:

  it("has a quadrilllion (10^15) initial total supply", async () => {
    const decimals = await this.token.decimals();
    const totalSupply = await this.token.totalSupply();
    // console.log(totalSupply);
    console.log(new BN(10 ** 32));
    // console.log((10 ** 15 * 10 ** decimals.toNumber()).toString());
    // console.log(totalSupply, expected);
    // expect(await this.token.totalSupply()).to.be.bignumber.equal(
    //   (10 ** 15 * 10 ** decimals.toNumber()).toString()
    // );
  });

But it seems that the library cannot handle this Big Number, see the following stack trace:

  1) Contract: Initial State
       has a quadrilllion (10^15) initial total supply:
     Error: Assertion failed
      at assert (node_modules\bn.js\lib\bn.js:6:21)
      at BN._initNumber (node_modules\bn.js\lib\bn.js:128:7)
      at BN.init [as _init] (node_modules\bn.js\lib\bn.js:82:19)
      at new BN (node_modules\bn.js\lib\bn.js:39:12)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:35:17)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

I also tried to initialise the Big Number with a string instead but the value is totally off, see:

  it("has a quadrilllion (10^15) initial total supply", async () => {
    const decimals = await this.token.decimals();
    const totalSupply = await this.token.totalSupply();
    // console.log(totalSupply);
    console.log(new BN((10 ** 32).toString()));
    // console.log((10 ** 15 * 10 ** decimals.toNumber()).toString());
    // console.log(totalSupply, expected);
    // expect(await this.token.totalSupply()).to.be.bignumber.equal(
    //   (10 ** 15 * 10 ** decimals.toNumber()).toString()
    // );
  });

  Contract: Initial State
    √ the deployer is the owner (104ms)
    √ has a name (124ms)
    √ has a symbol (124ms)
    √ has 18 decimals (87ms)
BN { negative: 0, words: [ 23532 ], length: 1, red: null }
    √ has a quadrilllion (10^15) initial total supply (173ms)
    1) assigns the initial total supply to the owner
    > No events were emitted

Could anyone please help?

aress31 commented 3 years ago

More details:

  it("has a quadrilllion (10^15) initial total supply", async () => {
    const decimals = await this.token.decimals();
    const totalSupply = await this.token.totalSupply();
    console.log("totalSupply", totalSupply.toString());
    console.log("1", 10 ** 32);
    console.log("2", BN(10 ** 32).toString());
    console.log("3", BN(10).exponentiatedBy(32).toString());
    // console.log(new BN(expected));
    // console.log((10 ** 15 * 10 ** decimals.toNumber()).toString());
    // console.log(totalSupply, expected);
    // expect(await this.token.totalSupply()).to.be.bignumber.equal(
    //   (10 ** 15 * 10 ** decimals.toNumber()).toString()
    // );
  });
  Contract: Initial State
    √ the deployer is the owner (95ms)
    √ has a name (96ms)
    √ has a symbol (156ms)
    √ has 18 decimals (104ms)
totalSupply 1000000000000000000000000000000000
1 1e+32
    1) has a quadrilllion (10^15) initial total supply
    > No events were emitted

  4 passing (2s)
  1 failing

  1) Contract: Initial State
       has a quadrilllion (10^15) initial total supply:
     TypeError: Cannot set property 'negative' of undefined
      at BN (node_modules\bn.js\lib\bn.js:26:19)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:36:22)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
gentasumantri commented 3 years ago

you should pass the exponent number as BN object, since you try to exponent a number in plain javascript. try using BN.pow() instead.

example : const totalExp = new BN(10).pow(new BN(32))