Skip to content

Commit 8f631e6

Browse files
release(runway): cherry-pick fix: dapp swap fix conversion rate for pol native token (#38136)
- 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: https://github.com/MetaMask/MetaMask-planning/issues/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 rate lookup for Polygon native token by mapping native address to `0x0000000000000000000000000000000000001010` and adds tests. > > - **Hooks**: > - Update `ui/pages/confirmations/hooks/transactions/dapp-swap-comparison/useDappSwapUSDValues.ts` to adjust fiat rate fetching: > - When `chainId === CHAIN_IDS.POLYGON`, map the native asset address to the rate from `0x0000000000000000000000000000000000001010`. > - Wrap rate fetching in an async function to inject the Polygon-specific mapping. > - **Tests**: > - Enhance `useDappSwapUSDValues.test.ts`: > - Allow `runHook` to accept custom `tokenAddresses` and `mockConfirmation`. > - Add Polygon-specific test asserting correct fiat rates for native POL (`0x...0000`) and `0x0000000000000000000000000000000000001010`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c7fe193. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> [0aa98dc](0aa98dc) Co-authored-by: Jyoti Puri <[email protected]>
1 parent eea4d3d commit 8f631e6

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
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 () => {
@@ -98,4 +105,35 @@ describe('useDappSwapUSDValues', () => {
98105
'0xfdcc3dd6671eab0709a4c0f3f53de9a333d80798': 1,
99106
});
100107
});
108+
109+
it('return correct fiat rates token on Polygon', async () => {
110+
jest.spyOn(Utils, 'fetchTokenExchangeRates').mockResolvedValue({
111+
'0x0000000000000000000000000000000000001010': 4052.27,
112+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
113+
});
114+
jest.spyOn(TokenUtils, 'fetchAllTokenDetails').mockResolvedValue({
115+
'0x0000000000000000000000000000000000000000': {
116+
symbol: 'POL',
117+
decimals: '18',
118+
} as TokenStandAndDetails,
119+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': {
120+
symbol: 'USDT',
121+
decimals: '6',
122+
} as TokenStandAndDetails,
123+
});
124+
125+
const result = await runHook(
126+
[
127+
'0x0000000000000000000000000000000000000000',
128+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
129+
],
130+
{ ...mockSwapConfirmation, chainId: CHAIN_IDS.POLYGON } as Confirmation,
131+
);
132+
133+
expect(result.fiatRates).toEqual({
134+
'0x0000000000000000000000000000000000000000': 4052.27,
135+
'0x0000000000000000000000000000000000001010': 4052.27,
136+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
137+
});
138+
});
101139
});

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
getNativeAssetForChainId,
66
isNativeAddress,
77
} from '@metamask/bridge-controller';
8-
import { TransactionMeta } from '@metamask/transaction-controller';
8+
import { CHAIN_IDS, TransactionMeta } from '@metamask/transaction-controller';
99
import { useCallback } from 'react';
1010

1111
import { TokenStandAndDetails } from '../../../../../store/actions';
@@ -15,6 +15,8 @@ import { fetchAllTokenDetails } from '../../../utils/token';
1515
import { getTokenValueFromRecord } from '../../../utils/dapp-swap-comparison-utils';
1616
import { useConfirmContext } from '../../../context/confirm';
1717

18+
const POLYGON_NATIVE_ASSET = '0x0000000000000000000000000000000000001010';
19+
1820
export function useDappSwapUSDValues({
1921
tokenAddresses = [],
2022
destTokenAddress,
@@ -30,11 +32,22 @@ export function useDappSwapUSDValues({
3032

3133
const { value: fiatRates, pending: fiatRatesPending } = useAsyncResult<
3234
Record<Hex, number | undefined>
33-
>(() => {
35+
>(async () => {
3436
const addresses = tokenAddresses.filter(
3537
(tokenAddress) => !isNativeAddress(tokenAddress),
3638
);
37-
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;
3851
}, [chainId, tokenAddresses?.length]);
3952

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

0 commit comments

Comments
 (0)