@reyaxyz/sdk 0.151.3 → 0.152.0
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/README.md +1 -1
- package/dist/services/orders/index.js +1 -1
- package/dist/services/orders/index.js.map +1 -1
- package/dist/services/orders/nonce.js +37 -0
- package/dist/services/orders/nonce.js.map +1 -0
- package/dist/services/orders/orderV2.js +416 -89
- package/dist/services/orders/orderV2.js.map +1 -1
- package/dist/services/orders/triggerSettlementHeadroom.js +51 -0
- package/dist/services/orders/triggerSettlementHeadroom.js.map +1 -0
- package/dist/services/orders/types.js.map +1 -1
- package/dist/types/services/orders/index.d.ts +1 -1
- package/dist/types/services/orders/index.d.ts.map +1 -1
- package/dist/types/services/orders/nonce.d.ts +2 -0
- package/dist/types/services/orders/nonce.d.ts.map +1 -0
- package/dist/types/services/orders/orderV2.d.ts +194 -14
- package/dist/types/services/orders/orderV2.d.ts.map +1 -1
- package/dist/types/services/orders/triggerSettlementHeadroom.d.ts +33 -0
- package/dist/types/services/orders/triggerSettlementHeadroom.d.ts.map +1 -0
- package/dist/types/services/orders/types.d.ts +1 -11
- package/dist/types/services/orders/types.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/services/orders/index.ts +1 -1
- package/src/services/orders/nonce.ts +34 -0
- package/src/services/orders/orderV2.ts +731 -122
- package/src/services/orders/triggerSettlementHeadroom.ts +56 -0
- package/src/services/orders/types.ts +0 -15
- package/dist/services/orders/order.js +0 -231
- package/dist/services/orders/order.js.map +0 -1
- package/dist/types/services/orders/order.d.ts +0 -4
- package/dist/types/services/orders/order.d.ts.map +0 -1
- package/src/services/orders/order.ts +0 -214
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
14
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
15
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -36,156 +47,472 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
47
|
}
|
|
37
48
|
};
|
|
38
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.
|
|
50
|
+
exports.modifyMEOrder = exports.cancelAllAfter = exports.closePosition = exports.createIOCOrder = exports.massCancelMEOrders = exports.cancelMEOrder = exports.createOrder = exports.toOnChainTimeInForce = exports.toOnChainOrderType = void 0;
|
|
40
51
|
var ethers_1 = require("ethers");
|
|
41
52
|
var api_v2_sdk_1 = require("@reyaxyz/api-v2-sdk");
|
|
42
|
-
Object.defineProperty(exports, "TimeInForce", { enumerable: true, get: function () { return api_v2_sdk_1.TimeInForce; } });
|
|
43
53
|
var common_1 = require("@reyaxyz/common");
|
|
44
54
|
var network_1 = require("../../utils/network");
|
|
45
55
|
var config_1 = require("../../config");
|
|
46
|
-
var
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
56
|
+
var nonce_1 = require("./nonce");
|
|
57
|
+
var triggerSettlementHeadroom_1 = require("./triggerSettlementHeadroom");
|
|
58
|
+
// A provided, non-zero order id. '' / whitespace / '0' / all-zeros are the
|
|
59
|
+
// engine's "not provided" sentinels; normalize them consistently across cancel
|
|
60
|
+
// + modify so a caller passing orderId:'0' (or '') can't sign/target the
|
|
61
|
+
// sentinel or slip past the "requires an id" guard.
|
|
62
|
+
var isProvidedNonZeroId = function (value) {
|
|
63
|
+
var trimmed = value === null || value === void 0 ? void 0 : value.trim();
|
|
64
|
+
return !!trimmed && !/^0+$/.test(trimmed);
|
|
65
|
+
};
|
|
66
|
+
// Maps the REST-surface `OrderType` (string: 'LIMIT' / 'STOP_LOSS' / 'TAKE_PROFIT',
|
|
67
|
+
// from @reyaxyz/api-v2-sdk) to the on-chain numeric enum signed into
|
|
68
|
+
// `OrderDetails.orderType` (0 / 1 / 2, from @reyaxyz/common).
|
|
69
|
+
var toOnChainOrderType = function (orderType) {
|
|
70
|
+
switch (orderType) {
|
|
71
|
+
case 'LIMIT':
|
|
72
|
+
return common_1.OrderType.Limit;
|
|
73
|
+
case 'STOP_LOSS':
|
|
74
|
+
return common_1.OrderType.StopLoss;
|
|
75
|
+
case 'TAKE_PROFIT':
|
|
76
|
+
return common_1.OrderType.TakeProfit;
|
|
77
|
+
default:
|
|
78
|
+
throw new Error("Unsupported orderType: ".concat(orderType));
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.toOnChainOrderType = toOnChainOrderType;
|
|
82
|
+
// Maps the REST-surface `TimeInForce` to the numeric value signed into
|
|
83
|
+
// `OrderDetails.timeInForce`, matching the off-chain verifier's
|
|
84
|
+
// REST_TIME_IN_FORCE_TO_NUMERIC (common-backend signature-validation):
|
|
85
|
+
// GTC=0, IOC=1, GTT=2.
|
|
86
|
+
var toOnChainTimeInForce = function (timeInForce) {
|
|
87
|
+
switch (timeInForce) {
|
|
88
|
+
case 'GTC':
|
|
89
|
+
return 0;
|
|
90
|
+
case 'IOC':
|
|
91
|
+
return 1;
|
|
92
|
+
case 'GTT':
|
|
93
|
+
return 2;
|
|
94
|
+
default:
|
|
95
|
+
throw new Error("Unsupported timeInForce: ".concat(timeInForce));
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.toOnChainTimeInForce = toOnChainTimeInForce;
|
|
99
|
+
var DEFAULT_DEADLINE_SECONDS = 30;
|
|
100
|
+
var isZeroClientOrderId = function (clientOrderId) {
|
|
101
|
+
return clientOrderId !== undefined && /^0+$/.test(clientOrderId.trim());
|
|
53
102
|
};
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
103
|
+
var assertNonZeroClientOrderId = function (clientOrderId) {
|
|
104
|
+
if (isZeroClientOrderId(clientOrderId)) {
|
|
105
|
+
throw new Error('clientOrderId must be omitted rather than set to 0');
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
// Spot unified market ids are offset by 1e10 (see CreateOrderParams.marketId).
|
|
109
|
+
// Spot markets don't support reduceOnly, so reject it at the SDK boundary
|
|
110
|
+
// rather than sign a request the backend will reject.
|
|
111
|
+
var SPOT_MARKET_ID_OFFSET = 10000000000;
|
|
112
|
+
var createOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
+
var reyaChainId, config, isTrigger, onChainTimeInForce, hasExpiry, signerAddress, defaultDeadline, deadline, expiresAfter, nonce, quantity, limitPrice, triggerPrice, serializedSignature, apiConfig, orderEntryApi, response;
|
|
114
|
+
var _a, _b, _c, _d, _e, _f;
|
|
115
|
+
return __generator(this, function (_g) {
|
|
116
|
+
switch (_g.label) {
|
|
58
117
|
case 0:
|
|
118
|
+
assertNonZeroClientOrderId(params.clientOrderId);
|
|
59
119
|
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
60
120
|
config = (0, config_1.getSdkConfig)();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
121
|
+
isTrigger = params.orderType === api_v2_sdk_1.OrderType.STOP_LOSS ||
|
|
122
|
+
params.orderType === api_v2_sdk_1.OrderType.TAKE_PROFIT;
|
|
123
|
+
if (isTrigger &&
|
|
124
|
+
(params.reduceOnly !== undefined || params.postOnly !== undefined)) {
|
|
125
|
+
throw new Error('reduceOnly and postOnly must be omitted for TP/SL orders');
|
|
126
|
+
}
|
|
127
|
+
if (isTrigger && params.triggerPx === undefined) {
|
|
128
|
+
throw new Error("triggerPx is required for ".concat(params.orderType, " orders"));
|
|
129
|
+
}
|
|
130
|
+
// qty is type-conditional (mirrors the modify-trigger path):
|
|
131
|
+
// - STOP_LOSS / TAKE_PROFIT: qty MUST be omitted; the signed quantity is
|
|
132
|
+
// the ±int256.max full-position sentinel (sign from `isBuy`) and qty is
|
|
133
|
+
// dropped from the wire below. The backend rejects a trigger create that
|
|
134
|
+
// carries qty.
|
|
135
|
+
// - LIMIT: qty is REQUIRED and signed as ±qty.
|
|
136
|
+
// Validated up front (before a nonce is minted), like the triggerPx guard.
|
|
137
|
+
if (isTrigger && params.qty !== undefined) {
|
|
138
|
+
throw new Error("qty must be omitted for ".concat(params.orderType, " orders (the signed quantity is the full-position sentinel)"));
|
|
139
|
+
}
|
|
140
|
+
if (!isTrigger && params.qty === undefined) {
|
|
141
|
+
throw new Error('qty is required for a LIMIT order');
|
|
142
|
+
}
|
|
143
|
+
onChainTimeInForce = (0, exports.toOnChainTimeInForce)(params.timeInForce);
|
|
144
|
+
hasExpiry = params.expiresAfter !== undefined && params.expiresAfter !== 0;
|
|
145
|
+
if (params.timeInForce === api_v2_sdk_1.TimeInForce.GTT && !hasExpiry) {
|
|
146
|
+
throw new Error('expiresAfter is required for GTT orders');
|
|
147
|
+
}
|
|
148
|
+
if (params.timeInForce !== api_v2_sdk_1.TimeInForce.GTT && hasExpiry) {
|
|
149
|
+
throw new Error('expiresAfter must be omitted for IOC / GTC orders — only GTT carries a lifetime');
|
|
150
|
+
}
|
|
151
|
+
if (params.reduceOnly && params.marketId >= SPOT_MARKET_ID_OFFSET) {
|
|
152
|
+
throw new Error('reduceOnly is not supported for spot markets');
|
|
153
|
+
}
|
|
154
|
+
// Venue admission rules the SDK can evaluate itself, checked while validation
|
|
155
|
+
// is still side-effect free — a nonce burnt on an order the engine refuses
|
|
156
|
+
// has to be resynced before the next one can be signed.
|
|
157
|
+
if (hasExpiry) {
|
|
158
|
+
(0, triggerSettlementHeadroom_1.assertSettlementHeadroom)(params.expiresAfter, params.settlementHeadroomSeconds);
|
|
159
|
+
}
|
|
77
160
|
return [4 /*yield*/, params.signer.getAddress()];
|
|
161
|
+
case 1:
|
|
162
|
+
signerAddress = _g.sent();
|
|
163
|
+
defaultDeadline = Math.floor(Date.now() / 1000) + DEFAULT_DEADLINE_SECONDS;
|
|
164
|
+
deadline = (_a = params.deadline) !== null && _a !== void 0 ? _a : defaultDeadline;
|
|
165
|
+
expiresAfter = (_b = params.expiresAfter) !== null && _b !== void 0 ? _b : 0;
|
|
166
|
+
nonce = (_c = params.nonce) !== null && _c !== void 0 ? _c : (0, nonce_1.nextNonce)(signerAddress);
|
|
167
|
+
quantity = isTrigger
|
|
168
|
+
? (0, common_1.fullPositionStopQuantity)(params.isBuy)
|
|
169
|
+
: params.isBuy
|
|
170
|
+
? (0, ethers_1.parseUnits)(params.qty, 18)
|
|
171
|
+
: -(0, ethers_1.parseUnits)(params.qty, 18);
|
|
172
|
+
limitPrice = (0, ethers_1.parseUnits)(params.limitPx, 18);
|
|
173
|
+
triggerPrice = params.triggerPx
|
|
174
|
+
? (0, ethers_1.parseUnits)(params.triggerPx, 18)
|
|
175
|
+
: BigInt(0);
|
|
176
|
+
return [4 /*yield*/, (0, common_1.signOrder)(params.signer, reyaChainId, {
|
|
177
|
+
accountId: params.accountId,
|
|
178
|
+
marketId: params.marketId,
|
|
179
|
+
exchangeId: params.exchangeId,
|
|
180
|
+
orderType: (0, exports.toOnChainOrderType)(params.orderType),
|
|
181
|
+
quantity: quantity,
|
|
182
|
+
limitPrice: limitPrice,
|
|
183
|
+
triggerPrice: triggerPrice,
|
|
184
|
+
timeInForce: onChainTimeInForce,
|
|
185
|
+
clientOrderId: BigInt((_d = params.clientOrderId) !== null && _d !== void 0 ? _d : 0),
|
|
186
|
+
reduceOnly: isTrigger ? false : (_e = params.reduceOnly) !== null && _e !== void 0 ? _e : false,
|
|
187
|
+
postOnly: isTrigger ? false : (_f = params.postOnly) !== null && _f !== void 0 ? _f : false,
|
|
188
|
+
expiresAfter: BigInt(expiresAfter),
|
|
189
|
+
nonce: nonce,
|
|
190
|
+
deadline: deadline,
|
|
191
|
+
})];
|
|
78
192
|
case 2:
|
|
79
|
-
|
|
193
|
+
serializedSignature = (_g.sent()).serializedSignature;
|
|
80
194
|
apiConfig = new api_v2_sdk_1.Configuration({
|
|
81
195
|
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
82
196
|
});
|
|
83
197
|
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
84
198
|
return [4 /*yield*/, orderEntryApi.createOrder({
|
|
85
|
-
createOrderRequest: {
|
|
86
|
-
exchangeId: params.exchangeId,
|
|
87
|
-
symbol: params.symbol,
|
|
88
|
-
accountId: params.accountId,
|
|
89
|
-
isBuy: params.isBuy,
|
|
90
|
-
limitPx: params.limitPx,
|
|
91
|
-
qty: params.qty,
|
|
92
|
-
orderType: api_v2_sdk_1.OrderType.LIMIT,
|
|
93
|
-
timeInForce: params.timeInForce,
|
|
199
|
+
createOrderRequest: __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({ exchangeId: params.exchangeId, symbol: params.symbol, accountId: params.accountId, isBuy: params.isBuy, limitPx: params.limitPx }, (params.qty !== undefined && { qty: params.qty })), { orderType: params.orderType, timeInForce: params.timeInForce }), (params.triggerPx !== undefined && { triggerPx: params.triggerPx })), (params.reduceOnly !== undefined && { reduceOnly: params.reduceOnly })), (params.postOnly !== undefined && { postOnly: params.postOnly })), (params.clientOrderId !== undefined && {
|
|
94
200
|
clientOrderId: params.clientOrderId,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
signerWallet: signerAddress,
|
|
98
|
-
expiresAfter: deadline,
|
|
99
|
-
},
|
|
201
|
+
})), { signature: serializedSignature, nonce: nonce.toString(), signerWallet: signerAddress, deadline: deadline }), (params.expiresAfter !== undefined &&
|
|
202
|
+
params.expiresAfter !== 0 && { expiresAfter: expiresAfter })),
|
|
100
203
|
})];
|
|
101
204
|
case 3:
|
|
102
|
-
response =
|
|
205
|
+
response = _g.sent();
|
|
103
206
|
return [2 /*return*/, response];
|
|
104
207
|
}
|
|
105
208
|
});
|
|
106
209
|
}); };
|
|
107
|
-
exports.
|
|
108
|
-
var
|
|
109
|
-
var reyaChainId, config,
|
|
110
|
-
var _a;
|
|
111
|
-
return __generator(this, function (
|
|
112
|
-
switch (
|
|
210
|
+
exports.createOrder = createOrder;
|
|
211
|
+
var cancelMEOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
212
|
+
var reyaChainId, config, orderIdProvided, clientOrderIdProvided, signedOrderId, signedClOrdId, signerAddress, defaultDeadline, deadline, nonce, signature, apiConfig, orderEntryApi, response;
|
|
213
|
+
var _a, _b, _c;
|
|
214
|
+
return __generator(this, function (_d) {
|
|
215
|
+
switch (_d.label) {
|
|
113
216
|
case 0:
|
|
114
|
-
|
|
115
|
-
throw new Error('Either orderId or clientOrderId must be provided');
|
|
116
|
-
}
|
|
217
|
+
assertNonZeroClientOrderId(params.clientOrderId);
|
|
117
218
|
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
118
219
|
config = (0, config_1.getSdkConfig)();
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
signedOrderId =
|
|
125
|
-
signedClOrdId =
|
|
126
|
-
|
|
220
|
+
orderIdProvided = isProvidedNonZeroId(params.orderId);
|
|
221
|
+
clientOrderIdProvided = isProvidedNonZeroId(params.clientOrderId);
|
|
222
|
+
if (!orderIdProvided && !clientOrderIdProvided) {
|
|
223
|
+
throw new Error('cancelMEOrder requires either `orderId` or `clientOrderId`');
|
|
224
|
+
}
|
|
225
|
+
signedOrderId = orderIdProvided ? BigInt(params.orderId) : BigInt(0);
|
|
226
|
+
signedClOrdId = !orderIdProvided && clientOrderIdProvided
|
|
227
|
+
? BigInt(params.clientOrderId)
|
|
228
|
+
: BigInt(0);
|
|
229
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
127
230
|
case 1:
|
|
128
|
-
|
|
231
|
+
signerAddress = _d.sent();
|
|
232
|
+
defaultDeadline = Math.floor(Date.now() / 1000) + DEFAULT_DEADLINE_SECONDS;
|
|
233
|
+
deadline = (_a = params.deadline) !== null && _a !== void 0 ? _a : defaultDeadline;
|
|
234
|
+
nonce = (_b = params.nonce) !== null && _b !== void 0 ? _b : (0, nonce_1.nextNonce)(signerAddress);
|
|
235
|
+
return [4 /*yield*/, (0, common_1.signMEOrderCancel)(params.signer, reyaChainId, params.accountId, params.marketId, signedOrderId, signedClOrdId, nonce, deadline)];
|
|
236
|
+
case 2:
|
|
237
|
+
signature = _d.sent();
|
|
129
238
|
apiConfig = new api_v2_sdk_1.Configuration({
|
|
130
239
|
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
131
240
|
});
|
|
132
241
|
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
133
242
|
return [4 /*yield*/, orderEntryApi.cancelOrder({
|
|
134
243
|
cancelOrderRequest: {
|
|
135
|
-
symbol: params.symbol,
|
|
136
244
|
accountId: params.accountId,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
245
|
+
symbol: params.symbol,
|
|
246
|
+
orderId: orderIdProvided ? params.orderId.trim() : undefined,
|
|
247
|
+
// See validation block above: clientOrderId is suppressed in the body
|
|
248
|
+
// whenever orderId is the canonical handle.
|
|
249
|
+
clientOrderId: orderIdProvided ? undefined : (_c = params.clientOrderId) === null || _c === void 0 ? void 0 : _c.trim(),
|
|
250
|
+
signature: signature,
|
|
140
251
|
nonce: nonce.toString(),
|
|
141
|
-
|
|
252
|
+
deadline: deadline,
|
|
142
253
|
},
|
|
143
254
|
})];
|
|
144
|
-
case
|
|
145
|
-
response =
|
|
255
|
+
case 3:
|
|
256
|
+
response = _d.sent();
|
|
146
257
|
return [2 /*return*/, response];
|
|
147
258
|
}
|
|
148
259
|
});
|
|
149
260
|
}); };
|
|
150
|
-
exports.
|
|
151
|
-
var
|
|
152
|
-
var hasSymbol, hasMarketId,
|
|
153
|
-
var _a, _b;
|
|
154
|
-
return __generator(this, function (
|
|
155
|
-
switch (
|
|
261
|
+
exports.cancelMEOrder = cancelMEOrder;
|
|
262
|
+
var massCancelMEOrders = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
263
|
+
var reyaChainId, config, hasSymbol, hasMarketId, signerAddress, defaultDeadline, deadline, nonce, signature, apiConfig, orderEntryApi, response;
|
|
264
|
+
var _a, _b, _c;
|
|
265
|
+
return __generator(this, function (_d) {
|
|
266
|
+
switch (_d.label) {
|
|
156
267
|
case 0:
|
|
268
|
+
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
269
|
+
config = (0, config_1.getSdkConfig)();
|
|
157
270
|
hasSymbol = params.symbol !== undefined;
|
|
158
271
|
hasMarketId = params.marketId !== undefined;
|
|
159
272
|
if (hasSymbol !== hasMarketId) {
|
|
160
|
-
throw new Error('symbol and marketId
|
|
273
|
+
throw new Error('massCancelMEOrders requires both `symbol` and `marketId` together, or neither (to mass-cancel across all markets)');
|
|
161
274
|
}
|
|
162
|
-
|
|
163
|
-
config = (0, config_1.getSdkConfig)();
|
|
164
|
-
nowMs = Date.now();
|
|
165
|
-
expiresAfter = Math.floor(nowMs / 1000) +
|
|
166
|
-
((_a = params.expiresAfterSeconds) !== null && _a !== void 0 ? _a : DEFAULT_CANCEL_DEADLINE_SECONDS);
|
|
167
|
-
nonce = BigInt(nowMs);
|
|
168
|
-
return [4 /*yield*/, (0, common_1.signMassCancel)(params.signer, reyaChainId, params.accountId, (_b = params.marketId) !== null && _b !== void 0 ? _b : 0, nonce, expiresAfter)];
|
|
275
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
169
276
|
case 1:
|
|
170
|
-
|
|
277
|
+
signerAddress = _d.sent();
|
|
278
|
+
defaultDeadline = Math.floor(Date.now() / 1000) + DEFAULT_DEADLINE_SECONDS;
|
|
279
|
+
deadline = (_a = params.deadline) !== null && _a !== void 0 ? _a : defaultDeadline;
|
|
280
|
+
nonce = (_b = params.nonce) !== null && _b !== void 0 ? _b : (0, nonce_1.nextNonce)(signerAddress);
|
|
281
|
+
return [4 /*yield*/, (0, common_1.signMEMassCancel)(params.signer, reyaChainId, params.accountId, (_c = params.marketId) !== null && _c !== void 0 ? _c : 0, nonce, deadline)];
|
|
282
|
+
case 2:
|
|
283
|
+
signature = _d.sent();
|
|
171
284
|
apiConfig = new api_v2_sdk_1.Configuration({
|
|
172
285
|
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
173
286
|
});
|
|
174
287
|
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
175
288
|
return [4 /*yield*/, orderEntryApi.cancelAll({
|
|
176
289
|
massCancelRequest: {
|
|
177
|
-
symbol: params.symbol,
|
|
178
290
|
accountId: params.accountId,
|
|
179
|
-
|
|
291
|
+
symbol: params.symbol,
|
|
292
|
+
signature: signature,
|
|
180
293
|
nonce: nonce.toString(),
|
|
181
|
-
|
|
294
|
+
deadline: deadline,
|
|
182
295
|
},
|
|
183
296
|
})];
|
|
297
|
+
case 3:
|
|
298
|
+
response = _d.sent();
|
|
299
|
+
return [2 /*return*/, response];
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}); };
|
|
303
|
+
exports.massCancelMEOrders = massCancelMEOrders;
|
|
304
|
+
// Convenience wrapper: LIMIT order with IOC timeInForce. UI teams may call
|
|
305
|
+
// createOrder directly with `timeInForce: TimeInForce.IOC` instead.
|
|
306
|
+
var createIOCOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
307
|
+
return __generator(this, function (_a) {
|
|
308
|
+
return [2 /*return*/, (0, exports.createOrder)(__assign(__assign({}, params), { timeInForce: api_v2_sdk_1.TimeInForce.IOC }))];
|
|
309
|
+
});
|
|
310
|
+
}); };
|
|
311
|
+
exports.createIOCOrder = createIOCOrder;
|
|
312
|
+
// Convenience wrapper: close a perp position by submitting a reduce-only LIMIT
|
|
313
|
+
// IOC. The ME will bust fills that overflow the position at the clearing layer.
|
|
314
|
+
var closePosition = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
315
|
+
return __generator(this, function (_a) {
|
|
316
|
+
return [2 /*return*/, (0, exports.createOrder)({
|
|
317
|
+
signer: params.signer,
|
|
318
|
+
accountId: params.accountId,
|
|
319
|
+
exchangeId: params.exchangeId,
|
|
320
|
+
marketId: params.marketId,
|
|
321
|
+
isBuy: params.isBuy,
|
|
322
|
+
limitPx: params.limitPx,
|
|
323
|
+
qty: params.qty,
|
|
324
|
+
symbol: params.symbol,
|
|
325
|
+
orderType: api_v2_sdk_1.OrderType.LIMIT,
|
|
326
|
+
timeInForce: api_v2_sdk_1.TimeInForce.IOC,
|
|
327
|
+
reduceOnly: true,
|
|
328
|
+
clientOrderId: params.clientOrderId,
|
|
329
|
+
deadline: params.deadline,
|
|
330
|
+
nonce: params.nonce,
|
|
331
|
+
})];
|
|
332
|
+
});
|
|
333
|
+
}); };
|
|
334
|
+
exports.closePosition = closePosition;
|
|
335
|
+
var CANCEL_ALL_AFTER_TIMEOUT_MIN_MS = 5000;
|
|
336
|
+
var CANCEL_ALL_AFTER_TIMEOUT_MAX_MS = 60000;
|
|
337
|
+
/**
|
|
338
|
+
* Arm, refresh, or disarm the account-scoped cancel-on-disconnect
|
|
339
|
+
* dead-man's-switch. The countdown lives in the matching engine; when it
|
|
340
|
+
* elapses, all of the account's open orders (all markets) are mass-cancelled.
|
|
341
|
+
*
|
|
342
|
+
* The matching engine owns nonce replay protection. A request rejected by
|
|
343
|
+
* local validation before it is forwarded does not consume its nonce. Once a
|
|
344
|
+
* request is sent, a transport failure may be outcome-unknown, and a request
|
|
345
|
+
* that reaches the matching engine may record its nonce in the WAL. Retry with
|
|
346
|
+
* a strictly higher nonce after an ME response or transport uncertainty.
|
|
347
|
+
* Reusing an explicit nonce is safe only after a definite local pre-forward
|
|
348
|
+
* rejection. When `nonce` is omitted, `nextNonce` advances automatically.
|
|
349
|
+
*/
|
|
350
|
+
var cancelAllAfter = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
351
|
+
var reyaChainId, config, signerAddress, defaultDeadline, deadline, nonce, signature, apiConfig, orderEntryApi, response;
|
|
352
|
+
var _a, _b;
|
|
353
|
+
return __generator(this, function (_c) {
|
|
354
|
+
switch (_c.label) {
|
|
355
|
+
case 0:
|
|
356
|
+
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
357
|
+
config = (0, config_1.getSdkConfig)();
|
|
358
|
+
// Fail fast before signing or sending a request the server will reject with
|
|
359
|
+
// INPUT_VALIDATION_ERROR anyway.
|
|
360
|
+
if (!Number.isInteger(params.timeoutMs) ||
|
|
361
|
+
params.timeoutMs < 0 ||
|
|
362
|
+
(params.timeoutMs !== 0 &&
|
|
363
|
+
(params.timeoutMs < CANCEL_ALL_AFTER_TIMEOUT_MIN_MS ||
|
|
364
|
+
params.timeoutMs > CANCEL_ALL_AFTER_TIMEOUT_MAX_MS))) {
|
|
365
|
+
throw new Error("cancelAllAfter timeoutMs must be 0 (disarm) or between ".concat(CANCEL_ALL_AFTER_TIMEOUT_MIN_MS, " and ").concat(CANCEL_ALL_AFTER_TIMEOUT_MAX_MS, " milliseconds"));
|
|
366
|
+
}
|
|
367
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
368
|
+
case 1:
|
|
369
|
+
signerAddress = _c.sent();
|
|
370
|
+
defaultDeadline = Math.floor(Date.now() / 1000) + DEFAULT_DEADLINE_SECONDS;
|
|
371
|
+
deadline = (_a = params.deadline) !== null && _a !== void 0 ? _a : defaultDeadline;
|
|
372
|
+
nonce = (_b = params.nonce) !== null && _b !== void 0 ? _b : (0, nonce_1.nextNonce)(signerAddress);
|
|
373
|
+
return [4 /*yield*/, (0, common_1.signMECancelAllAfter)(params.signer, reyaChainId, params.accountId, params.timeoutMs, nonce, deadline)];
|
|
184
374
|
case 2:
|
|
375
|
+
signature = _c.sent();
|
|
376
|
+
apiConfig = new api_v2_sdk_1.Configuration({
|
|
377
|
+
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
378
|
+
});
|
|
379
|
+
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
380
|
+
return [4 /*yield*/, orderEntryApi.cancelAllAfter({
|
|
381
|
+
cancelAllAfterRequest: {
|
|
382
|
+
accountId: params.accountId,
|
|
383
|
+
timeoutMs: params.timeoutMs,
|
|
384
|
+
signature: signature,
|
|
385
|
+
nonce: nonce.toString(),
|
|
386
|
+
signerWallet: signerAddress,
|
|
387
|
+
deadline: deadline,
|
|
388
|
+
},
|
|
389
|
+
})];
|
|
390
|
+
case 3:
|
|
185
391
|
response = _c.sent();
|
|
186
392
|
return [2 /*return*/, response];
|
|
187
393
|
}
|
|
188
394
|
});
|
|
189
395
|
}); };
|
|
190
|
-
exports.
|
|
396
|
+
exports.cancelAllAfter = cancelAllAfter;
|
|
397
|
+
/**
|
|
398
|
+
* Modify a resting order in place. The order keeps its orderId and
|
|
399
|
+
* clientOrderId; queue priority is preserved only for a qty decrease at an
|
|
400
|
+
* unchanged limitPx. The params carry the COMPLETE post-modify state; a request
|
|
401
|
+
* restating the resting order's exact current state is rejected with
|
|
402
|
+
* EMPTY_MODIFY_ERROR.
|
|
403
|
+
*/
|
|
404
|
+
var modifyMEOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
405
|
+
var reyaChainId, config, hasOrderId, hasClientOrderIdTarget, isTrigger, onChainTimeInForce, hasExpiry, signerAddress, defaultDeadline, deadline, nonce, quantity, limitPrice, triggerPrice, serializedSignature, apiConfig, orderEntryApi, response;
|
|
406
|
+
var _a, _b, _c, _d, _e, _f;
|
|
407
|
+
return __generator(this, function (_g) {
|
|
408
|
+
switch (_g.label) {
|
|
409
|
+
case 0:
|
|
410
|
+
assertNonZeroClientOrderId(params.clientOrderId);
|
|
411
|
+
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
412
|
+
config = (0, config_1.getSdkConfig)();
|
|
413
|
+
hasOrderId = isProvidedNonZeroId(params.orderId);
|
|
414
|
+
hasClientOrderIdTarget = isProvidedNonZeroId(params.clientOrderId);
|
|
415
|
+
if (!hasOrderId && !hasClientOrderIdTarget) {
|
|
416
|
+
throw new Error('modifyMEOrder requires `orderId` or a non-zero `clientOrderId`');
|
|
417
|
+
}
|
|
418
|
+
isTrigger = params.orderType === api_v2_sdk_1.OrderType.STOP_LOSS ||
|
|
419
|
+
params.orderType === api_v2_sdk_1.OrderType.TAKE_PROFIT;
|
|
420
|
+
if (isTrigger &&
|
|
421
|
+
(params.reduceOnly !== undefined || params.postOnly !== undefined)) {
|
|
422
|
+
throw new Error('reduceOnly and postOnly must be omitted for TP/SL orders');
|
|
423
|
+
}
|
|
424
|
+
if (!isTrigger && typeof params.postOnly !== 'boolean') {
|
|
425
|
+
throw new Error('postOnly is required for LIMIT modifications');
|
|
426
|
+
}
|
|
427
|
+
onChainTimeInForce = (0, exports.toOnChainTimeInForce)(params.timeInForce);
|
|
428
|
+
// triggerPx is required for trigger orders — without it we would silently
|
|
429
|
+
// sign triggerPrice=0. Fail at the SDK boundary (mirrors the cancel/mass-
|
|
430
|
+
// cancel SDK-boundary validation) instead of signing a 0-trigger order.
|
|
431
|
+
if (isTrigger && params.triggerPx === undefined) {
|
|
432
|
+
throw new Error("triggerPx is required for ".concat(params.orderType, " orders"));
|
|
433
|
+
}
|
|
434
|
+
// qty is type-conditional (mirrors the create-trigger path):
|
|
435
|
+
// - STOP_LOSS / TAKE_PROFIT: qty MUST be omitted; the signed quantity is
|
|
436
|
+
// the ±int256.max full-position sentinel (sign from `isBuy`) and qty is
|
|
437
|
+
// dropped from the wire below. The backend rejects a trigger modify that
|
|
438
|
+
// carries qty.
|
|
439
|
+
// - LIMIT: qty is REQUIRED and signed as the total post-modify quantity.
|
|
440
|
+
// Validated up front (before a nonce is minted), like the triggerPx guard.
|
|
441
|
+
if (isTrigger && params.qty !== undefined) {
|
|
442
|
+
throw new Error("qty must be omitted for ".concat(params.orderType, " orders (the signed quantity is the full-position sentinel)"));
|
|
443
|
+
}
|
|
444
|
+
if (!isTrigger && params.qty === undefined) {
|
|
445
|
+
throw new Error('qty is required when modifying a LIMIT order');
|
|
446
|
+
}
|
|
447
|
+
hasExpiry = params.expiresAfter !== undefined && params.expiresAfter !== 0;
|
|
448
|
+
if (params.timeInForce === api_v2_sdk_1.TimeInForce.GTT && !hasExpiry) {
|
|
449
|
+
throw new Error('expiresAfter is required for GTT orders');
|
|
450
|
+
}
|
|
451
|
+
if (params.timeInForce !== api_v2_sdk_1.TimeInForce.GTT && hasExpiry) {
|
|
452
|
+
throw new Error('expiresAfter must be omitted for IOC / GTC orders — only GTT carries a lifetime');
|
|
453
|
+
}
|
|
454
|
+
if (params.reduceOnly && params.marketId >= SPOT_MARKET_ID_OFFSET) {
|
|
455
|
+
throw new Error('reduceOnly is not supported for spot markets');
|
|
456
|
+
}
|
|
457
|
+
// Same venue admission rule as the create path: a restated GTT lifetime must
|
|
458
|
+
// still outlast the settlement headroom.
|
|
459
|
+
if (hasExpiry) {
|
|
460
|
+
(0, triggerSettlementHeadroom_1.assertSettlementHeadroom)(params.expiresAfter, params.settlementHeadroomSeconds);
|
|
461
|
+
}
|
|
462
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
463
|
+
case 1:
|
|
464
|
+
signerAddress = _g.sent();
|
|
465
|
+
defaultDeadline = Math.floor(Date.now() / 1000) + DEFAULT_DEADLINE_SECONDS;
|
|
466
|
+
deadline = (_a = params.deadline) !== null && _a !== void 0 ? _a : defaultDeadline;
|
|
467
|
+
nonce = (_b = params.nonce) !== null && _b !== void 0 ? _b : (0, nonce_1.nextNonce)(signerAddress);
|
|
468
|
+
quantity = isTrigger
|
|
469
|
+
? (0, common_1.fullPositionStopQuantity)(params.isBuy)
|
|
470
|
+
: params.isBuy
|
|
471
|
+
? (0, ethers_1.parseUnits)(params.qty, 18)
|
|
472
|
+
: -(0, ethers_1.parseUnits)(params.qty, 18);
|
|
473
|
+
limitPrice = (0, ethers_1.parseUnits)(params.limitPx, 18);
|
|
474
|
+
triggerPrice = params.triggerPx
|
|
475
|
+
? (0, ethers_1.parseUnits)(params.triggerPx, 18)
|
|
476
|
+
: BigInt(0);
|
|
477
|
+
return [4 /*yield*/, (0, common_1.signOrder)(params.signer, reyaChainId, {
|
|
478
|
+
accountId: params.accountId,
|
|
479
|
+
marketId: params.marketId,
|
|
480
|
+
exchangeId: params.exchangeId,
|
|
481
|
+
orderType: (0, exports.toOnChainOrderType)(params.orderType),
|
|
482
|
+
quantity: quantity,
|
|
483
|
+
limitPrice: limitPrice,
|
|
484
|
+
triggerPrice: triggerPrice,
|
|
485
|
+
timeInForce: onChainTimeInForce,
|
|
486
|
+
clientOrderId: BigInt((_c = params.clientOrderId) !== null && _c !== void 0 ? _c : '0'),
|
|
487
|
+
reduceOnly: isTrigger ? false : (_d = params.reduceOnly) !== null && _d !== void 0 ? _d : false,
|
|
488
|
+
postOnly: isTrigger ? false : params.postOnly,
|
|
489
|
+
expiresAfter: BigInt((_e = params.expiresAfter) !== null && _e !== void 0 ? _e : 0),
|
|
490
|
+
nonce: nonce,
|
|
491
|
+
deadline: deadline,
|
|
492
|
+
})];
|
|
493
|
+
case 2:
|
|
494
|
+
serializedSignature = (_g.sent()).serializedSignature;
|
|
495
|
+
apiConfig = new api_v2_sdk_1.Configuration({
|
|
496
|
+
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
497
|
+
});
|
|
498
|
+
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
499
|
+
return [4 /*yield*/, orderEntryApi.modifyOrder({
|
|
500
|
+
modifyOrderRequest: __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, (hasOrderId ? { orderId: params.orderId.trim() } : {})), (hasClientOrderIdTarget
|
|
501
|
+
? { clientOrderId: params.clientOrderId.trim() }
|
|
502
|
+
: {})), { symbol: params.symbol, accountId: params.accountId, exchangeId: params.exchangeId, isBuy: params.isBuy }), (params.orderType === api_v2_sdk_1.OrderType.LIMIT
|
|
503
|
+
? {
|
|
504
|
+
orderType: params.orderType,
|
|
505
|
+
reduceOnly: (_f = params.reduceOnly) !== null && _f !== void 0 ? _f : false,
|
|
506
|
+
postOnly: params.postOnly,
|
|
507
|
+
}
|
|
508
|
+
: { orderType: params.orderType })), { timeInForce: params.timeInForce }), (params.triggerPx !== undefined && { triggerPx: params.triggerPx })), { limitPx: params.limitPx }), (params.qty !== undefined && { qty: params.qty })), (params.expiresAfter !== undefined &&
|
|
509
|
+
params.expiresAfter !== 0 && { expiresAfter: params.expiresAfter })), { signature: serializedSignature, nonce: nonce.toString(), signerWallet: signerAddress, deadline: deadline }),
|
|
510
|
+
})];
|
|
511
|
+
case 3:
|
|
512
|
+
response = _g.sent();
|
|
513
|
+
return [2 /*return*/, response];
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}); };
|
|
517
|
+
exports.modifyMEOrder = modifyMEOrder;
|
|
191
518
|
//# sourceMappingURL=orderV2.js.map
|