Skip to content

feat(transaction-pay-controller): add secondLeg flag and to retarget to getAmountData - #11787

Open
tuna1207 wants to merge 7 commits into
mainfrom
feat/sub-1162-second-leg-chomp
Open

tuna1207 wants to merge 7 commits into
mainfrom
feat/sub-1162-second-leg-chomp

Conversation

@tuna1207

@tuna1207 tuna1207 commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Explanation

Money Account premium deposits need Sentinel to sign the exact amount that landed on the account. Only the non-atomic second leg knows that settled amount, and a premium deposit goes to a different teller contract than the default vault.
This PR adds two optional fields to TransactionPayController:getAmountData:

  • GetAmountDataRequest.secondLeg: set to true only when the second leg calls with the settled amount. Callers at quote time and the fiat calldata re-encoding callers don't set it.
  • GetAmountDataResponse.updates[].to: an optional new target for a nested call. The second leg applies it to both the submitted batch and the parent transaction's nestedTransactions. Updates without to keep the existing target.
    Both fields are optional, so existing getAmountData handlers don't change.

References

Checklist

  • Updated tests (2,079 passed)
  • Updated JSDoc and the package changelog
  • Messenger action, changed-file lint, changelog validation, and TypeScript checks passed
  • Breaking changes (not applicable)

Made with Cursor


Note

Medium Risk
Changes non-atomic pay second-leg submission and calldata encoding paths where wrong to or amount could mis-route funds; fields are optional so existing getAmountData handlers stay compatible.

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

Co-authored-by: Cursor <cursoragent@cursor.com>
@tuna1207
tuna1207 requested review from a team as code owners November 26, 2026 15:45
@tuna1207
tuna1207 deployed to default-branch November 26, 2026 15:45 — with GitHub Actions Active
Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

🛑 Files requiring codeowner review

To merge this PR, you'll need to get approvals from the following owners.

@MetaMask/confirmations (6 files)
  • packages/transaction-pay-controller/CHANGELOG.md
  • packages/transaction-pay-controller/src/strategy/fiat/fiat-submit-with-transaction-data.test.ts
  • packages/transaction-pay-controller/src/types.ts
  • packages/transaction-pay-controller/src/utils/max-quote.test.ts
  • packages/transaction-pay-controller/src/utils/second-leg.test.ts
  • packages/transaction-pay-controller/src/utils/second-leg.ts

ℹ️ Files co-owned by @MetaMask/core-platform

For convenience in maintaining this repo, this team co-owns the following files and thus may approve changes to them.

Note

You do not need to get approval from @MetaMask/core-platform to merge this PR.

(Expand to view list of files)
  • packages/transaction-pay-controller/CHANGELOG.md

