Skip to content

perf!: Use single call to update native balances when enabled #6099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

FrederikBolding
Copy link
Member

@FrederikBolding FrederikBolding commented Jul 10, 2025

Explanation

This PR modifies AccountTrackerController.refresh to use the contract from SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID used in other asset controllers when available. This reduces the amount of fetch calls made to update native balances drastically. If the contract is not available, we fall back to the previous behaviour.

Additionally this PR refactors getStakedBalanceForChain to take multiple addresses and use multicall to fetch staked balances. This also drastically reduces the number of fetch calls used, but is a breaking change since the parameters and return value has changed for getStakedBalanceForChain.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@FrederikBolding FrederikBolding changed the title perf: Use single call to update native balances when enabled perf!: Use single call to update native balances when enabled Jul 10, 2025
@FrederikBolding FrederikBolding marked this pull request as ready for review July 11, 2025 12:19
@FrederikBolding FrederikBolding requested a review from a team as a code owner July 11, 2025 12:19
@@ -38,22 +44,25 @@ import { reduceInBatchesSerially, TOKEN_PRICES_BATCH_SIZE } from './assetsUtil';
const controllerName = 'AccountTrackerController';

/**
* @type AccountInformation
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran lint:fix and modified a bunch of JSDocs like this, feel free to revert

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Single-Call Balance Fetch Fails Without Fallback

The new single-call contract logic for fetching native account balances lacks error handling. If the contract.balances() call fails (e.g., due to network issues, contract problems, or an individual address causing a revert), the entire balance refresh operation for that chain will fail. This reduces resilience compared to the previous implementation, which handled individual failures gracefully. The single-call logic should fall back to the individual query method on failure.

packages/assets-controllers/src/AccountTrackerController.ts#L376-L396

if (hasProperty(SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID, chainId)) {
const contractAddress = SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[
chainId
] as string;
const contract = new Contract(
contractAddress,
abiSingleCallBalancesContract,
new Web3Provider(provider),
);
const nativeBalances = await contract.balances(accountsToUpdate, [
'0x0000000000000000000000000000000000000000',
]);
accountsToUpdate.forEach((address, index) => {
accountsForChain[address] = {
balance: (nativeBalances[index] as BigNumber).toHexString(),
};
});

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant