Skip to content

Commit cd30173

Browse files
authored
Merge pull request #274 from Polymarket/fix/dev-398-perps-order-update-race
fix(client): prevent perps order update race
2 parents 7d13986 + 3e7927f commit cd30173

6 files changed

Lines changed: 600 additions & 370 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@polymarket/client": patch
3+
---
4+
5+
Prevent Perps `placeOrder` from missing private order updates that arrive before the command acknowledgement. High-level placement now generates a client order ID when callers omit it.

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
## Platform Invariants
6161

6262
- A market's minimum tick size may become finer, such as `0.01` to `0.001`, but it cannot become coarser, such as `0.001` to `0.01`. SDK caching and recovery logic may rely on this monotonic behavior and should not add defensive handling for tick-size coarsening.
63+
- When a Perps order is submitted with a client order ID, every corresponding private order update echoes that same client order ID. SDK order-placement workflows may rely on this invariant for pre-acknowledgement correlation.
6364

6465
## TypeScript config
6566

packages/client/src/websockets/perps/actions/trading.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PerpsTimeInForce } from '@polymarket/bindings/perps';
33
import { describe, expect, it } from 'vitest';
44
import { createPerpsOpTypedDataPayload } from '../signing';
55
import {
6-
type PerpsTradingTransport,
6+
type PerpsCommandExecutor,
77
postPerpsOrders,
88
toPerpsCommandBodyOp,
99
updatePerpsMargin,
@@ -17,8 +17,8 @@ const UPDATE_MARGIN_DATA_HASH =
1717
describe('Perps trading actions', () => {
1818
describe('createPerpsOpTypedDataPayload', () => {
1919
it('signs entry orders with backend-compatible createOrders bytes', async () => {
20-
const transport: PerpsTradingTransport = {
21-
async sendSignedWsCommand(request) {
20+
const client: PerpsCommandExecutor = {
21+
async executeCommand(request, responseSchema) {
2222
const payload = createPerpsOpTypedDataPayload({
2323
chainId: 31_337,
2424
op: request.op,
@@ -45,12 +45,12 @@ describe('Perps trading actions', () => {
4545
type: 'createOrders',
4646
});
4747

48-
return request.responseSchema.parse([{ oid: 123, status: 'ok' }]);
48+
return responseSchema.parse([{ oid: 123, status: 'ok' }]);
4949
},
5050
};
5151

5252
await expect(
53-
postPerpsOrders(transport, {
53+
postPerpsOrders(client, {
5454
orders: [
5555
{
5656
instrumentId: 1,
@@ -66,8 +66,8 @@ describe('Perps trading actions', () => {
6666
});
6767

6868
it('serializes reduce-only entry orders', async () => {
69-
const transport: PerpsTradingTransport = {
70-
async sendSignedWsCommand(request) {
69+
const client: PerpsCommandExecutor = {
70+
async executeCommand(request, responseSchema) {
7171
expect(toPerpsCommandBodyOp(request.op)).toEqual({
7272
args: [
7373
{
@@ -83,12 +83,12 @@ describe('Perps trading actions', () => {
8383
type: 'createOrders',
8484
});
8585

86-
return request.responseSchema.parse([{ oid: 123, status: 'ok' }]);
86+
return responseSchema.parse([{ oid: 123, status: 'ok' }]);
8787
},
8888
};
8989

9090
await expect(
91-
postPerpsOrders(transport, {
91+
postPerpsOrders(client, {
9292
orders: [
9393
{
9494
instrumentId: 1,
@@ -104,8 +104,8 @@ describe('Perps trading actions', () => {
104104
});
105105

106106
it('signs isolated margin adjustments with backend-compatible bytes', async () => {
107-
const transport: PerpsTradingTransport = {
108-
async sendSignedWsCommand(request) {
107+
const client: PerpsCommandExecutor = {
108+
async executeCommand(request, responseSchema) {
109109
const payload = createPerpsOpTypedDataPayload({
110110
chainId: 31_337,
111111
op: request.op,
@@ -126,12 +126,12 @@ describe('Perps trading actions', () => {
126126
type: 'updateMargin',
127127
});
128128

129-
return request.responseSchema.parse({ status: 'ok' });
129+
return responseSchema.parse({ status: 'ok' });
130130
},
131131
};
132132

133133
await expect(
134-
updatePerpsMargin(transport, {
134+
updatePerpsMargin(client, {
135135
amount: '-1234567890.123456789012345678',
136136
instrumentId: 7,
137137
}),

0 commit comments

Comments
 (0)