vechain / vechain-sdk-js

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

928 bug parseunits throws on numbers below 01 #936

Closed lucanicoladebiasi closed 1 month ago

lucanicoladebiasi commented 1 month ago

Description

The code fixes a bug in the function unitsUtils.parseUnits in the file packages/core/src/utils/units/units.ts leading to include non meaningful digits in BigNumber conversion to string to be parsed as bigint.

New tests are introduced to compare ethers behaviour with the drop-in functions provided by unitUtils, the latter are based on bignumber.js library.

The bignumberjs library configures its setting - specifically about the fixed digits precision and rounding methods - globally: this means if some code using this SDK needs to change BigNumber settings, the behaviour of unitUtils functions is altered in a potentially incorrect way.

This code stores the configuration of BigNumber, at the beginning of the execution of the format/parse functions, set it as needed to assure 80 meaningful digits and BigNumber.ROUND_HALF_UP rounding methods prescribed by Ethereum specifications, restoring the configuration when the functions return. This enhancement prevent BigNumber global settings collides with unitUtils needed setting.

Fixes # 928.

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Test Configuration:

Checklist:

github-actions[bot] commented 1 month ago

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (3390/3390) 100% (783/783) 100% (701/701)
Title Tests Skipped Failures Errors Time
core 485 0 :zzz: 0 :x: 0 :fire: 1m 4s :stopwatch:
network 660 0 :zzz: 0 :x: 0 :fire: 3m 43s :stopwatch:
errors 48 0 :zzz: 0 :x: 0 :fire: 11.237s :stopwatch: