Skip to content

Commit 3d08f96

Browse files
authored
fix: cp-13.10.1 dapp swap fix conversion rate for pol native token (#38102)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Fix conversion rate for POL native token. ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: ## **Related issues** Fixes: MetaMask/MetaMask-planning#6327 ## **Manual testing steps** 1. Trigger swap including POL native token 2. Check that metrics are recorded correctly and dapp-swap UI shows correct values ## **Screenshots/Recordings** TODO ## **Pre-merge author checklist** - [X] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Fixes USD conversion for Polygon native token by mapping POL’s special address and adds targeted unit tests. > > - **Hook `useDappSwapUSDValues` (`ui/pages/.../useDappSwapUSDValues.ts`)**: > - Map Polygon native token USD rate: when `chainId === CHAIN_IDS.POLYGON`, set the native asset’s address rate from `0x0000000000000000000000000000000000001010`. > - Switch `isNativeAddress` to `helpers/utils/token-insights` and import `CHAIN_IDS`. > - Refactor exchange-rate fetch to async IIFE within `useAsyncResult`. > - **Tests (`ui/pages/.../useDappSwapUSDValues.test.ts`)**: > - Enhance `runHook` to accept custom token addresses and confirmation. > - Add Polygon-specific test asserting fiat rates include both native addresses with correct USD value. > - Mock token details and rates accordingly. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit ccd13a6. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 2765f79 commit 3d08f96

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

ui/pages/confirmations/hooks/transactions/dapp-swap-comparison/useDappSwapUSDValues.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { BigNumber } from 'bignumber.js';
2+
import { CHAIN_IDS } from '@metamask/transaction-controller';
3+
import { Hex } from '@metamask/utils';
24
import { act } from '@testing-library/react';
35

46
import { getMockConfirmStateForTransaction } from '../../../../../../test/data/confirmations/helper';
@@ -10,18 +12,23 @@ import * as TokenUtils from '../../../utils/token';
1012
import { Confirmation } from '../../../types/confirm';
1113
import { useDappSwapUSDValues } from './useDappSwapUSDValues';
1214

13-
async function runHook() {
15+
async function runHook(
16+
tokenAddresses?: Hex[],
17+
mockConfirmation?: Confirmation,
18+
) {
1419
const response = renderHookWithConfirmContextProvider(
1520
() =>
1621
useDappSwapUSDValues({
17-
tokenAddresses: [
22+
tokenAddresses: tokenAddresses ?? [
1823
'0x0000000000000000000000000000000000000000',
1924
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
2025
'0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
2126
],
2227
destTokenAddress: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
2328
}),
24-
getMockConfirmStateForTransaction(mockSwapConfirmation as Confirmation),
29+
getMockConfirmStateForTransaction(
30+
mockConfirmation ?? (mockSwapConfirmation as Confirmation),
31+
),
2532
);
2633

2734
await act(async () => {
@@ -105,4 +112,35 @@ describe('useDappSwapUSDValues', () => {
105112
),
106113
).toBe('0.00');
107114
});
115+
116+
it('return correct fiat rates token on Polygon', async () => {
117+
jest.spyOn(Utils, 'fetchTokenExchangeRates').mockResolvedValue({
118+
'0x0000000000000000000000000000000000001010': 4052.27,
119+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
120+
});
121+
jest.spyOn(TokenUtils, 'fetchAllTokenDetails').mockResolvedValue({
122+
'0x0000000000000000000000000000000000000000': {
123+
symbol: 'POL',
124+
decimals: '18',
125+
} as TokenStandAndDetails,
126+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': {
127+
symbol: 'USDT',
128+
decimals: '6',
129+
} as TokenStandAndDetails,
130+
});
131+
132+
const result = await runHook(
133+
[
134+
'0x0000000000000000000000000000000000000000',
135+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
136+
],
137+
{ ...mockSwapConfirmation, chainId: CHAIN_IDS.POLYGON } as Confirmation,
138+
);
139+
140+
expect(result.fiatRates).toEqual({
141+
'0x0000000000000000000000000000000000000000': 4052.27,
142+
'0x0000000000000000000000000000000000001010': 4052.27,
143+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
144+
});
145+
});
108146
});

ui/pages/confirmations/hooks/transactions/dapp-swap-comparison/useDappSwapUSDValues.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { BigNumber } from 'bignumber.js';
22
import { getNativeTokenAddress } from '@metamask/assets-controllers';
33
import { Hex } from '@metamask/utils';
4-
import {
5-
getNativeAssetForChainId,
6-
isNativeAddress,
7-
} from '@metamask/bridge-controller';
8-
import { TransactionMeta } from '@metamask/transaction-controller';
4+
import { getNativeAssetForChainId } from '@metamask/bridge-controller';
5+
import { CHAIN_IDS, TransactionMeta } from '@metamask/transaction-controller';
96
import { useCallback } from 'react';
107

118
import { TokenStandAndDetails } from '../../../../../store/actions';
129
import { fetchTokenExchangeRates } from '../../../../../helpers/utils/util';
1310
import { useAsyncResult } from '../../../../../hooks/useAsync';
11+
import { isNativeAddress } from '../../../../../helpers/utils/token-insights';
1412
import {
1513
fetchAllTokenDetails,
1614
getTokenValueFromRecord,
1715
} from '../../../utils/token';
1816
import { useConfirmContext } from '../../../context/confirm';
1917

18+
const POLYGON_NATIVE_ASSET = '0x0000000000000000000000000000000000001010';
19+
2020
export function useDappSwapUSDValues({
2121
tokenAddresses = [],
2222
destTokenAddress,
@@ -32,11 +32,22 @@ export function useDappSwapUSDValues({
3232

3333
const { value: fiatRates, pending: fiatRatesPending } = useAsyncResult<
3434
Record<Hex, number | undefined>
35-
>(() => {
35+
>(async () => {
3636
const addresses = tokenAddresses.filter(
3737
(tokenAddress) => !isNativeAddress(tokenAddress),
3838
);
39-
return fetchTokenExchangeRates('usd', addresses as Hex[], chainId);
39+
const exchangeRates = await fetchTokenExchangeRates(
40+
'usd',
41+
addresses as Hex[],
42+
chainId,
43+
);
44+
45+
if (chainId === CHAIN_IDS.POLYGON) {
46+
const nativeAddress = getNativeAssetForChainId(chainId).address;
47+
exchangeRates[nativeAddress] = exchangeRates[POLYGON_NATIVE_ASSET];
48+
}
49+
50+
return exchangeRates;
4051
}, [chainId, tokenAddresses?.length]);
4152

4253
const { value: tokenDetails, pending: tokenDetailsPending } = useAsyncResult<

0 commit comments

Comments
 (0)