Skip to content

Commit 033e926

Browse files
committed
fix: EVM exchange rates
1 parent 8b5525d commit 033e926

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

shared/modules/bridge-utils/caip-formatters.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import {
66
toCaipChainId,
77
KnownCaipNamespace,
88
hexToBigInt,
9+
parseCaipChainId,
10+
isCaipReference,
911
} from '@metamask/utils';
1012
import { zeroAddress, toChecksumAddress } from 'ethereumjs-util';
1113
import { MultichainNetworks } from '../../constants/multichain/networks';
1214
import { ChainId } from '../../types/bridge';
13-
import { hexToDecimal } from '../conversion.utils';
15+
import { decimalToPrefixedHex, hexToDecimal } from '../conversion.utils';
1416
import { MULTICHAIN_NATIVE_CURRENCY_TO_CAIP19 } from '../../constants/multichain/assets';
17+
import { isValidNumber } from './validators';
1518

1619
// Converts a chainId to a CaipChainId
1720
export const normalizeChainId = (
@@ -31,6 +34,7 @@ export const normalizeChainId = (
3134
}
3235
return toCaipChainId(KnownCaipNamespace.Eip155, chainIdString);
3336
};
37+
3438
export const formatChainIdToDec = (chainId: number | Hex | CaipChainId) => {
3539
if (isStrictHexString(chainId)) {
3640
return Number(hexToDecimal(chainId));
@@ -45,6 +49,19 @@ export const formatChainIdToDec = (chainId: number | Hex | CaipChainId) => {
4549
return chainId;
4650
};
4751

52+
export const formatChainIdToHex = (chainId: number | Hex | CaipChainId) => {
53+
if (isStrictHexString(chainId)) {
54+
return chainId;
55+
}
56+
if (isCaipChainId(chainId)) {
57+
const { reference } = parseCaipChainId(chainId);
58+
if (isCaipReference(reference) && isValidNumber(reference)) {
59+
return decimalToPrefixedHex(reference);
60+
}
61+
}
62+
return undefined;
63+
};
64+
4865
export const formatAddressToString = (address?: string) => {
4966
if (!address) {
5067
return undefined;

ui/ducks/bridge/utils.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isStrictHexString, type Hex } from '@metamask/utils';
1+
import { type CaipChainId, isStrictHexString, type Hex } from '@metamask/utils';
22
import { BigNumber } from 'bignumber.js';
33
import type { ContractMarketData } from '@metamask/assets-controllers';
44
import {
@@ -8,9 +8,11 @@ import {
88
import { toChecksumAddress } from 'ethereumjs-util';
99
import { decGWEIToHexWEI } from '../../../shared/modules/conversion.utils';
1010
import { Numeric } from '../../../shared/modules/Numeric';
11-
import { type TxData } from '../../../shared/types/bridge';
11+
import { ChainId, type TxData } from '../../../shared/types/bridge';
1212
import { getTransaction1559GasFeeEstimates } from '../../pages/swaps/swaps.util';
1313
import { fetchTokenExchangeRates as fetchTokenExchangeRatesUtil } from '../../helpers/utils/util';
14+
import { formatChainIdToHex } from '../../../shared/modules/bridge-utils/caip-formatters';
15+
import { MultichainNetworks } from '../../../shared/constants/multichain/networks';
1416

1517
type GasFeeEstimate = {
1618
suggestedMaxPriorityFeePerGas: string;
@@ -73,14 +75,19 @@ export const getTxGasEstimates = async ({
7375
};
7476

7577
const fetchTokenExchangeRates = async (
76-
chainId: string,
78+
chainId: Hex | CaipChainId | ChainId,
7779
currency: string,
7880
...tokenAddresses: string[]
7981
) => {
82+
// TODO fetch exchange rates for solana
83+
if (chainId === MultichainNetworks.SOLANA) {
84+
return {};
85+
}
86+
8087
const exchangeRates = await fetchTokenExchangeRatesUtil(
8188
currency,
8289
tokenAddresses,
83-
chainId,
90+
formatChainIdToHex(chainId),
8491
);
8592
return Object.keys(exchangeRates).reduce(
8693
(acc: Record<string, number | undefined>, address) => {
@@ -95,7 +102,7 @@ const fetchTokenExchangeRates = async (
95102
// rate is not available in the TokenRatesController, which happens when the selected token has not been
96103
// imported into the wallet
97104
export const getTokenExchangeRate = async (request: {
98-
chainId: Hex;
105+
chainId: Hex | CaipChainId | ChainId;
99106
tokenAddress: string;
100107
currency: string;
101108
}) => {
@@ -115,11 +122,12 @@ export const getTokenExchangeRate = async (request: {
115122
// This extracts a token's exchange rate from the marketData state object
116123
// These exchange rates are against the native asset of the chain
117124
export const exchangeRateFromMarketData = (
118-
chainId: string,
125+
chainId: Hex | ChainId,
119126
tokenAddress: string,
120127
marketData?: Record<string, ContractMarketData>,
128+
// TODO get market data for solana
121129
) =>
122-
isStrictHexString(tokenAddress)
130+
isStrictHexString(tokenAddress) && isStrictHexString(chainId)
123131
? marketData?.[chainId]?.[tokenAddress]?.price
124132
: undefined;
125133

ui/hooks/bridge/useBridgeExchangeRates.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '../../ducks/bridge/selectors';
88
import { getMarketData, getParticipateInMetaMetrics } from '../../selectors';
99
import { getCurrentCurrency } from '../../ducks/metamask/metamask';
10-
import { decimalToPrefixedHex } from '../../../shared/modules/conversion.utils';
1110
import { getCurrentChainId } from '../../../shared/modules/selectors/networks';
1211
import {
1312
setDestTokenExchangeRates,
@@ -34,11 +33,9 @@ export const useBridgeExchangeRates = () => {
3433
const toTokenAddress = activeQuote
3534
? activeQuote.quote.destAsset.address
3635
: destTokenAddress;
37-
const fromChainId = activeQuote
38-
? decimalToPrefixedHex(activeQuote.quote.srcChainId)
39-
: chainId;
36+
const fromChainId = activeQuote ? activeQuote.quote.srcChainId : chainId;
4037
const toChainId = activeQuote
41-
? decimalToPrefixedHex(activeQuote.quote.destChainId)
38+
? activeQuote.quote.destChainId
4239
: toChain?.chainId;
4340

4441
const marketData = useSelector(getMarketData);

0 commit comments

Comments
 (0)