Skip to content

Defi controller v2#9503

Open
bergarces wants to merge 19 commits into
mainfrom
defi-controller-v2
Open

Defi controller v2#9503
bergarces wants to merge 19 commits into
mainfrom
defi-controller-v2

Conversation

@bergarces

@bergarces bergarces commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Explanation

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
  • 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
New persisted DeFi state and balance/valuation logic affect portfolio display; v6 API and CAIP account matching must stay correct across EVM and Solana, but changes are additive alongside the legacy controller with solid test coverage.

Overview
Introduces DeFiPositionsControllerV2, a parallel path to the legacy DeFi positions controller that loads positions from the Accounts API v6 multiaccount balances endpoint and keeps them in allDeFiPositionsV2 keyed by internal account ID (not address), in a shape the DeFi tab can read without extra grouping.

fetchDeFiPositions resolves accounts from the selected account group, builds the v6 query via buildDeFiBalancesQuery (first EVM + first Solana in the group, fixed supported networks, DeFi + prices flags), calls fetchV6MultiAccountBalances, maps per-chain CAIP-10 IDs back to internal IDs, and runs groupDeFiPositionsV6 into protocol groups with embedded detail sections, fiat marketValue ( lending treated as a liability), and token icons. Fetches are gated by isEnabled, skip unsupported-only groups, and are throttled in-memory per account set (cleared on failure); analytics from the old controller are intentionally not ported yet.

@metamask/core-backend now exports V6_DEFI_POSITION_TYPES / V6DeFiPositionType and types V6BalanceMetadata.positionType accordingly. Public exports include controller state/types, DEFI_POSITION_LIABILITY_TYPES, query helpers, and messenger action types. Unit tests cover query building, grouping, and controller behavior; a local defi-visualizer.html helps inspect API responses.

Reviewed by Cursor Bugbot for commit 5cb1671. Bugbot is set up for automated code reviews on this repo. Configure here.

@bergarces bergarces requested a review from a team as a code owner July 14, 2026 13:21
@bergarces bergarces requested a review from a team as a code owner July 14, 2026 15:45

@cursor cursor Bot left a comment

Copy link
Copy Markdown

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.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 06e53d3. Configure here.

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