-
Notifications
You must be signed in to change notification settings - Fork 195
Conversation
Thanks @shahthepro, looks like a lot of test failures that are probably related to these changes. I'll update the branch to run them again but would you mind checking them out if it still fails? |
@tomlinton Thanks. PR is not yet ready for review. Yeah, will be fixing the tests |
Sorry @shahthepro, someone (cough @micahalcorn) requested review 👺 😂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left one comment otherwise LGTM.
Since you have separated it so nicely into a class could be beneficial to add a few unit tests maybe.
|
||
const zeroCount = valueInWei.length - valueInWei.replace(/0/g, '').length | ||
|
||
return zeroCount === decimals |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT web3 units for each zero count don't exist. Sometimes there are 3 zeros between the 2 closest units. So the above function might miss the correct unit in case its' decimal count is between 2 units. (for example between szabo
-> 12 zeros and nano
-> 9 zeros)
Maybe one suggestion as how to amend the above implementation:
let biggestUnit = 'wei'
let c = 0
const unit = Object.keys(web3.utils.unitMap).forEach(unit => {
const valueInWei = web3.utils.unitMap[unit]
const zeroCount = valueInWei.length - valueInWei.replace(/0/g, '').length
if (zeroCount <= decimals && zeroCount > biggestUnitZeros) {
biggestUnit = unit
biggestUnitZeros = zeroCount
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this @sparrowDom, Will fix this
Sorry, it's early and I may have missed something but I don't get why we're doing string operations here instead of using BN.js? Here's a conversion from "human units" to an 8-decimal token value:
Seems a bit more accurate and straight forward than trying to do math this way? EDIT: Converting a decimal to a real token value:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Agreed with @sparrowDom that adding some simple unit tests would be nice.
So that next time we had a new currency we can easily make sure it works.
Ethersjs has great utility functions for this kind of thing. Maybe we should add it as a dependency and slowly try to move away from web3js. Tough job though 😞 |
Agreed. This seems simple. Will update it. Thanks
Thanks. Will do it
We could but Mike's solution should reduce the complexity of the code right now. I'm not sure if we should add a dependency right now. But thanks, let me check this |
@mikeshultz However, I did some other change that will simply it more (more like treating it as a string). @sparrowDom @franckc The issue with decimal places (that are not multiples of 3) is fixed now and have also added a couple of simple unit tests @tomlinton Tried ether.js, but the potential that Domen reported was still reproducible (i.e the function not working with decimals which are not multiples of 3). I managed to fix this without ether.js or even using web3. So skipping adding ether.js for now |
@shahthepro you can't feed BN a decimal. In fact, I'm surprised that didn't error. Here's a slightly more comprehensive example using your given numbers starting with a string:
EDIT: This also assumes relatively small numbers as user input that won't overflow in JS. Might want to add some checks if you expect extremely large numbers( |
Yep, this is what I'm concerned about. We had code to truncate anything longer than 18 decimals (before passing that to I'm probably gonna merge this change in and will fix forward, if we ever any issue with the present implementation Thanks @mikeshultz |
Should fix #4304
Link to test build
Checklist:
fbt
for translationnpm run translate