Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expectedUnits to include two decimal places in output #30

Merged
merged 1 commit into from
Sep 27, 2024

Conversation

squadgazzz
Copy link
Contributor

@squadgazzz squadgazzz commented Sep 27, 2024

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

Created a unit test to see the difference(which is not a part of this PR)
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}`
                );
            }
        });
    });
});

@squadgazzz squadgazzz force-pushed the expectedUnits-decimal-formatting branch from 6de53e9 to e3ca439 Compare September 27, 2024 08:25
Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. 👍

Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

@squadgazzz squadgazzz merged commit f55fe3b into main Sep 27, 2024
1 check passed
@squadgazzz squadgazzz deleted the expectedUnits-decimal-formatting branch September 27, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants