Truffle: BigNumber configuration not working in Truffle v5

Created on 26 Dec 2018  路  5Comments  路  Source: trufflesuite/truffle


Issue

In the Truffle v5 Release Notes, the breaking changes section says:

Numeric return values are now BN objects instead of the previously default BigNumber.js. These two projects use significantly different API semantics, so you may want to review those.
Truffle provides a compatibility mode for projects unable to make the switch wholesale. Set numberFormat to "BigNumber", "String", or "BN" to choose. See example number format override:

// Choices are:  `["BigNumber", "BN", "String"].
const Example = artifacts.require("Example");
Example.numberFormat = "BigNumber";

I had previously developed a test suite that includes someBigNumber.plus(1) and similar lines.
Upgrading to Truffle 5 and running truffle test, the tests failed with messages like TypeError: someBigNumber.plus is not a function.

Adding the MyContract.numberFormat = "BigNumber"; line after ...require("MyContract"); per the example, I now get the error:

TypeError: BigNumber is not a constructor
      at _convertNumber (C:\Users\me\AppData\Local\node\node_modules\truffle\build\webpack:\packages\truffle-contract\lib\reformat.js:17:1)
      at abiSegment.forEach (C:\Users\me\AppData\Local\node\node_modules\truffle\build\webpack:\packages\truffle-contract\lib\reformat.js:80:1)
      at Array.forEach (<anonymous>)
      at Function.numbers (C:\Users\me\AppData\Local\node\node_modules\truffle\build\webpack:\packages\truffle-contract\lib\reformat.js:47:1)
      at Promise (C:\Users\me\AppData\Local\node\node_modules\truffle\build\webpack:\packages\truffle-contract\lib\execute.js:125:1)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:160:7)

Steps to Reproduce

Develop tests using BigNumber.
Attempt to use the documented compatibility mode to not have to change over everything in the test suite immediately to Truffle v5.
Limit tests to one file and include the .numberFormat line as documented in release notes/above.
Encounter errors as shown above.

Expected Behavior

No errors. With the adaptation, the test suite runs as before.

Environment

  • Operating System: Win10 Pro (Username changed in output above.)
  • Truffle version (truffle version): 5.0.0
  • node version (node --version): 9.3.0
  • npm version (npm --version): 6.5.0
  • bignumber version (npm show bignumber version): 1.1.0 (this is the latest, with the same version being listed after npm i bignumber runs successfully).

All 5 comments

Same issue here

Same here

OS: linux
Truffle v5.0.1
Node verison v10.15.0
Npm version 6.4.1

I think there is some issue with bignumber.js bundling with webpack https://github.com/MikeMcl/bignumber.js/issues/166.

I will try and have a fix for this soon.

Truffle v5.0.2 should have fixed this problem, I am going to close this issue. Please re-open if you are still having problems with this!

Was this page helpful?
0 / 5 - 0 ratings