Skip to content

feat(predict): always use Permit2 fee auth and attach allowancesTx via feature flag#27028

Merged
matallui merged 7 commits intomainfrom
predict/relay-allowance-tx
Mar 5, 2026
Merged

feat(predict): always use Permit2 fee auth and attach allowancesTx via feature flag#27028
matallui merged 7 commits intomainfrom
predict/relay-allowance-tx

Conversation

@matallui
Copy link
Contributor

@matallui matallui commented Mar 4, 2026

Description

When the Permit2 feature flag is enabled, the provider now:

  1. Always uses Permit2 fee authorization — removes the on-chain allowance readiness gate that previously caused a fallback to Safe fee authorization when the Permit2 allowance wasn't set on-chain yet.
  2. Attaches allowancesTx to the relay order when the proxy wallet lacks the required Permit2 allowances, so the relay can submit the allowance transaction on-chain before processing the order.
  3. Simplifies previewOrder FAK logic — FAK order type is now determined purely from feature flags and Permit2 config, since placeOrder guarantees allowances are available.
  4. Gates FAK on actual allowance readiness — for fee-bearing Buy orders, FAK requires both Permit2 fee auth AND confirmed allowance availability (on-chain or via allowancesTx).
  5. Replaces on-chain Permit2 nonce bitmap with random nonce generation to avoid collisions on back-to-back orders and eliminate an RPC round-trip.

Changelog

CHANGELOG entry: null

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/PRED-720

Manual testing steps

Feature: Permit2 fee authorization and allowancesTx relay

  Scenario: user places a BUY order with Permit2 enabled and proxy wallet lacking allowances
    Given Permit2 feature flag is enabled
    And the proxy wallet does not have Permit2 allowances set on-chain

    When user places a BUY order with fees
    Then the order uses Permit2 fee authorization (not Safe fallback)
    And an allowancesTx is attached to the relay request
    And the order type is FAK (if fakOrdersEnabled)

  Scenario: user places a BUY order with Permit2 enabled and proxy wallet already has allowances
    Given Permit2 feature flag is enabled
    And the proxy wallet already has Permit2 allowances on-chain

    When user places a BUY order with fees
    Then the order uses Permit2 fee authorization
    And no allowancesTx is attached
    And the order type is FAK (if fakOrdersEnabled)

  Scenario: user places a SELL order with Permit2 enabled
    Given Permit2 feature flag is enabled

    When user places a SELL order (no fees)
    Then no fee authorization is generated
    And an allowancesTx is attached if proxy wallet lacks allowances
    And the order type is FAK (if fakOrdersEnabled)

  Scenario: allowancesTx generation fails gracefully
    Given Permit2 feature flag is enabled
    And getProxyWalletAllowancesTransaction throws an error

    When user places a BUY order with fees
    Then the order still submits (without allowancesTx)
    And the order type falls back to FOK (not FAK)
    And the error is logged but does not block order placement

Screenshots/Recordings

Before

https://www.loom.com/share/5bd9ee06414042e7a4ba944c666b1b9a

After

FOK + Permit2:
https://www.loom.com/share/1241e70594684c43bf86880ad29b9fdf

FAK + Permit2:
https://www.loom.com/share/94c01e493c6149ef806f09b32afe5430

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Medium Risk
Touches order submission/fee-collection logic and relayer request payloads; incorrect gating or allowance handling could cause orders to fail or use the wrong order type (FAK vs FOK). Changes are localized but impact a critical trading path.

Overview
Permit2 fee collection is now treated as always-available when enabled. placeOrder no longer falls back to Safe fee authorization based on an on-chain Permit2 allowance check, and previewOrder now selects FAK purely from feature flags/config.

Order submission can now include a prerequisite allowances transaction. When the Permit2 feature flag is on and the proxy wallet lacks allowances, placeOrder generates and sends an allowancesTx to the relayer (and logs/continues if generation fails); submitClobOrder includes this optional field in the request body. Permit2 nonce generation was also switched from on-chain bitmap reads to a random nonce to avoid extra RPC calls/collisions, with tests updated accordingly.

Written by Cursor Bugbot for commit 373d73b. This will update automatically on new commits. Configure here.

matallui added 6 commits March 4, 2026 09:08
… missing

During placeOrder(), check if the user's proxy wallet has the required
Permit2 allowances. When missing, generate a signed Safe TX and attach
it to the relay API request as allowancesTx so the relay can submit it
on-chain before processing the order.

The allowancesTx is only attached when:
- No feeAuthorization is present (no fees), OR
- feeAuthorization is of type safe-permit2

Also simplifies previewOrder() to always use FAK when the Permit2/FAK
config is active, since placeOrder now guarantees the allowance.
Add 6 test cases for placeOrder allowancesTx behavior:
- Attaches when permit2 enabled + hasAllowances false + safe-permit2 fee
- Attaches when permit2 enabled + hasAllowances false + no fees
- Skips when feeAuthorization is safe-transaction (fallback path)
- Skips when hasAllowances is true
- Skips when permit2 is disabled
- Continues order on allowance TX generation failure

Add 2 test cases for submitClobOrder allowancesTx serialization.

