Skip to content

Defi controller v2#9503

Merged
bergarces merged 46 commits into
mainfrom
defi-controller-v2
Jul 21, 2026
Merged

Defi controller v2#9503
bergarces merged 46 commits into
mainfrom
defi-controller-v2

Conversation

@bergarces

@bergarces bergarces commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Explanation

Export new DeFiPositionsControllerV2 and supporting types.

It fetches from accounts api v6 endpoint for a single account group and it does so only by demand.

Extension preview PR: MetaMask/metamask-extension#44392
Mobile preview PR: MetaMask/metamask-mobile#33488

Extension.DeFi.Demo.mp4
Mobile.DeFi.Demo.mp4

References

Related to https://consensyssoftware.atlassian.net/browse/ASSETS-3600

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 wallet/DeFi state and external API integration affect balance UX, but it is gated by isEnabled, isolated from the legacy controller key, and covered by extensive unit tests.

Overview
Introduces DeFiPositionsControllerV2, a parallel DeFi positions controller that loads data on demand from the Accounts API v6 multiaccount balances endpoint (via ApiPlatformClient), instead of the legacy controller’s flow. Clients call fetchDeFiPositions (messenger action supported); there are no automatic event subscriptions yet.

Fetched positions are normalized with groupDeFiPositionsV6 and persisted under allDeFiPositionsV2, keyed by internal account ID, in a shape meant for the DeFi tab and details UI (protocol groups per chain, sections, market values with lending treated as liabilities). buildDeFiBalancesQuery builds CAIP account IDs and supported EVM/Solana networks from the selected account group; mergePositionsForAccounts merges per-account state for group-level surfaces.

Behavior details: no-ops when disabled or when the group has no EVM/Solana accounts; respects processingDefiPositions by keeping prior state for still-indexing accounts; merges updates per account without wiping unrelated accounts; forceRefresh bypasses TanStack Query cache (staleTime: 0); failed fetches leave existing state. Types and helpers are exported from the package; changelog updated. A dev-only HTML visualizer is included for inspecting v6 DeFi responses.

