MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.68k stars 742 forks source link

Error from doing production optimization - Error: invalid BigNumber value (argument="value" #305

Closed jjhesk closed 3 years ago

jjhesk commented 3 years ago

version: 9.0.1 web3.js: 1.6.0 ethers: 5.0.17 @vue/cli-service: 4.5.13

The full package.json

Just call the web3.js the contract method that will take BigNumber or BN as parameters.

  public async approve(spender: string, amount: BigNumber,): Promise<void> {
        const self = this as any as Ori20Contract;

        assert.isString('spender', spender);
        assert.isNumberOrBigNumber('amount', amount);

        const promizz = self._contract.methods.approve(
            spender,
            amount,
        )

        await promizz.send({
            from: this._blockwrap.getAccountAddress(),
            gas: this.gas,
            gasPrice: this.gasPrice
        }).on('transactionHash', (hash) => {
            this.onBroadcast(hash);
        }).on('confirmation', (confirmationNumber, receipt) => {
            this.onConfirmation(receipt);
        }).on('receipt', (r) => {
            this.pushReceiptSuccess(r);
        }).on('error', (error, receipt) => {
            this.onError(receipt, error);
        }).catch((e) => {
            this.catchErro(e)
        });

    };

compile and optimize into distribution: npm run nuxtbuild-demo or vue-cli-service build --name index ./src/main.js

results:

chunk-vendors.7148939b.js:24 Error: invalid BigNumber value (argument="value", value="1000000000000000000000000000000000000", code=INVALID_ARGUMENT, version=bignumber/5.4.2)
    at h.makeError (chunk-vendors.7148939b.js:1)
    at h.throwError (chunk-vendors.7148939b.js:1)
    at h.throwArgumentError (chunk-vendors.7148939b.js:1)
    at Function.from (chunk-vendors.7148939b.js:54)
    at Z.encode (chunk-vendors.7148939b.js:18)
    at chunk-vendors.7148939b.js:18
    at Array.forEach (<anonymous>)
    at D (chunk-vendors.7148939b.js:18)
    at W.encode (chunk-vendors.7148939b.js:18)
    at ae.encode (chunk-vendors.7148939b.js:18)
nt @ chunk-vendors.7148939b.js:24
ct @ chunk-vendors.7148939b.js:24
at @ chunk-vendors.7148939b.js:24
(anonymous) @ chunk-vendors.7148939b.js:24
Promise.catch (async)
rt @ chunk-vendors.7148939b.js:24
a @ chunk-vendors.7148939b.js:24
Wc.n._wrapper @ chunk-vendors.7148939b.js:24

image

However, there is no problem when the function is called under development mode: image

It gives me the same result for 1000 or 100000000 or any integers. However, it does not act as error on dev mode. vue-cli-service serve

So the final problem is that it does normal in local development environment but it reports error exceptions in the production environment.

jjhesk commented 3 years ago

Looks like the same in https://github.com/MikeMcl/bignumber.js/issues/290

jjhesk commented 3 years ago

Try discuss: https://github.com/nuxt/nuxt.js/discussions/9957

MikeMcl commented 3 years ago

This is nothing to do with this library. It is to do with web3.js and ethers.js and your confusion about which BigNumber library is being used.

ethers.js has its own the BigNumber library, and it looks like your problems are caused by mixing the two.

Please stop opening issues or spamming other issues about this and linking here for no good reason.

jjhesk commented 3 years ago

It has nothing to with web3 and ethers.js I just confirmed. @MikeMcl

shuckster commented 3 years ago

Sorry to chip-in on a closed thread, but I notice Ethers has a BigNumber API that is NOT BigNumber.js. It is based on BN.js, but provides a custom API. See their documentation.

jjhesk commented 3 years ago

@shuckster BigNumber cannot mixed with BN.js. Thats all I got. But why it would still work if I have BigNumber.js as class BigNumber instance at the first place?

montera82 commented 2 years ago

You can monkey patch ethers by using this code

bignumber-monkeypatch.ts

import { BigNumber } from "bignumber.js";

class BigNumberExtended extends BigNumber {
  public toHexString(): string {
    return `0x${this.toString(16)}`;
  }
}

BigNumber.prototype = Object.create(BigNumberExtended.prototype);
BigNumber.prototype.constructor = BigNumber;

and then importing it in your hardhat.config.ts with the following line

import "./lib/bignumber-monkeypatch";