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: fix usd amount in swap notifications #25444

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ui/helpers/utils/notification.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ export const getUsdAmount = (amount: string, decimals: string, usd: string) => {
return '';
}

const amountInEther = getAmount(amount, decimals);
const numericAmount = parseFloat(amountInEther) * parseFloat(usd);
const amountInEther = calcTokenAmount(
amount,
parseFloat(decimals),
).toNumber();
Comment on lines +263 to +266
Copy link
Contributor

Choose a reason for hiding this comment

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

My only concern is how this is a different method compared to portfolio.

In portfolio we are using Ethers+BigNumber.js so little conversion loss.
But extension may have some formatting.

... we can deal with this in the future lolol

const numericAmount = parseFloat(`${amountInEther}`) * parseFloat(usd);

return formatAmount(numericAmount);
};
Expand Down
36 changes: 36 additions & 0 deletions ui/helpers/utils/notification.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
formatMenuItemDate,
getLeadingZeroCount,
getRandomKey,
getUsdAmount,
} from './notification.util';

describe('formatMenuItemDate', () => {
Expand Down Expand Up @@ -182,3 +183,38 @@ describe('getRandomKey', () => {
expect(result).toContain('testtext');
});
});

describe('getUsdAmount', () => {
it('should return formatted USD amount based on token amount, decimals, and USD rate', () => {
const amount = '1000000000000000000'; // 1 Ether (1e18 wei)
const decimals = '18';
const usdRate = '2000'; // 1 Ether = $2000

const result = getUsdAmount(amount, decimals, usdRate);
expect(result).toBe('2K'); // Since 1 Ether * $2000 = $2000, formatted as '2K'
});

it('should return an empty string if any of the parameters are missing', () => {
expect(getUsdAmount('', '18', '2000')).toBe('');
expect(getUsdAmount('1000000000000000000', '', '2000')).toBe('');
expect(getUsdAmount('1000000000000000000', '18', '')).toBe('');
});

it('should handle small amounts correctly', () => {
const amount = '1000000000000000'; // 0.001 Ether (1e15 wei)
const decimals = '18';
const usdRate = '1500'; // 1 Ether = $1500

const result = getUsdAmount(amount, decimals, usdRate);
expect(result).toBe('1.5'); // Since 0.001 Ether * $1500 = $1.5
});

it('should handle large amounts correctly', () => {
const amount = '5000000000000000000000'; // 5000 Ether
const decimals = '18';
const usdRate = '1000'; // 1 Ether = $1000

const result = getUsdAmount(amount, decimals, usdRate);
expect(result).toBe('5M'); // Since 5000 Ether * $1000 = $5,000,000, formatted as '5M'
});
});