Skip to content

Commit

Permalink
Unnecessary state update when the balance did not change at account t…
Browse files Browse the repository at this point in the history
…racker controller polling mechanism, which was causing unnecessary re renders
  • Loading branch information
tommasini committed Oct 22, 2024
1 parent 54ddebf commit c970faf
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,24 @@ export class AccountTrackerController extends StaticIntervalPollingController<
: [toChecksumHexAddress(selectedAccount.address)];

const accountsForChain = { ...accountsByChainId[chainId] };
let balanceChanged = false;
for (const address of accountsToUpdate) {
const balance = await this.#getBalanceFromChain(address, ethQuery);
if (balance) {
if (balance && accountsForChain[address]?.balance !== balance) {
accountsForChain[address] = {
balance,
};
balanceChanged = true;
}
}

this.update((state) => {
if (chainId === this.#getCurrentChainId()) {
state.accounts = accountsForChain;
}
state.accountsByChainId[chainId] = accountsForChain;
});
if (balanceChanged) {
this.update((state) => {
if (chainId === this.#getCurrentChainId()) {
state.accounts = accountsForChain;
}
state.accountsByChainId[chainId] = accountsForChain;
});
}
} finally {
releaseLock();
}
Expand Down

0 comments on commit c970faf

Please sign in to comment.