Update previewOrder tests to reflect simplified FAK logic (no more
Permit2 allowance check in preview).
* main: (34 commits)
  chore: Update a message for Withdraw: Not enough POL (#27001)
  fix: Ensure `redux-persist-filesystem-storage` returns a promise and throws correctly cp-7.67.2 (#26979)
  chore: Bump `snaps-controllers` cp-7.67.2 (#26992)
  fix(TMCU-508): trigger NFT detection on homepage focus (#26919)
  fix: gas_paid_with metrics parameter for MMPay transactions (#26778)
  fix(card): cashback UI fixes (#26993)
  test: mock getQuoteStream with SSE format to fix SmokeTrade abort failures                                            (#26977)
  feat: bump `@metamask-assets-controllers` to `^100.1.0` (#26987)
  feat(networks): add network deletion logic and update tests (#26983)
  fix(perps): remove duplicate AppState listener causing reconnection race cp-7.67.2 (#26982)
  feat(perps): inline deposit flow in pay-with token filter (#26543)
  feat: improve SDKConnectV2 error toasts (#26972)
  fix(predict): refresh balance/allowance before Polymarket order submission (#26954)
  fix(analytics): correct source prop for Perps section ">" navigation event (#26785)
  fix: market insights disclaimer text update (#26971)
  fix: request camera permission on Android during QR transaction signing (#26415)
  test: create Unified Gestures (#26932)
  feat: generic advanced charts component (#26459)
  fix(token-details): Use scoped account for EVM receive address after non-EVM network switch (#26965)
  chore: handle rewards 403 auth with retry (#26834)
  ...
… feature flag

- Fix allowancesTx gate to use feeCollection.permit2Enabled feature flag
  instead of per-order fees, so SELL orders (no fees) also trigger it
- Always use safe-permit2 fee authorization when permit2 is enabled,
  removing the safe-transaction fallback — backend submits allowancesTx
  on-chain first before redeeming the Permit2 authorization
- Remove shouldAttachAllowancesTx restriction that blocked allowancesTx
  when fee auth was safe-transaction (chicken-and-egg problem)
- Use random Permit2 nonce (crypto.getRandomValues) instead of on-chain
  bitmap lookup to avoid nonce collisions on back-to-back trades
- Remove unused safeAddress param from getPermit2Nonce signature
…der type gating

- Remove unused #isPermit2AllowanceReady method and hasPermit2Allowance import
- Gate FAK order type on permit2AllowanceReady for fee-bearing orders
- Remove all dead mockHasPermit2Allowance setup from tests
- Extract setupAllowancesTxTest helper to reduce test duplication
- Simplify multi-condition test names per unit testing guidelines
Add JSDoc explaining why getPermit2Nonce uses random values instead of
on-chain bitmap reads: avoids nonce collisions on back-to-back orders
whose fee collection hasn't settled, and removes an RPC round-trip.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot metamaskbot added the team-predict Predict team label Mar 4, 2026
@github-actions github-actions bot added the size-L label Mar 4, 2026
@matallui matallui marked this pull request as ready for review March 4, 2026 23:39
@matallui matallui requested a review from a team as a code owner March 4, 2026 23:39
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 2 potential issues.

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

…once conflict

Both createSafeFeeAuthorization and getProxyWalletAllowancesTransaction
read the same on-chain Safe nonce. If both execute in the same placeOrder
call, the relay invalidates one when executing the other. Guard the
allowancesTx block with hasSafeFeeAuth to skip it when a Safe fee
authorization was already signed.

Also remove unused fees param from setupAllowancesTxTest helper.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePredictions, SmokeWalletPlatform, SmokeConfirmations
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: medium
  • AI Confidence: 90%
click to see 🤖 AI reasoning details

E2E Test Selection:
The changes are focused on the Polymarket prediction market provider, specifically modifying how Permit2 allowances and order types (FAK/FOK) are handled. Key changes include:

  1. Removed on-chain Permit2 allowance checks in favor of always using Permit2 fee authorization when enabled
  2. Added new allowancesTx parameter to attach allowance transactions to orders for the relay to submit
  3. Changed Permit2 nonce generation from on-chain bitmap reading to random generation to avoid RPC calls and nonce collisions

These changes affect the order placement flow in Polymarket predictions. Per the tag descriptions:

  • SmokePredictions: Primary tag for Polymarket prediction market testing (opening positions, cashing out, claiming winnings)
  • SmokeWalletPlatform: Required per SmokePredictions description as Predictions is a section inside Trending
  • SmokeConfirmations: Required per SmokePredictions description as opening/closing positions are on-chain transactions

The risk is medium because:

  • Changes are isolated to Polymarket provider (no shared components affected)
  • Logic changes could affect order placement success/failure
  • Comprehensive unit tests were updated alongside the changes
  • No UI or navigation changes that could break other tests

Performance Test Selection:
No performance tests needed. The changes are internal logic modifications to the Polymarket provider that don't affect UI rendering, list performance, or data loading patterns. The change to random nonce generation actually reduces RPC calls (improves performance by avoiding on-chain reads). No changes to performance test infrastructure or critical user flow performance characteristics.

View GitHub Actions results

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

⚠️ E2E Fixture Validation — Structural changes detected

Category Count
New keys 66
Missing keys 7
Type mismatches 0
Value mismatches 7 (informational)

The committed fixture schema is out of date. To update, comment:

@metamaskbot update-mobile-fixture

View full details | Download diff report

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 5, 2026

Copy link
Contributor

@caieu caieu left a comment

Choose a reason for hiding this comment

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

LGTM

@matallui matallui added this pull request to the merge queue Mar 5, 2026
Merged via the queue into main with commit 2b288d1 Mar 5, 2026
105 checks passed
@matallui matallui deleted the predict/relay-allowance-tx branch March 5, 2026 19:55
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2026
@metamaskbot metamaskbot added the release-7.69.0 Issue or pull request that will be included in release 7.69.0 label Mar 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.69.0 Issue or pull request that will be included in release 7.69.0 size-L team-predict Predict team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants