Skip to content

feat: convert non-evm amounts to fiat #30568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 26, 2025
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
28 changes: 23 additions & 5 deletions ui/ducks/bridge/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { createSelector } from 'reselect';
import type { GasFeeEstimates } from '@metamask/gas-fee-controller';
import { BigNumber } from 'bignumber.js';
import { calcTokenAmount } from '@metamask/notification-services-controller/push-services';
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
import {
MultichainNetworks,
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
MULTICHAIN_PROVIDER_CONFIGS,
///: END:ONLY_INCLUDE_IF
} from '../../../shared/constants/multichain/networks';
///: END:ONLY_INCLUDE_IF
import {
getIsBridgeEnabled,
getMarketData,
Expand Down Expand Up @@ -60,12 +60,14 @@ import {
FEATURED_RPCS,
} from '../../../shared/constants/network';
import {
getMultichainCoinRates,
getMultichainProviderConfig,
getImageForChainId,
} from '../../selectors/multichain';
import { getAssetsRates } from '../../selectors/assets';
import {
exchangeRatesFromNativeAndCurrencyRates,
exchangeRateFromMarketData,
exchangeRatesFromNativeAndCurrencyRates,
tokenPriceInNativeAsset,
} from './utils';
import type { BridgeState } from './bridge';
Expand Down Expand Up @@ -227,27 +229,43 @@ export const getBridgeSortOrder = (state: BridgeAppState) =>
export const getFromTokenConversionRate = createSelector(
getFromChain,
getMarketData,
getAssetsRates,
getFromToken,
getUSDConversionRate,
getMultichainCoinRates,
getConversionRate,
(state) => state.bridge.fromTokenExchangeRate,
(
fromChain,
marketData,
assetsRates,
fromToken,
nativeToUsdRate,
nonEvmNativeToUsdRate,
nativeToCurrencyRate,
fromTokenExchangeRate,
) => {
if (fromChain?.chainId && fromToken && marketData) {
if (fromChain?.chainId && fromToken) {
if (fromChain.chainId === MultichainNetworks.SOLANA) {
// For SOLANA tokens, we use the conversion rates provided by the multichain rates controller
const tokenToNativeAssetRate = tokenPriceInNativeAsset(
assetsRates[fromToken.address]?.rate,
nonEvmNativeToUsdRate.sol.conversionRate,
);
return exchangeRatesFromNativeAndCurrencyRates(
tokenToNativeAssetRate,
nonEvmNativeToUsdRate.sol.conversionRate,
nonEvmNativeToUsdRate.sol.usdConversionRate,
);
}
// For EVM tokens, we use the market data to get the exchange rate
const tokenToNativeAssetRate =
exchangeRateFromMarketData(
fromChain.chainId,
fromToken.address,
marketData,
) ??
tokenPriceInNativeAsset(fromTokenExchangeRate, nativeToCurrencyRate);

return exchangeRatesFromNativeAndCurrencyRates(
tokenToNativeAssetRate,
nativeToCurrencyRate,
Expand Down
23 changes: 21 additions & 2 deletions ui/ducks/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getTransaction1559GasFeeEstimates } from '../../pages/swaps/swaps.util'
import { fetchTokenExchangeRates as fetchTokenExchangeRatesUtil } from '../../helpers/utils/util';
import { formatChainIdToHex } from '../../../shared/modules/bridge-utils/caip-formatters';
import { MultichainNetworks } from '../../../shared/constants/multichain/networks';
import fetchWithCache from '../../../shared/lib/fetch-with-cache';

type GasFeeEstimate = {
suggestedMaxPriorityFeePerGas: string;
Expand Down Expand Up @@ -79,9 +80,24 @@ const fetchTokenExchangeRates = async (
currency: string,
...tokenAddresses: string[]
) => {
// TODO fetch exchange rates for solana
if (chainId === MultichainNetworks.SOLANA) {
return {};
const queryParams = new URLSearchParams({
assetIds: tokenAddresses.join(','),
includeMarketData: 'true',
vsCurrency: currency,
});
const url = `https://price.api.cx.metamask.io/v3/spot-prices?${queryParams}`;
const exchangeRates = await fetchWithCache({
url,
fetchOptions: {
method: 'GET',
headers: 'metamask',
},
cacheOptions: { cacheRefreshTime: 0 },
functionName: 'fetchTokenExchangeRates',
});

return exchangeRates;
}

const exchangeRates = await fetchTokenExchangeRatesUtil(
Expand Down Expand Up @@ -112,6 +128,9 @@ export const getTokenExchangeRate = async (request: {
currency,
tokenAddress,
);
if (chainId === MultichainNetworks.SOLANA) {
return exchangeRates?.[tokenAddress];
}
// The exchange rate can be checksummed or not, so we need to check both
const exchangeRate =
exchangeRates?.[toChecksumAddress(tokenAddress)] ??
Expand Down
8 changes: 5 additions & 3 deletions ui/hooks/bridge/useBridgeExchangeRates.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { isStrictHexString } from '@metamask/utils';
import {
getBridgeQuotes,
getQuoteRequest,
getToChain,
} from '../../ducks/bridge/selectors';
import { getMarketData, getParticipateInMetaMetrics } from '../../selectors';
import { getCurrentCurrency } from '../../ducks/metamask/metamask';
import { getCurrentChainId } from '../../../shared/modules/selectors/networks';
import {
setDestTokenExchangeRates,
setDestTokenUsdExchangeRates,
setSrcTokenExchangeRates,
} from '../../ducks/bridge/bridge';
import { exchangeRateFromMarketData } from '../../ducks/bridge/utils';
import { useMultichainSelector } from '../useMultichainSelector';
import { getMultichainCurrentChainId } from '../../selectors/multichain';

export const useBridgeExchangeRates = () => {
const { srcTokenAddress, destTokenAddress } = useSelector(getQuoteRequest);
const { activeQuote } = useSelector(getBridgeQuotes);
const chainId = useSelector(getCurrentChainId);
const chainId = useMultichainSelector(getMultichainCurrentChainId);
const toChain = useSelector(getToChain);
const isMetaMetricsEnabled = useSelector(getParticipateInMetaMetrics);

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

// Fetch exchange rates for selected src token if not found in marketData
useEffect(() => {
if (fromChainId && fromTokenAddress) {
if (fromChainId && fromTokenAddress && isStrictHexString(fromChainId)) {
const exchangeRate = exchangeRateFromMarketData(
fromChainId,
fromTokenAddress,
Expand Down
Loading