Skip to content

chore: reduce state writes from O(2n) to O(2) for NFT detection#8079

Open
juanmigdr wants to merge 10 commits intomainfrom
chore/reduce-state-writes-for-nft-detection
Open

chore: reduce state writes from O(2n) to O(2) for NFT detection#8079
juanmigdr wants to merge 10 commits intomainfrom
chore/reduce-state-writes-for-nft-detection

Conversation

@juanmigdr
Copy link
Member

@juanmigdr juanmigdr commented Mar 2, 2026

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:

  • Process all NFTs/contracts in a single mutex acquisition.
  • Accumulate per-chain changes in memory, then call #updateNestedNftState once per modified chain rather than once per NFT.
  • Wrap each item in an inner try/catch so a single failing NFT does not abort the entire batch.

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

  • 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
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

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/#addIndividualNft calls with #addNftContracts and #addMultipleNfts, reducing repeated mutex acquisitions and state writes to once per modified chainId.

Updates addNfts to accumulate contracts/NFTs in memory and write back in grouped updates, and updates addNft to delegate to the same batched path (surfacing the first collected error when present). #getNftContractInformation is also tightened to merge API metadata with on-chain name/symbol and 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.

@juanmigdr juanmigdr requested a review from a team as a code owner March 2, 2026 11:12
@juanmigdr
Copy link
Member Author

@cursor update the assets-controllers changelog

@cursor
Copy link

cursor bot commented Mar 2, 2026

I've successfully updated the assets-controllers changelog! Here's what I accomplished:

Changelog Update:

  • Added an entry in the "Changed" section under "Unreleased" describing the NFT Controller optimization
  • The entry documents how the PR reduces state writes from O(n) to O(1) for NFT detection through batching
  • Properly formatted with PR reference chore: reduce state writes from O(2n) to O(2) for NFT detection #8079 following the project's changelog guidelines

Validation:

  • Enabled corepack and installed dependencies to prepare the environment
  • Ran yarn changelog:validate to ensure the format meets project standards
  • All changelog validation checks passed successfully

Git Operations:

  • Staged the updated changelog file
  • Committed with descriptive message: "chore: update assets-controllers changelog for NFT detection optimization"
  • Pushed changes to the remote branch

The changelog now properly documents this performance optimization, which should resolve the failing CI check for changelog validation.

Open in Web Open in Cursor 

…tion

Co-authored-by: Juanmi <juanmigdr@users.noreply.github.com>
@cursor cursor bot requested a review from a team as a code owner March 2, 2026 12:05
@juanmigdr juanmigdr changed the title chore: reduce state writes from O(n) to O(1) for NFT detection chore: reduce state writes from O(2n) to O(2) for NFT detection Mar 2, 2026
bergarces
bergarces previously approved these changes Mar 2, 2026
@juanmigdr juanmigdr dismissed stale reviews from Prithpal-Sooriya and bergarces via 78d4cb2 March 2, 2026 15:54
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.

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 };
Copy link

Choose a reason for hiding this comment

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

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.

Fix in Cursor Fix in Web

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @juanmigdr , is this comment valid?

…f it has already been returned by API provider
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.

5 participants