Skip to content

Commit b0fd6ae

Browse files
salimtbbergarces
andauthored
fix: fix api fetcher balances (#6812)
## Explanation **Current Issue:** The `AccountsApiBalanceFetcher` had a bug caused by address format incompatibility between controllers: - `TokenBalancesController` uses lowercase addresses for ERC20 tokens - `AccountTrackerController` uses checksum addresses for native tokens The original code always checksummed all addresses, causing a wrong token balances state **Solution:** Fixed by implementing conditional address formatting: - **Native tokens**: Use checksummed addresses (compatible with `AccountTrackerController`) - **ERC20 tokens**: Use lowercase addresses (compatible with `TokenBalancesController`) Also fixed the `|| ''` fallback bug that could pass empty strings to the `checksum()` function. ## References - Fixes duplicate native token entries in `AccountsApiBalanceFetcher` by ensuring controller-compatible address formatting ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Aligns address casing between controllers by conditionally formatting account addresses in `AccountsApiBalanceFetcher` and updating `TokenBalancesController` state access; updates changelog. > > - **Balances**: > - `multi-chain-accounts-service/api-balance-fetcher.ts`: > - Conditionally format `account` per token type (checksum for native `ZERO_ADDRESS`, original for ERC20) and propagate via `finalAccount`. > - Track native balances using the formatted key and widen `ProcessedBalance.account` to `ChecksumAddress | string`. > - `TokenBalancesController.ts`: > - Access and write `state.tokenBalances` using `account as ChecksumAddress` to handle mixed address formats during updates. > - **Docs**: > - `CHANGELOG.md`: Add "Fixed" note about address format compatibility between `TokenBalancesController` and `AccountTrackerController` in `AccountsApiBalanceFetcher`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 29bd9b8. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Bernardo Garces Chapero <[email protected]>
1 parent 1baf335 commit b0fd6ae

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

packages/assets-controllers/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
- Reduce NFT detection API calls by 83% (from 6 calls to 1 call per 100 tokens) by eliminating collection endpoint requests
3434
- Remove unused collection metadata fields: `contractDeployedAt`, `creator`, and `topBid`
3535

36+
### Fixed
37+
38+
- Fix address format compatibility between `TokenBalancesController` and `AccountTrackerController` in `AccountsApiBalanceFetcher` ([#6812](https://github.com/MetaMask/core/pull/6812))
39+
3640
## [79.0.1]
3741

3842
### Changed

packages/assets-controllers/src/TokenBalancesController.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,14 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
734734
const newBalance = toHex(value);
735735
const tokenAddress = checksum(token);
736736
const currentBalance =
737-
d.tokenBalances[account]?.[chainId]?.[tokenAddress];
737+
d.tokenBalances[account as ChecksumAddress]?.[chainId]?.[
738+
tokenAddress
739+
];
738740

739741
// Only update if the balance has actually changed
740742
if (currentBalance !== newBalance) {
741-
((d.tokenBalances[account] ??= {})[chainId] ??= {})[tokenAddress] =
742-
newBalance;
743+
((d.tokenBalances[account as ChecksumAddress] ??= {})[chainId] ??=
744+
{})[tokenAddress] = newBalance;
743745
}
744746
}
745747
});

packages/assets-controllers/src/multi-chain-accounts-service/api-balance-fetcher.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type ChecksumAddress = Hex;
2828
export type ProcessedBalance = {
2929
success: boolean;
3030
value?: BN;
31-
account: ChecksumAddress;
31+
account: ChecksumAddress | string;
3232
token: ChecksumAddress;
3333
chainId: ChainIdHex;
3434
};
@@ -302,6 +302,11 @@ export class AccountsApiBalanceFetcher implements BalanceFetcher {
302302
}
303303
const account = checksum(addressPart);
304304
const token = checksum(b.address);
305+
// Use original address for zero address tokens, checksummed for others
306+
// TODO: this is a hack to get the correct account address type but needs to be fixed
307+
// by mgrating tokenBalancesController to checksum addresses
308+
const finalAccount: ChecksumAddress | string =
309+
token === ZERO_ADDRESS ? account : addressPart;
305310
const chainId = toHex(b.chainId) as ChainIdHex;
306311

307312
let value: BN | undefined;
@@ -326,14 +331,14 @@ export class AccountsApiBalanceFetcher implements BalanceFetcher {
326331

327332
// Track native balances for later
328333
if (token === ZERO_ADDRESS && value !== undefined) {
329-
nativeBalancesFromAPI.set(`${account}-${chainId}`, value);
334+
nativeBalancesFromAPI.set(`${finalAccount}-${chainId}`, value);
330335
}
331336

332337
return [
333338
{
334339
success: value !== undefined,
335340
value,
336-
account,
341+
account: finalAccount,
337342
token,
338343
chainId,
339344
},

0 commit comments

Comments
 (0)