Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3.toBigNumber() is not a function #280

Closed
andytudhope opened this issue Apr 10, 2019 · 10 comments
Closed

web3.toBigNumber() is not a function #280

andytudhope opened this issue Apr 10, 2019 · 10 comments

Comments

@andytudhope
Copy link

Running the test script, i.e. using Ganache under the hood works well.

However, even after looking at #268 and changing the web3-utils version to either 1.0.0-beta37 or 1.0.0-beta41, I still cannot get this test suite to pass when not using the script, but rather running truffle develop and then truffle test from within the solidity directory. When I do, I always get:

TypeError: web3.toBigNumber is not a function
    at contract (~/test/contracts/solidity/test/BancorFormula.js:14:28)

Why does truffle develop differ from ganache? I ask because I am trying to solve this issue currently: status-im/discover-dapps#14

@barakman
Copy link
Collaborator

barakman commented Apr 10, 2019

@andytudhope :

The test-suite executed via truffle test or via npm test 1 relies on Truffle v4.1.14, which in turn relies on Web3 v0.18.4.

And for Web3 v0.x, toBigNumber is a valid function.

I'm not sure how else you are trying to run the tests, but the error which you describe implies that your setup uses Web3 v1.x instead, where web3.toBigNumber has been replaced with web3.utils.toBN.

Be cautious - these two functions are different not only in name, but also in behavior (if I remember correctly, the former truncates non-integer values while the latter throws exception on such input).

Note that in either case, it has nothing to do with Ganache.

@andytudhope
Copy link
Author

Yeah, sorry for the confusion, have figured it out and should be able to push a proper Embark test suite to the above repo shortly 👍

@andytudhope
Copy link
Author

@barakman status-im/discover-dapps@3609bf9

So close! All I need now is to figure out how to handle decimals with a different BigNumber library 😓 Any helpful leads?

@barakman
Copy link
Collaborator

@andytudhope :

In file package.json:

  • Add "bignumber.js": "^8.0.0" (under devDependencies)

In any test script that you need to update:

  • Add const BigNumber = require("bignumber.js");
  • Replace web3.toBigNumber with new BigNumber

See here for the official documentation of this module.

@andytudhope
Copy link
Author

andytudhope commented Apr 10, 2019

@barakman yeah, that's what that commit above does, it's the decimals I am struggling with now: https://github.com/status-im/discover-dapps/blob/develop/test/BancorFormula_spec.js#L203

(The Math.floor() shouldn't be there, was just trying some random stuff...) If you uncomment that block, you get:

AssertionError [ERR_ASSERTION]: Error: [number-to-bn] while converting number "4.5956818223498375920701783577961055462157623760458422993543318099514112549881422701542626479610988660428118371519990977285977858991135391744e+38" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "4.5956818223498375920701783577961055462157623760458422993543318099514112549881422701542626479610988660428118371519990977285977858991135391744e+38"

@barakman
Copy link
Collaborator

barakman commented Apr 10, 2019

@andytudhope :
You can't (or shouldn't) use Math.floor on BigNumber objects.

If you want to get the floor value of a BigNumber object x, then use x.truncated().

Also, I believe that you should use new BigNumber (instead of just BigNumber) conclusively.

As to the problem that you're describing - I believe that you are actually getting the error for one of the lines above the line which you have marked, because function toBN is not used in that line.

@andytudhope
Copy link
Author

andytudhope commented Apr 10, 2019

Yeah, I know, but x.truncated() is not a function in that bignumber.js library unfortunately.

Thanks for the tip on new, will update.

My issue is 2-fold:

  1. truncate() is not a function. I can use toFormat() which should be the same, but doesn't appear to be functioning as such.
  2. I'm having to web3.utils.toHex() all the bignumbers before passing them as args to the contract call because Embark doesn't like bignums as args.

Somewhere between removing truncate and adding toHex, things go wrong and I can't get the last chunk of tests to pass: https://github.com/status-im/discover-dapps/blob/develop/test/BancorFormula_spec.js#L201

BTW, I should just say that this test suite is an inspiration and you, @barakman, are a wizard ❤️

@barakman
Copy link
Collaborator

@andytudhope :
Thank you!!!

What BigNumber version are you using?

You can get it by entering the command-line npm ls bignumber.

@andytudhope
Copy link
Author

andytudhope commented Apr 11, 2019

[email protected] - trying with "bignumber.js" : "^8.0.0" still installs 8.1.1, and trying with "bignumber.js" : "8.0.0" tells me that there is No matching version found for [email protected]

@barakman
Copy link
Collaborator

barakman commented Apr 11, 2019

@andytudhope :
Then you can use integerValue instead of truncated.

The difference between integerValue and toForrmat/toFixed is that the former returns a BigNumber instance, while the latter two return a string.

You might find the official documentation quite useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants