chore: reduce state writes from O(2n) to O(2) for NFT detection#8079
chore: reduce state writes from O(2n) to O(2) for NFT detection#8079
Conversation
|
@cursor update the assets-controllers changelog |
|
I've successfully updated the assets-controllers changelog! Here's what I accomplished: Changelog Update:
Validation:
Git Operations:
The changelog now properly documents this performance optimization, which should resolve the failing CI check for changelog validation. |
…tion Co-authored-by: Juanmi <juanmigdr@users.noreply.github.com>
78d4cb2
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
| } | ||
|
|
||
| return newNftContracts; | ||
| return { contracts: nftContractsForUserPerChain, errors }; |
There was a problem hiding this comment.
Mutex held during N sequential async network calls
Medium Severity
#addNftContracts acquires the mutex at the start and holds it across ALL iterations of the loop, including the await this.#getNftContractInformation() calls that make blockchain RPC requests. For a detection batch of up to 50 NFTs (per the API's limit=50) with many unique contracts, this means N sequential network calls (~100-500ms each) all execute under a single continuous mutex hold. The old per-item #addNftContract released the mutex between each contract, allowing other mutex-dependent operations like removeNft, updateNftMetadata, or user-initiated addNft to interleave. The network calls don't need mutex protection — only the state reads/writes do.
…f it has already been returned by API provider




Explanation
Previously, addNfts called #addNftContract and #addIndividualNft in a loop, acquiring the mutex and writing to state once per NFT. For large detection batches this produced O(2n) state updates, triggering unnecessary downstream re-renders on every iteration.
This PR replaces the per-item private methods (#addNftContract, #addIndividualNft) with batched equivalents (#addNftContracts, #addMultipleNfts) that:
As a side effect, addNft (singular) is updated to delegate to the same batched methods with a single-element array, keeping the two code paths consistent.
References
https://consensyssoftware.atlassian.net/browse/ASSETS-2819
Checklist
Note
Medium Risk
Refactors NFT add/detection flows to batch mutex acquisition and per-chain state updates, which could subtly change ordering/callback timing and error handling for NFT/contract additions. Also adjusts contract metadata fetching logic to conditionally skip RPC calls based on API-provided fields.
Overview
Batches NFT detection/addition state updates by replacing per-NFT
#addNftContract/#addIndividualNftcalls with#addNftContractsand#addMultipleNfts, reducing repeated mutex acquisitions and state writes to once per modifiedchainId.Updates
addNftsto accumulate contracts/NFTs in memory and write back in grouped updates, and updatesaddNftto delegate to the same batched path (surfacing the first collected error when present).#getNftContractInformationis also tightened to merge API metadata with on-chainname/symboland skip RPC calls when the API already provided those fields.Written by Cursor Bugbot for commit 700a43e. This will update automatically on new commits. Configure here.