vechain / vechain-sdk-js

The official JavaScript SDK for VeChain.
19 stars 6 forks source link

🐛 [BUG] - parseUnits(formatUnits()) results in a different number #927

Closed ifavo closed 1 month ago

ifavo commented 1 month ago

Description

I have a big int number that is converted correctly to a human readable version using formatUnits but when applying parseUnits on the same string, it does return a different number. There might be a rounding error at a certain point.

Reproduction URL

No response

Reproduction steps

import { unitsUtils } from '@vechain/sdk-core';

const bintBalance = 398913019603365720000n
console.log(unitsUtils.parseUnits(parseFloat(unitsUtils.formatUnits(bintBalance)), 18))

It results in:

Screenshots

![DESCRIPTION](LINK.png)

Logs

No response

OS

No response

ifavo commented 1 month ago

I am sorry, I was a bit too early, using parseFloat is an error I did and did not test in the ethers comparison. All good an no issue here. Sorry again.

lucanicoladebiasi commented 1 month ago

Never less, I'm going to investigate a possible side effect because the bignumber.js library backing the parseUnit has a global variable to set the precision, this could lead to side effects poking with the same library in other parts of a project using our SDK and bignumber.js.

ifavo commented 1 month ago

I created a second issue with reproduction code here: https://github.com/vechain/vechain-sdk-js/issues/928 - maybe best to handle it there?

lucanicoladebiasi commented 1 month ago

This ticket is closed because investigation moved to https://github.com/orgs/vechain/projects/9/views/1?pane=issue&itemId=65498793 as suggested.