Reviewed by Cursor Bugbot for commit 7144a63. 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
@bergarces

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.5-preview-2a8139a3e
@metamask-previews/accounts-controller@39.0.5-preview-2a8139a3e
@metamask-previews/address-book-controller@7.1.2-preview-2a8139a3e
@metamask-previews/ai-controllers@0.8.0-preview-2a8139a3e
@metamask-previews/analytics-controller@1.2.1-preview-2a8139a3e
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-2a8139a3e
@metamask-previews/announcement-controller@8.1.0-preview-2a8139a3e
@metamask-previews/app-metadata-controller@2.0.1-preview-2a8139a3e
@metamask-previews/approval-controller@9.0.2-preview-2a8139a3e
@metamask-previews/assets-controller@11.0.0-preview-2a8139a3e
@metamask-previews/assets-controllers@109.4.1-preview-2a8139a3e
@metamask-previews/authenticated-user-storage@3.0.1-preview-2a8139a3e
@metamask-previews/base-controller@9.1.0-preview-2a8139a3e
@metamask-previews/base-data-service@0.1.3-preview-2a8139a3e
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-2a8139a3e
@metamask-previews/bridge-controller@77.5.0-preview-2a8139a3e
@metamask-previews/bridge-status-controller@74.2.0-preview-2a8139a3e
@metamask-previews/build-utils@3.0.4-preview-2a8139a3e
@metamask-previews/chain-agnostic-permission@1.7.0-preview-2a8139a3e
@metamask-previews/chomp-api-service@3.1.0-preview-2a8139a3e
@metamask-previews/claims-controller@0.5.3-preview-2a8139a3e
@metamask-previews/client-controller@1.0.1-preview-2a8139a3e
@metamask-previews/client-utils@1.1.0-preview-2a8139a3e
@metamask-previews/compliance-controller@2.1.0-preview-2a8139a3e
@metamask-previews/composable-controller@12.0.1-preview-2a8139a3e
@metamask-previews/config-registry-controller@0.4.1-preview-2a8139a3e
@metamask-previews/connectivity-controller@0.3.0-preview-2a8139a3e
@metamask-previews/controller-utils@12.3.0-preview-2a8139a3e
@metamask-previews/core-backend@6.5.0-preview-2a8139a3e
@metamask-previews/delegation-controller@3.0.2-preview-2a8139a3e
@metamask-previews/earn-controller@12.2.2-preview-2a8139a3e
@metamask-previews/eip-5792-middleware@3.0.5-preview-2a8139a3e
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-2a8139a3e
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-2a8139a3e
@metamask-previews/ens-controller@19.1.5-preview-2a8139a3e
@metamask-previews/eth-block-tracker@15.0.1-preview-2a8139a3e
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-2a8139a3e
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-2a8139a3e
@metamask-previews/foundryup@1.0.1-preview-2a8139a3e
@metamask-previews/gas-fee-controller@26.2.4-preview-2a8139a3e
@metamask-previews/gator-permissions-controller@4.2.3-preview-2a8139a3e
@metamask-previews/geolocation-controller@0.1.3-preview-2a8139a3e
@metamask-previews/java-tron-up@1.0.0-preview-2a8139a3e
@metamask-previews/json-rpc-engine@10.5.0-preview-2a8139a3e
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-2a8139a3e
@metamask-previews/keyring-controller@27.1.0-preview-2a8139a3e
@metamask-previews/local-node-utils@1.0.0-preview-2a8139a3e
@metamask-previews/logging-controller@8.0.2-preview-2a8139a3e
@metamask-previews/message-manager@14.1.2-preview-2a8139a3e
@metamask-previews/messenger@2.0.0-preview-2a8139a3e
@metamask-previews/messenger-cli@0.2.0-preview-2a8139a3e
@metamask-previews/money-account-api-data-service@0.1.0-preview-2a8139a3e
@metamask-previews/money-account-balance-service@2.2.0-preview-2a8139a3e
@metamask-previews/money-account-controller@0.3.3-preview-2a8139a3e
@metamask-previews/money-account-upgrade-controller@2.2.1-preview-2a8139a3e
@metamask-previews/multichain-account-service@13.0.0-preview-2a8139a3e
@metamask-previews/multichain-api-middleware@4.0.1-preview-2a8139a3e
@metamask-previews/multichain-network-controller@3.2.1-preview-2a8139a3e
@metamask-previews/multichain-transactions-controller@7.1.1-preview-2a8139a3e
@metamask-previews/name-controller@9.1.2-preview-2a8139a3e
@metamask-previews/network-connection-banner-controller@0.1.0-preview-2a8139a3e
@metamask-previews/network-controller@34.0.0-preview-2a8139a3e
@metamask-previews/network-enablement-controller@5.5.0-preview-2a8139a3e
@metamask-previews/notification-services-controller@25.0.0-preview-2a8139a3e
@metamask-previews/passkey-controller@2.0.1-preview-2a8139a3e
@metamask-previews/permission-controller@13.1.1-preview-2a8139a3e
@metamask-previews/permission-log-controller@5.1.0-preview-2a8139a3e
@metamask-previews/perps-controller@9.2.1-preview-2a8139a3e
@metamask-previews/phishing-controller@17.2.1-preview-2a8139a3e
@metamask-previews/platform-api-docs@0.0.0-preview-2a8139a3e
@metamask-previews/polling-controller@16.0.8-preview-2a8139a3e
@metamask-previews/preferences-controller@23.1.0-preview-2a8139a3e
@metamask-previews/profile-metrics-controller@4.0.2-preview-2a8139a3e
@metamask-previews/profile-sync-controller@28.3.0-preview-2a8139a3e
@metamask-previews/ramps-controller@17.0.0-preview-2a8139a3e
@metamask-previews/rate-limit-controller@7.0.1-preview-2a8139a3e
@metamask-previews/react-data-query@0.2.1-preview-2a8139a3e
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-2a8139a3e
@metamask-previews/sample-controllers@5.0.3-preview-2a8139a3e
@metamask-previews/seedless-onboarding-controller@10.0.3-preview-2a8139a3e
@metamask-previews/selected-network-controller@26.1.5-preview-2a8139a3e
@metamask-previews/shield-controller@5.1.3-preview-2a8139a3e
@metamask-previews/signature-controller@39.2.7-preview-2a8139a3e
@metamask-previews/smart-transactions-controller@25.0.1-preview-2a8139a3e
@metamask-previews/snap-account-service@2.0.0-preview-2a8139a3e
@metamask-previews/social-controllers@2.5.0-preview-2a8139a3e
@metamask-previews/solana-test-validator-up@1.0.0-preview-2a8139a3e
@metamask-previews/stellar-quickstart-up@0.0.0-preview-2a8139a3e
@metamask-previews/storage-service@1.0.2-preview-2a8139a3e
@metamask-previews/subscription-controller@6.2.1-preview-2a8139a3e
@metamask-previews/transaction-controller@69.0.0-preview-2a8139a3e
@metamask-previews/transaction-pay-controller@25.0.0-preview-2a8139a3e
@metamask-previews/user-operation-controller@41.2.7-preview-2a8139a3e
@metamask-previews/wallet@7.0.1-preview-2a8139a3e
@metamask-previews/wallet-cli@0.0.0-preview-2a8139a3e

