@reyaxyz/sdk 0.147.4 → 0.149.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/orderV2.js +104 -11
- package/dist/services/orders/orderV2.js.map +1 -1
- package/dist/types/services/orders/orderV2.d.ts +22 -2
- package/dist/types/services/orders/orderV2.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/services/orders/orderV2.ts +143 -4
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -36,12 +36,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.createSpotOrder = void 0;
|
|
39
|
+
exports.massCancelSpotOrders = exports.cancelSpotOrder = exports.createSpotOrder = exports.TimeInForce = void 0;
|
|
40
40
|
var ethers_1 = require("ethers");
|
|
41
41
|
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; } });
|
|
42
43
|
var common_1 = require("@reyaxyz/common");
|
|
43
44
|
var network_1 = require("../../utils/network");
|
|
44
45
|
var config_1 = require("../../config");
|
|
46
|
+
var DEFAULT_CANCEL_DEADLINE_SECONDS = 60;
|
|
45
47
|
var encodeSpotLimitOrderInputs = function (isBuy, limitPx, qty) {
|
|
46
48
|
var baseDelta = isBuy
|
|
47
49
|
? (0, common_1.scale)(18)(parseFloat(qty))
|
|
@@ -50,24 +52,31 @@ var encodeSpotLimitOrderInputs = function (isBuy, limitPx, qty) {
|
|
|
50
52
|
return ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [baseDelta, price]);
|
|
51
53
|
};
|
|
52
54
|
var createSpotOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
53
|
-
var reyaChainId, config, inputs, creationTimestampMs,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
switch (_c.label) {
|
|
55
|
+
var reyaChainId, config, inputs, creationTimestampMs, deadline, spotNonce, _a, serializedSignature, nonce, signerAddress, apiConfig, orderEntryApi, response;
|
|
56
|
+
return __generator(this, function (_b) {
|
|
57
|
+
switch (_b.label) {
|
|
57
58
|
case 0:
|
|
58
59
|
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
59
60
|
config = (0, config_1.getSdkConfig)();
|
|
60
61
|
inputs = encodeSpotLimitOrderInputs(params.isBuy, params.limitPx, params.qty);
|
|
61
62
|
creationTimestampMs = Date.now();
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
deadline = (function () {
|
|
64
|
+
switch (params.timeInForce) {
|
|
65
|
+
case api_v2_sdk_1.TimeInForce.IOC:
|
|
66
|
+
return Math.floor(creationTimestampMs / 1000) + 30;
|
|
67
|
+
case api_v2_sdk_1.TimeInForce.GTC:
|
|
68
|
+
return common_1.CONDITIONAL_ORDER_SIG_DEADLINE;
|
|
69
|
+
default:
|
|
70
|
+
throw new Error("Unsupported timeInForce: ".concat(params.timeInForce));
|
|
71
|
+
}
|
|
72
|
+
})();
|
|
64
73
|
spotNonce = BigInt(creationTimestampMs);
|
|
65
74
|
return [4 /*yield*/, (0, common_1.signOrdersGatewayOrder)(params.signer, reyaChainId, params.accountId, params.marketId, params.exchangeId, [], common_1.OrdersGatewayOrderType.LIMIT_ORDER_SPOT, inputs, deadline, creationTimestampMs, spotNonce)];
|
|
66
75
|
case 1:
|
|
67
|
-
_a =
|
|
76
|
+
_a = _b.sent(), serializedSignature = _a.serializedSignature, nonce = _a.nonce;
|
|
68
77
|
return [4 /*yield*/, params.signer.getAddress()];
|
|
69
78
|
case 2:
|
|
70
|
-
signerAddress =
|
|
79
|
+
signerAddress = _b.sent();
|
|
71
80
|
apiConfig = new api_v2_sdk_1.Configuration({
|
|
72
81
|
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
73
82
|
});
|
|
@@ -81,7 +90,8 @@ var createSpotOrder = function (params) { return __awaiter(void 0, void 0, void
|
|
|
81
90
|
limitPx: params.limitPx,
|
|
82
91
|
qty: params.qty,
|
|
83
92
|
orderType: api_v2_sdk_1.OrderType.LIMIT,
|
|
84
|
-
timeInForce:
|
|
93
|
+
timeInForce: params.timeInForce,
|
|
94
|
+
clientOrderId: params.clientOrderId,
|
|
85
95
|
signature: serializedSignature,
|
|
86
96
|
nonce: nonce.toString(),
|
|
87
97
|
signerWallet: signerAddress,
|
|
@@ -89,10 +99,93 @@ var createSpotOrder = function (params) { return __awaiter(void 0, void 0, void
|
|
|
89
99
|
},
|
|
90
100
|
})];
|
|
91
101
|
case 3:
|
|
92
|
-
response =
|
|
102
|
+
response = _b.sent();
|
|
93
103
|
return [2 /*return*/, response];
|
|
94
104
|
}
|
|
95
105
|
});
|
|
96
106
|
}); };
|
|
97
107
|
exports.createSpotOrder = createSpotOrder;
|
|
108
|
+
var cancelSpotOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
109
|
+
var reyaChainId, config, nowMs, expiresAfter, nonce, useOrderId, signedOrderId, signedClOrdId, serializedSignature, apiConfig, orderEntryApi, response;
|
|
110
|
+
var _a;
|
|
111
|
+
return __generator(this, function (_b) {
|
|
112
|
+
switch (_b.label) {
|
|
113
|
+
case 0:
|
|
114
|
+
if (!params.orderId && params.clientOrderId === undefined) {
|
|
115
|
+
throw new Error('Either orderId or clientOrderId must be provided');
|
|
116
|
+
}
|
|
117
|
+
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
118
|
+
config = (0, config_1.getSdkConfig)();
|
|
119
|
+
nowMs = Date.now();
|
|
120
|
+
expiresAfter = Math.floor(nowMs / 1000) +
|
|
121
|
+
((_a = params.expiresAfterSeconds) !== null && _a !== void 0 ? _a : DEFAULT_CANCEL_DEADLINE_SECONDS);
|
|
122
|
+
nonce = BigInt(nowMs);
|
|
123
|
+
useOrderId = !!params.orderId;
|
|
124
|
+
signedOrderId = useOrderId ? BigInt(params.orderId) : BigInt(0);
|
|
125
|
+
signedClOrdId = useOrderId ? BigInt(0) : BigInt(params.clientOrderId);
|
|
126
|
+
return [4 /*yield*/, (0, common_1.signOrderCancel)(params.signer, reyaChainId, params.accountId, params.marketId, signedOrderId, signedClOrdId, nonce, expiresAfter)];
|
|
127
|
+
case 1:
|
|
128
|
+
serializedSignature = (_b.sent()).serializedSignature;
|
|
129
|
+
apiConfig = new api_v2_sdk_1.Configuration({
|
|
130
|
+
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
131
|
+
});
|
|
132
|
+
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
133
|
+
return [4 /*yield*/, orderEntryApi.cancelOrder({
|
|
134
|
+
cancelOrderRequest: {
|
|
135
|
+
symbol: params.symbol,
|
|
136
|
+
accountId: params.accountId,
|
|
137
|
+
orderId: useOrderId ? params.orderId : undefined,
|
|
138
|
+
clientOrderId: useOrderId ? undefined : params.clientOrderId,
|
|
139
|
+
signature: serializedSignature,
|
|
140
|
+
nonce: nonce.toString(),
|
|
141
|
+
expiresAfter: expiresAfter,
|
|
142
|
+
},
|
|
143
|
+
})];
|
|
144
|
+
case 2:
|
|
145
|
+
response = _b.sent();
|
|
146
|
+
return [2 /*return*/, response];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}); };
|
|
150
|
+
exports.cancelSpotOrder = cancelSpotOrder;
|
|
151
|
+
var massCancelSpotOrders = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
152
|
+
var hasSymbol, hasMarketId, reyaChainId, config, nowMs, expiresAfter, nonce, serializedSignature, apiConfig, orderEntryApi, response;
|
|
153
|
+
var _a, _b;
|
|
154
|
+
return __generator(this, function (_c) {
|
|
155
|
+
switch (_c.label) {
|
|
156
|
+
case 0:
|
|
157
|
+
hasSymbol = params.symbol !== undefined;
|
|
158
|
+
hasMarketId = params.marketId !== undefined;
|
|
159
|
+
if (hasSymbol !== hasMarketId) {
|
|
160
|
+
throw new Error('symbol and marketId must be provided together (or both omitted to cancel across all markets)');
|
|
161
|
+
}
|
|
162
|
+
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
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)];
|
|
169
|
+
case 1:
|
|
170
|
+
serializedSignature = (_c.sent()).serializedSignature;
|
|
171
|
+
apiConfig = new api_v2_sdk_1.Configuration({
|
|
172
|
+
basePath: "".concat(config.apiEndpoint, "/v2"),
|
|
173
|
+
});
|
|
174
|
+
orderEntryApi = new api_v2_sdk_1.OrderEntryApi(apiConfig);
|
|
175
|
+
return [4 /*yield*/, orderEntryApi.cancelAll({
|
|
176
|
+
massCancelRequest: {
|
|
177
|
+
symbol: params.symbol,
|
|
178
|
+
accountId: params.accountId,
|
|
179
|
+
signature: serializedSignature,
|
|
180
|
+
nonce: nonce.toString(),
|
|
181
|
+
expiresAfter: expiresAfter,
|
|
182
|
+
},
|
|
183
|
+
})];
|
|
184
|
+
case 2:
|
|
185
|
+
response = _c.sent();
|
|
186
|
+
return [2 /*return*/, response];
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}); };
|
|
190
|
+
exports.massCancelSpotOrders = massCancelSpotOrders;
|
|
98
191
|
//# sourceMappingURL=orderV2.js.map
|
|
@@ -1 +1 @@
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Configuration,\n CreateOrderResponse,\n OrderType,\n TimeInForce,\n} from '@reyaxyz/api-v2-sdk';\nimport {\n signOrdersGatewayOrder,\n OrdersGatewayOrderType,\n scale,\n} from '@reyaxyz/common';\nimport { getReyaNetwork } from '../../utils/network';\nimport { getSdkConfig } from '../../config';\n\nexport type CreateSpotOrderParams = {\n signer: Signer | JsonRpcSigner;\n accountId: number;\n exchangeId: number;\n marketId: number;\n isBuy: boolean;\n limitPx: string;\n qty: string;\n symbol: string;\n expiresAfter?: number;\n};\n\nconst encodeSpotLimitOrderInputs = (\n isBuy: boolean,\n limitPx: string,\n qty: string,\n): string => {\n const baseDelta = isBuy\n ? scale(18)(parseFloat(qty))\n : -scale(18)(parseFloat(qty));\n const price = scale(18)(parseFloat(limitPx));\n return AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [baseDelta, price],\n );\n};\n\nexport const createSpotOrder = async (\n params: CreateSpotOrderParams,\n): Promise<CreateOrderResponse> => {\n const reyaChainId = getReyaNetwork();\n const config = getSdkConfig();\n\n const inputs = encodeSpotLimitOrderInputs(\n params.isBuy,\n params.limitPx,\n params.qty,\n );\n\n const creationTimestampMs = Date.now();\n const defaultDeadlineSeconds = Math.floor(creationTimestampMs / 1000) + 30; // 30 seconds from now\n const deadline = params.expiresAfter ?? defaultDeadlineSeconds;\n\n // For spot orders, nonce is just the timestamp (uint64)\n const spotNonce = BigInt(creationTimestampMs);\n\n const { serializedSignature, nonce } = await signOrdersGatewayOrder(\n params.signer,\n reyaChainId,\n params.accountId,\n params.marketId,\n params.exchangeId,\n [],\n OrdersGatewayOrderType.LIMIT_ORDER_SPOT,\n inputs,\n deadline,\n creationTimestampMs,\n spotNonce, // custom nonce for spot orders\n );\n\n const signerAddress = await params.signer.getAddress();\n\n const apiConfig = new Configuration({\n basePath: `${config.apiEndpoint}/v2`,\n });\n\n const orderEntryApi = new OrderEntryApi(apiConfig);\n\n const response = await orderEntryApi.createOrder({\n createOrderRequest: {\n exchangeId: params.exchangeId,\n symbol: params.symbol,\n accountId: params.accountId,\n isBuy: params.isBuy,\n limitPx: params.limitPx,\n qty: params.qty,\n orderType: OrderType.LIMIT,\n timeInForce: TimeInForce.IOC,\n signature: serializedSignature,\n nonce: nonce.toString(),\n signerWallet: signerAddress,\n expiresAfter: deadline,\n },\n });\n\n return response;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"orderV2.js","sourceRoot":"/","sources":["services/orders/orderV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAyD;AACzD,kDAQ6B;AAcpB,4FAfP,wBAAW,OAeO;AAbpB,0CAOyB;AACzB,+CAAqD;AACrD,uCAA4C;AAE5C,IAAM,+BAA+B,GAAG,EAAE,CAAC;AAiB3C,IAAM,0BAA0B,GAAG,UACjC,KAAc,EACd,OAAe,EACf,GAAW;IAEX,IAAM,SAAS,GAAG,KAAK;QACrB,CAAC,CAAC,IAAA,cAAK,EAAC,EAAE,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC,IAAA,cAAK,EAAC,EAAE,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;IAChC,IAAM,KAAK,GAAG,IAAA,cAAK,EAAC,EAAE,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7C,OAAO,iBAAQ,CAAC,eAAe,EAAE,CAAC,MAAM,CACtC,CAAC,QAAQ,EAAE,SAAS,CAAC,EACrB,CAAC,SAAS,EAAE,KAAK,CAAC,CACnB,CAAC;AACJ,CAAC,CAAC;AAEK,IAAM,eAAe,GAAG,UAC7B,MAA6B;;;;;gBAEvB,WAAW,GAAG,IAAA,wBAAc,GAAE,CAAC;gBAC/B,MAAM,GAAG,IAAA,qBAAY,GAAE,CAAC;gBAExB,MAAM,GAAG,0BAA0B,CACvC,MAAM,CAAC,KAAK,EACZ,MAAM,CAAC,OAAO,EACd,MAAM,CA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C;gBACJ,CAAC;gBAEK,WAAW,GAAG,IAAA,wBAAc,GAAE,CAAC;gBAC/B,MAAM,GAAG,IAAA,qBAAY,GAAE,CAAC;gBAExB,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBACnB,YAAY,GAChB,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;oBACxB,CAAC,MAAA,MAAM,CAAC,mBAAmB,mCAAI,+BAA+B,CAAC,CAAC;gBAC5D,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;gBAEI,qBAAM,IAAA,uBAAc,EAClD,MAAM,CAAC,MAAM,EACb,WAAW,EACX,MAAM,CAAC,SAAS,EAChB,MAAA,MAAM,CAAC,QAAQ,mCAAI,CAAC,EACpB,KAAK,EACL,YAAY,CACb,EAAA;;gBAPO,mBAAmB,GAAK,CAAA,SAO/B,CAAA,oBAP0B;gBASrB,SAAS,GAAG,IAAI,0BAAa,CAAC;oBAClC,QAAQ,EAAE,UAAG,MAAM,CAAC,WAAW,QAAK;iBACrC,CAAC,CAAC;gBAEG,aAAa,GAAG,IAAI,0BAAa,CAAC,SAAS,CAAC,CAAC;gBAElC,qBAAM,aAAa,CAAC,SAAS,CAAC;wBAC7C,iBAAiB,EAAE;4BACjB,MAAM,EAAE,MAAM,CAAC,MAAM;4BACrB,SAAS,EAAE,MAAM,CAAC,SAAS;4BAC3B,SAAS,EAAE,mBAAmB;4BAC9B,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;4BACvB,YAAY,cAAA;yBACb;qBACF,CAAC,EAAA;;gBARI,QAAQ,GAAG,SAQf;gBAEF,sBAAO,QAAQ,EAAC;;;KACjB,CAAC;AA9CW,QAAA,oBAAoB,wBA8C/B","sourcesContent":["import { Signer, JsonRpcSigner, AbiCoder } from 'ethers';\nimport {\n OrderEntryApi,\n Configuration,\n CancelOrderResponse,\n CreateOrderResponse,\n MassCancelResponse,\n OrderType,\n TimeInForce,\n} from '@reyaxyz/api-v2-sdk';\nimport {\n signOrdersGatewayOrder,\n signOrderCancel,\n signMassCancel,\n OrdersGatewayOrderType,\n scale,\n CONDITIONAL_ORDER_SIG_DEADLINE,\n} from '@reyaxyz/common';\nimport { getReyaNetwork } from '../../utils/network';\nimport { getSdkConfig } from '../../config';\n\nconst DEFAULT_CANCEL_DEADLINE_SECONDS = 60;\n\nexport { TimeInForce };\n\nexport type CreateSpotOrderParams = {\n signer: Signer | JsonRpcSigner;\n accountId: number;\n exchangeId: number;\n marketId: number;\n isBuy: boolean;\n limitPx: string;\n qty: string;\n symbol: string;\n timeInForce: TimeInForce;\n clientOrderId?: number;\n};\n\nconst encodeSpotLimitOrderInputs = (\n isBuy: boolean,\n limitPx: string,\n qty: string,\n): string => {\n const baseDelta = isBuy\n ? scale(18)(parseFloat(qty))\n : -scale(18)(parseFloat(qty));\n const price = scale(18)(parseFloat(limitPx));\n return AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [baseDelta, price],\n );\n};\n\nexport const createSpotOrder = async (\n params: CreateSpotOrderParams,\n): Promise<CreateOrderResponse> => {\n const reyaChainId = getReyaNetwork();\n const config = getSdkConfig();\n\n const inputs = encodeSpotLimitOrderInputs(\n params.isBuy,\n params.limitPx,\n params.qty,\n );\n\n const creationTimestampMs = Date.now();\n // IOC orders are capped at 10 minutes by the API; GTC orders use the\n // conditional-order sentinel deadline so they rest until cancelled.\n const deadline = (() => {\n switch (params.timeInForce) {\n case TimeInForce.IOC:\n return Math.floor(creationTimestampMs / 1000) + 30;\n case TimeInForce.GTC:\n return CONDITIONAL_ORDER_SIG_DEADLINE;\n default:\n throw new Error(`Unsupported timeInForce: ${params.timeInForce}`);\n }\n })();\n\n // For spot orders, nonce is just the timestamp (uint64)\n const spotNonce = BigInt(creationTimestampMs);\n\n const { serializedSignature, nonce } = await signOrdersGatewayOrder(\n params.signer,\n reyaChainId,\n params.accountId,\n params.marketId,\n params.exchangeId,\n [],\n OrdersGatewayOrderType.LIMIT_ORDER_SPOT,\n inputs,\n deadline,\n creationTimestampMs,\n spotNonce, // custom nonce for spot orders\n );\n\n const signerAddress = await params.signer.getAddress();\n\n const apiConfig = new Configuration({\n basePath: `${config.apiEndpoint}/v2`,\n });\n\n const orderEntryApi = new OrderEntryApi(apiConfig);\n\n const response = await orderEntryApi.createOrder({\n createOrderRequest: {\n exchangeId: params.exchangeId,\n symbol: params.symbol,\n accountId: params.accountId,\n isBuy: params.isBuy,\n limitPx: params.limitPx,\n qty: params.qty,\n orderType: OrderType.LIMIT,\n timeInForce: params.timeInForce,\n clientOrderId: params.clientOrderId,\n signature: serializedSignature,\n nonce: nonce.toString(),\n signerWallet: signerAddress,\n expiresAfter: deadline,\n },\n });\n\n return response;\n};\n\nexport type CancelSpotOrderParams = {\n signer: Signer | JsonRpcSigner;\n accountId: number;\n marketId: number;\n symbol: string;\n orderId?: string;\n clientOrderId?: number;\n expiresAfterSeconds?: number;\n};\n\nexport const cancelSpotOrder = async (\n params: CancelSpotOrderParams,\n): Promise<CancelOrderResponse> => {\n if (!params.orderId && params.clientOrderId === undefined) {\n throw new Error('Either orderId or clientOrderId must be provided');\n }\n\n const reyaChainId = getReyaNetwork();\n const config = getSdkConfig();\n\n const nowMs = Date.now();\n const expiresAfter =\n Math.floor(nowMs / 1000) +\n (params.expiresAfterSeconds ?? DEFAULT_CANCEL_DEADLINE_SECONDS);\n const nonce = BigInt(nowMs);\n\n const useOrderId = !!params.orderId;\n const signedOrderId = useOrderId ? BigInt(params.orderId!) : BigInt(0);\n const signedClOrdId = useOrderId ? BigInt(0) : BigInt(params.clientOrderId!);\n\n const { serializedSignature } = await signOrderCancel(\n params.signer,\n reyaChainId,\n params.accountId,\n params.marketId,\n signedOrderId,\n signedClOrdId,\n nonce,\n expiresAfter,\n );\n\n const apiConfig = new Configuration({\n basePath: `${config.apiEndpoint}/v2`,\n });\n\n const orderEntryApi = new OrderEntryApi(apiConfig);\n\n const response = await orderEntryApi.cancelOrder({\n cancelOrderRequest: {\n symbol: params.symbol,\n accountId: params.accountId,\n orderId: useOrderId ? params.orderId : undefined,\n clientOrderId: useOrderId ? undefined : params.clientOrderId,\n signature: serializedSignature,\n nonce: nonce.toString(),\n expiresAfter,\n },\n });\n\n return response;\n};\n\nexport type MassCancelSpotOrdersParams = {\n signer: Signer | JsonRpcSigner;\n accountId: number;\n symbol?: string;\n marketId?: number;\n expiresAfterSeconds?: number;\n};\n\nexport const massCancelSpotOrders = async (\n params: MassCancelSpotOrdersParams,\n): Promise<MassCancelResponse> => {\n const hasSymbol = params.symbol !== undefined;\n const hasMarketId = params.marketId !== undefined;\n if (hasSymbol !== hasMarketId) {\n throw new Error(\n 'symbol and marketId must be provided together (or both omitted to cancel across all markets)',\n );\n }\n\n const reyaChainId = getReyaNetwork();\n const config = getSdkConfig();\n\n const nowMs = Date.now();\n const expiresAfter =\n Math.floor(nowMs / 1000) +\n (params.expiresAfterSeconds ?? DEFAULT_CANCEL_DEADLINE_SECONDS);\n const nonce = BigInt(nowMs);\n\n const { serializedSignature } = await signMassCancel(\n params.signer,\n reyaChainId,\n params.accountId,\n params.marketId ?? 0,\n nonce,\n expiresAfter,\n );\n\n const apiConfig = new Configuration({\n basePath: `${config.apiEndpoint}/v2`,\n });\n\n const orderEntryApi = new OrderEntryApi(apiConfig);\n\n const response = await orderEntryApi.cancelAll({\n massCancelRequest: {\n symbol: params.symbol,\n accountId: params.accountId,\n signature: serializedSignature,\n nonce: nonce.toString(),\n expiresAfter,\n },\n });\n\n return response;\n};\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Signer, JsonRpcSigner } from 'ethers';
|
|
2
|
-
import { CreateOrderResponse } from '@reyaxyz/api-v2-sdk';
|
|
2
|
+
import { CancelOrderResponse, CreateOrderResponse, MassCancelResponse, TimeInForce } from '@reyaxyz/api-v2-sdk';
|
|
3
|
+
export { TimeInForce };
|
|
3
4
|
export type CreateSpotOrderParams = {
|
|
4
5
|
signer: Signer | JsonRpcSigner;
|
|
5
6
|
accountId: number;
|
|
@@ -9,7 +10,26 @@ export type CreateSpotOrderParams = {
|
|
|
9
10
|
limitPx: string;
|
|
10
11
|
qty: string;
|
|
11
12
|
symbol: string;
|
|
12
|
-
|
|
13
|
+
timeInForce: TimeInForce;
|
|
14
|
+
clientOrderId?: number;
|
|
13
15
|
};
|
|
14
16
|
export declare const createSpotOrder: (params: CreateSpotOrderParams) => Promise<CreateOrderResponse>;
|
|
17
|
+
export type CancelSpotOrderParams = {
|
|
18
|
+
signer: Signer | JsonRpcSigner;
|
|
19
|
+
accountId: number;
|
|
20
|
+
marketId: number;
|
|
21
|
+
symbol: string;
|
|
22
|
+
orderId?: string;
|
|
23
|
+
clientOrderId?: number;
|
|
24
|
+
expiresAfterSeconds?: number;
|
|
25
|
+
};
|
|
26
|
+
export declare const cancelSpotOrder: (params: CancelSpotOrderParams) => Promise<CancelOrderResponse>;
|
|
27
|
+
export type MassCancelSpotOrdersParams = {
|
|
28
|
+
signer: Signer | JsonRpcSigner;
|
|
29
|
+
accountId: number;
|
|
30
|
+
symbol?: string;
|
|
31
|
+
marketId?: number;
|
|
32
|
+
expiresAfterSeconds?: number;
|
|
33
|
+
};
|
|
34
|
+
export declare const massCancelSpotOrders: (params: MassCancelSpotOrdersParams) => Promise<MassCancelResponse>;
|
|
15
35
|
//# sourceMappingURL=orderV2.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orderV2.d.ts","sourceRoot":"/","sources":["services/orders/orderV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAY,MAAM,QAAQ,CAAC;AACzD,OAAO,EAGL,mBAAmB,
|
|
1
|
+
{"version":3,"file":"orderV2.d.ts","sourceRoot":"/","sources":["services/orders/orderV2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAY,MAAM,QAAQ,CAAC;AACzD,OAAO,EAGL,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAElB,WA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
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.149.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -27,15 +27,16 @@
|
|
|
27
27
|
"test": "npx jest --silent",
|
|
28
28
|
"test:coverage": "jest test --coverage",
|
|
29
29
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent",
|
|
30
|
-
"test:spot-order": "npx ts-node scripts/test-spot-order.ts"
|
|
30
|
+
"test:spot-order": "npx ts-node scripts/test-spot-order.ts",
|
|
31
|
+
"test:cancel-spot-order": "npx ts-node scripts/test-cancel-spot-order.ts"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@reyaxyz/api-v2-sdk": "0.304.0",
|
|
34
|
-
"@reyaxyz/common": "0.
|
|
35
|
+
"@reyaxyz/common": "0.348.0",
|
|
35
36
|
"axios": "1.6.2",
|
|
36
37
|
"bignumber.js": "9.1.2",
|
|
37
38
|
"ethers": "6.9.0"
|
|
38
39
|
},
|
|
39
40
|
"packageManager": "pnpm@8.3.1",
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "8bec7fed474be08b0f213b6fd0467ae327684d9a"
|
|
41
42
|
}
|
|
@@ -2,18 +2,27 @@ import { Signer, JsonRpcSigner, AbiCoder } from 'ethers';
|
|
|
2
2
|
import {
|
|
3
3
|
OrderEntryApi,
|
|
4
4
|
Configuration,
|
|
5
|
+
CancelOrderResponse,
|
|
5
6
|
CreateOrderResponse,
|
|
7
|
+
MassCancelResponse,
|
|
6
8
|
OrderType,
|
|
7
9
|
TimeInForce,
|
|
8
10
|
} from '@reyaxyz/api-v2-sdk';
|
|
9
11
|
import {
|
|
10
12
|
signOrdersGatewayOrder,
|
|
13
|
+
signOrderCancel,
|
|
14
|
+
signMassCancel,
|
|
11
15
|
OrdersGatewayOrderType,
|
|
12
16
|
scale,
|
|
17
|
+
CONDITIONAL_ORDER_SIG_DEADLINE,
|
|
13
18
|
} from '@reyaxyz/common';
|
|
14
19
|
import { getReyaNetwork } from '../../utils/network';
|
|
15
20
|
import { getSdkConfig } from '../../config';
|
|
16
21
|
|
|
22
|
+
const DEFAULT_CANCEL_DEADLINE_SECONDS = 60;
|
|
23
|
+
|
|
24
|
+
export { TimeInForce };
|
|
25
|
+
|
|
17
26
|
export type CreateSpotOrderParams = {
|
|
18
27
|
signer: Signer | JsonRpcSigner;
|
|
19
28
|
accountId: number;
|
|
@@ -23,7 +32,8 @@ export type CreateSpotOrderParams = {
|
|
|
23
32
|
limitPx: string;
|
|
24
33
|
qty: string;
|
|
25
34
|
symbol: string;
|
|
26
|
-
|
|
35
|
+
timeInForce: TimeInForce;
|
|
36
|
+
clientOrderId?: number;
|
|
27
37
|
};
|
|
28
38
|
|
|
29
39
|
const encodeSpotLimitOrderInputs = (
|
|
@@ -54,8 +64,18 @@ export const createSpotOrder = async (
|
|
|
54
64
|
);
|
|
55
65
|
|
|
56
66
|
const creationTimestampMs = Date.now();
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
// IOC orders are capped at 10 minutes by the API; GTC orders use the
|
|
68
|
+
// conditional-order sentinel deadline so they rest until cancelled.
|
|
69
|
+
const deadline = (() => {
|
|
70
|
+
switch (params.timeInForce) {
|
|
71
|
+
case TimeInForce.IOC:
|
|
72
|
+
return Math.floor(creationTimestampMs / 1000) + 30;
|
|
73
|
+
case TimeInForce.GTC:
|
|
74
|
+
return CONDITIONAL_ORDER_SIG_DEADLINE;
|
|
75
|
+
default:
|
|
76
|
+
throw new Error(`Unsupported timeInForce: ${params.timeInForce}`);
|
|
77
|
+
}
|
|
78
|
+
})();
|
|
59
79
|
|
|
60
80
|
// For spot orders, nonce is just the timestamp (uint64)
|
|
61
81
|
const spotNonce = BigInt(creationTimestampMs);
|
|
@@ -91,7 +111,8 @@ export const createSpotOrder = async (
|
|
|
91
111
|
limitPx: params.limitPx,
|
|
92
112
|
qty: params.qty,
|
|
93
113
|
orderType: OrderType.LIMIT,
|
|
94
|
-
timeInForce:
|
|
114
|
+
timeInForce: params.timeInForce,
|
|
115
|
+
clientOrderId: params.clientOrderId,
|
|
95
116
|
signature: serializedSignature,
|
|
96
117
|
nonce: nonce.toString(),
|
|
97
118
|
signerWallet: signerAddress,
|
|
@@ -101,3 +122,121 @@ export const createSpotOrder = async (
|
|
|
101
122
|
|
|
102
123
|
return response;
|
|
103
124
|
};
|
|
125
|
+
|
|
126
|
+
export type CancelSpotOrderParams = {
|
|
127
|
+
signer: Signer | JsonRpcSigner;
|
|
128
|
+
accountId: number;
|
|
129
|
+
marketId: number;
|
|
130
|
+
symbol: string;
|
|
131
|
+
orderId?: string;
|
|
132
|
+
clientOrderId?: number;
|
|
133
|
+
expiresAfterSeconds?: number;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const cancelSpotOrder = async (
|
|
137
|
+
params: CancelSpotOrderParams,
|
|
138
|
+
): Promise<CancelOrderResponse> => {
|
|
139
|
+
if (!params.orderId && params.clientOrderId === undefined) {
|
|
140
|
+
throw new Error('Either orderId or clientOrderId must be provided');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const reyaChainId = getReyaNetwork();
|
|
144
|
+
const config = getSdkConfig();
|
|
145
|
+
|
|
146
|
+
const nowMs = Date.now();
|
|
147
|
+
const expiresAfter =
|
|
148
|
+
Math.floor(nowMs / 1000) +
|
|
149
|
+
(params.expiresAfterSeconds ?? DEFAULT_CANCEL_DEADLINE_SECONDS);
|
|
150
|
+
const nonce = BigInt(nowMs);
|
|
151
|
+
|
|
152
|
+
const useOrderId = !!params.orderId;
|
|
153
|
+
const signedOrderId = useOrderId ? BigInt(params.orderId!) : BigInt(0);
|
|
154
|
+
const signedClOrdId = useOrderId ? BigInt(0) : BigInt(params.clientOrderId!);
|
|
155
|
+
|
|
156
|
+
const { serializedSignature } = await signOrderCancel(
|
|
157
|
+
params.signer,
|
|
158
|
+
reyaChainId,
|
|
159
|
+
params.accountId,
|
|
160
|
+
params.marketId,
|
|
161
|
+
signedOrderId,
|
|
162
|
+
signedClOrdId,
|
|
163
|
+
nonce,
|
|
164
|
+
expiresAfter,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const apiConfig = new Configuration({
|
|
168
|
+
basePath: `${config.apiEndpoint}/v2`,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const orderEntryApi = new OrderEntryApi(apiConfig);
|
|
172
|
+
|
|
173
|
+
const response = await orderEntryApi.cancelOrder({
|
|
174
|
+
cancelOrderRequest: {
|
|
175
|
+
symbol: params.symbol,
|
|
176
|
+
accountId: params.accountId,
|
|
177
|
+
orderId: useOrderId ? params.orderId : undefined,
|
|
178
|
+
clientOrderId: useOrderId ? undefined : params.clientOrderId,
|
|
179
|
+
signature: serializedSignature,
|
|
180
|
+
nonce: nonce.toString(),
|
|
181
|
+
expiresAfter,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
return response;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
export type MassCancelSpotOrdersParams = {
|
|
189
|
+
signer: Signer | JsonRpcSigner;
|
|
190
|
+
accountId: number;
|
|
191
|
+
symbol?: string;
|
|
192
|
+
marketId?: number;
|
|
193
|
+
expiresAfterSeconds?: number;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export const massCancelSpotOrders = async (
|
|
197
|
+
params: MassCancelSpotOrdersParams,
|
|
198
|
+
): Promise<MassCancelResponse> => {
|
|
199
|
+
const hasSymbol = params.symbol !== undefined;
|
|
200
|
+
const hasMarketId = params.marketId !== undefined;
|
|
201
|
+
if (hasSymbol !== hasMarketId) {
|
|
202
|
+
throw new Error(
|
|
203
|
+
'symbol and marketId must be provided together (or both omitted to cancel across all markets)',
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const reyaChainId = getReyaNetwork();
|
|
208
|
+
const config = getSdkConfig();
|
|
209
|
+
|
|
210
|
+
const nowMs = Date.now();
|
|
211
|
+
const expiresAfter =
|
|
212
|
+
Math.floor(nowMs / 1000) +
|
|
213
|
+
(params.expiresAfterSeconds ?? DEFAULT_CANCEL_DEADLINE_SECONDS);
|
|
214
|
+
const nonce = BigInt(nowMs);
|
|
215
|
+
|
|
216
|
+
const { serializedSignature } = await signMassCancel(
|
|
217
|
+
params.signer,
|
|
218
|
+
reyaChainId,
|
|
219
|
+
params.accountId,
|
|
220
|
+
params.marketId ?? 0,
|
|
221
|
+
nonce,
|
|
222
|
+
expiresAfter,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const apiConfig = new Configuration({
|
|
226
|
+
basePath: `${config.apiEndpoint}/v2`,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const orderEntryApi = new OrderEntryApi(apiConfig);
|
|
230
|
+
|
|
231
|
+
const response = await orderEntryApi.cancelAll({
|
|
232
|
+
massCancelRequest: {
|
|
233
|
+
symbol: params.symbol,
|
|
234
|
+
accountId: params.accountId,
|
|
235
|
+
signature: serializedSignature,
|
|
236
|
+
nonce: nonce.toString(),
|
|
237
|
+
expiresAfter,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return response;
|
|
242
|
+
};
|