Skip to content

Commit

Permalink
chore: show solana as src or dest network
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Feb 4, 2025
1 parent bd8439c commit 683d792
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
65 changes: 46 additions & 19 deletions ui/ducks/bridge/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import {
} from '../../selectors/selectors';
import {
ALLOWED_BRIDGE_CHAIN_IDS,
type AllowedBridgeChainIds,
BRIDGE_PREFERRED_GAS_ESTIMATE,
BRIDGE_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
} from '../../../shared/constants/bridge';
import type { BridgeControllerState } from '../../../shared/types/bridge';
import { createDeepEqualSelector } from '../../../shared/modules/selectors/util';
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../../../shared/constants/swaps';
import {
getProviderConfig,
getNetworkConfigurationsByChainId,
} from '../../../shared/modules/selectors/networks';
import { getNetworkConfigurationsByChainId } from '../../../shared/modules/selectors/networks';
import { getConversionRate, getGasFeeEstimates } from '../metamask/metamask';
import {
type L1GasFees,
Expand All @@ -53,6 +51,12 @@ import {
CHAIN_ID_TOKEN_IMAGE_MAP,
FEATURED_RPCS,
} from '../../../shared/constants/network';
import { getMultichainProviderConfig } from '../../selectors/multichain';
import {
MULTICHAIN_PROVIDER_CONFIGS,
MultichainNetworks,
MultichainProviderConfig,
} from '../../../shared/constants/multichain/networks';
import {
exchangeRatesFromNativeAndCurrencyRates,
exchangeRateFromMarketData,
Expand All @@ -74,12 +78,18 @@ type BridgeAppState = {
bridge: BridgeState;
};

// only includes networks user has added
// includes all networks that are supported and imported
export const getAllBridgeableNetworks = createDeepEqualSelector(
getNetworkConfigurationsByChainId,
[getNetworkConfigurationsByChainId],
(networkConfigurationsByChainId) => {
return uniqBy(
Object.values(networkConfigurationsByChainId),
[
...Object.values(networkConfigurationsByChainId),
// TODO check whether this is imported
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
MULTICHAIN_PROVIDER_CONFIGS[MultichainNetworks.SOLANA],
///: END:ONLY_INCLUDE_IF
],
'chainId',
).filter(({ chainId }) =>
ALLOWED_BRIDGE_CHAIN_IDS.includes(
Expand All @@ -89,6 +99,7 @@ export const getAllBridgeableNetworks = createDeepEqualSelector(
},
);

// only includes networks that are enabled
export const getFromChains = createDeepEqualSelector(
getAllBridgeableNetworks,
(state: BridgeAppState) => state.metamask.bridgeState?.bridgeFeatureFlags,
Expand All @@ -102,15 +113,16 @@ export const getFromChains = createDeepEqualSelector(
);

export const getFromChain = createDeepEqualSelector(
getNetworkConfigurationsByChainId,
getProviderConfig,
getMultichainProviderConfig,
getFromChains,
(
networkConfigurationsByChainId,
providerConfig,
): NetworkConfiguration | undefined =>
providerConfig?.chainId
? networkConfigurationsByChainId[providerConfig.chainId]
: undefined,
fromChains,
): NetworkConfiguration | MultichainProviderConfig | undefined => {
return providerConfig?.chainId
? fromChains.find(({ chainId }) => chainId === providerConfig.chainId)
: undefined;
},
);

export const getToChains = createDeepEqualSelector(
Expand All @@ -119,8 +131,17 @@ export const getToChains = createDeepEqualSelector(
(
allBridgeableNetworks,
bridgeFeatureFlags,
): (AddNetworkFields | NetworkConfiguration)[] =>
uniqBy([...allBridgeableNetworks, ...FEATURED_RPCS], 'chainId').filter(
): (AddNetworkFields | MultichainProviderConfig | NetworkConfiguration)[] =>
uniqBy(
[
...allBridgeableNetworks,
...FEATURED_RPCS,
///: BEGIN:ONLY_INCLUDE_IF(solana-swaps)
MULTICHAIN_PROVIDER_CONFIGS[MultichainNetworks.SOLANA],
///: END:ONLY_INCLUDE_IF
],
'chainId',
).filter(
({ chainId }) =>
bridgeFeatureFlags[BridgeFeatureFlagsKey.EXTENSION_CONFIG].chains[
chainId
Expand All @@ -131,8 +152,14 @@ export const getToChains = createDeepEqualSelector(
export const getToChain = createDeepEqualSelector(
getToChains,
(state: BridgeAppState) => state.bridge.toChainId,
(toChains, toChainId): NetworkConfiguration | AddNetworkFields | undefined =>
toChains.find(({ chainId }) => chainId === toChainId),
(
toChains,
toChainId,
):
| NetworkConfiguration
| MultichainProviderConfig
| AddNetworkFields
| undefined => toChains.find(({ chainId }) => chainId === toChainId),
);

export const getFromToken = createSelector(
Expand All @@ -149,7 +176,7 @@ export const getFromToken = createSelector(
...SWAPS_CHAINID_DEFAULT_TOKEN_MAP[
fromChain.chainId as keyof typeof SWAPS_CHAINID_DEFAULT_TOKEN_MAP
],
chainId: fromChain.chainId,
chainId: fromChain.chainId as AllowedBridgeChainIds,
image:
CHAIN_ID_TOKEN_IMAGE_MAP[
fromChain.chainId as keyof typeof CHAIN_ID_TOKEN_IMAGE_MAP
Expand Down
5 changes: 2 additions & 3 deletions ui/hooks/bridge/useLatestBalance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useSelector } from 'react-redux';
import { isCaipChainId } from '@metamask/utils';
import { type CaipChainId, type Hex, isCaipChainId } from '@metamask/utils';
import { Numeric } from '../../../shared/modules/Numeric';
import { getCurrentChainId } from '../../../shared/modules/selectors/networks';
import { getSelectedInternalAccount } from '../../selectors';
import { calcLatestSrcBalance } from '../../../shared/modules/bridge-utils/balance';
import { useAsyncResult } from '../useAsyncResult';
import { calcTokenAmount } from '../../../shared/lib/transactions-controller-utils';
import { type AllowedBridgeChainIds } from '../../../shared/constants/bridge';

/**
* Custom hook to fetch and format the latest balance of a given token or native asset.
Expand All @@ -21,7 +20,7 @@ const useLatestBalance = (
decimals: number;
symbol: string;
} | null,
chainId?: AllowedBridgeChainIds,
chainId?: Hex | CaipChainId,
) => {
const { address: selectedAddress } = useSelector(getSelectedInternalAccount);
const currentChainId = useSelector(getCurrentChainId);
Expand Down

0 comments on commit 683d792

Please sign in to comment.