Commit 721177c
authored
feat(predict): relay sell orders (MetaMask#22290)
## **Description**
This PR enables relaying of SELL orders through the MetaMask Predict API
relay, similar to how BUY orders are already relayed. Previously, only
BUY orders were sent through the relay to collect fees, while SELL
orders went directly to the Polymarket CLOB endpoint.
**Reason for change:**
- SELL orders should also be relayed to enable consistent fee collection
and monitoring across all order types
- Provides better control and observability for all trading activity
**Improvements:**
- All orders (BUY and SELL) now route through the `CLOB_RELAYER`
endpoint
- Rate limiting now applies to both BUY and SELL orders based on the
last BUY order timestamp
- Simplified order submission logic by removing conditional endpoint
selection
- Updated `OrderResponse` type fields to be optional for better error
handling
- Renamed `BUY_ORDER_RATE_LIMIT_MS` to `ORDER_RATE_LIMIT_MS` to reflect
that rate limiting applies to all orders
## **Changelog**
CHANGELOG entry: null
## **Related issues**
Fixes: N/A
## **Manual testing steps**
```gherkin
Feature: Polymarket SELL order relaying
Scenario: user places a SELL order
Given user has an open position in a Polymarket market
And user is connected to Polygon mainnet
And the Predict feature is enabled
When user navigates to the position details
And user initiates a SELL order
And user confirms the transaction
Then the order should be relayed through the MetaMask API endpoint
And the order should be successfully submitted to Polymarket
And fees should be collected appropriately
Scenario: user is rate limited after placing orders
Given user has placed a BUY order within the last 5 seconds
When user attempts to preview a new order (BUY or SELL)
Then the preview should show rateLimited: true
And user should be prevented from placing another order too quickly
```
## **Screenshots/Recordings**
N/A - Backend/API changes only, no UI changes
### **Before**
- BUY orders → CLOB_RELAYER endpoint (with fees)
- SELL orders → Direct to Polymarket CLOB endpoint (no fees)
- Rate limiting only applied to BUY order previews
### **After**
- BUY orders → CLOB_RELAYER endpoint (with fees)
- SELL orders → CLOB_RELAYER endpoint (no fees)
- Rate limiting applies to all order previews after a BUY order
## **Pre-merge author checklist**
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **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.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Routes both BUY and SELL orders through the MetaMask Predict relayer,
applies unified rate limiting, and updates response handling/types with
corresponding tests.
>
> - **Order routing (utils/submitClobOrder)**:
> - Always post to `CLOB_RELAYER` (`/order`), removing conditional CLOB
endpoint usage.
> - Send both underscore and dash variants of `POLY_*` headers; include
`feeAuthorization` when provided.
> - **Rate limiting**:
> - Rename `BUY_ORDER_RATE_LIMIT_MS` to `ORDER_RATE_LIMIT_MS` and apply
in `PolymarketProvider.isRateLimited`.
> - `previewOrder` now rate-limits when a `signer` is present (affects
SELL after a recent BUY).
> - **Order handling**:
> - `OrderResponse` fields (`errorMsg`, `makingAmount`, `orderID`,
`status`, `takingAmount`, `transactionsHashes`) made optional.
> - `placeOrder` checks top-level `success` and nested
`response.success`; returns `errorMsg` when present.
> - **Tests**:
> - Update submit order tests to expect relayer URL, dual-format
headers, and serialized `feeAuthorization`.
> - Adjust expectations for `response` shape (includes `success: true`).
> - Update rate limit test to expect SELL previews to be rate-limited
after a BUY.
> - Remove unused `errorCode` from 403 path expectations.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
33a2d3e. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 461614b commit 721177c
6 files changed
Lines changed: 100 additions & 48 deletions
File tree
- app/components/UI/Predict/providers/polymarket
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
823 | 823 | | |
824 | 824 | | |
825 | 825 | | |
| 826 | + | |
826 | 827 | | |
827 | 828 | | |
828 | 829 | | |
| |||
1131 | 1132 | | |
1132 | 1133 | | |
1133 | 1134 | | |
1134 | | - | |
| 1135 | + | |
1135 | 1136 | | |
1136 | 1137 | | |
1137 | 1138 | | |
| |||
2599 | 2600 | | |
2600 | 2601 | | |
2601 | 2602 | | |
2602 | | - | |
| 2603 | + | |
2603 | 2604 | | |
2604 | 2605 | | |
2605 | 2606 | | |
| |||
2610 | 2611 | | |
2611 | 2612 | | |
2612 | 2613 | | |
2613 | | - | |
| 2614 | + | |
2614 | 2615 | | |
2615 | 2616 | | |
2616 | 2617 | | |
| |||
2620 | 2621 | | |
2621 | 2622 | | |
2622 | 2623 | | |
2623 | | - | |
| 2624 | + | |
2624 | 2625 | | |
2625 | 2626 | | |
2626 | 2627 | | |
| |||
Lines changed: 11 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
206 | | - | |
| 206 | + | |
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| |||
463 | 463 | | |
464 | 464 | | |
465 | 465 | | |
466 | | - | |
| 466 | + | |
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
| |||
603 | 603 | | |
604 | 604 | | |
605 | 605 | | |
606 | | - | |
| 606 | + | |
607 | 607 | | |
608 | 608 | | |
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
612 | 612 | | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
613 | 620 | | |
614 | 621 | | |
615 | 622 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
309 | 309 | | |
310 | | - | |
311 | | - | |
| 310 | + | |
| 311 | + | |
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
| |||
Lines changed: 61 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
712 | 712 | | |
713 | 713 | | |
714 | 714 | | |
715 | | - | |
| 715 | + | |
716 | 716 | | |
717 | 717 | | |
718 | | - | |
719 | | - | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
720 | 734 | | |
721 | 735 | | |
722 | 736 | | |
| |||
781 | 795 | | |
782 | 796 | | |
783 | 797 | | |
784 | | - | |
| 798 | + | |
785 | 799 | | |
786 | 800 | | |
787 | | - | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
788 | 813 | | |
789 | 814 | | |
| 815 | + | |
790 | 816 | | |
791 | 817 | | |
792 | 818 | | |
| |||
820 | 846 | | |
821 | 847 | | |
822 | 848 | | |
823 | | - | |
| 849 | + | |
824 | 850 | | |
825 | 851 | | |
826 | 852 | | |
827 | 853 | | |
828 | 854 | | |
829 | 855 | | |
830 | | - | |
| 856 | + | |
831 | 857 | | |
832 | 858 | | |
833 | | - | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
834 | 871 | | |
835 | 872 | | |
| 873 | + | |
836 | 874 | | |
837 | 875 | | |
838 | 876 | | |
839 | 877 | | |
840 | 878 | | |
841 | | - | |
| 879 | + | |
842 | 880 | | |
843 | 881 | | |
844 | 882 | | |
| |||
867 | 905 | | |
868 | 906 | | |
869 | 907 | | |
870 | | - | |
| 908 | + | |
871 | 909 | | |
872 | 910 | | |
873 | | - | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
874 | 923 | | |
875 | 924 | | |
| 925 | + | |
876 | 926 | | |
877 | 927 | | |
878 | 928 | | |
| |||
1894 | 1944 | | |
1895 | 1945 | | |
1896 | 1946 | | |
1897 | | - | |
1898 | 1947 | | |
1899 | 1948 | | |
1900 | 1949 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
324 | 325 | | |
325 | 326 | | |
326 | 327 | | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
331 | 332 | | |
| 333 | + | |
332 | 334 | | |
333 | 335 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
350 | 346 | | |
351 | 347 | | |
352 | 348 | | |
| |||
359 | 355 | | |
360 | 356 | | |
361 | 357 | | |
362 | | - | |
363 | 358 | | |
364 | 359 | | |
365 | 360 | | |
| |||
0 commit comments