Skip to content

Commit 0631587

Browse files
committed
working wip
1 parent 6321aff commit 0631587

File tree

25 files changed

+1034
-725
lines changed

25 files changed

+1034
-725
lines changed

shared/constants/bridge.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import {
33
BRIDGE_DEV_API_BASE_URL,
44
BRIDGE_PROD_API_BASE_URL,
55
ChainId,
6+
formatChainIdToCaip,
67
} from '@metamask/bridge-controller';
78
import { MultichainNetworks } from './multichain/networks';
8-
import { CHAIN_IDS, NETWORK_TO_NAME_MAP } from './network';
9+
import {
10+
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP,
11+
CHAIN_IDS,
12+
NETWORK_TO_NAME_MAP,
13+
} from './network';
914

1015
const ALLOWED_MULTICHAIN_BRIDGE_CHAIN_IDS = [
1116
MultichainNetworks.SOLANA,
@@ -55,6 +60,18 @@ export const BRIDGE_API_BASE_URL = process.env.BRIDGE_USE_DEV_APIS
5560
? BRIDGE_DEV_API_BASE_URL
5661
: BRIDGE_PROD_API_BASE_URL;
5762

63+
export const BRIDGE_CHAIN_ID_TO_NETWORK_IMAGE_MAP: Record<
64+
(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number],
65+
string
66+
> = ALLOWED_BRIDGE_CHAIN_IDS.reduce(
67+
(acc, chainId) => {
68+
acc[formatChainIdToCaip(chainId)] =
69+
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP[chainId];
70+
return acc;
71+
},
72+
{} as Record<(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number], string>,
73+
);
74+
5875
export const ETH_USDT_ADDRESS = '0xdac17f958d2ee523a2206206994597c13d831ec7';
5976
export const NETWORK_TO_SHORT_NETWORK_NAME_MAP: Record<
6077
AllowedBridgeChainIds,

shared/lib/asset-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ const STATIC_METAMASK_BASE_URL = 'https://static.cx.metamask.io';
2727
export const toAssetId = (
2828
address: Hex | CaipAssetType | string,
2929
chainId: CaipChainId,
30-
): CaipAssetType | undefined => {
30+
): CaipAssetType => {
3131
if (isCaipAssetType(address)) {
3232
return address;
3333
}
3434
if (isNativeAddress(address)) {
35-
return getNativeAssetForChainId(chainId)?.assetId;
35+
return getNativeAssetForChainId(chainId).assetId;
3636
}
3737
if (chainId === MultichainNetwork.Solana) {
3838
return CaipAssetTypeStruct.create(`${chainId}/token:${address}`);
@@ -43,7 +43,7 @@ export const toAssetId = (
4343
`${chainId}/erc20:${address.toLowerCase()}`,
4444
);
4545
}
46-
return undefined;
46+
throw new Error(`Invalid address: ${address} for chainId: ${chainId}`);
4747
};
4848

4949
/**

ui/components/multichain/asset-picker-amount/asset-picker-modal/hooks/useAssetMetadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const useAssetMetadata = (
2121
searchQuery: string,
2222
shouldFetchMetadata: boolean,
2323
abortControllerRef: React.MutableRefObject<AbortController | null>,
24-
chainId?: Hex | CaipChainId,
24+
chainId: CaipChainId,
2525
) => {
2626
const allowExternalServices = useSelector(getUseExternalServices);
2727

@@ -31,15 +31,15 @@ export const useAssetMetadata = (
3131
symbol: string;
3232
decimals: number;
3333
image: string;
34-
chainId: Hex | CaipChainId;
34+
chainId: CaipChainId;
3535
isNative: boolean;
3636
type: AssetType.token;
3737
balance: string;
3838
string: string;
3939
}
4040
| undefined
4141
>(async () => {
42-
if (!chainId || !searchQuery) {
42+
if (!searchQuery) {
4343
return undefined;
4444
}
4545

ui/components/multichain/network-list-item/network-list-item.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const NetworkListItem = ({
6262
disabled = false,
6363
variant,
6464
notSelectable = false,
65+
avatarNetworkProps = {},
6566
}: {
6667
name: string;
6768
iconSrc?: string;
@@ -81,6 +82,7 @@ export const NetworkListItem = ({
8182
disabled?: boolean;
8283
variant?: TextVariant;
8384
notSelectable?: boolean;
85+
avatarNetworkProps?: Partial<React.ComponentProps<typeof AvatarNetwork>>;
8486
}) => {
8587
const t = useI18nContext();
8688
const networkRef = useRef<HTMLInputElement>(null);
@@ -175,6 +177,7 @@ export const NetworkListItem = ({
175177
name={name}
176178
src={iconSrc}
177179
size={iconSize as AvatarNetworkSize}
180+
{...avatarNetworkProps}
178181
/>
179182
)}
180183
<Box

ui/ducks/bridge/actions.ts

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,31 @@ import {
22
BridgeBackgroundAction,
33
type BridgeController,
44
BridgeUserAction,
5-
formatChainIdToCaip,
5+
formatChainIdToHex,
66
isNativeAddress,
7-
getNativeAssetForChainId,
87
type RequiredEventContextFromClient,
98
UnifiedSwapBridgeEventName,
109
} from '@metamask/bridge-controller';
11-
import { type InternalAccount } from '@metamask/keyring-internal-api';
1210
import { type CaipChainId } from '@metamask/utils';
13-
import type {
14-
AddNetworkFields,
15-
NetworkConfiguration,
16-
} from '@metamask/network-controller';
11+
import { zeroAddress } from 'ethereumjs-util';
1712
import { trace, TraceName } from '../../../shared/lib/trace';
18-
import {
19-
forceUpdateMetamaskState,
20-
setActiveNetworkWithError,
21-
} from '../../store/actions';
13+
import { forceUpdateMetamaskState } from '../../store/actions';
2214
import { submitRequestToBackground } from '../../store/background-connection';
2315
import type { MetaMaskReduxDispatch } from '../../store/store';
16+
import { getInternalAccountBySelectedAccountGroupAndCaip } from '../../selectors/multichain-accounts/account-tree';
17+
import type { MetaMaskReduxState } from '../../selectors';
2418
import {
2519
bridgeSlice,
2620
setDestTokenExchangeRates,
2721
setDestTokenUsdExchangeRates,
2822
setSrcTokenExchangeRates,
2923
setTxAlerts,
30-
setEVMSrcTokenBalance as setEVMSrcTokenBalance_,
31-
setEVMSrcNativeBalance,
3224
} from './bridge';
3325
import type { TokenPayload } from './types';
34-
import { isNetworkAdded, isNonEvmChain } from './utils';
26+
import { isNonEvmChain } from './utils';
3527

3628
const {
29+
setFromChainId,
3730
setToChainId,
3831
setFromToken,
3932
setToToken,
@@ -60,6 +53,7 @@ export {
6053
setSortOrder,
6154
setSelectedQuote,
6255
setWasTxDeclined,
56+
setSlippage,
6357
setTxAlerts,
6458
restoreQuoteRequestFromState,
6559
};
@@ -120,94 +114,97 @@ export const updateQuoteRequestParams = (
120114
};
121115
};
122116

123-
export const setEVMSrcTokenBalance = (
124-
token: TokenPayload['payload'],
125-
selectedAddress?: string,
126-
) => {
127-
return async (dispatch: MetaMaskReduxDispatch) => {
128-
if (token) {
129-
trace({
117+
export const setBalanceAmount = ({
118+
chainId,
119+
shouldSetGasTokenBalance = false,
120+
token,
121+
}: {
122+
chainId: CaipChainId;
123+
shouldSetGasTokenBalance?: boolean;
124+
token?: TokenPayload['payload'];
125+
}) => {
126+
return async (
127+
dispatch: MetaMaskReduxDispatch,
128+
getState: () => MetaMaskReduxState,
129+
) => {
130+
if (isNonEvmChain(chainId)) {
131+
return;
132+
}
133+
const chainIdToUse = chainId ?? token?.chainId;
134+
const accountAddress = getInternalAccountBySelectedAccountGroupAndCaip(
135+
getState(),
136+
chainIdToUse,
137+
)?.address;
138+
139+
await trace(
140+
{
130141
name: TraceName.BridgeBalancesUpdated,
131142
data: {
132-
srcChainId: formatChainIdToCaip(token.chainId),
133-
isNative: isNativeAddress(token.address),
143+
srcChainId: chainId,
144+
isNative: isNativeAddress(token?.address),
134145
},
135146
startTime: Date.now(),
136-
});
137-
await dispatch(
138-
setEVMSrcTokenBalance_({
139-
selectedAddress,
140-
tokenAddress: token.address,
141-
chainId: token.chainId,
142-
}),
143-
);
144-
}
147+
},
148+
async () => {
149+
const balance = (await dispatch(
150+
await callBackgroundHandler(
151+
'getBalanceAmount',
152+
accountAddress,
153+
token?.address || zeroAddress(),
154+
formatChainIdToHex(chainIdToUse),
155+
),
156+
)) as string;
157+
if (shouldSetGasTokenBalance) {
158+
dispatch(
159+
setEVMSrcNativeBalance({
160+
balance,
161+
chainId: chainIdToUse,
162+
}),
163+
);
164+
} else {
165+
dispatch(
166+
setEVMSrcTokenBalance({
167+
balance,
168+
assetId: token?.assetId,
169+
}),
170+
);
171+
}
172+
},
173+
);
145174
};
146175
};
147176

148177
export const setFromChain = ({
149-
networkConfig,
150-
selectedAccount,
178+
chainId,
151179
token = null,
152180
}: {
153-
networkConfig?:
154-
| NetworkConfiguration
155-
| AddNetworkFields
156-
| (Omit<NetworkConfiguration, 'chainId'> & { chainId: CaipChainId });
157-
selectedAccount: InternalAccount | null;
181+
chainId?: CaipChainId | null;
158182
token?: TokenPayload['payload'];
159183
}) => {
160184
return async (dispatch: MetaMaskReduxDispatch) => {
161-
if (!networkConfig) {
185+
// Unset the fromChainId if no chainId is provided (All networks will be enabled)
186+
if (!chainId) {
187+
dispatch(setFromChainId(null));
162188
return;
163189
}
164-
165-
// Check for ALL non-EVM chains
166-
const isNonEvm = isNonEvmChain(networkConfig.chainId);
167-
168190
// Set the src network
169-
if (isNonEvm) {
170-
dispatch(setActiveNetworkWithError(networkConfig.chainId));
171-
} else {
172-
const networkId = isNetworkAdded(networkConfig)
173-
? networkConfig.rpcEndpoints?.[networkConfig.defaultRpcEndpointIndex]
174-
?.networkClientId
175-
: null;
176-
if (networkId) {
177-
dispatch(setActiveNetworkWithError(networkId));
178-
}
179-
}
180-
181-
// Set the src token - if no token provided, set native token for non-EVM chains
191+
dispatch(setFromChainId(chainId));
192+
// Set the src token if provided
182193
if (token) {
183194
dispatch(setFromToken(token));
184-
} else if (isNonEvm) {
185-
// Auto-select native token for non-EVM chains when switching
186-
const nativeAsset = getNativeAssetForChainId(networkConfig.chainId);
187-
if (nativeAsset) {
188-
dispatch(
189-
setFromToken({
190-
...nativeAsset,
191-
chainId: networkConfig.chainId,
192-
}),
193-
);
194-
}
195+
dispatch(
196+
setBalanceAmount({
197+
token,
198+
chainId,
199+
}),
200+
);
195201
}
196-
197202
// Fetch the native balance (EVM only)
198-
if (selectedAccount && !isNonEvm) {
199-
trace({
200-
name: TraceName.BridgeBalancesUpdated,
201-
data: {
202-
srcChainId: formatChainIdToCaip(networkConfig.chainId),
203-
isNative: true,
204-
},
205-
startTime: Date.now(),
206-
});
203+
if (chainId) {
207204
await dispatch(
208-
setEVMSrcNativeBalance({
209-
selectedAddress: selectedAccount.address,
210-
chainId: networkConfig.chainId,
205+
setBalanceAmount({
206+
shouldSetGasTokenBalance: true,
207+
chainId,
211208
}),
212209
);
213210
}

0 commit comments

Comments
 (0)