@bergarces
bergarces marked this pull request as ready for review July 21, 2026 13:06
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
'eip155:999',
'eip155:5042',
];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is no API that we can hit to get this list.

It's the intersection between what accounts API supports and zerion supports.

Definitely not ideal if we want to dynamically support new chains.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The goal of this file is to create the query string parameters that need to be passed to accounts api, as well as a mapping between the internal accountIds used in state and the CAIP-10 account ids that the request and response from accounts-api deals with.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think we want to lose this visualizer. It has been very helpful to inspect the response from the API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we want to exclude this from the library build via the tsconfig.build.json?

"exclude": ["**/devtools/"]

@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.

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 a8f2b54. Configure here.

// staleTime: 0 makes TanStack treat the cache as stale for this call.
...(options?.forceRefresh ? { staleTime: 0 } : {}),
},
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Throttling is managed at apiClient level (Tanstack) using the parameters (key).

We can still force a fetch with forceRefresh.

@bergarces

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.5-preview-7144a6308
@metamask-previews/accounts-controller@39.0.5-preview-7144a6308
@metamask-previews/address-book-controller@7.1.2-preview-7144a6308
@metamask-previews/ai-controllers@0.8.0-preview-7144a6308
@metamask-previews/analytics-controller@1.2.1-preview-7144a6308
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-7144a6308
@metamask-previews/announcement-controller@8.1.0-preview-7144a6308
@metamask-previews/app-metadata-controller@2.0.1-preview-7144a6308
@metamask-previews/approval-controller@9.0.2-preview-7144a6308
@metamask-previews/assets-controller@11.0.0-preview-7144a6308
@metamask-previews/assets-controllers@109.4.1-preview-7144a6308
@metamask-previews/authenticated-user-storage@3.0.1-preview-7144a6308
@metamask-previews/base-controller@9.1.0-preview-7144a6308
@metamask-previews/base-data-service@0.1.3-preview-7144a6308
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-7144a6308
@metamask-previews/bridge-controller@77.6.0-preview-7144a6308
@metamask-previews/bridge-status-controller@74.3.0-preview-7144a6308
@metamask-previews/build-utils@3.0.4-preview-7144a6308
@metamask-previews/chain-agnostic-permission@1.7.0-preview-7144a6308
@metamask-previews/chomp-api-service@3.1.0-preview-7144a6308
@metamask-previews/claims-controller@0.5.3-preview-7144a6308
@metamask-previews/client-controller@1.0.1-preview-7144a6308
@metamask-previews/client-utils@1.2.0-preview-7144a6308
@metamask-previews/compliance-controller@2.1.0-preview-7144a6308
@metamask-previews/composable-controller@12.0.1-preview-7144a6308
@metamask-previews/config-registry-controller@0.4.1-preview-7144a6308
@metamask-previews/connectivity-controller@0.3.0-preview-7144a6308
@metamask-previews/controller-utils@12.3.0-preview-7144a6308
@metamask-previews/core-backend@6.5.0-preview-7144a6308
@metamask-previews/delegation-controller@3.0.2-preview-7144a6308
@metamask-previews/earn-controller@12.2.2-preview-7144a6308
@metamask-previews/eip-5792-middleware@3.0.5-preview-7144a6308
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-7144a6308
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-7144a6308
@metamask-previews/ens-controller@19.1.5-preview-7144a6308
@metamask-previews/eth-block-tracker@15.0.1-preview-7144a6308
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-7144a6308
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-7144a6308
@metamask-previews/foundryup@1.0.1-preview-7144a6308
@metamask-previews/gas-fee-controller@26.2.4-preview-7144a6308
@metamask-previews/gator-permissions-controller@4.2.3-preview-7144a6308
@metamask-previews/geolocation-controller@0.1.3-preview-7144a6308
@metamask-previews/java-tron-up@1.0.0-preview-7144a6308
@metamask-previews/json-rpc-engine@10.5.0-preview-7144a6308
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-7144a6308
@metamask-previews/keyring-controller@27.1.0-preview-7144a6308
@metamask-previews/local-node-utils@1.0.0-preview-7144a6308
@metamask-previews/logging-controller@8.0.2-preview-7144a6308
@metamask-previews/message-manager@14.1.2-preview-7144a6308
@metamask-previews/messenger@2.0.0-preview-7144a6308
@metamask-previews/messenger-cli@0.2.0-preview-7144a6308
@metamask-previews/money-account-api-data-service@0.2.0-preview-7144a6308
@metamask-previews/money-account-balance-service@2.3.0-preview-7144a6308
@metamask-previews/money-account-controller@0.3.3-preview-7144a6308
@metamask-previews/money-account-upgrade-controller@2.2.1-preview-7144a6308
@metamask-previews/multichain-account-service@13.0.0-preview-7144a6308
@metamask-previews/multichain-api-middleware@4.0.1-preview-7144a6308
@metamask-previews/multichain-network-controller@3.2.1-preview-7144a6308
@metamask-previews/multichain-transactions-controller@7.1.1-preview-7144a6308
@metamask-previews/name-controller@9.1.2-preview-7144a6308
@metamask-previews/network-connection-banner-controller@0.1.0-preview-7144a6308
@metamask-previews/network-controller@34.0.0-preview-7144a6308
@metamask-previews/network-enablement-controller@5.6.0-preview-7144a6308
@metamask-previews/notification-services-controller@25.0.0-preview-7144a6308
@metamask-previews/passkey-controller@3.0.0-preview-7144a6308
@metamask-previews/permission-controller@13.1.1-preview-7144a6308
@metamask-previews/permission-log-controller@5.1.0-preview-7144a6308
@metamask-previews/perps-controller@9.3.0-preview-7144a6308
@metamask-previews/phishing-controller@17.3.0-preview-7144a6308
@metamask-previews/platform-api-docs@0.0.0-preview-7144a6308
@metamask-previews/polling-controller@16.0.8-preview-7144a6308
@metamask-previews/preferences-controller@23.1.0-preview-7144a6308
@metamask-previews/profile-metrics-controller@4.0.2-preview-7144a6308
@metamask-previews/profile-sync-controller@28.3.0-preview-7144a6308
@metamask-previews/ramps-controller@17.0.0-preview-7144a6308
@metamask-previews/rate-limit-controller@7.0.1-preview-7144a6308
@metamask-previews/react-data-query@0.2.2-preview-7144a6308
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-7144a6308
@metamask-previews/sample-controllers@5.0.3-preview-7144a6308
@metamask-previews/seedless-onboarding-controller@10.0.3-preview-7144a6308
@metamask-previews/selected-network-controller@26.1.5-preview-7144a6308
@metamask-previews/sentinel-api-service@1.0.0-preview-7144a6308
@metamask-previews/shield-controller@5.1.3-preview-7144a6308
@metamask-previews/signature-controller@39.2.7-preview-7144a6308
@metamask-previews/smart-transactions-controller@25.0.1-preview-7144a6308
@metamask-previews/snap-account-service@2.0.0-preview-7144a6308
@metamask-previews/social-controllers@2.5.0-preview-7144a6308
@metamask-previews/solana-test-validator-up@1.0.0-preview-7144a6308
@metamask-previews/stellar-quickstart-up@0.0.0-preview-7144a6308
@metamask-previews/storage-service@1.0.2-preview-7144a6308
@metamask-previews/subscription-controller@6.2.1-preview-7144a6308
@metamask-previews/transaction-controller@69.1.0-preview-7144a6308
@metamask-previews/transaction-pay-controller@25.0.0-preview-7144a6308
@metamask-previews/user-operation-controller@41.2.7-preview-7144a6308
@metamask-previews/wallet@7.0.1-preview-7144a6308
@metamask-previews/wallet-cli@0.0.0-preview-7144a6308

@bergarces
bergarces added this pull request to the merge queue Jul 21, 2026
Merged via the queue into main with commit 0390844 Jul 21, 2026
427 checks passed
@bergarces
bergarces deleted the defi-controller-v2 branch July 21, 2026 16:26
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.

2 participants