Skip to content

Commit a9e6d00

Browse files
committed
Merge branch 'fix/coin-overview-threshold' of github.com:MetaMask/metamask-extension into fix/coin-overview-threshold
2 parents 7b6e84d + f3e9aef commit a9e6d00

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

ui/ducks/bridge/selectors.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { createSelector } from 'reselect';
99
import type { GasFeeEstimates } from '@metamask/gas-fee-controller';
1010
import { BigNumber } from 'bignumber.js';
1111
import { calcTokenAmount } from '@metamask/notification-services-controller/push-services';
12-
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
1312
import {
1413
MultichainNetworks,
14+
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
1515
MULTICHAIN_PROVIDER_CONFIGS,
16+
///: END:ONLY_INCLUDE_IF
1617
} from '../../../shared/constants/multichain/networks';
17-
///: END:ONLY_INCLUDE_IF
1818
import {
1919
getIsBridgeEnabled,
2020
getMarketData,
@@ -60,12 +60,14 @@ import {
6060
FEATURED_RPCS,
6161
} from '../../../shared/constants/network';
6262
import {
63+
getMultichainCoinRates,
6364
getMultichainProviderConfig,
6465
getImageForChainId,
6566
} from '../../selectors/multichain';
67+
import { getAssetsRates } from '../../selectors/assets';
6668
import {
67-
exchangeRatesFromNativeAndCurrencyRates,
6869
exchangeRateFromMarketData,
70+
exchangeRatesFromNativeAndCurrencyRates,
6971
tokenPriceInNativeAsset,
7072
} from './utils';
7173
import type { BridgeState } from './bridge';
@@ -227,27 +229,43 @@ export const getBridgeSortOrder = (state: BridgeAppState) =>
227229
export const getFromTokenConversionRate = createSelector(
228230
getFromChain,
229231
getMarketData,
232+
getAssetsRates,
230233
getFromToken,
231234
getUSDConversionRate,
235+
getMultichainCoinRates,
232236
getConversionRate,
233237
(state) => state.bridge.fromTokenExchangeRate,
234238
(
235239
fromChain,
236240
marketData,
241+
assetsRates,
237242
fromToken,
238243
nativeToUsdRate,
244+
nonEvmNativeToUsdRate,
239245
nativeToCurrencyRate,
240246
fromTokenExchangeRate,
241247
) => {
242-
if (fromChain?.chainId && fromToken && marketData) {
248+
if (fromChain?.chainId && fromToken) {
249+
if (fromChain.chainId === MultichainNetworks.SOLANA) {
250+
// For SOLANA tokens, we use the conversion rates provided by the multichain rates controller
251+
const tokenToNativeAssetRate = tokenPriceInNativeAsset(
252+
assetsRates[fromToken.address]?.rate,
253+
nonEvmNativeToUsdRate.sol.conversionRate,
254+
);
255+
return exchangeRatesFromNativeAndCurrencyRates(
256+
tokenToNativeAssetRate,
257+
nonEvmNativeToUsdRate.sol.conversionRate,
258+
nonEvmNativeToUsdRate.sol.usdConversionRate,
259+
);
260+
}
261+
// For EVM tokens, we use the market data to get the exchange rate
243262
const tokenToNativeAssetRate =
244263
exchangeRateFromMarketData(
245264
fromChain.chainId,
246265
fromToken.address,
247266
marketData,
248267
) ??
249268
tokenPriceInNativeAsset(fromTokenExchangeRate, nativeToCurrencyRate);
250-
251269
return exchangeRatesFromNativeAndCurrencyRates(
252270
tokenToNativeAssetRate,
253271
nativeToCurrencyRate,

ui/ducks/bridge/utils.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getTransaction1559GasFeeEstimates } from '../../pages/swaps/swaps.util'
1313
import { fetchTokenExchangeRates as fetchTokenExchangeRatesUtil } from '../../helpers/utils/util';
1414
import { formatChainIdToHex } from '../../../shared/modules/bridge-utils/caip-formatters';
1515
import { MultichainNetworks } from '../../../shared/constants/multichain/networks';
16+
import fetchWithCache from '../../../shared/lib/fetch-with-cache';
1617

1718
type GasFeeEstimate = {
1819
suggestedMaxPriorityFeePerGas: string;
@@ -79,9 +80,24 @@ const fetchTokenExchangeRates = async (
7980
currency: string,
8081
...tokenAddresses: string[]
8182
) => {
82-
// TODO fetch exchange rates for solana
8383
if (chainId === MultichainNetworks.SOLANA) {
84-
return {};
84+
const queryParams = new URLSearchParams({
85+
assetIds: tokenAddresses.join(','),
86+
includeMarketData: 'true',
87+
vsCurrency: currency,
88+
});
89+
const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;
90+
const exchangeRates = await fetchWithCache({
91+
url,
92+
fetchOptions: {
93+
method: 'GET',
94+
headers: 'metamask',
95+
},
96+
cacheOptions: { cacheRefreshTime: 0 },
97+
functionName: 'fetchTokenExchangeRates',
98+
});
99+
100+
return exchangeRates;
85101
}
86102

87103
const exchangeRates = await fetchTokenExchangeRatesUtil(
@@ -112,6 +128,9 @@ export const getTokenExchangeRate = async (request: {
112128
currency,
113129
tokenAddress,
114130
);
131+
if (chainId === MultichainNetworks.SOLANA) {
132+
return exchangeRates?.[tokenAddress];
133+
}
115134
// The exchange rate can be checksummed or not, so we need to check both
116135
const exchangeRate =
117136
exchangeRates?.[toChecksumAddress(tokenAddress)] ??

ui/hooks/bridge/useBridgeExchangeRates.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import { useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
3+
import { isStrictHexString } from '@metamask/utils';
34
import {
45
getBridgeQuotes,
56
getQuoteRequest,
67
getToChain,
78
} from '../../ducks/bridge/selectors';
89
import { getMarketData, getParticipateInMetaMetrics } from '../../selectors';
910
import { getCurrentCurrency } from '../../ducks/metamask/metamask';
10-
import { getCurrentChainId } from '../../../shared/modules/selectors/networks';
1111
import {
1212
setDestTokenExchangeRates,
1313
setDestTokenUsdExchangeRates,
1414
setSrcTokenExchangeRates,
1515
} from '../../ducks/bridge/bridge';
1616
import { exchangeRateFromMarketData } from '../../ducks/bridge/utils';
17+
import { useMultichainSelector } from '../useMultichainSelector';
18+
import { getMultichainCurrentChainId } from '../../selectors/multichain';
1719

1820
export const useBridgeExchangeRates = () => {
1921
const { srcTokenAddress, destTokenAddress } = useSelector(getQuoteRequest);
2022
const { activeQuote } = useSelector(getBridgeQuotes);
21-
const chainId = useSelector(getCurrentChainId);
23+
const chainId = useMultichainSelector(getMultichainCurrentChainId);
2224
const toChain = useSelector(getToChain);
2325
const isMetaMetricsEnabled = useSelector(getParticipateInMetaMetrics);
2426

@@ -42,7 +44,7 @@ export const useBridgeExchangeRates = () => {
4244

4345
// Fetch exchange rates for selected src token if not found in marketData
4446
useEffect(() => {
45-
if (fromChainId && fromTokenAddress) {
47+
if (fromChainId && fromTokenAddress && isStrictHexString(fromChainId)) {
4648
const exchangeRate = exchangeRateFromMarketData(
4749
fromChainId,
4850
fromTokenAddress,

0 commit comments

Comments
 (0)