@0xmonaco/core 0.7.7 → 0.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/applications/api.d.ts +43 -0
- package/dist/api/applications/api.js +54 -0
- package/dist/api/applications/index.d.ts +4 -0
- package/dist/api/applications/index.js +4 -0
- package/dist/api/auth/api.d.ts +206 -0
- package/dist/api/auth/api.js +305 -0
- package/dist/api/auth/index.d.ts +4 -0
- package/dist/api/auth/index.js +4 -0
- package/dist/api/base.d.ts +123 -0
- package/dist/api/base.js +286 -0
- package/dist/api/fees/api.d.ts +72 -0
- package/dist/api/fees/api.js +90 -0
- package/dist/api/fees/index.d.ts +6 -0
- package/dist/api/fees/index.js +6 -0
- package/dist/api/index.d.ts +18 -0
- package/dist/api/index.js +18 -0
- package/dist/api/margin-accounts/api.d.ts +12 -0
- package/dist/api/margin-accounts/api.js +69 -0
- package/dist/api/margin-accounts/index.d.ts +1 -0
- package/dist/api/margin-accounts/index.js +1 -0
- package/dist/api/market/api.d.ts +20 -0
- package/dist/api/market/api.js +97 -0
- package/dist/api/market/index.d.ts +1 -0
- package/dist/api/market/index.js +1 -0
- package/dist/api/orderbook/api.d.ts +15 -0
- package/dist/api/orderbook/api.js +37 -0
- package/dist/api/orderbook/index.d.ts +1 -0
- package/dist/api/orderbook/index.js +1 -0
- package/dist/api/perp/index.d.ts +1 -0
- package/dist/api/perp/index.js +1 -0
- package/dist/api/perp/routes.d.ts +133 -0
- package/dist/api/perp/routes.js +85 -0
- package/dist/api/positions/api.d.ts +12 -0
- package/dist/api/positions/api.js +86 -0
- package/dist/api/positions/index.d.ts +1 -0
- package/dist/api/positions/index.js +1 -0
- package/dist/api/profile/api.d.ts +191 -0
- package/dist/api/profile/api.js +259 -0
- package/dist/api/profile/index.d.ts +6 -0
- package/dist/api/profile/index.js +6 -0
- package/dist/api/trades/api.d.ts +44 -0
- package/dist/api/trades/api.js +42 -0
- package/dist/api/trades/index.d.ts +1 -0
- package/dist/api/trades/index.js +1 -0
- package/dist/api/trading/api.d.ts +297 -0
- package/dist/api/trading/api.js +481 -0
- package/dist/api/trading/index.d.ts +4 -0
- package/dist/api/trading/index.js +4 -0
- package/dist/api/vault/api.d.ts +261 -0
- package/dist/api/vault/api.js +506 -0
- package/dist/api/vault/index.d.ts +4 -0
- package/dist/api/vault/index.js +4 -0
- package/dist/api/websocket/index.d.ts +3 -0
- package/dist/api/websocket/index.js +3 -0
- package/dist/api/websocket/types.d.ts +41 -0
- package/dist/api/websocket/types.js +0 -0
- package/dist/api/websocket/utils.d.ts +8 -0
- package/dist/api/websocket/utils.js +22 -0
- package/dist/api/websocket/websocket.d.ts +5 -0
- package/dist/api/websocket/websocket.js +556 -0
- package/dist/errors/errors.d.ts +381 -0
- package/dist/errors/errors.js +815 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/networks/index.d.ts +1 -0
- package/dist/networks/index.js +1 -0
- package/dist/networks/networks.d.ts +21 -0
- package/dist/networks/networks.js +46 -0
- package/dist/sdk.d.ts +134 -0
- package/dist/sdk.js +294 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/magnitude.d.ts +26 -0
- package/dist/utils/magnitude.js +31 -0
- package/package.json +3 -3
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trading API Implementation
|
|
3
|
+
*
|
|
4
|
+
* Handles trading operations including order placement and management.
|
|
5
|
+
* All operations use JWT authentication and go through the API Gateway.
|
|
6
|
+
*
|
|
7
|
+
* This class provides a complete interface for trading operations on the Monaco protocol,
|
|
8
|
+
* including various order types, order management, and position operations.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const tradingAPI = new TradingAPIImpl(apiUrl);
|
|
13
|
+
* tradingAPI.setAccessToken(jwtToken);
|
|
14
|
+
*
|
|
15
|
+
* // Place a limit order
|
|
16
|
+
* const order = await tradingAPI.placeLimitOrder(
|
|
17
|
+
* "123e4567-e89b-12d3-a456-426614174000", // trading pair UUID
|
|
18
|
+
* "BUY",
|
|
19
|
+
* "1.5",
|
|
20
|
+
* "2000.50"
|
|
21
|
+
* );
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
import { BatchCreateOrdersSchema, BatchReplaceOrdersSchema, CancelConditionalOrderSchema, CancelOrderSchema, CreateConditionalOrderSchema, GetPaginatedOrdersSchema, ListConditionalOrdersSchema, PlaceLimitOrderSchema, PlaceMarketOrderSchema, ReplaceOrderSchema, validate, } from "@0xmonaco/types";
|
|
25
|
+
import { BaseAPI } from "../base";
|
|
26
|
+
import { perpRoutes } from "../perp";
|
|
27
|
+
export class TradingAPIImpl extends BaseAPI {
|
|
28
|
+
/**
|
|
29
|
+
* Places a limit order on the order book.
|
|
30
|
+
*
|
|
31
|
+
* Limit orders are executed only when the market price reaches or improves upon
|
|
32
|
+
* the specified price. They provide price protection but may not execute immediately.
|
|
33
|
+
*
|
|
34
|
+
* @param tradingPairId - The trading pair UUID
|
|
35
|
+
* @param side - The order side ("BUY" or "SELL")
|
|
36
|
+
* @param quantity - The order quantity as string
|
|
37
|
+
* @param price - The limit price as string
|
|
38
|
+
* @param options - Optional parameters for the limit order
|
|
39
|
+
* @param options.tradingMode - Trading mode (e.g., "SPOT")
|
|
40
|
+
* @param options.useMasterBalance - Whether to use master balance for sub-accounts
|
|
41
|
+
* @param options.expirationDate - Optional expiration date for GTC orders (ISO 8601 format)
|
|
42
|
+
* @param options.timeInForce - Time in force ("GTC", "IOC", or "FOK")
|
|
43
|
+
* @returns Promise resolving to CreateOrderResponse with order details
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // Regular limit order
|
|
48
|
+
* const order = await tradingAPI.placeLimitOrder(
|
|
49
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
50
|
+
* "BUY",
|
|
51
|
+
* "0.5",
|
|
52
|
+
* "35000.00",
|
|
53
|
+
* { tradingMode: "SPOT" }
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* // GTC order with custom expiration
|
|
57
|
+
* const gtcOrder = await tradingAPI.placeLimitOrder(
|
|
58
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
59
|
+
* "BUY",
|
|
60
|
+
* "0.5",
|
|
61
|
+
* "35000.00",
|
|
62
|
+
* {
|
|
63
|
+
* tradingMode: "SPOT",
|
|
64
|
+
* expirationDate: "2024-12-31T23:59:59Z" // GTC until this date
|
|
65
|
+
* }
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // IOC order (execute immediately or cancel)
|
|
69
|
+
* const iocOrder = await tradingAPI.placeLimitOrder(
|
|
70
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
71
|
+
* "BUY",
|
|
72
|
+
* "0.5",
|
|
73
|
+
* "35000.00",
|
|
74
|
+
* { timeInForce: "IOC" }
|
|
75
|
+
* );
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
async placeLimitOrder(tradingPairId, side, quantity, price, options) {
|
|
79
|
+
// Validate inputs before making API call
|
|
80
|
+
validate(PlaceLimitOrderSchema, {
|
|
81
|
+
tradingPairId,
|
|
82
|
+
side,
|
|
83
|
+
quantity,
|
|
84
|
+
price,
|
|
85
|
+
options,
|
|
86
|
+
});
|
|
87
|
+
const requestBody = {
|
|
88
|
+
trading_pair_id: tradingPairId,
|
|
89
|
+
order_type: "LIMIT",
|
|
90
|
+
side,
|
|
91
|
+
price,
|
|
92
|
+
quantity,
|
|
93
|
+
trading_mode: options?.tradingMode || "SPOT",
|
|
94
|
+
use_master_balance: options?.useMasterBalance,
|
|
95
|
+
expiration_date: options?.expirationDate,
|
|
96
|
+
time_in_force: options?.timeInForce,
|
|
97
|
+
margin_account_id: options?.marginAccountId,
|
|
98
|
+
position_side: options?.positionSide,
|
|
99
|
+
leverage: options?.leverage,
|
|
100
|
+
reduce_only: options?.reduceOnly,
|
|
101
|
+
};
|
|
102
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.create(), {
|
|
103
|
+
method: "POST",
|
|
104
|
+
body: JSON.stringify(requestBody),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Places a market order for immediate execution.
|
|
109
|
+
*
|
|
110
|
+
* Market orders execute immediately at the current market price. They provide
|
|
111
|
+
* immediate execution but may experience slippage.
|
|
112
|
+
*
|
|
113
|
+
* @param tradingPairId - The trading pair UUID
|
|
114
|
+
* @param side - The order side ("BUY" or "SELL")
|
|
115
|
+
* @param quantity - The order quantity as string
|
|
116
|
+
* @param options - Optional parameters for the market order
|
|
117
|
+
* @param options.tradingMode - Trading mode (e.g., "SPOT")
|
|
118
|
+
* @param options.slippageTolerance - Slippage tolerance as decimal (e.g., 0.01 for 1%, 0 for best price only, undefined for unlimited slippage)
|
|
119
|
+
* @returns Promise resolving to CreateOrderResponse with order details
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* // Market order with 1% slippage tolerance
|
|
124
|
+
* const order1 = await tradingAPI.placeMarketOrder(
|
|
125
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
126
|
+
* "SELL",
|
|
127
|
+
* "0.5",
|
|
128
|
+
* { tradingMode: "SPOT", slippageTolerance: 0.01 } // 1% slippage
|
|
129
|
+
* );
|
|
130
|
+
*
|
|
131
|
+
* // Market order with zero slippage (only execute at best price)
|
|
132
|
+
* const order2 = await tradingAPI.placeMarketOrder(
|
|
133
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
134
|
+
* "BUY",
|
|
135
|
+
* "1.0",
|
|
136
|
+
* { slippageTolerance: 0 } // Only execute at best ask price
|
|
137
|
+
* );
|
|
138
|
+
*
|
|
139
|
+
* // Market order with unlimited slippage
|
|
140
|
+
* const order3 = await tradingAPI.placeMarketOrder(
|
|
141
|
+
* "123e4567-e89b-12d3-a456-426614174000",
|
|
142
|
+
* "BUY",
|
|
143
|
+
* "1.0"
|
|
144
|
+
* // No slippageTolerance specified = unlimited slippage
|
|
145
|
+
* );
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
async placeMarketOrder(tradingPairId, side, quantity, options) {
|
|
149
|
+
// Validate inputs before making API call
|
|
150
|
+
validate(PlaceMarketOrderSchema, {
|
|
151
|
+
tradingPairId,
|
|
152
|
+
side,
|
|
153
|
+
quantity,
|
|
154
|
+
options,
|
|
155
|
+
});
|
|
156
|
+
const requestBody = {
|
|
157
|
+
trading_pair_id: tradingPairId,
|
|
158
|
+
order_type: "MARKET",
|
|
159
|
+
side,
|
|
160
|
+
slippage_tolerance_bps: options?.slippageTolerance !== undefined ? Math.round(options.slippageTolerance * 10000) : undefined,
|
|
161
|
+
price: null, // Market orders don't need price
|
|
162
|
+
quantity,
|
|
163
|
+
trading_mode: options?.tradingMode || "SPOT",
|
|
164
|
+
margin_account_id: options?.marginAccountId,
|
|
165
|
+
position_side: options?.positionSide,
|
|
166
|
+
leverage: options?.leverage,
|
|
167
|
+
reduce_only: options?.reduceOnly,
|
|
168
|
+
};
|
|
169
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.create(), {
|
|
170
|
+
method: "POST",
|
|
171
|
+
body: JSON.stringify(requestBody),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Cancels an existing order.
|
|
176
|
+
*
|
|
177
|
+
* Cancels an open order by its ID. The order must be in an open state to be canceled.
|
|
178
|
+
* Canceled orders cannot be recovered.
|
|
179
|
+
*
|
|
180
|
+
* @param orderId - The ID of the order to cancel
|
|
181
|
+
* @returns Promise resolving to CancelOrderResponse with cancellation details
|
|
182
|
+
* @throws {APIError} When API communication fails
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const result = await tradingAPI.cancelOrder("order_123");
|
|
187
|
+
* console.log(`Order canceled: ${result.status}`);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
async cancelOrder(orderId) {
|
|
191
|
+
// Validate inputs before making API call
|
|
192
|
+
validate(CancelOrderSchema, { orderId });
|
|
193
|
+
const requestBody = {
|
|
194
|
+
order_id: orderId,
|
|
195
|
+
};
|
|
196
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.cancel(), {
|
|
197
|
+
method: "POST",
|
|
198
|
+
body: JSON.stringify(requestBody),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
async createConditionalOrder(params) {
|
|
202
|
+
validate(CreateConditionalOrderSchema, params);
|
|
203
|
+
const requestBody = {
|
|
204
|
+
trading_pair_id: params.tradingPairId,
|
|
205
|
+
margin_account_id: params.marginAccountId,
|
|
206
|
+
condition_type: params.conditionType,
|
|
207
|
+
trigger_price: params.triggerPrice,
|
|
208
|
+
trigger_source: params.triggerSource ?? "MARK_PRICE",
|
|
209
|
+
side: params.side,
|
|
210
|
+
position_side: params.positionSide,
|
|
211
|
+
order_type: params.orderType,
|
|
212
|
+
limit_price: params.limitPrice,
|
|
213
|
+
quantity: params.quantity,
|
|
214
|
+
reduce_only: params.reduceOnly ?? true,
|
|
215
|
+
time_in_force: params.timeInForce,
|
|
216
|
+
slippage_tolerance_bps: params.slippageToleranceBps,
|
|
217
|
+
expires_at: params.expiresAt,
|
|
218
|
+
};
|
|
219
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.createConditional(), {
|
|
220
|
+
method: "POST",
|
|
221
|
+
body: JSON.stringify(requestBody),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
async cancelConditionalOrder(conditionalOrderId) {
|
|
225
|
+
validate(CancelConditionalOrderSchema, { conditionalOrderId });
|
|
226
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.cancelConditional(conditionalOrderId), {
|
|
227
|
+
method: "DELETE",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
async listConditionalOrders(params) {
|
|
231
|
+
if (params) {
|
|
232
|
+
validate(ListConditionalOrdersSchema, params);
|
|
233
|
+
}
|
|
234
|
+
const { page = 1, page_size = 20, margin_account_id, trading_pair_id, state } = params || {};
|
|
235
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.listConditional({
|
|
236
|
+
page,
|
|
237
|
+
page_size: Math.min(Math.max(page_size, 1), 100),
|
|
238
|
+
margin_account_id,
|
|
239
|
+
trading_pair_id,
|
|
240
|
+
state,
|
|
241
|
+
}), { method: "GET" });
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Batch cancels specific orders by their IDs.
|
|
245
|
+
*
|
|
246
|
+
* @param orderIds - Array of order IDs to cancel
|
|
247
|
+
* @returns Promise resolving to BatchCancelOrdersResponse with cancellation details
|
|
248
|
+
* @throws {APIError} When API communication fails
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* const result = await tradingAPI.batchCancel(["order_123", "order_456"]);
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
async batchCancel(orderIds) {
|
|
256
|
+
if (!orderIds || orderIds.length === 0) {
|
|
257
|
+
throw new Error("orderIds is required and must not be empty");
|
|
258
|
+
}
|
|
259
|
+
return this.makeAuthenticatedRequest(perpRoutes.orders.batchCancel(), {
|
|
260
|
+
method: "POST",
|
|
261
|
+
body: JSON.stringify({ order_ids: orderIds }),
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Cancels all active orders, optionally filtered by trading pair.
|
|
266
|
+
*
|
|
267
|
+
* @param tradingPairId - Optional trading pair ID to filter cancellation
|
|
268
|
+
* @returns Promise resolving to BatchCancelOrdersResponse with cancellation details
|
|
269
|
+
* @throws {APIError} When API communication fails
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* // Cancel all active orders globally
|
|
274
|
+
* await tradingAPI.batchCancelAll();
|
|
275
|
+
*
|
|
276
|
+
* // Cancel all orders for a specific trading pair
|
|
277
|
+
* await tradingAPI.batchCancelAll("123e4567-e89b-12d3-a456-426614174000");
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
async batchCancelAll(tradingPairId) {
|
|
281
|
+
const endpoint = tradingPairId ? perpRoutes.orders.batchCancelAllByPair(tradingPairId) : perpRoutes.orders.batchCancelAll();
|
|
282
|
+
return this.makeAuthenticatedRequest(endpoint, {
|
|
283
|
+
method: "POST",
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
async replaceOrder(orderId, newOrder) {
|
|
287
|
+
// Validate inputs before making API call
|
|
288
|
+
validate(ReplaceOrderSchema, { orderId, newOrder });
|
|
289
|
+
const requestBody = {
|
|
290
|
+
use_master_balance: newOrder.useMasterBalance ?? false,
|
|
291
|
+
};
|
|
292
|
+
if (newOrder.price !== undefined) {
|
|
293
|
+
requestBody.price = newOrder.price;
|
|
294
|
+
}
|
|
295
|
+
if (newOrder.quantity !== undefined) {
|
|
296
|
+
requestBody.quantity = newOrder.quantity;
|
|
297
|
+
}
|
|
298
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.replace(orderId), {
|
|
299
|
+
method: "PUT",
|
|
300
|
+
body: JSON.stringify(requestBody),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Batch creates multiple orders in a single request.
|
|
305
|
+
*
|
|
306
|
+
* Each order is validated and processed through the matching engine.
|
|
307
|
+
* Failed orders do not prevent other orders from being created.
|
|
308
|
+
*
|
|
309
|
+
* @param orders - Array of order parameters to create
|
|
310
|
+
* @returns Promise resolving to BatchCreateOrdersResponse with individual results
|
|
311
|
+
* @throws {APIError} When API communication fails
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```typescript
|
|
315
|
+
* const result = await tradingAPI.batchCreate([
|
|
316
|
+
* {
|
|
317
|
+
* tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
|
|
318
|
+
* orderType: "LIMIT",
|
|
319
|
+
* side: "BUY",
|
|
320
|
+
* price: "35000.00",
|
|
321
|
+
* quantity: "0.5",
|
|
322
|
+
* },
|
|
323
|
+
* {
|
|
324
|
+
* tradingPairId: "123e4567-e89b-12d3-a456-426614174000",
|
|
325
|
+
* orderType: "LIMIT",
|
|
326
|
+
* side: "SELL",
|
|
327
|
+
* price: "36000.00",
|
|
328
|
+
* quantity: "0.3",
|
|
329
|
+
* },
|
|
330
|
+
* ]);
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
async batchCreate(orders) {
|
|
334
|
+
validate(BatchCreateOrdersSchema, { orders });
|
|
335
|
+
const requestBody = {
|
|
336
|
+
orders: orders.map((order) => ({
|
|
337
|
+
trading_pair_id: order.tradingPairId,
|
|
338
|
+
order_type: order.orderType,
|
|
339
|
+
side: order.side,
|
|
340
|
+
price: order.price,
|
|
341
|
+
quantity: order.quantity,
|
|
342
|
+
trading_mode: order.tradingMode || "SPOT",
|
|
343
|
+
slippage_tolerance_bps: order.slippageTolerance !== undefined ? Math.round(order.slippageTolerance * 10000) : undefined,
|
|
344
|
+
use_master_balance: order.useMasterBalance,
|
|
345
|
+
expiration_date: order.expirationDate,
|
|
346
|
+
time_in_force: order.timeInForce,
|
|
347
|
+
margin_account_id: order.marginAccountId,
|
|
348
|
+
position_side: order.positionSide,
|
|
349
|
+
leverage: order.leverage,
|
|
350
|
+
reduce_only: order.reduceOnly,
|
|
351
|
+
})),
|
|
352
|
+
};
|
|
353
|
+
return this.makeAuthenticatedRequest(perpRoutes.orders.batchCreate(), {
|
|
354
|
+
method: "POST",
|
|
355
|
+
body: JSON.stringify(requestBody),
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Batch replaces multiple orders in a single request.
|
|
360
|
+
*
|
|
361
|
+
* Each order is canceled and re-created with new parameters.
|
|
362
|
+
* Failed replacements do not prevent other orders from being replaced.
|
|
363
|
+
*
|
|
364
|
+
* @param orders - Array of order replacement parameters
|
|
365
|
+
* @returns Promise resolving to BatchReplaceOrdersResponse with individual results
|
|
366
|
+
* @throws {APIError} When API communication fails
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```typescript
|
|
370
|
+
* const result = await tradingAPI.batchReplace([
|
|
371
|
+
* {
|
|
372
|
+
* orderId: "f47ac10b-58cc-4372-a567-0e02b2c3d479",
|
|
373
|
+
* price: "35500.00",
|
|
374
|
+
* quantity: "0.7",
|
|
375
|
+
* },
|
|
376
|
+
* {
|
|
377
|
+
* orderId: "7c9e6679-7425-40de-944b-e07fc1f90ae7",
|
|
378
|
+
* price: "36500.00",
|
|
379
|
+
* },
|
|
380
|
+
* ]);
|
|
381
|
+
* ```
|
|
382
|
+
*/
|
|
383
|
+
async batchReplace(orders) {
|
|
384
|
+
validate(BatchReplaceOrdersSchema, { orders });
|
|
385
|
+
const requestBody = {
|
|
386
|
+
orders: orders.map((order) => ({
|
|
387
|
+
order_id: order.orderId,
|
|
388
|
+
price: order.price,
|
|
389
|
+
quantity: order.quantity,
|
|
390
|
+
use_master_balance: order.useMasterBalance,
|
|
391
|
+
})),
|
|
392
|
+
};
|
|
393
|
+
return this.makeAuthenticatedRequest(perpRoutes.orders.batchReplace(), {
|
|
394
|
+
method: "POST",
|
|
395
|
+
body: JSON.stringify(requestBody),
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Gets paginated orders based on query parameters with automatic pagination.
|
|
400
|
+
*
|
|
401
|
+
* Retrieves orders with optional filtering by status, trading pair, and pagination.
|
|
402
|
+
* Pagination parameters are always included with sensible defaults for consistent API behavior.
|
|
403
|
+
*
|
|
404
|
+
* @param params - Query parameters for filtering orders
|
|
405
|
+
* @param params.status - Filter by order status (e.g., "SUBMITTED", "FILLED") (optional)
|
|
406
|
+
* @param params.trading_pair_id - Filter by trading pair UUID (optional)
|
|
407
|
+
* @param params.page - Page number for pagination (defaults to 1, must be > 0)
|
|
408
|
+
* @param params.page_size - Number of orders per page (defaults to 10, max 100, must be > 0)
|
|
409
|
+
* @returns Promise resolving to GetPaginatedOrdersResponse with orders and pagination info
|
|
410
|
+
* @throws {APIError} When API communication fails
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```typescript
|
|
414
|
+
* // Get submitted orders for a specific trading pair with custom pagination
|
|
415
|
+
* const orders = await tradingAPI.getPaginatedOrders({
|
|
416
|
+
* status: "SUBMITTED",
|
|
417
|
+
* trading_pair_id: "456e7890-e12b-12d3-a456-426614174000",
|
|
418
|
+
* page: 1,
|
|
419
|
+
* page_size: 20
|
|
420
|
+
* });
|
|
421
|
+
* console.log(`Found ${orders.total} orders, showing page ${orders.page}`);
|
|
422
|
+
*
|
|
423
|
+
* // Get all orders with default pagination (page=1, page_size=10)
|
|
424
|
+
* const allOrders = await tradingAPI.getPaginatedOrders();
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
async getPaginatedOrders(params) {
|
|
428
|
+
// Validate inputs before making API call
|
|
429
|
+
if (params) {
|
|
430
|
+
validate(GetPaginatedOrdersSchema, params);
|
|
431
|
+
}
|
|
432
|
+
// Set pagination defaults with destructuring and validation
|
|
433
|
+
const { page = 1, page_size = 10, status, trading_pair_id, trading_mode, margin_account_id } = params || {};
|
|
434
|
+
const validPage = page > 0 ? page : 1;
|
|
435
|
+
const validPageSize = page_size > 0 ? page_size : 10;
|
|
436
|
+
const pageSize = Math.min(validPageSize, 100);
|
|
437
|
+
const searchParams = new URLSearchParams();
|
|
438
|
+
// Always include pagination parameters for consistent API behavior
|
|
439
|
+
searchParams.append("page", validPage.toString());
|
|
440
|
+
searchParams.append("page_size", pageSize.toString());
|
|
441
|
+
if (status) {
|
|
442
|
+
searchParams.append("status", status);
|
|
443
|
+
}
|
|
444
|
+
if (trading_pair_id) {
|
|
445
|
+
searchParams.append("trading_pair_id", trading_pair_id);
|
|
446
|
+
}
|
|
447
|
+
if (trading_mode) {
|
|
448
|
+
searchParams.append("trading_mode", trading_mode);
|
|
449
|
+
}
|
|
450
|
+
if (margin_account_id) {
|
|
451
|
+
searchParams.append("margin_account_id", margin_account_id);
|
|
452
|
+
}
|
|
453
|
+
const endpoint = `/api/v1/orders?${searchParams.toString()}`;
|
|
454
|
+
return await this.makeAuthenticatedRequest(endpoint, {
|
|
455
|
+
method: "GET",
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Gets a single order by its ID.
|
|
460
|
+
*
|
|
461
|
+
* Retrieves detailed information about a specific order using its unique identifier.
|
|
462
|
+
* This endpoint provides complete order details including status, quantities, and timestamps.
|
|
463
|
+
*
|
|
464
|
+
* @param orderId - The unique identifier of the order to retrieve
|
|
465
|
+
* @returns Promise resolving to GetOrderResponse with order details
|
|
466
|
+
* @throws {APIError} When API communication fails or order is not found
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```typescript
|
|
470
|
+
* // Get order details by ID
|
|
471
|
+
* const orderDetails = await tradingAPI.getOrder("order_123");
|
|
472
|
+
* console.log(`Order status: ${orderDetails.order.status}`);
|
|
473
|
+
* console.log(`Remaining quantity: ${orderDetails.order.remaining_quantity}`);
|
|
474
|
+
* ```
|
|
475
|
+
*/
|
|
476
|
+
async getOrder(orderId) {
|
|
477
|
+
return await this.makeAuthenticatedRequest(perpRoutes.orders.get(orderId), {
|
|
478
|
+
method: "GET",
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|