@reyaxyz/api-sdk 0.111.8 → 0.111.10

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 CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-1.72%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-7.75%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-0.64%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-1.74%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-1.7%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-7.89%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-0.63%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-1.71%25-red.svg?style=flat) |
10
10
 
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
29
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
30
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -103,39 +114,39 @@ var ConditionalOrdersClient = /** @class */ (function (_super) {
103
114
  });
104
115
  });
105
116
  };
106
- ConditionalOrdersClient.prototype.getPendingSLOrder = function (params) {
117
+ ConditionalOrdersClient.prototype.cancelSLOrder = function (params) {
107
118
  return __awaiter(this, void 0, void 0, function () {
108
- var uri, response;
119
+ var signature, uri;
109
120
  return __generator(this, function (_a) {
110
121
  switch (_a.label) {
111
- case 0:
112
- uri = "/api/conditional-orders/sl/get-orders-by-position/".concat(common_1.StopLossOrderStatus.PENDING, "/").concat(params.marketId, "/").concat(params.accountId);
113
- return [4 /*yield*/, this.get(uri)];
122
+ case 0: return [4 /*yield*/, (0, common_1.signCancelConditionalOrder)(params.signer, params.orderId)];
114
123
  case 1:
115
- response = _a.sent();
116
- if (response.length > 1) {
117
- throw new Error('Multiple SL pending orders on a single position');
118
- }
119
- if (response.length === 0)
120
- return [2 /*return*/, null];
121
- return [2 /*return*/, response[0]];
124
+ signature = _a.sent();
125
+ uri = "/api/conditional-orders/sl/cancel-order";
126
+ return [2 /*return*/, this.put(uri, {}, {
127
+ orderId: params.orderId,
128
+ userSignature: signature,
129
+ })];
122
130
  }
123
131
  });
124
132
  });
125
133
  };
126
- ConditionalOrdersClient.prototype.cancelSLOrder = function (params) {
134
+ ConditionalOrdersClient.prototype.cancelConditionalOrder = function (params) {
127
135
  return __awaiter(this, void 0, void 0, function () {
128
- var signature, uri;
136
+ var signature, uri, res;
129
137
  return __generator(this, function (_a) {
130
138
  switch (_a.label) {
131
139
  case 0: return [4 /*yield*/, (0, common_1.signCancelConditionalOrder)(params.signer, params.orderId)];
132
140
  case 1:
133
141
  signature = _a.sent();
134
142
  uri = "/api/conditional-orders/sl/cancel-order";
135
- return [2 /*return*/, this.put(uri, {}, {
143
+ return [4 /*yield*/, this.put(uri, {}, {
136
144
  orderId: params.orderId,
137
145
  userSignature: signature,
138
146
  })];
147
+ case 2:
148
+ res = _a.sent();
149
+ return [2 /*return*/, this.mapFromStopLossToConditionalOrder(res)];
139
150
  }
140
151
  });
141
152
  });
@@ -173,6 +184,42 @@ var ConditionalOrdersClient = /** @class */ (function (_super) {
173
184
  });
174
185
  });
175
186
  };
187
+ ConditionalOrdersClient.prototype.registerConditionalOrder = function (params) {
188
+ return __awaiter(this, void 0, void 0, function () {
189
+ var inputs, uri, result, _a, _b;
190
+ var _c;
191
+ return __generator(this, function (_d) {
192
+ switch (_d.label) {
193
+ case 0: return [4 /*yield*/, this.parseSlOrderInputs(params.signer, params.marginAccountId, params.marketId, params.stopLossPrice, params.supportingParams.exchangeId, params.supportingParams.counterpartyAccountIds)];
194
+ case 1:
195
+ inputs = _d.sent();
196
+ uri = "/api/conditional-orders/sl/create-order";
197
+ _a = this.post;
198
+ _b = [uri,
199
+ {}];
200
+ _c = {
201
+ accountId: params.marginAccountId,
202
+ marketId: params.marketId,
203
+ isLong: inputs.positionBase < 0,
204
+ stopPrice: params.stopLossPrice
205
+ };
206
+ return [4 /*yield*/, params.signer.getAddress()];
207
+ case 2: return [4 /*yield*/, _a.apply(this, _b.concat([(_c.signerWallet = _d.sent(),
208
+ _c.nonce = inputs.nonce.toString(),
209
+ _c.signature = inputs.signature,
210
+ _c.deadline = inputs.deadline,
211
+ _c.orderPriceLimit = inputs.orderPriceLimit.toString(),
212
+ _c.exchangeId = params.supportingParams.exchangeId,
213
+ _c.poolId = params.supportingParams.counterpartyAccountIds[0],
214
+ _c.timestampMs = inputs.creationTimestampMs,
215
+ _c)]))];
216
+ case 3:
217
+ result = _d.sent();
218
+ return [2 /*return*/, this.mapFromStopLossToConditionalOrder(result)];
219
+ }
220
+ });
221
+ });
222
+ };
176
223
  ConditionalOrdersClient.prototype.updateSLOrder = function (params) {
177
224
  return __awaiter(this, void 0, void 0, function () {
178
225
  var inputs, uri, _a, _b;
@@ -206,6 +253,42 @@ var ConditionalOrdersClient = /** @class */ (function (_super) {
206
253
  });
207
254
  });
208
255
  };
256
+ ConditionalOrdersClient.prototype.updateConditionalOrder = function (params) {
257
+ return __awaiter(this, void 0, void 0, function () {
258
+ var inputs, uri, result, _a, _b;
259
+ var _c;
260
+ return __generator(this, function (_d) {
261
+ switch (_d.label) {
262
+ case 0: return [4 /*yield*/, this.parseSlOrderInputs(params.signer, params.marginAccountId, params.marketId, params.stopLossPrice, params.supportingParams.exchangeId, params.supportingParams.counterpartyAccountIds)];
263
+ case 1:
264
+ inputs = _d.sent();
265
+ uri = "/api/conditional-orders/sl/update-order";
266
+ _a = this.post;
267
+ _b = [uri,
268
+ {}];
269
+ _c = {
270
+ accountId: params.marginAccountId,
271
+ marketId: params.marketId,
272
+ isLong: inputs.positionBase < 0,
273
+ stopPrice: params.stopLossPrice
274
+ };
275
+ return [4 /*yield*/, params.signer.getAddress()];
276
+ case 2: return [4 /*yield*/, _a.apply(this, _b.concat([(_c.signerWallet = _d.sent(),
277
+ _c.nonce = inputs.nonce.toString(),
278
+ _c.signature = inputs.signature,
279
+ _c.deadline = inputs.deadline,
280
+ _c.orderPriceLimit = inputs.orderPriceLimit.toString(),
281
+ _c.exchangeId = params.supportingParams.exchangeId,
282
+ _c.poolId = params.supportingParams.counterpartyAccountIds[0],
283
+ _c.timestampMs = inputs.creationTimestampMs,
284
+ _c)]))];
285
+ case 3:
286
+ result = _d.sent();
287
+ return [2 /*return*/, this.mapFromStopLossToConditionalOrder(result)];
288
+ }
289
+ });
290
+ });
291
+ };
209
292
  ConditionalOrdersClient.prototype.getPosition = function (accountId, marketId) {
210
293
  return __awaiter(this, void 0, void 0, function () {
211
294
  var uri;
@@ -232,7 +315,7 @@ var ConditionalOrdersClient = /** @class */ (function (_super) {
232
315
  inputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['bool', 'uint256', 'uint256'], [isLongOrder, (0, common_1.scale)(18)(stopLossPrice), orderPriceLimit]);
233
316
  creationTimestampMs = Date.now();
234
317
  nonce = (0, utils_1.createNonce)(accountId, marketId, creationTimestampMs);
235
- return [4 /*yield*/, (0, common_1.signConditionalOrder)(signer, this.reyaChainId, accountId, marketId, exchangeId, counterpartyAccountIds, types_1.ConditionalOrderType.StopLoss, inputs, nonce, common_1.CONDITIONAL_ORDER_SIG_DEADLINE)];
318
+ return [4 /*yield*/, (0, common_1.signConditionalOrder)(signer, this.reyaChainId, accountId, marketId, exchangeId, counterpartyAccountIds, types_1.ConditionalOrderTypeInContract.StopLoss, inputs, nonce, common_1.CONDITIONAL_ORDER_SIG_DEADLINE)];
236
319
  case 2:
237
320
  signature = _a.sent();
238
321
  return [2 /*return*/, {
@@ -247,6 +330,10 @@ var ConditionalOrdersClient = /** @class */ (function (_super) {
247
330
  });
248
331
  });
249
332
  };
333
+ // todo: IR deprecate after mocks are implementes
334
+ ConditionalOrdersClient.prototype.mapFromStopLossToConditionalOrder = function (order) {
335
+ return __assign(__assign({}, order), { orderType: common_1.conditionalOrderTypeNames[common_1.ConditionalOrderType.STOP_LOSS], triggerPrice: order.stopPrice });
336
+ };
250
337
  return ConditionalOrdersClient;
251
338
  }(common_1.RestClient));
252
339
  exports.default = ConditionalOrdersClient;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/conditional-orders/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAWyB;AACzB,iCAAkC;AAElC,iCAgBiB;AACjB,iCAAsC;AAEtC;IAAqD,2CAAU;IAG7D,iCAAY,WAAwB,EAAE,IAAY;QAChD,YAAA,MAAK,YAAC,IAAI,CAAC,SAAC;QACZ,KAAI,CAAC,WAAW,GAAG,WAAW,CAAC;;IACjC,CAAC;IAED,yCAAyC;IACnC,6EAA2C,GAAjD,UACE,MAAyD;;;;gBAEnD,GAAG,GAAG,wDAAiD,MAAM,CAAC,eAAe,CAAE,CAAC;gBACtF,sBAAO,IAAI,CAAC,GAAG,CAAoD,GAAG,EAAE;wBACtE,KAAK,EAAE,MAAM,CAAC,KAAK;qBACpB,CAAC,EAAC;;;KACJ;IAEK,sFAAoD,GAA1D,UACE,MAAkE;;;;gBAE5D,GAAG,GAAG,wDAAiD,MAAM,CAAC,eAAe,eAAY,CAAC;gBAChG,sBAAO,IAAI,CAAC,GAAG,CACb,GAAG,EACH;wBACE,IAAI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AC,EAAE,CAAC;4BACvB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;wBAC/C,CAAC;wBAEK,WAAW,GAAG,YAAY,GAAG,CAAC,CAAC;wBAC/B,eAAe,GAAG,IAAA,+BAAsB,EAAC,WAAW,CAAC,CAAC;wBAEtD,MAAM,GAAG,iBAAQ,CAAC,eAAe,EAAE,CAAC,MAAM,CAC9C,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,EAC9B,CAAC,WAAW,EAAE,IAAA,cAAK,EAAC,EAAE,CAAC,CAAC,aAAa,CAAC,EAAE,eAAe,CAAC,CACzD,CAAC;wBACI,mBAAmB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;wBACjC,KAAK,GAAG,IAAA,mBAAW,EAAC,SAAS,EAAE,QAAQ,EAAE,mBAAmB,CAAC,CAAC;wBAElD,qBAAM,IAAA,6BAAoB,EAC1C,MAAM,EACN,IAAI,CAAC,WAAW,EAChB,SAAS,EACT,QAAQ,EACR,UAAU,EACV,sBAAsB,EACtB,4BAAoB,CAAC,QAAQ,EAC7B,MAAM,EACN,KAAK,EACL,uCAA8B,CAC/B,EAAA;;wBAXK,SAAS,GAAG,SAWjB;wBAED,sBAAO;gCACL,SAAS,WAAA;gCACT,YAAY,cAAA;gCACZ,eAAe,iBAAA;gCACf,KAAK,OAAA;gCACL,QAAQ,EAAE,uCAA8B;gCACxC,mBAAmB,qBAAA;6BACpB,EAAC;;;;KACH;IACH,8BAAC;AAAD,CAAC,AA9MD,CAAqD,mBAAU,GA8M9D","sourcesContent":["import {\n calculateMaxPriceLimit,\n PositionEntity,\n RestClient,\n ReyaChainId,\n signConditionalOrder,\n signCancelConditionalOrder,\n StopLossOrder,\n StopLossOrderStatus,\n scale,\n CONDITIONAL_ORDER_SIG_DEADLINE,\n} from '@reyaxyz/common';\nimport { AbiCoder } from 'ethers';\nimport { Signer, JsonRpcSigner } from 'ethers';\nimport {\n AlreadyGaveTradePermissionParams,\n AlreadyGaveTradePermissionResult,\n CancelSLOrderParams,\n CancelSLOrderResult,\n ConditionalOrderType,\n GetConditionalOrdersHistoryForMarginAccountPaginatedParams,\n GetConditionalOrdersHistoryForMarginAccountPaginatedResult,\n GetConditionalOrdersHistoryForMarginAccountParams,\n GetConditionalOrdersHistoryForMarginAccountResult,\n GetPendingSLOrderParams,\n GetPendingSLOrderResult,\n RegisterSLOrderParams,\n RegisterSLOrderResult,\n UpdateSLOrderParams,\n UpdateSLOrderResult,\n} from './types';\nimport { createNonce } from './utils';\n\nexport default class ConditionalOrdersClient extends RestClient {\n private reyaChainId: ReyaChainId;\n\n constructor(reyaChainId: ReyaChainId, host: string) {\n super(host);\n this.reyaChainId = reyaChainId;\n }\n\n // ------ Conditional Orders History ---\n async getConditionalOrdersHistoryForMarginAccount(\n params: GetConditionalOrdersHistoryForMarginAccountParams,\n ): Promise<GetConditionalOrdersHistoryForMarginAccountResult> {\n const uri = `/api/conditional-orders/get-orders-by-account/${params.marginAccountId}`;\n return this.get<GetConditionalOrdersHistoryForMarginAccountResult>(uri, {\n limit: params.limit,\n });\n }\n\n async getConditionalOrdersHistoryForMarginAccountPaginated(\n params: GetConditionalOrdersHistoryForMarginAccountPaginatedParams,\n ): Promise<GetConditionalOrdersHistoryForMarginAccountPaginatedResult> {\n const uri = `/api/conditional-orders/get-orders-by-account/${params.marginAccountId}/paginated`;\n return this.get<GetConditionalOrdersHistoryForMarginAccountPaginatedResult>(\n uri,\n {\n page: params.page,\n perPage: params.perPage,\n },\n );\n }\n\n async alreadyGaveTradePermissions(\n params: AlreadyGaveTradePermissionParams,\n ): Promise<AlreadyGaveTradePermissionResult> {\n const uri = `/api/conditional-orders/gave-trade-permission/${params.accountId}`;\n const response = await this.get<boolean>(uri);\n\n return {\n permissionGiven: response,\n };\n }\n\n async getPendingSLOrder(\n params: GetPendingSLOrderParams,\n ): Promise<GetPendingSLOrderResult> {\n const uri = `/api/conditional-orders/sl/get-orders-by-position/${StopLossOrderStatus.PENDING}/${params.marketId}/${params.accountId}`;\n const response = await this.get<GetPendingSLOrderResult[]>(uri);\n\n if (response.length > 1) {\n throw new Error('Multiple SL pending orders on a single position');\n }\n\n if (response.length === 0) return null;\n\n return response[0];\n }\n\n async cancelSLOrder(\n params: CancelSLOrderParams,\n ): Promise<CancelSLOrderResult> {\n const signature = await signCancelConditionalOrder(\n params.signer,\n params.orderId,\n );\n\n const uri = `/api/conditional-orders/sl/cancel-order`;\n return this.put<StopLossOrder>(\n uri,\n {},\n {\n orderId: params.orderId,\n userSignature: signature,\n },\n );\n }\n\n async registerSLOrder(\n params: RegisterSLOrderParams,\n ): Promise<RegisterSLOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/create-order`;\n return this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n }\n\n async updateSLOrder(\n params: UpdateSLOrderParams,\n ): Promise<UpdateSLOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/update-order`;\n return this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n }\n\n private async getPosition(\n accountId: number,\n marketId: number,\n ): Promise<PositionEntity> {\n const uri = `/api/accounts/marginAccount/position/${accountId}/${marketId}`;\n return this.get<PositionEntity>(uri);\n }\n\n private async parseSlOrderInputs(\n signer: Signer | JsonRpcSigner,\n accountId: number,\n marketId: number,\n stopLossPrice: number,\n exchangeId: number,\n counterpartyAccountIds: number[],\n ): Promise<{\n signature: string;\n positionBase: number;\n orderPriceLimit: bigint;\n nonce: bigint;\n deadline: number;\n creationTimestampMs: number;\n }> {\n const position = await this.getPosition(accountId, marketId);\n const positionBase = position.base;\n\n if (positionBase === 0) {\n throw new Error('Position with no exposure');\n }\n\n const isLongOrder = positionBase < 0;\n const orderPriceLimit = calculateMaxPriceLimit(isLongOrder);\n\n const inputs = AbiCoder.defaultAbiCoder().encode(\n ['bool', 'uint256', 'uint256'],\n [isLongOrder, scale(18)(stopLossPrice), orderPriceLimit],\n );\n const creationTimestampMs = Date.now();\n const nonce = createNonce(accountId, marketId, creationTimestampMs);\n\n const signature = await signConditionalOrder(\n signer,\n this.reyaChainId,\n accountId,\n marketId,\n exchangeId,\n counterpartyAccountIds,\n ConditionalOrderType.StopLoss,\n inputs,\n nonce,\n CONDITIONAL_ORDER_SIG_DEADLINE,\n );\n\n return {\n signature,\n positionBase,\n orderPriceLimit,\n nonce,\n deadline: CONDITIONAL_ORDER_SIG_DEADLINE,\n creationTimestampMs,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["clients/modules/conditional-orders/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAayB;A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DAAiD;IACzC,mEAAiC,GAAzC,UACE,KAAoB;QAEpB,6BACK,KAAK,KACR,SAAS,EAAE,kCAAyB,CAAC,6BAAoB,CAAC,SAAS,CAAC,EACpE,YAAY,EAAE,KAAK,CAAC,SAAS,IAC7B;IACJ,CAAC;IACH,8BAAC;AAAD,CAAC,AApSD,CAAqD,mBAAU,GAoS9D","sourcesContent":["import {\n calculateMaxPriceLimit,\n PositionEntity,\n RestClient,\n ReyaChainId,\n signConditionalOrder,\n signCancelConditionalOrder,\n StopLossOrder,\n ConditionalOrder,\n ConditionalOrderType,\n scale,\n CONDITIONAL_ORDER_SIG_DEADLINE,\n conditionalOrderTypeNames,\n} from '@reyaxyz/common';\nimport { AbiCoder } from 'ethers';\nimport { Signer, JsonRpcSigner } from 'ethers';\nimport {\n AlreadyGaveTradePermissionParams,\n AlreadyGaveTradePermissionResult,\n CancelConditionalOrderParams,\n CancelConditionalOrderResult,\n CancelSLOrderParams,\n CancelSLOrderResult,\n ConditionalOrderTypeInContract,\n GetConditionalOrdersHistoryForMarginAccountPaginatedParams,\n GetConditionalOrdersHistoryForMarginAccountPaginatedResult,\n GetConditionalOrdersHistoryForMarginAccountParams,\n GetConditionalOrdersHistoryForMarginAccountResult,\n RegisterConditionalOrderParams,\n RegisterConditionalOrderResult,\n RegisterSLOrderParams,\n RegisterSLOrderResult,\n UpdateConditionalOrderParams,\n UpdateConditionalOrderResult,\n UpdateSLOrderParams,\n UpdateSLOrderResult,\n} from './types';\nimport { createNonce } from './utils';\n\nexport default class ConditionalOrdersClient extends RestClient {\n private reyaChainId: ReyaChainId;\n\n constructor(reyaChainId: ReyaChainId, host: string) {\n super(host);\n this.reyaChainId = reyaChainId;\n }\n\n // ------ Conditional Orders History ---\n async getConditionalOrdersHistoryForMarginAccount(\n params: GetConditionalOrdersHistoryForMarginAccountParams,\n ): Promise<GetConditionalOrdersHistoryForMarginAccountResult> {\n const uri = `/api/conditional-orders/get-orders-by-account/${params.marginAccountId}`;\n return this.get<GetConditionalOrdersHistoryForMarginAccountResult>(uri, {\n limit: params.limit,\n });\n }\n\n async getConditionalOrdersHistoryForMarginAccountPaginated(\n params: GetConditionalOrdersHistoryForMarginAccountPaginatedParams,\n ): Promise<GetConditionalOrdersHistoryForMarginAccountPaginatedResult> {\n const uri = `/api/conditional-orders/get-orders-by-account/${params.marginAccountId}/paginated`;\n return this.get<GetConditionalOrdersHistoryForMarginAccountPaginatedResult>(\n uri,\n {\n page: params.page,\n perPage: params.perPage,\n },\n );\n }\n\n async alreadyGaveTradePermissions(\n params: AlreadyGaveTradePermissionParams,\n ): Promise<AlreadyGaveTradePermissionResult> {\n const uri = `/api/conditional-orders/gave-trade-permission/${params.accountId}`;\n const response = await this.get<boolean>(uri);\n\n return {\n permissionGiven: response,\n };\n }\n\n async cancelSLOrder(\n params: CancelSLOrderParams,\n ): Promise<CancelSLOrderResult> {\n const signature = await signCancelConditionalOrder(\n params.signer,\n params.orderId,\n );\n\n const uri = `/api/conditional-orders/sl/cancel-order`;\n return this.put<StopLossOrder>(\n uri,\n {},\n {\n orderId: params.orderId,\n userSignature: signature,\n },\n );\n }\n\n async cancelConditionalOrder(\n params: CancelConditionalOrderParams,\n ): Promise<CancelConditionalOrderResult> {\n const signature = await signCancelConditionalOrder(\n params.signer,\n params.orderId,\n );\n\n const uri = `/api/conditional-orders/sl/cancel-order`;\n const res = await this.put<StopLossOrder>(\n uri,\n {},\n {\n orderId: params.orderId,\n userSignature: signature,\n },\n );\n return this.mapFromStopLossToConditionalOrder(res);\n }\n\n async registerSLOrder(\n params: RegisterSLOrderParams,\n ): Promise<RegisterSLOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/create-order`;\n return this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n }\n\n async registerConditionalOrder(\n params: RegisterConditionalOrderParams,\n ): Promise<RegisterConditionalOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/create-order`;\n const result = await this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n return this.mapFromStopLossToConditionalOrder(result);\n }\n\n async updateSLOrder(\n params: UpdateSLOrderParams,\n ): Promise<UpdateSLOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/update-order`;\n return this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n }\n\n async updateConditionalOrder(\n params: UpdateConditionalOrderParams,\n ): Promise<UpdateConditionalOrderResult> {\n const inputs = await this.parseSlOrderInputs(\n params.signer,\n params.marginAccountId,\n params.marketId,\n params.stopLossPrice,\n params.supportingParams.exchangeId,\n params.supportingParams.counterpartyAccountIds,\n );\n\n // create new entry\n const uri = `/api/conditional-orders/sl/update-order`;\n const result = await this.post<StopLossOrder>(\n uri,\n {},\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n isLong: inputs.positionBase < 0,\n stopPrice: params.stopLossPrice,\n signerWallet: await params.signer.getAddress(),\n nonce: inputs.nonce.toString(),\n signature: inputs.signature,\n deadline: inputs.deadline,\n orderPriceLimit: inputs.orderPriceLimit.toString(),\n exchangeId: params.supportingParams.exchangeId,\n poolId: params.supportingParams.counterpartyAccountIds[0],\n timestampMs: inputs.creationTimestampMs,\n },\n );\n return this.mapFromStopLossToConditionalOrder(result);\n }\n\n private async getPosition(\n accountId: number,\n marketId: number,\n ): Promise<PositionEntity> {\n const uri = `/api/accounts/marginAccount/position/${accountId}/${marketId}`;\n return this.get<PositionEntity>(uri);\n }\n\n private async parseSlOrderInputs(\n signer: Signer | JsonRpcSigner,\n accountId: number,\n marketId: number,\n stopLossPrice: number,\n exchangeId: number,\n counterpartyAccountIds: number[],\n ): Promise<{\n signature: string;\n positionBase: number;\n orderPriceLimit: bigint;\n nonce: bigint;\n deadline: number;\n creationTimestampMs: number;\n }> {\n const position = await this.getPosition(accountId, marketId);\n const positionBase = position.base;\n\n if (positionBase === 0) {\n throw new Error('Position with no exposure');\n }\n\n const isLongOrder = positionBase < 0;\n const orderPriceLimit = calculateMaxPriceLimit(isLongOrder);\n\n const inputs = AbiCoder.defaultAbiCoder().encode(\n ['bool', 'uint256', 'uint256'],\n [isLongOrder, scale(18)(stopLossPrice), orderPriceLimit],\n );\n const creationTimestampMs = Date.now();\n const nonce = createNonce(accountId, marketId, creationTimestampMs);\n\n const signature = await signConditionalOrder(\n signer,\n this.reyaChainId,\n accountId,\n marketId,\n exchangeId,\n counterpartyAccountIds,\n ConditionalOrderTypeInContract.StopLoss,\n inputs,\n nonce,\n CONDITIONAL_ORDER_SIG_DEADLINE,\n );\n\n return {\n signature,\n positionBase,\n orderPriceLimit,\n nonce,\n deadline: CONDITIONAL_ORDER_SIG_DEADLINE,\n creationTimestampMs,\n };\n }\n\n // todo: IR deprecate after mocks are implementes\n private mapFromStopLossToConditionalOrder(\n order: StopLossOrder,\n ): ConditionalOrder {\n return {\n ...order,\n orderType: conditionalOrderTypeNames[ConditionalOrderType.STOP_LOSS],\n triggerPrice: order.stopPrice,\n };\n }\n}\n"]}
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConditionalOrderType = void 0;
4
- var ConditionalOrderType;
5
- (function (ConditionalOrderType) {
6
- ConditionalOrderType[ConditionalOrderType["StopLoss"] = 0] = "StopLoss";
7
- })(ConditionalOrderType || (exports.ConditionalOrderType = ConditionalOrderType = {}));
3
+ exports.ConditionalOrderTypeInContract = void 0;
4
+ var ConditionalOrderTypeInContract;
5
+ (function (ConditionalOrderTypeInContract) {
6
+ ConditionalOrderTypeInContract[ConditionalOrderTypeInContract["StopLoss"] = 0] = "StopLoss";
7
+ })(ConditionalOrderTypeInContract || (exports.ConditionalOrderTypeInContract = ConditionalOrderTypeInContract = {}));
8
8
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"/","sources":["clients/modules/conditional-orders/types.ts"],"names":[],"mappings":";;;AAkBA,IAAY,oBAEX;AAFD,WAAY,oBAAoB;IAC9B,uEAAc,CAAA;AAChB,CAAC,EAFW,oBAAoB,oCAApB,oBAAoB,QAE/B","sourcesContent":["import { StopLossOrder, UnifiedConditionalOrderType } from '@reyaxyz/common';\nimport { Signer, JsonRpcSigner } from 'ethers';\nimport { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';\n\nexport type CancelSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n orderId: string;\n};\n\nexport type CancelSLOrderResult = StopLossOrder;\n\nexport type GetPendingSLOrderParams = {\n accountId: number;\n marketId: number;\n};\n\nexport type GetPendingSLOrderResult = StopLossOrder | null;\n\nexport enum ConditionalOrderType {\n 'StopLoss' = 0,\n}\n\nexport type ConditionalOrderDetails = {\n accountId: number;\n marketId: number;\n exchangeId: number;\n counterpartyAccountIds: number[];\n orderType: number;\n inputs: string;\n signer: string;\n nonce: bigint;\n};\n\nexport type OrderSupportingParams = {\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n currentPrice: number;\n};\n\nexport type RegisterSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\nexport type UpdateSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\nexport type RegisterSLOrderResult = StopLossOrder;\nexport type UpdateSLOrderResult = StopLossOrder;\n\nexport type AlreadyGaveTradePermissionParams = {\n accountId: number;\n};\n\nexport type AlreadyGaveTradePermissionResult = {\n permissionGiven: boolean;\n};\n\n// ---- Conditional Orders History ---\nexport type GetConditionalOrdersHistoryForMarginAccountParams = {\n address: string;\n marginAccountId: MarginAccountEntity['id'];\n limit?: number;\n};\nexport type GetConditionalOrdersHistoryForMarginAccountPaginatedParams = {\n address: string;\n marginAccountId: MarginAccountEntity['id'];\n page: number;\n perPage: number;\n};\n\nexport type GetConditionalOrdersHistoryForMarginAccountResult =\n UnifiedConditionalOrderType[];\nexport type GetConditionalOrdersHistoryForMarginAccountPaginatedResult = {\n data: UnifiedConditionalOrderType[];\n totalCount: number;\n};\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"/","sources":["clients/modules/conditional-orders/types.ts"],"names":[],"mappings":";;;AAwBA,IAAY,8BAEX;AAFD,WAAY,8BAA8B;IACxC,2FAAc,CAAA;AAChB,CAAC,EAFW,8BAA8B,8CAA9B,8BAA8B,QAEzC","sourcesContent":["import {\n StopLossOrder,\n ConditionalOrder,\n UnifiedConditionalOrderType,\n} from '@reyaxyz/common';\nimport { Signer, JsonRpcSigner } from 'ethers';\nimport { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';\n\n// todo: IR to deprecate\nexport type CancelSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n orderId: string;\n};\n\n// todo: IR to deprecate\nexport type CancelSLOrderResult = StopLossOrder;\n\nexport type CancelConditionalOrderParams = {\n signer: Signer | JsonRpcSigner;\n orderId: string;\n};\n\nexport type CancelConditionalOrderResult = ConditionalOrder;\n\nexport enum ConditionalOrderTypeInContract {\n 'StopLoss' = 0,\n}\n\nexport type ConditionalOrderDetails = {\n accountId: number;\n marketId: number;\n exchangeId: number;\n counterpartyAccountIds: number[];\n orderType: number;\n inputs: string;\n signer: string;\n nonce: bigint;\n};\n\nexport type OrderSupportingParams = {\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n currentPrice: number;\n};\n\n// todo: IR to deprecate\nexport type RegisterSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\nexport type RegisterConditionalOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\n// todo: IR to deprecate\nexport type UpdateSLOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\nexport type UpdateConditionalOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: MarginAccountEntity['id'];\n stopLossPrice: number;\n marketId: number;\n supportingParams: OrderSupportingParams;\n};\n\n// todo: IR to deprecate\nexport type RegisterSLOrderResult = StopLossOrder;\nexport type UpdateSLOrderResult = StopLossOrder;\n\nexport type RegisterConditionalOrderResult = ConditionalOrder;\nexport type UpdateConditionalOrderResult = ConditionalOrder;\n\nexport type AlreadyGaveTradePermissionParams = {\n accountId: number;\n};\n\nexport type AlreadyGaveTradePermissionResult = {\n permissionGiven: boolean;\n};\n\n// ---- Conditional Orders History ---\nexport type GetConditionalOrdersHistoryForMarginAccountParams = {\n address: string;\n marginAccountId: MarginAccountEntity['id'];\n limit?: number;\n};\nexport type GetConditionalOrdersHistoryForMarginAccountPaginatedParams = {\n address: string;\n marginAccountId: MarginAccountEntity['id'];\n page: number;\n perPage: number;\n};\n\nexport type GetConditionalOrdersHistoryForMarginAccountResult =\n UnifiedConditionalOrderType[];\nexport type GetConditionalOrdersHistoryForMarginAccountPaginatedResult = {\n data: UnifiedConditionalOrderType[];\n totalCount: number;\n};\n"]}
@@ -1,16 +1,19 @@
1
1
  import { RestClient, ReyaChainId } from '@reyaxyz/common';
2
- import { AlreadyGaveTradePermissionParams, AlreadyGaveTradePermissionResult, CancelSLOrderParams, CancelSLOrderResult, GetConditionalOrdersHistoryForMarginAccountPaginatedParams, GetConditionalOrdersHistoryForMarginAccountPaginatedResult, GetConditionalOrdersHistoryForMarginAccountParams, GetConditionalOrdersHistoryForMarginAccountResult, GetPendingSLOrderParams, GetPendingSLOrderResult, RegisterSLOrderParams, RegisterSLOrderResult, UpdateSLOrderParams, UpdateSLOrderResult } from './types';
2
+ import { AlreadyGaveTradePermissionParams, AlreadyGaveTradePermissionResult, CancelConditionalOrderParams, CancelConditionalOrderResult, CancelSLOrderParams, CancelSLOrderResult, GetConditionalOrdersHistoryForMarginAccountPaginatedParams, GetConditionalOrdersHistoryForMarginAccountPaginatedResult, GetConditionalOrdersHistoryForMarginAccountParams, GetConditionalOrdersHistoryForMarginAccountResult, RegisterConditionalOrderParams, RegisterConditionalOrderResult, RegisterSLOrderParams, RegisterSLOrderResult, UpdateConditionalOrderParams, UpdateConditionalOrderResult, UpdateSLOrderParams, UpdateSLOrderResult } from './types';
3
3
  export default class ConditionalOrdersClient extends RestClient {
4
4
  private reyaChainId;
5
5
  constructor(reyaChainId: ReyaChainId, host: string);
6
6
  getConditionalOrdersHistoryForMarginAccount(params: GetConditionalOrdersHistoryForMarginAccountParams): Promise<GetConditionalOrdersHistoryForMarginAccountResult>;
7
7
  getConditionalOrdersHistoryForMarginAccountPaginated(params: GetConditionalOrdersHistoryForMarginAccountPaginatedParams): Promise<GetConditionalOrdersHistoryForMarginAccountPaginatedResult>;
8
8
  alreadyGaveTradePermissions(params: AlreadyGaveTradePermissionParams): Promise<AlreadyGaveTradePermissionResult>;
9
- getPendingSLOrder(params: GetPendingSLOrderParams): Promise<GetPendingSLOrderResult>;
10
9
  cancelSLOrder(params: CancelSLOrderParams): Promise<CancelSLOrderResult>;
10
+ cancelConditionalOrder(params: CancelConditionalOrderParams): Promise<CancelConditionalOrderResult>;
11
11
  registerSLOrder(params: RegisterSLOrderParams): Promise<RegisterSLOrderResult>;
12
+ registerConditionalOrder(params: RegisterConditionalOrderParams): Promise<RegisterConditionalOrderResult>;
12
13
  updateSLOrder(params: UpdateSLOrderParams): Promise<UpdateSLOrderResult>;
14
+ updateConditionalOrder(params: UpdateConditionalOrderParams): Promise<UpdateConditionalOrderResult>;
13
15
  private getPosition;
14
16
  private parseSlOrderInputs;
17
+ private mapFromStopLossToConditionalOrder;
15
18
  }
16
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["clients/modules/conditional-orders/index.ts"],"names":[],"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,EACnB,mBAAmB,EAEnB,0DAA0D,EAC1D,0DAA0D,EAC1D,iDAAiD,EACjD,iDAAiD,EACjD,8BAA8B,EAC9B,8BAA8B,EAC9B,qBAAqB,EACrB,qBAAqB,EACrB,4BAA4B,EAC5B,4BAA4B,EAC5B,mBAAmB,EACnB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAGjB,MAAM,CAAC,OAAO,OAAO,uBAAwB,SAAQ,UAAU;IAC7D,OAAO,CAAC,WAAW,CAAc;gBAErB,WAAW,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM;IAM5C,2CAA2C,CAC/C,MAAM,EAAE,iDAAiD,GACxD,OAAO,CAAC,iDAAiD,CAAC;IAOvD,oDAAoD,CACxD,MAAM,EAAE,0DAA0D,GACjE,OAAO,CAAC,0DAA0D,CAAC;IAWhE,2BAA2B,CAC/B,MAAM,EAAE,gCAAgC,GACvC,OAAO,CAAC,gCAAgC,CAAC;IAStC,aAAa,CACjB,MAAM,EAAE,mBAAmB,GAC1B,OAAO,CAAC,mBAAmB,CAAC;IAiBzB,sBAAsB,CAC1B,MAAM,EAAE,4BAA4B,GACnC,OAAO,CAAC,4BAA4B,CAAC;IAkBlC,eAAe,CACnB,MAAM,EAAE,qBAAqB,GAC5B,OAAO,CAAC,qBAAqB,CAAC;IAgC3B,wBAAwB,CAC5B,MAAM,EAAE,8BAA8B,GACrC,OAAO,CAAC,8BAA8B,CAAC;IAiCpC,aAAa,CACjB,MAAM,EAAE,mBAAmB,GAC1B,OAAO,CAAC,mBAAmB,CAAC;IAgCzB,sBAAsB,CAC1B,MAAM,EAAE,4BAA4B,GACnC,OAAO,CAAC,4BAA4B,CAAC;YAiC1B,WAAW;YAQX,kBAAkB;IAwDhC,OAAO,CAAC,iCAAiC;CAS1C"}
@@ -1,4 +1,4 @@
1
- import { StopLossOrder, UnifiedConditionalOrderType } from '@reyaxyz/common';
1
+ import { StopLossOrder, ConditionalOrder, UnifiedConditionalOrderType } from '@reyaxyz/common';
2
2
  import { Signer, JsonRpcSigner } from 'ethers';
3
3
  import { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';
4
4
  export type CancelSLOrderParams = {
@@ -6,12 +6,12 @@ export type CancelSLOrderParams = {
6
6
  orderId: string;
7
7
  };
8
8
  export type CancelSLOrderResult = StopLossOrder;
9
- export type GetPendingSLOrderParams = {
10
- accountId: number;
11
- marketId: number;
9
+ export type CancelConditionalOrderParams = {
10
+ signer: Signer | JsonRpcSigner;
11
+ orderId: string;
12
12
  };
13
- export type GetPendingSLOrderResult = StopLossOrder | null;
14
- export declare enum ConditionalOrderType {
13
+ export type CancelConditionalOrderResult = ConditionalOrder;
14
+ export declare enum ConditionalOrderTypeInContract {
15
15
  'StopLoss' = 0
16
16
  }
17
17
  export type ConditionalOrderDetails = {
@@ -36,6 +36,13 @@ export type RegisterSLOrderParams = {
36
36
  marketId: number;
37
37
  supportingParams: OrderSupportingParams;
38
38
  };
39
+ export type RegisterConditionalOrderParams = {
40
+ signer: Signer | JsonRpcSigner;
41
+ marginAccountId: MarginAccountEntity['id'];
42
+ stopLossPrice: number;
43
+ marketId: number;
44
+ supportingParams: OrderSupportingParams;
45
+ };
39
46
  export type UpdateSLOrderParams = {
40
47
  signer: Signer | JsonRpcSigner;
41
48
  marginAccountId: MarginAccountEntity['id'];
@@ -43,8 +50,17 @@ export type UpdateSLOrderParams = {
43
50
  marketId: number;
44
51
  supportingParams: OrderSupportingParams;
45
52
  };
53
+ export type UpdateConditionalOrderParams = {
54
+ signer: Signer | JsonRpcSigner;
55
+ marginAccountId: MarginAccountEntity['id'];
56
+ stopLossPrice: number;
57
+ marketId: number;
58
+ supportingParams: OrderSupportingParams;
59
+ };
46
60
  export type RegisterSLOrderResult = StopLossOrder;
47
61
  export type UpdateSLOrderResult = StopLossOrder;
62
+ export type RegisterConditionalOrderResult = ConditionalOrder;
63
+ export type UpdateConditionalOrderResult = ConditionalOrder;
48
64
  export type AlreadyGaveTradePermissionParams = {
49
65
  accountId: number;
50
66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BAAmB,CAAC,IAAI,CAAC,CAAC;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AACF,MAAM,MAAM,0DAA0D,GAAG;IACvE,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,EAAE,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,iDAAiD,GAC3D,2BAA2B,EAAE,CAAC;AAChC,MAAM,MAAM,0DAA0D,GAAG;IACvE,IAAI,EAAE,2BAA2B,EAAE,CAAC;IACpC,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/api-sdk",
3
- "version": "0.111.8",
3
+ "version": "0.111.10",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -33,14 +33,14 @@
33
33
  "generate:coverage-badges": "npx istanbul-badges-readme --silent"
34
34
  },
35
35
  "dependencies": {
36
- "@reyaxyz/common": "0.167.0",
36
+ "@reyaxyz/common": "0.168.1",
37
37
  "bignumber.js": "^9.1.2",
38
38
  "ethers": "6.9.0",
39
39
  "isomorphic-ws": "^5.0.0",
40
40
  "ws": "^8.16.0"
41
41
  },
42
42
  "packageManager": "pnpm@8.3.1",
43
- "gitHead": "514a1f7fee84d39dfbeab8f8def9f5b98f01af0b",
43
+ "gitHead": "4cca344dcbcba17409a4c7fd098e6a18fec015b7",
44
44
  "devDependencies": {
45
45
  "@types/ws": "8.5.10"
46
46
  }
@@ -6,26 +6,32 @@ import {
6
6
  signConditionalOrder,
7
7
  signCancelConditionalOrder,
8
8
  StopLossOrder,
9
- StopLossOrderStatus,
9
+ ConditionalOrder,
10
+ ConditionalOrderType,
10
11
  scale,
11
12
  CONDITIONAL_ORDER_SIG_DEADLINE,
13
+ conditionalOrderTypeNames,
12
14
  } from '@reyaxyz/common';
13
15
  import { AbiCoder } from 'ethers';
14
16
  import { Signer, JsonRpcSigner } from 'ethers';
15
17
  import {
16
18
  AlreadyGaveTradePermissionParams,
17
19
  AlreadyGaveTradePermissionResult,
20
+ CancelConditionalOrderParams,
21
+ CancelConditionalOrderResult,
18
22
  CancelSLOrderParams,
19
23
  CancelSLOrderResult,
20
- ConditionalOrderType,
24
+ ConditionalOrderTypeInContract,
21
25
  GetConditionalOrdersHistoryForMarginAccountPaginatedParams,
22
26
  GetConditionalOrdersHistoryForMarginAccountPaginatedResult,
23
27
  GetConditionalOrdersHistoryForMarginAccountParams,
24
28
  GetConditionalOrdersHistoryForMarginAccountResult,
25
- GetPendingSLOrderParams,
26
- GetPendingSLOrderResult,
29
+ RegisterConditionalOrderParams,
30
+ RegisterConditionalOrderResult,
27
31
  RegisterSLOrderParams,
28
32
  RegisterSLOrderResult,
33
+ UpdateConditionalOrderParams,
34
+ UpdateConditionalOrderResult,
29
35
  UpdateSLOrderParams,
30
36
  UpdateSLOrderResult,
31
37
  } from './types';
@@ -73,21 +79,6 @@ export default class ConditionalOrdersClient extends RestClient {
73
79
  };
74
80
  }
75
81
 
76
- async getPendingSLOrder(
77
- params: GetPendingSLOrderParams,
78
- ): Promise<GetPendingSLOrderResult> {
79
- const uri = `/api/conditional-orders/sl/get-orders-by-position/${StopLossOrderStatus.PENDING}/${params.marketId}/${params.accountId}`;
80
- const response = await this.get<GetPendingSLOrderResult[]>(uri);
81
-
82
- if (response.length > 1) {
83
- throw new Error('Multiple SL pending orders on a single position');
84
- }
85
-
86
- if (response.length === 0) return null;
87
-
88
- return response[0];
89
- }
90
-
91
82
  async cancelSLOrder(
92
83
  params: CancelSLOrderParams,
93
84
  ): Promise<CancelSLOrderResult> {
@@ -107,6 +98,26 @@ export default class ConditionalOrdersClient extends RestClient {
107
98
  );
108
99
  }
109
100
 
101
+ async cancelConditionalOrder(
102
+ params: CancelConditionalOrderParams,
103
+ ): Promise<CancelConditionalOrderResult> {
104
+ const signature = await signCancelConditionalOrder(
105
+ params.signer,
106
+ params.orderId,
107
+ );
108
+
109
+ const uri = `/api/conditional-orders/sl/cancel-order`;
110
+ const res = await this.put<StopLossOrder>(
111
+ uri,
112
+ {},
113
+ {
114
+ orderId: params.orderId,
115
+ userSignature: signature,
116
+ },
117
+ );
118
+ return this.mapFromStopLossToConditionalOrder(res);
119
+ }
120
+
110
121
  async registerSLOrder(
111
122
  params: RegisterSLOrderParams,
112
123
  ): Promise<RegisterSLOrderResult> {
@@ -141,6 +152,41 @@ export default class ConditionalOrdersClient extends RestClient {
141
152
  );
142
153
  }
143
154
 
155
+ async registerConditionalOrder(
156
+ params: RegisterConditionalOrderParams,
157
+ ): Promise<RegisterConditionalOrderResult> {
158
+ const inputs = await this.parseSlOrderInputs(
159
+ params.signer,
160
+ params.marginAccountId,
161
+ params.marketId,
162
+ params.stopLossPrice,
163
+ params.supportingParams.exchangeId,
164
+ params.supportingParams.counterpartyAccountIds,
165
+ );
166
+
167
+ // create new entry
168
+ const uri = `/api/conditional-orders/sl/create-order`;
169
+ const result = await this.post<StopLossOrder>(
170
+ uri,
171
+ {},
172
+ {
173
+ accountId: params.marginAccountId,
174
+ marketId: params.marketId,
175
+ isLong: inputs.positionBase < 0,
176
+ stopPrice: params.stopLossPrice,
177
+ signerWallet: await params.signer.getAddress(),
178
+ nonce: inputs.nonce.toString(),
179
+ signature: inputs.signature,
180
+ deadline: inputs.deadline,
181
+ orderPriceLimit: inputs.orderPriceLimit.toString(),
182
+ exchangeId: params.supportingParams.exchangeId,
183
+ poolId: params.supportingParams.counterpartyAccountIds[0],
184
+ timestampMs: inputs.creationTimestampMs,
185
+ },
186
+ );
187
+ return this.mapFromStopLossToConditionalOrder(result);
188
+ }
189
+
144
190
  async updateSLOrder(
145
191
  params: UpdateSLOrderParams,
146
192
  ): Promise<UpdateSLOrderResult> {
@@ -175,6 +221,41 @@ export default class ConditionalOrdersClient extends RestClient {
175
221
  );
176
222
  }
177
223
 
224
+ async updateConditionalOrder(
225
+ params: UpdateConditionalOrderParams,
226
+ ): Promise<UpdateConditionalOrderResult> {
227
+ const inputs = await this.parseSlOrderInputs(
228
+ params.signer,
229
+ params.marginAccountId,
230
+ params.marketId,
231
+ params.stopLossPrice,
232
+ params.supportingParams.exchangeId,
233
+ params.supportingParams.counterpartyAccountIds,
234
+ );
235
+
236
+ // create new entry
237
+ const uri = `/api/conditional-orders/sl/update-order`;
238
+ const result = await this.post<StopLossOrder>(
239
+ uri,
240
+ {},
241
+ {
242
+ accountId: params.marginAccountId,
243
+ marketId: params.marketId,
244
+ isLong: inputs.positionBase < 0,
245
+ stopPrice: params.stopLossPrice,
246
+ signerWallet: await params.signer.getAddress(),
247
+ nonce: inputs.nonce.toString(),
248
+ signature: inputs.signature,
249
+ deadline: inputs.deadline,
250
+ orderPriceLimit: inputs.orderPriceLimit.toString(),
251
+ exchangeId: params.supportingParams.exchangeId,
252
+ poolId: params.supportingParams.counterpartyAccountIds[0],
253
+ timestampMs: inputs.creationTimestampMs,
254
+ },
255
+ );
256
+ return this.mapFromStopLossToConditionalOrder(result);
257
+ }
258
+
178
259
  private async getPosition(
179
260
  accountId: number,
180
261
  marketId: number,
@@ -222,7 +303,7 @@ export default class ConditionalOrdersClient extends RestClient {
222
303
  marketId,
223
304
  exchangeId,
224
305
  counterpartyAccountIds,
225
- ConditionalOrderType.StopLoss,
306
+ ConditionalOrderTypeInContract.StopLoss,
226
307
  inputs,
227
308
  nonce,
228
309
  CONDITIONAL_ORDER_SIG_DEADLINE,
@@ -237,4 +318,15 @@ export default class ConditionalOrdersClient extends RestClient {
237
318
  creationTimestampMs,
238
319
  };
239
320
  }
321
+
322
+ // todo: IR deprecate after mocks are implementes
323
+ private mapFromStopLossToConditionalOrder(
324
+ order: StopLossOrder,
325
+ ): ConditionalOrder {
326
+ return {
327
+ ...order,
328
+ orderType: conditionalOrderTypeNames[ConditionalOrderType.STOP_LOSS],
329
+ triggerPrice: order.stopPrice,
330
+ };
331
+ }
240
332
  }
@@ -1,22 +1,28 @@
1
- import { StopLossOrder, UnifiedConditionalOrderType } from '@reyaxyz/common';
1
+ import {
2
+ StopLossOrder,
3
+ ConditionalOrder,
4
+ UnifiedConditionalOrderType,
5
+ } from '@reyaxyz/common';
2
6
  import { Signer, JsonRpcSigner } from 'ethers';
3
7
  import { MarginAccountEntity, MarketEntity } from '@reyaxyz/common';
4
8
 
9
+ // todo: IR to deprecate
5
10
  export type CancelSLOrderParams = {
6
11
  signer: Signer | JsonRpcSigner;
7
12
  orderId: string;
8
13
  };
9
14
 
15
+ // todo: IR to deprecate
10
16
  export type CancelSLOrderResult = StopLossOrder;
11
17
 
12
- export type GetPendingSLOrderParams = {
13
- accountId: number;
14
- marketId: number;
18
+ export type CancelConditionalOrderParams = {
19
+ signer: Signer | JsonRpcSigner;
20
+ orderId: string;
15
21
  };
16
22
 
17
- export type GetPendingSLOrderResult = StopLossOrder | null;
23
+ export type CancelConditionalOrderResult = ConditionalOrder;
18
24
 
19
- export enum ConditionalOrderType {
25
+ export enum ConditionalOrderTypeInContract {
20
26
  'StopLoss' = 0,
21
27
  }
22
28
 
@@ -37,6 +43,7 @@ export type OrderSupportingParams = {
37
43
  currentPrice: number;
38
44
  };
39
45
 
46
+ // todo: IR to deprecate
40
47
  export type RegisterSLOrderParams = {
41
48
  signer: Signer | JsonRpcSigner;
42
49
  marginAccountId: MarginAccountEntity['id'];
@@ -45,6 +52,15 @@ export type RegisterSLOrderParams = {
45
52
  supportingParams: OrderSupportingParams;
46
53
  };
47
54
 
55
+ export type RegisterConditionalOrderParams = {
56
+ signer: Signer | JsonRpcSigner;
57
+ marginAccountId: MarginAccountEntity['id'];
58
+ stopLossPrice: number;
59
+ marketId: number;
60
+ supportingParams: OrderSupportingParams;
61
+ };
62
+
63
+ // todo: IR to deprecate
48
64
  export type UpdateSLOrderParams = {
49
65
  signer: Signer | JsonRpcSigner;
50
66
  marginAccountId: MarginAccountEntity['id'];
@@ -53,9 +69,21 @@ export type UpdateSLOrderParams = {
53
69
  supportingParams: OrderSupportingParams;
54
70
  };
55
71
 
72
+ export type UpdateConditionalOrderParams = {
73
+ signer: Signer | JsonRpcSigner;
74
+ marginAccountId: MarginAccountEntity['id'];
75
+ stopLossPrice: number;
76
+ marketId: number;
77
+ supportingParams: OrderSupportingParams;
78
+ };
79
+
80
+ // todo: IR to deprecate
56
81
  export type RegisterSLOrderResult = StopLossOrder;
57
82
  export type UpdateSLOrderResult = StopLossOrder;
58
83
 
84
+ export type RegisterConditionalOrderResult = ConditionalOrder;
85
+ export type UpdateConditionalOrderResult = ConditionalOrder;
86
+
59
87
  export type AlreadyGaveTradePermissionParams = {
60
88
  accountId: number;
61
89
  };