Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BigNumber } from 'bignumber.js';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { Hex } from '@metamask/utils';
import { act } from '@testing-library/react';

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

async function runHook() {
async function runHook(
tokenAddresses?: Hex[],
mockConfirmation?: Confirmation,
) {
const response = renderHookWithConfirmContextProvider(
() =>
useDappSwapUSDValues({
tokenAddresses: [
tokenAddresses: tokenAddresses ?? [
'0x0000000000000000000000000000000000000000',
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
'0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
],
destTokenAddress: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
}),
getMockConfirmStateForTransaction(mockSwapConfirmation as Confirmation),
getMockConfirmStateForTransaction(
mockConfirmation ?? (mockSwapConfirmation as Confirmation),
),
);

await act(async () => {
Expand Down Expand Up @@ -105,4 +112,35 @@ describe('useDappSwapUSDValues', () => {
),
).toBe('0.00');
});

it('return correct fiat rates token on Polygon', async () => {
jest.spyOn(Utils, 'fetchTokenExchangeRates').mockResolvedValue({
'0x0000000000000000000000000000000000001010': 4052.27,
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
});
Copy link

Choose a reason for hiding this comment

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

Bug: Test mocks unrequested data masking logic bug

The test mocks fetchTokenExchangeRates to return a rate for the Polygon native asset (0x...1010), even though the hook implementation does not request this address. This mismatch between the mock and the actual implementation masks the bug where the rate is never fetched in production.

Fix in Cursor Fix in Web

jest.spyOn(TokenUtils, 'fetchAllTokenDetails').mockResolvedValue({
'0x0000000000000000000000000000000000000000': {
symbol: 'POL',
decimals: '18',
} as TokenStandAndDetails,
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': {
symbol: 'USDT',
decimals: '6',
} as TokenStandAndDetails,
});

const result = await runHook(
[
'0x0000000000000000000000000000000000000000',
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
],
{ ...mockSwapConfirmation, chainId: CHAIN_IDS.POLYGON } as Confirmation,
);

expect(result.fiatRates).toEqual({
'0x0000000000000000000000000000000000000000': 4052.27,
'0x0000000000000000000000000000000000001010': 4052.27,
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': 0.999804,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { BigNumber } from 'bignumber.js';
import { getNativeTokenAddress } from '@metamask/assets-controllers';
import { Hex } from '@metamask/utils';
import {
getNativeAssetForChainId,
isNativeAddress,
} from '@metamask/bridge-controller';
import { TransactionMeta } from '@metamask/transaction-controller';
import { getNativeAssetForChainId } from '@metamask/bridge-controller';
import { CHAIN_IDS, TransactionMeta } from '@metamask/transaction-controller';
import { useCallback } from 'react';

import { TokenStandAndDetails } from '../../../../../store/actions';
import { fetchTokenExchangeRates } from '../../../../../helpers/utils/util';
import { useAsyncResult } from '../../../../../hooks/useAsync';
import { isNativeAddress } from '../../../../../helpers/utils/token-insights';
import {
fetchAllTokenDetails,
getTokenValueFromRecord,
} from '../../../utils/token';
import { useConfirmContext } from '../../../context/confirm';

const POLYGON_NATIVE_ASSET = '0x0000000000000000000000000000000000001010';
Copy link
Member

Choose a reason for hiding this comment

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

We can delegate to getNativeTokenAddress for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the function is returning different address 0x0000000000000000000000000000000000000000

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We should use getNativeTokenAddress.. it should return 0x0000000000000000000000000000000000001010 for Polygon network and 0x0000000000000000000000000000000000000000 for anything else.


export function useDappSwapUSDValues({
tokenAddresses = [],
destTokenAddress,
Expand All @@ -32,11 +32,22 @@ export function useDappSwapUSDValues({

const { value: fiatRates, pending: fiatRatesPending } = useAsyncResult<
Record<Hex, number | undefined>
>(() => {
>(async () => {
const addresses = tokenAddresses.filter(
(tokenAddress) => !isNativeAddress(tokenAddress),
);
return fetchTokenExchangeRates('usd', addresses as Hex[], chainId);
const exchangeRates = await fetchTokenExchangeRates(
'usd',
addresses as Hex[],
chainId,
);

if (chainId === CHAIN_IDS.POLYGON) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be encapsulated inside fetchTokenExchangeRates for all usages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure of other code will need this too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally the dedupe should be done in the controller side.. but I have just noticed that there was a massive breaking change added to the codefi-v2.ts in core https://github.com/MetaMask/core/pull/7119/files#diff-5801aea8052edaaf64cbe9cbe48734d6e35865a36f37d8a96c4e538536c7b1c2
As this filtering should be short lived I think we can keep it in the useDappSwapUSDValues.ts

Copy link
Member

Choose a reason for hiding this comment

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

Unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep adding those

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR is upadated

const nativeAddress = getNativeAssetForChainId(chainId).address;
exchangeRates[nativeAddress] = exchangeRates[POLYGON_NATIVE_ASSET];
Copy link
Member

Choose a reason for hiding this comment

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

What is the difference between nativeAddress and POLYGON_NATIVE_ASSET?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

0x0000000000000000000000000000000000001010 vs 0x0000000000000000000000000000000000000000

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Price service is using first one.

Copy link
Contributor

Choose a reason for hiding this comment

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

0x0000000000000000000000000000000000001010 is the native address for Polygon.. are we not getting that address when swapping Pol on Polygon? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

ok the reason for it is because our Bridge API and the dapp swap considers the zero address as the native address for Polygon

}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes the issue for pol token only, I could not find a more generic fix.


return exchangeRates;
}, [chainId, tokenAddresses?.length]);

const { value: tokenDetails, pending: tokenDetailsPending } = useAsyncResult<
Expand Down
Loading