Skip to content

fix(predict): refresh balance/allowance before Polymarket order submission#26954

Merged
matallui merged 1 commit into
mainfrom
predict/fix-polymarket-400-allowance-error
Mar 4, 2026
Merged

fix(predict): refresh balance/allowance before Polymarket order submission#26954
matallui merged 1 commit into
mainfrom
predict/fix-polymarket-400-allowance-error

Conversation

@matallui

@matallui matallui commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Description

Polymarket's CLOB infrastructure is intermittently returning 400 errors with "not enough balance / allowance" when placing orders at high request rates. As a temporary workaround communicated by the Polymarket team, we now call GET /balance-allowance/update before each order to refresh the balance/allowance state on their end.

Changes:

  • utils.ts: Add refreshBalanceAllowance() utility that calls the CLOB balance-allowance/update endpoint with L2 HMAC auth headers
    • BUY orders → asset_type=COLLATERAL (USDC)
    • SELL orders → asset_type=CONDITIONAL with the order's token_id
  • PolymarketProvider.ts: Call refreshBalanceAllowance() in placeOrder() right before submitClobOrder(), wrapped in try/catch so failures don't block order submission
  • utils.test.ts: 5 new tests covering BUY/SELL paths, auth headers, error resilience, and custom signature types
  • PolymarketProvider.test.ts: 4 new integration tests verifying call ordering, parameter passing for both sides, and graceful degradation on refresh failure

Note: This is a temporary workaround. TODO comments are in place for removal once Polymarket resolves the underlying infrastructure issue.

Changelog

CHANGELOG entry: null

Related issues

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

Manual testing steps

Feature: Polymarket order placement with balance/allowance refresh

  Scenario: user places a BUY order on a prediction market
    Given the user has USDC deposited in their Polymarket proxy wallet

    When user places a BUY order on any market
    Then the balance/allowance refresh call fires before the order submission
    And the order completes successfully

  Scenario: user places a SELL order on a prediction market
    Given the user holds outcome tokens for a market

    When user places a SELL order
    Then the balance/allowance refresh call fires with the token_id before the order
    And the order completes successfully

  Scenario: balance/allowance refresh endpoint is down
    Given the refresh endpoint returns an error

    When user places any order
    Then the order submission still proceeds normally

Screenshots/Recordings

N/A — no UI changes

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
Adds a new preflight network call in the order placement path; while failures are best-effort and don’t block orders, it changes timing/behavior in a core trading flow and could affect reliability or rate limits.

Overview
Adds a temporary workaround for intermittent Polymarket CLOB not enough balance / allowance errors by introducing refreshBalanceAllowance() (calls GET /balance-allowance/update with L2 HMAC headers) and invoking it in PolymarketProvider.placeOrder() immediately before submitClobOrder().

The refresh is best-effort (wrapped in try/catch with logging) and varies request params by side: BUY refreshes asset_type=COLLATERAL, SELL refreshes asset_type=CONDITIONAL with token_id. Updates unit/integration tests to cover parameter selection, call ordering, header usage, and non-blocking behavior on refresh failure.

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

…ssion

Polymarket CLOB infrastructure intermittently returns 400 'not enough
balance / allowance' errors at high request rates. As a temporary
workaround, call GET /balance-allowance/update before each order to
refresh the balance/allowance state on their end.

- Add refreshBalanceAllowance() utility with L2 HMAC auth
- Call it in placeOrder() before submitClobOrder (best-effort)
- BUY orders refresh COLLATERAL, SELL orders refresh CONDITIONAL
- Failure does not block order submission
@matallui matallui requested a review from a team as a code owner March 3, 2026 22:41
@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

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 3, 2026
@github-actions github-actions Bot added the size-M label Mar 3, 2026
@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

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

E2E Test Selection:
The changes add a temporary workaround for a Polymarket infrastructure issue by calling a balance/allowance refresh endpoint before placing orders. The changes are isolated to the Polymarket prediction market provider (PolymarketProvider.ts and utils.ts).

Key changes:

  1. New refreshBalanceAllowance function in utils.ts that calls Polymarket's /balance-allowance/update endpoint
  2. Modified placeOrder method in PolymarketProvider.ts to call this function before submitting orders (wrapped in try-catch so failures don't block order submission)
  3. Comprehensive unit tests added for the new functionality

Tag selection rationale:

  • SmokePredictions: Primary tag - directly tests Polymarket prediction market integration including opening positions, which is exactly what this change affects
  • SmokeWalletPlatform: Required per SmokePredictions description - "Predictions is also a section inside the Trending tab (SmokeWalletPlatform)"
  • SmokeConfirmations: Required per SmokePredictions description - "opening/closing positions are on-chain transactions"

The risk is medium because while the changes are isolated and the workaround is best-effort (non-blocking on failure), it does modify the order placement flow which is a critical user action in the predictions feature.

Performance Test Selection:
The changes add one additional API call before order submission, but it's designed to be non-blocking (wrapped in try-catch). The performance impact is minimal - it's a single GET request that doesn't block the main order flow if it fails. The @PerformancePredict tag covers prediction market flows, but this minor addition of a best-effort API call doesn't warrant performance testing as it won't significantly impact user-perceived performance.

View GitHub Actions results

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

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

Comment thread app/components/UI/Predict/providers/polymarket/utils.ts
@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

⚠️ E2E Fixture Validation — Structural changes detected

Category Count
New keys 60
Missing keys 0
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

sonarqubecloud Bot commented Mar 3, 2026

Copy link
Copy Markdown

@matallui matallui enabled auto-merge March 3, 2026 23:17

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

LGTM

@matallui matallui added this pull request to the merge queue Mar 4, 2026
Merged via the queue into main with commit 2ec96ff Mar 4, 2026
131 checks passed
@matallui matallui deleted the predict/fix-polymarket-400-allowance-error branch March 4, 2026 11:27
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 4, 2026
@metamaskbot metamaskbot added the release-7.69.0 Issue or pull request that will be included in release 7.69.0 label Mar 4, 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-M team-predict Predict team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants