Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix expectedUnits to include two decimal places in output (#30)
### Description This pull request addresses an issue where `expectedUnits.toString()` was producing an integer string without decimal places (e.g., “3” instead of “3.33”), which led to inaccurate or less precise representations in logs. ### How to test <details> <summary>Created a unit test to see the difference(which is not a part of this PR)</summary> ``` const { ethers } = require('ethers'); const assert = require('assert'); describe('Compare BigNumber division methods', () => { it('should compare integer division with ethers.utils.formatUnits()', async () => { const testCases = [ { description: 'Exact multiple of 10^decimals', expectedBuy: ethers.BigNumber.from('10000000000000000000'), // 10 * 1e18 decimals: 18, }, { description: 'Fractional amount less than 1', expectedBuy: ethers.BigNumber.from('333333333333333333'), // ~0.333 ETH decimals: 18, }, { description: 'Large number with decimals', expectedBuy: ethers.BigNumber.from('123456789012345678901234567890'), decimals: 18, }, { description: 'Small number with high decimals', expectedBuy: ethers.BigNumber.from('1234567890'), decimals: 8, }, { description: 'Zero value', expectedBuy: ethers.BigNumber.from('0'), decimals: 18, }, ]; testCases.forEach((testCase) => { const { description, expectedBuy, decimals } = testCase; const expectedUnitsDiv = expectedBuy.div(ethers.BigNumber.from(10).pow(decimals)); const expectedUnitsDivString = expectedUnitsDiv.toString(); const expectedUnitsFormat = ethers.utils.formatUnits(expectedBuy, decimals); const expectedUnitsFormatIntegerPart = expectedUnitsFormat.split('.')[0]; const resultsMatch = expectedUnitsDivString === expectedUnitsFormatIntegerPart; console.log(`\nTest Case: ${description}`); console.log(`Expected Buy: ${expectedBuy.toString()}`); console.log(`Decimals: ${decimals}`); console.log(`Original Method Result: ${expectedUnitsDivString}`); console.log(`New Method Result: ${expectedUnitsFormat}`); console.log(`Results Match: ${resultsMatch}`); if (resultsMatch) { assert.strictEqual( expectedUnitsDivString, expectedUnitsFormatIntegerPart, `Results should match for ${description}` ); } else { assert.notStrictEqual( expectedUnitsDivString, expectedUnitsFormatIntegerPart, `Results should differ for ${description}` ); } }); }); }); ``` </details>
- Loading branch information