Skip to content

Commit 0ca5655

Browse files
committed
chore: fetch balances using correct provider
fix: merge issue fix: undo json
1 parent ebf4a3f commit 0ca5655

File tree

6 files changed

+132
-200
lines changed

6 files changed

+132
-200
lines changed

app/scripts/metamask-controller.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
BRIDGE_CONTROLLER_NAME,
4949
BridgeUserAction,
5050
BridgeBackgroundAction,
51+
calcLatestSrcBalance,
5152
} from '@metamask/bridge-controller';
5253

5354
import {
@@ -3179,6 +3180,19 @@ export default class MetamaskController extends EventEmitter {
31793180
this.controllerMessenger,
31803181
`${BRIDGE_CONTROLLER_NAME}:${BridgeBackgroundAction.FETCH_QUOTES}`,
31813182
),
3183+
getBalanceAmount: async (selectedAddress, tokenAddress, chainId) => {
3184+
const networkClientId =
3185+
await this.networkController.findNetworkClientIdByChainId(chainId);
3186+
const networkClient =
3187+
await this.networkController.getNetworkClientById(networkClientId);
3188+
const balance = await calcLatestSrcBalance(
3189+
networkClient.provider,
3190+
selectedAddress,
3191+
tokenAddress,
3192+
chainId,
3193+
);
3194+
return balance?.toString();
3195+
},
31823196

31833197
// Bridge Tx submission
31843198
[BridgeStatusAction.SUBMIT_TX]: this.controllerMessenger.call.bind(

ui/ducks/bridge/actions.ts

Lines changed: 76 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,36 @@ import {
44
BridgeUserAction,
55
formatChainIdToCaip,
66
isNativeAddress,
7-
getNativeAssetForChainId,
87
type RequiredEventContextFromClient,
98
UnifiedSwapBridgeEventName,
9+
formatChainIdToHex,
1010
} from '@metamask/bridge-controller';
11-
import { type InternalAccount } from '@metamask/keyring-internal-api';
12-
import { type CaipChainId } from '@metamask/utils';
13-
import type {
14-
AddNetworkFields,
15-
NetworkConfiguration,
16-
} from '@metamask/network-controller';
11+
import { type Hex } from '@metamask/utils';
12+
import { zeroAddress } from 'ethereumjs-util';
1713
import { trace, TraceName } from '../../../shared/lib/trace';
18-
import {
19-
forceUpdateMetamaskState,
20-
setActiveNetworkWithError,
21-
} from '../../store/actions';
14+
import { forceUpdateMetamaskState } from '../../store/actions';
2215
import { submitRequestToBackground } from '../../store/background-connection';
23-
import type { MetaMaskReduxDispatch } from '../../store/store';
16+
import { getInternalAccountBySelectedAccountGroupAndCaip } from '../../selectors/multichain-accounts/account-tree';
17+
import type {
18+
MetaMaskReduxDispatch,
19+
MetaMaskReduxState,
20+
} from '../../store/store';
2421
import {
2522
bridgeSlice,
2623
setDestTokenExchangeRates,
2724
setDestTokenUsdExchangeRates,
2825
setSrcTokenExchangeRates,
2926
setTxAlerts,
30-
setEVMSrcTokenBalance as setEVMSrcTokenBalance_,
31-
setEVMSrcNativeBalance,
3227
} from './bridge';
33-
import type { TokenPayload } from './types';
34-
import { isNetworkAdded, isNonEvmChain } from './utils';
28+
import type { BridgeToken } from './types';
29+
import { isNonEvmChain } from './utils';
3530

3631
const {
3732
setFromToken,
3833
setToToken,
3934
setFromTokenInputValue,
35+
setEVMSrcTokenBalance,
36+
setEVMSrcNativeBalance,
4037
resetInputFields,
4138
setSortOrder,
4239
setSelectedQuote,
@@ -59,18 +56,18 @@ export {
5956
setWasTxDeclined,
6057
setSlippage,
6158
setTxAlerts,
62-
setEVMSrcNativeBalance,
6359
restoreQuoteRequestFromState,
6460
switchTokens,
6561
};
6662

6763
const callBridgeControllerMethod = (
68-
bridgeAction: BridgeUserAction | BridgeBackgroundAction,
64+
bridgeAction: BridgeUserAction | BridgeBackgroundAction | 'getBalanceAmount',
6965
...args: unknown[]
7066
) => {
7167
return async (dispatch: MetaMaskReduxDispatch) => {
72-
await submitRequestToBackground(bridgeAction, args);
68+
const result = await submitRequestToBackground(bridgeAction, args);
7369
await forceUpdateMetamaskState(dispatch);
70+
return result;
7471
};
7572
};
7673

@@ -119,96 +116,76 @@ export const updateQuoteRequestParams = (
119116
};
120117
};
121118

122-
export const setEVMSrcTokenBalance = (
123-
token: TokenPayload['payload'],
124-
selectedAddress?: string,
119+
const getEVMBalance = async (
120+
accountAddress: string,
121+
chainId: Hex,
122+
address?: string,
125123
) => {
126-
return async (dispatch: MetaMaskReduxDispatch) => {
127-
if (token) {
128-
trace({
129-
name: TraceName.BridgeBalancesUpdated,
130-
data: {
131-
srcChainId: formatChainIdToCaip(token.chainId),
132-
isNative: isNativeAddress(token.address),
133-
},
134-
startTime: Date.now(),
135-
});
136-
await dispatch(
137-
setEVMSrcTokenBalance_({
138-
selectedAddress,
139-
tokenAddress: token.address,
140-
chainId: token.chainId,
141-
}),
142-
);
143-
}
144-
};
124+
return async (dispatch: MetaMaskReduxDispatch) =>
125+
((await dispatch(
126+
await callBridgeControllerMethod(
127+
'getBalanceAmount',
128+
accountAddress,
129+
address || zeroAddress(),
130+
chainId,
131+
),
132+
)) as string) || null;
145133
};
146134

147-
const setFromChain = ({
148-
networkConfig,
149-
selectedAccount,
150-
token = null,
151-
}: {
152-
networkConfig?:
153-
| NetworkConfiguration
154-
| AddNetworkFields
155-
| (Omit<NetworkConfiguration, 'chainId'> & { chainId: CaipChainId });
156-
selectedAccount: InternalAccount | null;
157-
token?: TokenPayload['payload'];
158-
}) => {
159-
return async (dispatch: MetaMaskReduxDispatch) => {
160-
if (!networkConfig) {
161-
return;
162-
}
135+
/**
136+
* This action reads the latest on chain balance for the selected token and its chain's native token
137+
* It also traces the balance update.
138+
*
139+
* @param token - The token to fetch the balance for
140+
*/
141+
export const setLatestEVMBalances = (token: BridgeToken) => {
142+
return async (
143+
dispatch: MetaMaskReduxDispatch,
144+
getState: () => MetaMaskReduxState,
145+
) => {
146+
const { chainId, assetId, address } = token;
163147

164-
// Check for ALL non-EVM chains
165-
const isNonEvm = isNonEvmChain(networkConfig.chainId);
166-
167-
// Set the src network
168-
if (isNonEvm) {
169-
dispatch(setActiveNetworkWithError(networkConfig.chainId));
170-
} else {
171-
const networkId = isNetworkAdded(networkConfig)
172-
? networkConfig.rpcEndpoints?.[networkConfig.defaultRpcEndpointIndex]
173-
?.networkClientId
174-
: null;
175-
if (networkId) {
176-
dispatch(setActiveNetworkWithError(networkId));
177-
}
148+
if (isNonEvmChain(chainId)) {
149+
return null;
178150
}
179-
180-
// Set the src token - if no token provided, set native token for non-EVM chains
181-
if (token) {
182-
dispatch(setFromToken(token));
183-
} else if (isNonEvm) {
184-
// Auto-select native token for non-EVM chains when switching
185-
const nativeAsset = getNativeAssetForChainId(networkConfig.chainId);
186-
if (nativeAsset) {
187-
dispatch(
188-
setFromToken({
189-
...nativeAsset,
190-
chainId: networkConfig.chainId,
191-
}),
192-
);
193-
}
151+
const hexChainId = formatChainIdToHex(chainId);
152+
const caipChainId = formatChainIdToCaip(hexChainId);
153+
const account = getInternalAccountBySelectedAccountGroupAndCaip(
154+
getState(),
155+
caipChainId,
156+
);
157+
if (!account?.address) {
158+
return null;
194159
}
195160

196-
// Fetch the native balance (EVM only)
197-
if (selectedAccount && !isNonEvm) {
198-
trace({
161+
return await trace(
162+
{
199163
name: TraceName.BridgeBalancesUpdated,
200164
data: {
201-
srcChainId: formatChainIdToCaip(networkConfig.chainId),
202-
isNative: true,
165+
chainId,
166+
isNative: isNativeAddress(address),
203167
},
204168
startTime: Date.now(),
205-
});
206-
await dispatch(
207-
setEVMSrcNativeBalance({
208-
selectedAddress: selectedAccount.address,
209-
chainId: networkConfig.chainId,
210-
}),
211-
);
212-
}
169+
},
170+
async () => {
171+
dispatch(
172+
setEVMSrcTokenBalance({
173+
balance: await dispatch(
174+
await getEVMBalance(account.address, hexChainId, address),
175+
),
176+
assetId,
177+
}),
178+
);
179+
180+
dispatch(
181+
setEVMSrcNativeBalance({
182+
balance: await dispatch(
183+
await getEVMBalance(account.address, hexChainId),
184+
),
185+
chainId,
186+
}),
187+
);
188+
},
189+
);
213190
};
214191
};

0 commit comments

Comments
 (0)