Estimated Effort: 3 weeks / 6 days
Impacted Subsystems / Files:
contracts/phase-protocol/src/lib.rs
contracts/phase-protocol/Cargo.toml
Context & Problem Statement:
In contracts/phase-protocol/src/lib.rs, owner_tokens_remove performs a linear $O(N)$ scan over a Vec<u64> to find and remove a token ID when an NFT is transferred. As user portfolio sizes scale, transferring tokens consumes exponentially higher WASM CPU instructions and ledger read/write fees, risking transaction failure due to exceeding Soroban gas budget limits.
Technical Requirements & Scope:
- Replace
DataKey::OwnerTokenList(Address) vector storage with a dynamic mapping structure using DataKey::OwnerTokenIndex(Address, u64) and DataKey::OwnerTokenAt(Address, u32).
- Implement constant-time $O(1)$ swap-and-pop logic during token transfers.
- Maintain backwards compatibility for
simulateTokenOfOwnerByIndex RPC queries.
- Add Rust unit tests in
contracts/phase-protocol/src/lib.rs verifying gas costs remain constant for wallets owning >1,000 tokens.
Multi-File Change Surface:
Modifies core storage structures in contracts/phase-protocol/src/lib.rs and requires updating simulateListedTokenIdsForOwner in lib/phase-protocol.ts to consume the updated index scheme.
Acceptance Criteria:
Suggested Approach / Investigation Steps:
- Benchmark gas usage of
owner_tokens_remove for vector lengths N=10 to N=1000.
- Implement swap-and-pop index key pattern in Rust.
- Update SDK helper simulation functions to test new storage key layout.
Estimated Effort: 3 weeks / 6 days
Impacted Subsystems / Files:
contracts/phase-protocol/src/lib.rscontracts/phase-protocol/Cargo.tomlContext & Problem Statement:$O(N)$ scan over a
In
contracts/phase-protocol/src/lib.rs,owner_tokens_removeperforms a linearVec<u64>to find and remove a token ID when an NFT is transferred. As user portfolio sizes scale, transferring tokens consumes exponentially higher WASM CPU instructions and ledger read/write fees, risking transaction failure due to exceeding Soroban gas budget limits.Technical Requirements & Scope:
DataKey::OwnerTokenList(Address)vector storage with a dynamic mapping structure usingDataKey::OwnerTokenIndex(Address, u64)andDataKey::OwnerTokenAt(Address, u32).simulateTokenOfOwnerByIndexRPC queries.contracts/phase-protocol/src/lib.rsverifying gas costs remain constant for wallets owning >1,000 tokens.Multi-File Change Surface:
Modifies core storage structures in
contracts/phase-protocol/src/lib.rsand requires updatingsimulateListedTokenIdsForOwnerinlib/phase-protocol.tsto consume the updated index scheme.Acceptance Criteria:
contracts/phase-protocolpass cleanly.owner_tokens_remove.Suggested Approach / Investigation Steps:
owner_tokens_removefor vector lengths N=10 to N=1000.