Skip to content

Commit

Permalink
Fix expectedUnits to include two decimal places in output (#30)
Browse files Browse the repository at this point in the history
### 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
squadgazzz authored Sep 27, 2024
1 parent 9d7b8a7 commit f55fe3b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,12 @@ export const dripItAll = async () => {
provider
);
const decimals = await buyTokenContract.decimals();
const expectedUnits = expectedBuy.div(ethers.BigNumber.from(10).pow(decimals));
const expectedUnits = ethers.utils.formatUnits(expectedBuy, decimals);
const expectedUnitsFormatted = parseFloat(expectedUnits).toFixed(2);
console.log(
`Fee collection for chain ${config.network} initiated (${
tokensToSwap.length
} orders). Expecting proceeds of ${expectedUnits.toString()} ${await buyTokenContract.symbol()}!\n\nFollow the progress at ${
} orders). Expecting proceeds of ${expectedUnitsFormatted} ${await buyTokenContract.symbol()}!\n\nFollow the progress at ${
networkSpecificConfigs[config.network].explorer
}/address/${config.gpv2Settlement}`
);
Expand Down

0 comments on commit f55fe3b

Please sign in to comment.