transaction,
});
} catch (error) {
const transactionHash = await waitForChompDeposit(chompOptions);

Copy link
Copy Markdown
Member

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 delay here nor directly couple ourselves to CHOMP execution before throwing.

Rather we had planned just to treat this as a success if the first leg was successful and it was CHOMP compatible, likely based on a feature flag, since CHOMP may get to it immediately or in future based on a retry mechanism for example.

The recovery flow exists just to handle a race condition where CHOMP has already acted at the same time.

This is already in the confirmations backlog, I believe with @OGPoyraz 👍

@OGPoyraz OGPoyraz Nov 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @matthewwalsh0, agreed we shouldn't delay or couple to CHOMP execution here. Treating a successful, CHOMP-compatible first leg as success is the better direction.

I'm not sure this recovery path is urgently needed for the membershipSubscription flow as it's not even released in production yet:

  • Crypto deposit: membershipSubscription locks the pay amount to X, so it never reaches the second leg. The second leg only applies to "max" mode.
  • Fiat deposit: Most users will wait for the order to land in the app, so the regular second leg will cover it. This is an edge case. Plus we just delivered a resumable transactions for fiat, meaning that this fiat transactions will continue where it's left off if user re-opens app.

I'll keep this in the confirmations backlog and revisit once we have meaningful Confirmation analytics fyi @tuna1207

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agree, removed the CHOMP wait. This PR now only adds secondLeg and to. Thanks @matthewwalsh0 @OGPoyraz

tuna1207 and others added 4 commits November 27, 2026 14:02
…t updates

getAmountData updates can now include a `to`. The second leg applies it to both the submitted calls and the stored parent calls, so a premium teller deposit is not sent to the base teller.

Co-authored-by: Cursor <cursoragent@cursor.com>
…eadline

Measure the CHOMP deposit wait with performance.now() so that a wall-clock
change cannot extend the 60 second wait. Also drop an unreachable return
after the polling loop, which left a line uncovered and failed the 100%
coverage threshold.

Co-authored-by: Cursor <cursoragent@cursor.com>
…leg-chomp

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	packages/transaction-pay-controller/CHANGELOG.md
…leg call resolution fails

Co-authored-by: Cursor <cursoragent@cursor.com>
@tuna1207 tuna1207 changed the title feat(transaction-pay-controller): recover deferred second-leg deposits feat(transaction-pay-controller): add secondLeg flag and to retarget to getAmountData Nov 27, 2026
@tuna1207
tuna1207 enabled auto-merge November 27, 2026 14:20
matthewwalsh0
matthewwalsh0 previously approved these changes Nov 27, 2026

@tanguyenvn tanguyenvn left a comment

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.

This PR adds the secondLeg flag and lets getAmountData change the to of a call. Both changes work and have tests. But SUB-1162 also asks Core for a bounded CHOMP wait (up to 2 minutes) when the deposit build fails. The final diff has no wait, and a getAmountData throw (the mobile 'defer to CHOMP' path) fails the second leg before any CHOMP check runs. Separately, outside this diff: findRecentChompVaultDeposit in chomp.ts matches any outgoing mUSD transfer from the Money account with amount >= the settled amount, and does not check the recipient. So an unrelated transfer can show as a CHOMP success. Please check if a recipient or vault check is needed.


### Added

- Add the optional `secondLeg` flag to `GetAmountDataRequest`, set only when encoding calls for the settled amount in the non-atomic second leg ([#11787](https://github.com/MetaMask/core/pull/11787)).

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.

[Must fix] The bounded CHOMP wait from SUB-1162 is not in this PR.

What happens:

  1. A later commit removed the CHOMP wait.
  2. withChompRecovery now does one scan before submit and one scan after a submit failure.
  3. CHOMP deposits about 1 minute after the funds land, so a single scan usually misses it.
  4. The user sees a failure, but the ticket expects success with the CHOMP transaction.

Suggestion: Did the team drop the 2-minute wait on purpose? If not, add a bounded poll (for example, up to 2 minutes) for the CHOMP deposit, and add tests for success and timeout.

'TransactionPayController:getAmountData',
{
amount: sourceAmountRaw,
secondLeg: true,

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.

[Must fix] A getAmountData throw (defer to CHOMP) fails the second leg with no CHOMP check.

What happens:

  1. Mobile throws from its getAmountData handler when Sentinel errors or times out.
  2. resolveSecondLegCalls runs before withChompRecovery and outside the try.
  3. The error goes up at once: no CHOMP scan, no wait, and no Second leg: prefix.
  4. Acceptance criterion 3 fails. The deposit shows failure even when CHOMP deposits later.

Suggestion: Move call resolution inside the recovery path, or catch the throw and run the bounded CHOMP wait. Is a throw the agreed defer signal, or should the response have an explicit defer field?

const nested = nestedTransactions[nestedTransactionIndex];
if (nested) {
nested.data = data;
nested.to = to ?? nested.to;

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.

[Should fix] The new to is used with no check.

What happens:

  1. A handler returns any to in updates.
  2. Core writes it to the parent transaction and submits the batch to that address.
  3. A wrong to can send the user's funds to the wrong contract.
  4. to is on the shared GetAmountDataResponse type, but max-quote and fiat ignore it with no warning.

Suggestion: Accept to only when secondLeg is true, and check it (for example, a valid address, or an allow list of known tellers). Document on the type that only the second leg honours to.

);
});

it('passes the settled amount to getAmountData as a second-leg request', async () => {

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.

[Should fix] No tests for the defer and timeout paths.

What happens:

  1. The tests cover the secondLeg flag and the to retarget.
  2. No test makes getAmountData reject (Sentinel error or timeout).
  3. SUB-1162 asks for unit tests for defer, timeout, and no second leg.

Suggestion: Add tests where getAmountData rejects. Assert the expected result: CHOMP success inside the wait, and failure after the wait ends.

for (const { nestedTransactionIndex, data } of updates) {
if (tx.nestedTransactions?.[nestedTransactionIndex]) {
tx.nestedTransactions[nestedTransactionIndex].data = data;
for (const { nestedTransactionIndex, data, to } of updates) {

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.

[Nit] The update loop is now in two places.

What happens:

  1. The loop at line 189 and this loop do the same data and to update.
  2. A future change to one loop can miss the other.

Suggestion: Extract a helper, for example applyAmountUpdates(nestedTransactions, updates), and call it in both places.

This branch was successfully deployed

1 active (outdated) deployment
default-branch — b6568dad Deployed Nov 26, 2026 by tuna1207 via Determine whether this PR is a release PR #7686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants