@@ -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' ;
1210import { type CaipChainId } from '@metamask/utils' ;
13- import type {
14- AddNetworkFields ,
15- NetworkConfiguration ,
16- } from '@metamask/network-controller' ;
11+ import { zeroAddress } from 'ethereumjs-util' ;
1712import { trace , TraceName } from '../../../shared/lib/trace' ;
18- import {
19- forceUpdateMetamaskState ,
20- setActiveNetworkWithError ,
21- } from '../../store/actions' ;
13+ import { forceUpdateMetamaskState } from '../../store/actions' ;
2214import { submitRequestToBackground } from '../../store/background-connection' ;
2315import type { MetaMaskReduxDispatch } from '../../store/store' ;
16+ import { getInternalAccountBySelectedAccountGroupAndCaip } from '../../selectors/multichain-accounts/account-tree' ;
17+ import type { MetaMaskReduxState } from '../../selectors' ;
2418import {
2519 bridgeSlice ,
2620 setDestTokenExchangeRates ,
2721 setDestTokenUsdExchangeRates ,
2822 setSrcTokenExchangeRates ,
2923 setTxAlerts ,
30- setEVMSrcTokenBalance as setEVMSrcTokenBalance_ ,
31- setEVMSrcNativeBalance ,
3224} from './bridge' ;
3325import type { TokenPayload } from './types' ;
34- import { isNetworkAdded , isNonEvmChain } from './utils' ;
26+ import { isNonEvmChain } from './utils' ;
3527
3628const {
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
148177export 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