-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathorders.ts
More file actions
450 lines (418 loc) · 12.8 KB
/
Copy pathorders.ts
File metadata and controls
450 lines (418 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import { z } from 'zod';
import {
DecimalStringSchema,
EpochMillisecondsSchema,
OrderSide,
} from '../shared';
import {
PerpsAssetSchema,
PerpsClientOrderIdSchema,
PerpsDataResponseSchema,
PerpsInstrumentIdSchema,
PerpsOrderIdSchema,
PerpsSideSchema,
PerpsTimeInForceSchema,
PerpsTpSlKindSchema,
PerpsTpSlScopeSchema,
PerpsTradeIdSchema,
PerpsTxHashSchema,
} from './common';
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export enum PerpsOrderStatus {
Accepted = 'accepted',
Open = 'open',
Partial = 'partial',
Filled = 'filled',
Cancelled = 'cancelled',
AutoCancelled = 'auto_cancelled',
PostOnlyRejected = 'post_only_rejected',
FokUnfilled = 'fok_unfilled',
IocNoFill = 'ioc_no_fill',
IocExpired = 'ioc_expired',
StpCancelled = 'stp_cancelled',
ZeroQuantity = 'zero_quantity',
DuplicateOrder = 'duplicate_order',
OrderNotFound = 'order_not_found',
ReduceOnlyInvalid = 'reduce_only_invalid',
ReduceOnlyExpired = 'reduce_only_expired',
OrderExpired = 'order_expired',
Untriggered = 'untriggered',
Armed = 'armed',
Triggered = 'triggered',
ParentCancelled = 'parent_cancelled',
PositionClosed = 'position_closed',
PositionFlipped = 'position_flipped',
ReduceOnlyInvalidAtTrigger = 'reduce_only_invalid_at_trigger',
Expired = 'expired',
}
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsTpSlOrderFieldsSchema = z
.object({
kind: PerpsTpSlKindSchema,
scope: PerpsTpSlScopeSchema,
trp: DecimalStringSchema,
parent_oid: PerpsOrderIdSchema.optional(),
armed_qty: DecimalStringSchema.optional(),
slip_bps: z.number().int().nonnegative().optional(),
})
.transform((fields) => ({
kind: fields.kind,
scope: fields.scope,
triggerPrice: fields.trp,
parentOrderId: fields.parent_oid,
armedQuantity: fields.armed_qty,
slippageBps: fields.slip_bps,
}));
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsTpSlOrderFields = z.infer<typeof PerpsTpSlOrderFieldsSchema>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsOrderStatusSchema = z.enum(PerpsOrderStatus);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsCommandStatusSchema = z.enum(['ok', 'err']);
/**
* Known rejection identifiers returned when a Perps order cancellation fails.
*
* The service evolves this set independently of released clients, so
* cancellation parsing accepts unknown identifiers as plain strings; see
* {@link PerpsCancelOrderErrorCode}.
*
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export enum PerpsKnownCancelOrderErrorCode {
OrderUnknown = 'order_unknown',
OrderNotInOrderbook = 'order_not_in_orderbook',
OrderInFlight = 'order_in_flight',
OrderNotPendingEngine = 'order_not_pending_engine',
OrderNotFound = 'order_not_found',
}
/**
* A Perps order cancellation rejection identifier. Known identifiers are
* enumerated in {@link PerpsKnownCancelOrderErrorCode}; newly introduced
* identifiers flow through as plain strings so they can be handled before a
* client release that enumerates them.
*
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsCancelOrderErrorCode =
| PerpsKnownCancelOrderErrorCode
| (string & {});
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsCancelOrderErrorCodeSchema = z
.string()
.min(1)
.transform((value): PerpsCancelOrderErrorCode => value);
const PerpsAckErrorSchema = z
.string()
.min(1)
.optional()
.transform(perpsAckError);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsCommandAckSchema = z.discriminatedUnion('status', [
z.object({ status: z.literal('ok') }),
z.object({
status: z.literal('err'),
error: PerpsAckErrorSchema,
}),
]);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsCommandAck = z.infer<typeof PerpsCommandAckSchema>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsPostOrderAckSchema = z
.object({
status: PerpsCommandStatusSchema,
oid: PerpsOrderIdSchema.optional(),
coid: PerpsClientOrderIdSchema.optional(),
error: z.string().optional(),
})
.transform((ack, ctx) => {
if (ack.status === 'err') {
return {
status: 'err' as const,
error: perpsAckError(ack.error),
clientOrderId: ack.coid,
};
}
if (ack.oid === undefined) {
ctx.addIssue({
code: 'custom',
message: 'Expected Perps place order acknowledgement order id.',
});
return z.NEVER;
}
return {
status: 'ok' as const,
orderId: ack.oid,
clientOrderId: ack.coid,
};
});
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsPostOrderAck = z.infer<typeof PerpsPostOrderAckSchema>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsCancelOrderResultSchema = z
.discriminatedUnion('status', [
z.object({
status: z.literal('ok'),
oid: PerpsOrderIdSchema.optional(),
coid: PerpsClientOrderIdSchema.optional(),
}),
z.object({
status: z.literal('err'),
error: PerpsCancelOrderErrorCodeSchema,
oid: PerpsOrderIdSchema.optional(),
coid: PerpsClientOrderIdSchema.optional(),
}),
])
.transform((ack) => {
if (ack.status === 'err') {
return {
status: 'err' as const,
error: ack.error,
orderId: ack.oid,
clientOrderId: ack.coid,
};
}
return {
status: 'ok' as const,
orderId: ack.oid,
clientOrderId: ack.coid,
};
});
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsCancelOrderResult = z.infer<
typeof PerpsCancelOrderResultSchema
>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsCancelAllOrdersResponseSchema = z.object({
status: z.literal('ok'),
});
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsCancelAllOrdersResponse = z.infer<
typeof PerpsCancelAllOrdersResponseSchema
>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsUpdateLeverageResultSchema = z
.object({
status: z.literal('ok'),
instrument_id: PerpsInstrumentIdSchema,
leverage: z.number().int().positive(),
cross: z.boolean(),
})
.transform((result) => ({
status: result.status,
instrumentId: result.instrument_id,
leverage: result.leverage,
crossMargin: result.cross,
}));
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsUpdateLeverageResult = z.infer<
typeof PerpsUpdateLeverageResultSchema
>;
function perpsAckError(error: string | undefined): string {
return error ?? 'Perps command was rejected.';
}
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsOrderSchema = z
.object({
order_id: PerpsOrderIdSchema,
instrument_id: PerpsInstrumentIdSchema,
buy: z.boolean(),
price: DecimalStringSchema,
quantity: DecimalStringSchema,
tif: PerpsTimeInForceSchema,
post_only: z.boolean(),
ro: z.boolean(),
status: PerpsOrderStatusSchema,
resting_quantity: DecimalStringSchema,
filled_quantity: DecimalStringSchema,
created_timestamp: EpochMillisecondsSchema,
updated_timestamp: EpochMillisecondsSchema,
client_order_id: z.string().optional(),
tpsl: PerpsTpSlOrderFieldsSchema.nullish(),
})
.transform((order) => ({
id: order.order_id,
instrumentId: order.instrument_id,
side: order.buy ? OrderSide.BUY : OrderSide.SELL,
price: order.price,
quantity: order.quantity,
timeInForce: order.tif,
postOnly: order.post_only,
reduceOnly: order.ro,
status: order.status,
restingQuantity: order.resting_quantity,
filledQuantity: order.filled_quantity,
createdTimestamp: order.created_timestamp,
updatedTimestamp: order.updated_timestamp,
clientOrderId: order.client_order_id,
tpSl: order.tpsl ?? undefined,
}));
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsOrder = z.infer<typeof PerpsOrderSchema>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const FetchPerpsOrdersResponseSchema = z.array(PerpsOrderSchema);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const FetchPerpsOpenOrdersResponseSchema = z.array(PerpsOrderSchema);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsOrderUpdateSchema = z
.object({
oid: PerpsOrderIdSchema,
iid: PerpsInstrumentIdSchema,
buy: z.boolean(),
p: DecimalStringSchema,
qty: DecimalStringSchema,
tif: PerpsTimeInForceSchema,
po: z.boolean(),
ro: z.boolean(),
status: PerpsOrderStatusSchema,
rest: DecimalStringSchema,
fill: DecimalStringSchema,
cts: EpochMillisecondsSchema,
uts: EpochMillisecondsSchema,
coid: z.string().optional(),
tpsl: PerpsTpSlOrderFieldsSchema.nullish(),
})
.transform((order) => ({
id: order.oid,
instrumentId: order.iid,
side: order.buy ? OrderSide.BUY : OrderSide.SELL,
price: order.p,
quantity: order.qty,
timeInForce: order.tif,
postOnly: order.po,
reduceOnly: order.ro,
status: order.status,
restingQuantity: order.rest,
filledQuantity: order.fill,
createdTimestamp: order.cts,
updatedTimestamp: order.uts,
clientOrderId: order.coid,
tpSl: order.tpsl ?? undefined,
}));
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsAccountFillSchema = z
.object({
trade_id: PerpsTradeIdSchema,
order_id: PerpsOrderIdSchema,
instrument_id: PerpsInstrumentIdSchema,
side: PerpsSideSchema,
price: DecimalStringSchema,
quantity: DecimalStringSchema,
taker: z.boolean(),
fee: DecimalStringSchema,
fee_asset: PerpsAssetSchema,
previous_size: DecimalStringSchema,
previous_entry_price: DecimalStringSchema,
pnl: DecimalStringSchema,
liquidation: z.boolean(),
timestamp: EpochMillisecondsSchema,
hash: PerpsTxHashSchema,
})
.transform((fill) => ({
tradeId: fill.trade_id,
orderId: fill.order_id,
instrumentId: fill.instrument_id,
side: fill.side,
price: fill.price,
quantity: fill.quantity,
taker: fill.taker,
fee: fill.fee,
feeAsset: fill.fee_asset,
previousSize: fill.previous_size,
previousEntryPrice: fill.previous_entry_price,
pnl: fill.pnl,
liquidation: fill.liquidation,
timestamp: fill.timestamp,
hash: fill.hash,
}));
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export type PerpsAccountFill = z.infer<typeof PerpsAccountFillSchema>;
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const ListPerpsFillsResponseSchema = PerpsDataResponseSchema(
PerpsAccountFillSchema,
);
/**
* @experimental This API may change in a breaking way in any release, including patch releases.
*/
export const PerpsAccountFillUpdateSchema = z
.object({
tid: PerpsTradeIdSchema,
oid: PerpsOrderIdSchema,
iid: PerpsInstrumentIdSchema,
side: PerpsSideSchema,
p: DecimalStringSchema,
qty: DecimalStringSchema,
taker: z.boolean(),
fee: DecimalStringSchema,
fea: PerpsAssetSchema,
psz: DecimalStringSchema,
pep: DecimalStringSchema,
pnl: DecimalStringSchema,
liq: z.boolean(),
ts: EpochMillisecondsSchema,
coid: z.string().optional(),
})
.transform((fill) => ({
tradeId: fill.tid,
orderId: fill.oid,
instrumentId: fill.iid,
side: fill.side,
price: fill.p,
quantity: fill.qty,
taker: fill.taker,
fee: fill.fee,
feeAsset: fill.fea,
previousSize: fill.psz,
previousEntryPrice: fill.pep,
pnl: fill.pnl,
liquidation: fill.liq,
timestamp: fill.ts,
clientOrderId: fill.coid,
}));