@reyaxyz/sdk 0.139.5 → 0.140.1

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-9.19%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-6.06%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-4%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-8.85%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-9.14%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-6.06%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-3.89%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-8.8%25-red.svg?style=flat) |
10
10
 
@@ -48,15 +48,14 @@ var common_3 = require("@reyaxyz/common");
48
48
  var network_1 = require("../../utils/network");
49
49
  var ethers_1 = require("ethers");
50
50
  var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(void 0, void 0, void 0, function () {
51
- var inputs, reyaChainId, creationTimestampMs, creationTimestampSeconds, _a, signature, nonce, fnSig, fnArgs, _b, ogInterface, data, result;
51
+ var reyaChainId, creationTimestampMs, creationTimestampSeconds, _a, signature, nonce, fnSig, fnArgs, _b, ogInterface, data, result;
52
52
  return __generator(this, function (_c) {
53
53
  switch (_c.label) {
54
54
  case 0:
55
- inputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [params.order.baseE18, params.order.priceLimitE18]);
56
55
  reyaChainId = (0, network_1.getReyaNetwork)();
57
56
  creationTimestampMs = Date.now();
58
57
  creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);
59
- return [4 /*yield*/, (0, common_1.signOrdersGatewayOrder)(params.signer, reyaChainId, params.marginAccountId, params.marketId, params.exchangeId, params.counterpartyAccountIds, params.order.type, inputs, creationTimestampSeconds + common_1.ORDERS_GATEWAY_DEADLINE_IN_SECONDS, creationTimestampMs)];
58
+ return [4 /*yield*/, (0, common_1.signOrdersGatewayOrder)(params.signer, reyaChainId, params.marginAccountId, params.marketId, params.exchangeId, params.counterpartyAccountIds, params.orderType, params.orderInputs, creationTimestampSeconds + common_1.ORDERS_GATEWAY_DEADLINE_IN_SECONDS, creationTimestampMs)];
60
59
  case 1:
61
60
  _a = _c.sent(), signature = _a.signature, nonce = _a.nonce;
62
61
  fnSig = 'execute';
@@ -64,8 +63,8 @@ var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(vo
64
63
  params.marketId,
65
64
  params.exchangeId,
66
65
  params.counterpartyAccountIds,
67
- params.order.type,
68
- inputs];
66
+ params.orderType,
67
+ params.orderInputs];
69
68
  return [4 /*yield*/, params.signer.getAddress()];
70
69
  case 2:
71
70
  fnArgs = [
@@ -90,7 +89,7 @@ var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(vo
90
89
  });
91
90
  }); };
92
91
  var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
93
- var snappedAmount, orderBaseE18, orderPriceLimitE18, result;
92
+ var snappedAmount, orderBaseE18, orderPriceLimitE18, orderInputs, result;
94
93
  var _a, _b, _c, _d, _e, _f;
95
94
  return __generator(this, function (_g) {
96
95
  switch (_g.label) {
@@ -113,6 +112,7 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
113
112
  }
114
113
  orderBaseE18 = (0, common_3.scale)(18)(snappedAmount);
115
114
  orderPriceLimitE18 = (0, common_1.calculateMaxPriceLimit)(snappedAmount > 0);
115
+ orderInputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [orderBaseE18, orderPriceLimitE18]);
116
116
  return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
117
117
  signer: params.signer,
118
118
  marginAccountId: params.marginAccountId,
@@ -120,11 +120,8 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
120
120
  marketId: params.market.id,
121
121
  exchangeId: params.market.exchangeId,
122
122
  tradeSource: params.tradeSource,
123
- order: {
124
- baseE18: orderBaseE18,
125
- priceLimitE18: orderPriceLimitE18,
126
- type: common_1.OrdersGatewayOrderType.MARKET_ORDER,
127
- },
123
+ orderType: common_1.OrdersGatewayOrderType.MARKET_ORDER,
124
+ orderInputs: orderInputs,
128
125
  })];
129
126
  case 1:
130
127
  result = _g.sent();
@@ -146,56 +143,86 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
146
143
  });
147
144
  }); };
148
145
  exports.matchOrder = matchOrder;
149
- var closeOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
150
- var closingBase, orderPriceLimit, result;
151
- var _a, _b, _c, _d, _e, _f;
152
- return __generator(this, function (_g) {
153
- switch (_g.label) {
146
+ var fullCloseOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
147
+ var result;
148
+ return __generator(this, function (_a) {
149
+ switch (_a.label) {
150
+ case 0: return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
151
+ signer: params.signer,
152
+ marginAccountId: params.marginAccountId,
153
+ counterpartyAccountIds: params.market.counterpartyAccountIds,
154
+ marketId: params.market.id,
155
+ exchangeId: params.market.exchangeId,
156
+ orderType: common_1.OrdersGatewayOrderType.FULL_CLOSE_ORDER,
157
+ orderInputs: ethers_1.AbiCoder.defaultAbiCoder().encode([], []),
158
+ })];
159
+ case 1:
160
+ result = _a.sent();
161
+ return [2 /*return*/, result];
162
+ }
163
+ });
164
+ }); };
165
+ var partialCloseOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
166
+ var orderPriceLimit, orderInputs, result;
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
154
169
  case 0:
155
170
  if (params.orderBase === 0) {
156
171
  throw new Error('Position is already closed.');
157
172
  }
158
- closingBase = 0;
159
- if (params.type === 'full') {
160
- closingBase = params.orderBase;
173
+ if (params.closingOrderBase === 0) {
174
+ throw new Error('Specified base to close is zero.');
175
+ }
176
+ if (params.closingOrderBase * params.orderBase < 0) {
177
+ throw new Error('Specified base to close is in different direction that the actual order.');
161
178
  }
162
- else {
163
- closingBase = params.closingOrderBase;
164
- if (closingBase === 0) {
165
- throw new Error('Specified base to close is zero.');
166
- }
167
- if (closingBase * params.orderBase < 0) {
168
- throw new Error('Specified base to close is in different direction that the actual order.');
169
- }
170
- if (Math.abs(closingBase) > Math.abs(params.orderBase)) {
171
- throw new Error('Specified base to close is larger than the actual order.');
172
- }
179
+ if (Math.abs(params.closingOrderBase) > Math.abs(params.orderBase)) {
180
+ throw new Error('Specified base to close is larger than the actual order.');
173
181
  }
174
- orderPriceLimit = (0, common_1.calculateMaxPriceLimit)(closingBase < 0);
182
+ orderPriceLimit = (0, common_1.calculateMaxPriceLimit)(params.closingOrderBase < 0);
183
+ orderInputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [-(0, common_3.scale)(18)(params.closingOrderBase), orderPriceLimit]);
175
184
  return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
176
185
  signer: params.signer,
177
186
  marginAccountId: params.marginAccountId,
178
187
  counterpartyAccountIds: params.market.counterpartyAccountIds,
179
188
  marketId: params.market.id,
180
189
  exchangeId: params.market.exchangeId,
181
- order: {
182
- baseE18: -(0, common_3.scale)(18)(closingBase),
183
- priceLimitE18: orderPriceLimit,
184
- type: common_1.OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
185
- },
190
+ orderType: common_1.OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
191
+ orderInputs: orderInputs,
186
192
  })];
187
193
  case 1:
188
- result = _g.sent();
194
+ result = _a.sent();
195
+ return [2 /*return*/, result];
196
+ }
197
+ });
198
+ }); };
199
+ var closeOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
200
+ var result, _a;
201
+ var _b, _c, _d, _e, _f, _g;
202
+ return __generator(this, function (_h) {
203
+ switch (_h.label) {
204
+ case 0:
205
+ if (!(params.type === 'full')) return [3 /*break*/, 2];
206
+ return [4 /*yield*/, fullCloseOrder(params)];
207
+ case 1:
208
+ _a = _h.sent();
209
+ return [3 /*break*/, 4];
210
+ case 2: return [4 /*yield*/, partialCloseOrder(params)];
211
+ case 3:
212
+ _a = _h.sent();
213
+ _h.label = 4;
214
+ case 4:
215
+ result = _a;
189
216
  return [2 /*return*/, {
190
217
  transactionHash: (result === null || result === void 0 ? void 0 : result.txHash) || null,
191
218
  coreSigNonce: Number(result === null || result === void 0 ? void 0 : result.coreSigNonce) || null,
192
- xpBoost: ((_a = result.miscellaneous) === null || _a === void 0 ? void 0 : _a.tradeXpBoost) || 0,
193
- lotteryXpBoost: ((_b = result.miscellaneous) === null || _b === void 0 ? void 0 : _b.lotteryXpBoost) || 0,
194
- isNftWon: ((_c = result.miscellaneous) === null || _c === void 0 ? void 0 : _c.xpNftAwarded) || false,
219
+ xpBoost: ((_b = result.miscellaneous) === null || _b === void 0 ? void 0 : _b.tradeXpBoost) || 0,
220
+ lotteryXpBoost: ((_c = result.miscellaneous) === null || _c === void 0 ? void 0 : _c.lotteryXpBoost) || 0,
221
+ isNftWon: ((_d = result.miscellaneous) === null || _d === void 0 ? void 0 : _d.xpNftAwarded) || false,
195
222
  marginAccountId: params.marginAccountId,
196
- xpEarned: ((_d = result.miscellaneous) === null || _d === void 0 ? void 0 : _d.xpEarned) || 0,
197
- executionPrice: ((_e = result.tradeDetails) === null || _e === void 0 ? void 0 : _e.executionPrice) || 0,
198
- base: ((_f = result.tradeDetails) === null || _f === void 0 ? void 0 : _f.base) || 0,
223
+ xpEarned: ((_e = result.miscellaneous) === null || _e === void 0 ? void 0 : _e.xpEarned) || 0,
224
+ executionPrice: ((_f = result.tradeDetails) === null || _f === void 0 ? void 0 : _f.executionPrice) || 0,
225
+ base: ((_g = result.tradeDetails) === null || _g === void 0 ? void 0 : _g.base) || 0,
199
226
  }];
200
227
  }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AAI,KAAI,CAAC;qBACrC,EAAC;;;KACH,CAAC;AAxDW,QAAA,UAAU,cAwDrB","sourcesContent":["import {\n CloseOrderParams,\n CloseOrderResult,\n MatchOrderParams,\n MatchOrderResult,\n OrdersGatewayMarketOrderParams,\n} from './types';\nimport { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';\nimport {\n calculateMaxPriceLimit,\n ORDERS_GATEWAY_DEADLINE_IN_SECONDS,\n OrdersGatewayAbi,\n OrdersGatewayOrderType,\n signOrdersGatewayOrder,\n TransactionExecutionOutput,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { ContractType } from '@reyaxyz/common';\nimport { scale } from '@reyaxyz/common';\nimport { getReyaNetwork } from '../../utils/network';\nimport { AbiCoder, Interface } from 'ethers';\n\nconst executeMarketOrderViaOrdersGateway = async (\n params: OrdersGatewayMarketOrderParams,\n): Promise<TransactionExecutionOutput> => {\n const inputs = AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [params.order.baseE18, params.order.priceLimitE18],\n );\n\n const reyaChainId = getReyaNetwork();\n const creationTimestampMs = Date.now();\n const creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);\n\n const { signature, nonce } = await signOrdersGatewayOrder(\n params.signer,\n reyaChainId,\n params.marginAccountId,\n params.marketId,\n params.exchangeId,\n params.counterpartyAccountIds,\n params.order.type,\n inputs,\n creationTimestampSeconds + ORDERS_GATEWAY_DEADLINE_IN_SECONDS,\n creationTimestampMs,\n );\n\n const fnSig = 'execute';\n const fnArgs = [\n [\n params.marginAccountId,\n params.marketId,\n params.exchangeId,\n params.counterpartyAccountIds,\n params.order.type,\n inputs,\n await params.signer.getAddress(),\n nonce,\n ],\n [signature.v, signature.r, signature.s, signature.deadline],\n ];\n\n const ogInterface = new Interface(OrdersGatewayAbi);\n const data = ogInterface.encodeFunctionData(fnSig, fnArgs);\n\n const result = await signAndBroadcastTransaction(\n data,\n reyaChainId,\n ContractType.ORDERS_GATEWAY_PROXY,\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n tradeSource: params.tradeSource,\n action: 'ioc-order-ui',\n },\n );\n\n return result;\n};\n\nexport const matchOrder = async (\n params: MatchOrderParams,\n): Promise<MatchOrderResult> => {\n if (params.amountInBase === 0) {\n throw new Error('Order cannot be of size zero.');\n }\n\n let snappedAmount = BigNumber(params.amountInBase)\n .abs()\n .dividedBy(params.market.baseSpacing)\n .integerValue(BigNumber.ROUND_FLOOR)\n .multipliedBy(params.market.baseSpacing)\n .toNumber();\n\n if (params.amountInBase < 0) {\n snappedAmount = -snappedAmount;\n }\n\n if (Math.abs(snappedAmount) < params.market.minOrderSizeBase) {\n console.error(\n `Minimum trade amount ${params.market.minOrderSizeBase}. Entered amount ${snappedAmount}, ${params.amountInBase}`,\n );\n throw new Error(`Minimum trade amount ${params.market.minOrderSizeBase}`);\n }\n\n const orderBaseE18 = scale(18)(snappedAmount);\n const orderPriceLimitE18 = calculateMaxPriceLimit(snappedAmount > 0);\n\n const result = await executeMarketOrderViaOrdersGateway({\n signer: params.signer,\n marginAccountId: params.marginAccountId,\n counterpartyAccountIds: params.market.counterpartyAccountIds,\n marketId: params.market.id,\n exchangeId: params.market.exchangeId,\n tradeSource: params.tradeSource,\n order: {\n baseE18: orderBaseE18,\n priceLimitE18: orderPriceLimitE18,\n type: OrdersGatewayOrderType.MARKET_ORDER,\n },\n });\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n xpBoost: result.miscellaneous?.tradeXpBoost || 0,\n lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,\n isNftWon: result.miscellaneous?.xpNftAwarded || false,\n marginAccountId: params.marginAccountId,\n xpEarned: result.miscellaneous?.xpEarned || 0,\n executionPrice: result.tradeDetails?.executionPrice || 0,\n base: result.tradeDetails?.base || 0,\n positions: result.positions,\n orders: result.orders,\n accounts: result.accounts,\n };\n};\n\nexport const closeOrder = async (\n params: CloseOrderParams,\n): Promise<CloseOrderResult> => {\n if (params.orderBase === 0) {\n throw new Error('Position is already closed.');\n }\n\n let closingBase: number = 0;\n if (params.type === 'full') {\n closingBase = params.orderBase;\n } else {\n closingBase = params.closingOrderBase;\n\n if (closingBase === 0) {\n throw new Error('Specified base to close is zero.');\n }\n\n if (closingBase * params.orderBase < 0) {\n throw new Error(\n 'Specified base to close is in different direction that the actual order.',\n );\n }\n\n if (Math.abs(closingBase) > Math.abs(params.orderBase)) {\n throw new Error(\n 'Specified base to close is larger than the actual order.',\n );\n }\n }\n\n const orderPriceLimit = calculateMaxPriceLimit(closingBase < 0);\n\n const result = await executeMarketOrderViaOrdersGateway({\n signer: params.signer,\n marginAccountId: params.marginAccountId,\n counterpartyAccountIds: params.market.counterpartyAccountIds,\n marketId: params.market.id,\n exchangeId: params.market.exchangeId,\n order: {\n baseE18: -scale(18)(closingBase),\n priceLimitE18: orderPriceLimit,\n type: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,\n },\n });\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n xpBoost: result.miscellaneous?.tradeXpBoost || 0,\n lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,\n isNftWon: result.miscellaneous?.xpNftAwarded || false,\n marginAccountId: params.marginAccountId,\n xpEarned: result.miscellaneous?.xpEarned || 0,\n executionPrice: result.tradeDetails?.executionPrice || 0,\n base: result.tradeDetails?.base || 0,\n };\n};\n"]}
1
+ {"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,8EAA6E;AAC7E,0CAQyB;AACzB,8DAAqC;AACrC,0CAA+C;AAC/C,0CAAwC;AACxC,+CAAqD;AACrD,iCAA6C;AAE7C,IAAM,kCAAkC,GAAG,UACzC,MAAsC;;;;;gBAEhC,WAAW,GAAG,IAAA,wBAAc,GAAE,CAAC;gBAC/B,mBAAmB,GA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wBACzD,QAAQ,EAAE,CAAA,MAAA,MAAM,CAAC,aAAa,0CAAE,YAAY,KAAI,KAAK;wBACrD,eAAe,EAAE,MAAM,CAAC,eAAe;wBACvC,QAAQ,EAAE,CAAA,MAAA,MAAM,CAAC,aAAa,0CAAE,QAAQ,KAAI,CAAC;wBAC7C,cAAc,EAAE,CAAA,MAAA,MAAM,CAAC,YAAY,0CAAE,cAAc,KAAI,CAAC;wBACxD,IAAI,EAAE,CAAA,MAAA,MAAM,CAAC,YAAY,0CAAE,IAAI,KAAI,CAAC;qBACrC,EAAC;;;KACH,CAAC;AAnBW,QAAA,UAAU,cAmBrB","sourcesContent":["import {\n CloseOrderParams,\n CloseOrderResult,\n MatchOrderParams,\n MatchOrderResult,\n OrdersGatewayMarketOrderParams,\n} from './types';\nimport { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';\nimport {\n calculateMaxPriceLimit,\n ORDERS_GATEWAY_DEADLINE_IN_SECONDS,\n OrdersGatewayAbi,\n OrdersGatewayOrderType,\n PositionEntity,\n signOrdersGatewayOrder,\n TransactionExecutionOutput,\n} from '@reyaxyz/common';\nimport BigNumber from 'bignumber.js';\nimport { ContractType } from '@reyaxyz/common';\nimport { scale } from '@reyaxyz/common';\nimport { getReyaNetwork } from '../../utils/network';\nimport { AbiCoder, Interface } from 'ethers';\n\nconst executeMarketOrderViaOrdersGateway = async (\n params: OrdersGatewayMarketOrderParams,\n): Promise<TransactionExecutionOutput> => {\n const reyaChainId = getReyaNetwork();\n const creationTimestampMs = Date.now();\n const creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);\n\n const { signature, nonce } = await signOrdersGatewayOrder(\n params.signer,\n reyaChainId,\n params.marginAccountId,\n params.marketId,\n params.exchangeId,\n params.counterpartyAccountIds,\n params.orderType,\n params.orderInputs,\n creationTimestampSeconds + ORDERS_GATEWAY_DEADLINE_IN_SECONDS,\n creationTimestampMs,\n );\n\n const fnSig = 'execute';\n const fnArgs = [\n [\n params.marginAccountId,\n params.marketId,\n params.exchangeId,\n params.counterpartyAccountIds,\n params.orderType,\n params.orderInputs,\n await params.signer.getAddress(),\n nonce,\n ],\n [signature.v, signature.r, signature.s, signature.deadline],\n ];\n\n const ogInterface = new Interface(OrdersGatewayAbi);\n const data = ogInterface.encodeFunctionData(fnSig, fnArgs);\n\n const result = await signAndBroadcastTransaction(\n data,\n reyaChainId,\n ContractType.ORDERS_GATEWAY_PROXY,\n {\n accountId: params.marginAccountId,\n marketId: params.marketId,\n tradeSource: params.tradeSource,\n action: 'ioc-order-ui',\n },\n );\n\n return result;\n};\n\nexport const matchOrder = async (\n params: MatchOrderParams,\n): Promise<MatchOrderResult> => {\n if (params.amountInBase === 0) {\n throw new Error('Order cannot be of size zero.');\n }\n\n let snappedAmount = BigNumber(params.amountInBase)\n .abs()\n .dividedBy(params.market.baseSpacing)\n .integerValue(BigNumber.ROUND_FLOOR)\n .multipliedBy(params.market.baseSpacing)\n .toNumber();\n\n if (params.amountInBase < 0) {\n snappedAmount = -snappedAmount;\n }\n\n if (Math.abs(snappedAmount) < params.market.minOrderSizeBase) {\n console.error(\n `Minimum trade amount ${params.market.minOrderSizeBase}. Entered amount ${snappedAmount}, ${params.amountInBase}`,\n );\n throw new Error(`Minimum trade amount ${params.market.minOrderSizeBase}`);\n }\n\n const orderBaseE18 = scale(18)(snappedAmount);\n const orderPriceLimitE18 = calculateMaxPriceLimit(snappedAmount > 0);\n const orderInputs = AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [orderBaseE18, orderPriceLimitE18],\n );\n\n const result = await executeMarketOrderViaOrdersGateway({\n signer: params.signer,\n marginAccountId: params.marginAccountId,\n counterpartyAccountIds: params.market.counterpartyAccountIds,\n marketId: params.market.id,\n exchangeId: params.market.exchangeId,\n tradeSource: params.tradeSource,\n orderType: OrdersGatewayOrderType.MARKET_ORDER,\n orderInputs,\n });\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n xpBoost: result.miscellaneous?.tradeXpBoost || 0,\n lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,\n isNftWon: result.miscellaneous?.xpNftAwarded || false,\n marginAccountId: params.marginAccountId,\n xpEarned: result.miscellaneous?.xpEarned || 0,\n executionPrice: result.tradeDetails?.executionPrice || 0,\n base: result.tradeDetails?.base || 0,\n positions: result.positions,\n orders: result.orders,\n accounts: result.accounts,\n };\n};\n\nconst fullCloseOrder = async (\n params: Pick<CloseOrderParams, 'signer' | 'marginAccountId' | 'market'>,\n): Promise<TransactionExecutionOutput> => {\n const result = await executeMarketOrderViaOrdersGateway({\n signer: params.signer,\n marginAccountId: params.marginAccountId,\n counterpartyAccountIds: params.market.counterpartyAccountIds,\n marketId: params.market.id,\n exchangeId: params.market.exchangeId,\n orderType: OrdersGatewayOrderType.FULL_CLOSE_ORDER,\n orderInputs: AbiCoder.defaultAbiCoder().encode([], []),\n });\n\n return result;\n};\n\nconst partialCloseOrder = async (\n params: Pick<CloseOrderParams, 'signer' | 'marginAccountId' | 'market'> & {\n orderBase: PositionEntity['base'];\n closingOrderBase: number;\n },\n): Promise<TransactionExecutionOutput> => {\n if (params.orderBase === 0) {\n throw new Error('Position is already closed.');\n }\n\n if (params.closingOrderBase === 0) {\n throw new Error('Specified base to close is zero.');\n }\n\n if (params.closingOrderBase * params.orderBase < 0) {\n throw new Error(\n 'Specified base to close is in different direction that the actual order.',\n );\n }\n\n if (Math.abs(params.closingOrderBase) > Math.abs(params.orderBase)) {\n throw new Error('Specified base to close is larger than the actual order.');\n }\n\n const orderPriceLimit = calculateMaxPriceLimit(params.closingOrderBase < 0);\n const orderInputs = AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [-scale(18)(params.closingOrderBase), orderPriceLimit],\n );\n\n const result = await executeMarketOrderViaOrdersGateway({\n signer: params.signer,\n marginAccountId: params.marginAccountId,\n counterpartyAccountIds: params.market.counterpartyAccountIds,\n marketId: params.market.id,\n exchangeId: params.market.exchangeId,\n orderType: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,\n orderInputs,\n });\n\n return result;\n};\n\nexport const closeOrder = async (\n params: CloseOrderParams,\n): Promise<CloseOrderResult> => {\n const result =\n params.type === 'full'\n ? await fullCloseOrder(params)\n : await partialCloseOrder(params);\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n xpBoost: result.miscellaneous?.tradeXpBoost || 0,\n lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,\n isNftWon: result.miscellaneous?.xpNftAwarded || false,\n marginAccountId: params.marginAccountId,\n xpEarned: result.miscellaneous?.xpEarned || 0,\n executionPrice: result.tradeDetails?.executionPrice || 0,\n base: result.tradeDetails?.base || 0,\n };\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"/","sources":["services/orders/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Signer, JsonRpcSigner } from 'ethers';\nimport {\n CoreCommandsEIP712SignatureAndPayload,\n MarketEntity,\n MethodParameters,\n OrdersGatewayOrderType,\n OwnerMetadataEntity,\n PositionEntity,\n TransactionExecutionOutput,\n} from '@reyaxyz/common';\n\nexport type MarketParams = {\n id: MarketEntity['id'];\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n minOrderSizeBase: MarketEntity['minOrderSizeBase'];\n baseSpacing: MarketEntity['baseSpacing'];\n};\n\nexport type MatchOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n amountInBase: number; // amount in base\n market: MarketParams;\n tradeSource?: 'reya' | 'rage' | 'other';\n};\n\nexport type CloseOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n orderBase: PositionEntity['base'];\n market: MarketParams;\n} & (\n | {\n type: 'full';\n }\n | {\n type: 'partial';\n closingOrderBase: number;\n }\n);\n\nexport type MatchOrderResult = {\n transactionHash: string | null;\n coreSigNonce: OwnerMetadataEntity['coreSigNonce'] | null;\n xpBoost: number;\n lotteryXpBoost: number;\n isNftWon: boolean;\n marginAccountId: number;\n xpEarned: number;\n executionPrice: number;\n base: number;\n positions?: TransactionExecutionOutput['positions'];\n orders?: TransactionExecutionOutput['orders'];\n accounts?: TransactionExecutionOutput['accounts'];\n};\n\nexport type CloseOrderResult = {\n transactionHash: string | null;\n coreSigNonce: OwnerMetadataEntity['coreSigNonce'] | null;\n xpBoost: number;\n lotteryXpBoost: number;\n isNftWon: boolean;\n marginAccountId: number;\n xpEarned: number;\n executionPrice: number;\n base: number;\n};\n\nexport type MethodParametersAndEIP712Payload = MethodParameters &\n CoreCommandsEIP712SignatureAndPayload;\n\nexport type OrdersGatewayMarketOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n marketId: MarketEntity['id'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n order: {\n baseE18: bigint;\n priceLimitE18: bigint;\n type:\n | OrdersGatewayOrderType.MARKET_ORDER\n | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER;\n };\n tradeSource?: 'reya' | 'rage' | 'other';\n};\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"/","sources":["services/orders/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Signer, JsonRpcSigner } from 'ethers';\nimport {\n CoreCommandsEIP712SignatureAndPayload,\n MarketEntity,\n MethodParameters,\n OrdersGatewayOrderType,\n OwnerMetadataEntity,\n PositionEntity,\n TransactionExecutionOutput,\n} from '@reyaxyz/common';\n\nexport type MarketParams = {\n id: MarketEntity['id'];\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n minOrderSizeBase: MarketEntity['minOrderSizeBase'];\n baseSpacing: MarketEntity['baseSpacing'];\n};\n\nexport type MatchOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n amountInBase: number; // amount in base\n market: MarketParams;\n tradeSource?: 'reya' | 'rage' | 'other';\n};\n\nexport type CloseOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n market: MarketParams;\n} & (\n | {\n type: 'full';\n }\n | {\n type: 'partial';\n orderBase: PositionEntity['base'];\n closingOrderBase: number;\n }\n);\n\nexport type MatchOrderResult = {\n transactionHash: string | null;\n coreSigNonce: OwnerMetadataEntity['coreSigNonce'] | null;\n xpBoost: number;\n lotteryXpBoost: number;\n isNftWon: boolean;\n marginAccountId: number;\n xpEarned: number;\n executionPrice: number;\n base: number;\n positions?: TransactionExecutionOutput['positions'];\n orders?: TransactionExecutionOutput['orders'];\n accounts?: TransactionExecutionOutput['accounts'];\n};\n\nexport type CloseOrderResult = {\n transactionHash: string | null;\n coreSigNonce: OwnerMetadataEntity['coreSigNonce'] | null;\n xpBoost: number;\n lotteryXpBoost: number;\n isNftWon: boolean;\n marginAccountId: number;\n xpEarned: number;\n executionPrice: number;\n base: number;\n};\n\nexport type MethodParametersAndEIP712Payload = MethodParameters &\n CoreCommandsEIP712SignatureAndPayload;\n\nexport type OrdersGatewayMarketOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n marketId: MarketEntity['id'];\n counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];\n exchangeId: MarketEntity['orderInfo']['exchangeId'];\n orderType:\n | OrdersGatewayOrderType.MARKET_ORDER\n | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER\n | OrdersGatewayOrderType.FULL_CLOSE_ORDER;\n orderInputs: string;\n tradeSource?: 'reya' | 'rage' | 'other';\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"order.d.ts","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AA0EjB,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAsD1B,CAAC;AAEF,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAsD1B,CAAC"}
1
+ {"version":3,"file":"order.d.ts","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAsEjB,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAuD1B,CAAC;AA6DF,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAiB1B,CAAC"}
@@ -17,12 +17,12 @@ export type MatchOrderParams = {
17
17
  export type CloseOrderParams = {
18
18
  signer: Signer | JsonRpcSigner;
19
19
  marginAccountId: number;
20
- orderBase: PositionEntity['base'];
21
20
  market: MarketParams;
22
21
  } & ({
23
22
  type: 'full';
24
23
  } | {
25
24
  type: 'partial';
25
+ orderBase: PositionEntity['base'];
26
26
  closingOrderBase: number;
27
27
  });
28
28
  export type MatchOrderResult = {
@@ -57,11 +57,8 @@ export type OrdersGatewayMarketOrderParams = {
57
57
  marketId: MarketEntity['id'];
58
58
  counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];
59
59
  exchangeId: MarketEntity['orderInfo']['exchangeId'];
60
- order: {
61
- baseE18: bigint;
62
- priceLimitE18: bigint;
63
- type: OrdersGatewayOrderType.MARKET_ORDER | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER;
64
- };
60
+ orderType: OrdersGatewayOrderType.MARKET_ORDER | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER | OrdersGatewayOrderType.FULL_CLOSE_ORDER;
61
+ orderInputs: string;
65
62
  tradeSource?: 'reya' | 'rage' | 'other';
66
63
  };
67
64
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"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AE,YAAY,CAAC,WAAW,CAAC,CAAC,wBAAwB,CAAC,CAAC;IAC5E,UAAU,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,YAAY,CAAC,CAAC;IACpD,SAAS,EACL,sBAAsB,CAAC,YAAY,GACnC,sBAAsB,CAAC,wBAAwB,GAC/C,sBAAsB,CAAC,gBAAgB,CAAC;IAC5C,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CACzC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/sdk",
3
- "version": "0.139.5",
3
+ "version": "0.140.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -29,11 +29,11 @@
29
29
  "generate:coverage-badges": "npx istanbul-badges-readme --silent"
30
30
  },
31
31
  "dependencies": {
32
- "@reyaxyz/common": "0.321.1",
32
+ "@reyaxyz/common": "0.323.0",
33
33
  "axios": "1.6.2",
34
34
  "bignumber.js": "9.1.2",
35
35
  "ethers": "6.9.0"
36
36
  },
37
37
  "packageManager": "pnpm@8.3.1",
38
- "gitHead": "ba5292ccbb2e932aacd321bbb9b1e1cf742a1b90"
38
+ "gitHead": "14753daf1fd9d785f10e14ce5d923d2fa8cb71b1"
39
39
  }
@@ -11,6 +11,7 @@ import {
11
11
  ORDERS_GATEWAY_DEADLINE_IN_SECONDS,
12
12
  OrdersGatewayAbi,
13
13
  OrdersGatewayOrderType,
14
+ PositionEntity,
14
15
  signOrdersGatewayOrder,
15
16
  TransactionExecutionOutput,
16
17
  } from '@reyaxyz/common';
@@ -23,11 +24,6 @@ import { AbiCoder, Interface } from 'ethers';
23
24
  const executeMarketOrderViaOrdersGateway = async (
24
25
  params: OrdersGatewayMarketOrderParams,
25
26
  ): Promise<TransactionExecutionOutput> => {
26
- const inputs = AbiCoder.defaultAbiCoder().encode(
27
- ['int256', 'uint256'],
28
- [params.order.baseE18, params.order.priceLimitE18],
29
- );
30
-
31
27
  const reyaChainId = getReyaNetwork();
32
28
  const creationTimestampMs = Date.now();
33
29
  const creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);
@@ -39,8 +35,8 @@ const executeMarketOrderViaOrdersGateway = async (
39
35
  params.marketId,
40
36
  params.exchangeId,
41
37
  params.counterpartyAccountIds,
42
- params.order.type,
43
- inputs,
38
+ params.orderType,
39
+ params.orderInputs,
44
40
  creationTimestampSeconds + ORDERS_GATEWAY_DEADLINE_IN_SECONDS,
45
41
  creationTimestampMs,
46
42
  );
@@ -52,8 +48,8 @@ const executeMarketOrderViaOrdersGateway = async (
52
48
  params.marketId,
53
49
  params.exchangeId,
54
50
  params.counterpartyAccountIds,
55
- params.order.type,
56
- inputs,
51
+ params.orderType,
52
+ params.orderInputs,
57
53
  await params.signer.getAddress(),
58
54
  nonce,
59
55
  ],
@@ -105,6 +101,10 @@ export const matchOrder = async (
105
101
 
106
102
  const orderBaseE18 = scale(18)(snappedAmount);
107
103
  const orderPriceLimitE18 = calculateMaxPriceLimit(snappedAmount > 0);
104
+ const orderInputs = AbiCoder.defaultAbiCoder().encode(
105
+ ['int256', 'uint256'],
106
+ [orderBaseE18, orderPriceLimitE18],
107
+ );
108
108
 
109
109
  const result = await executeMarketOrderViaOrdersGateway({
110
110
  signer: params.signer,
@@ -113,11 +113,8 @@ export const matchOrder = async (
113
113
  marketId: params.market.id,
114
114
  exchangeId: params.market.exchangeId,
115
115
  tradeSource: params.tradeSource,
116
- order: {
117
- baseE18: orderBaseE18,
118
- priceLimitE18: orderPriceLimitE18,
119
- type: OrdersGatewayOrderType.MARKET_ORDER,
120
- },
116
+ orderType: OrdersGatewayOrderType.MARKET_ORDER,
117
+ orderInputs,
121
118
  });
122
119
 
123
120
  return {
@@ -136,37 +133,51 @@ export const matchOrder = async (
136
133
  };
137
134
  };
138
135
 
139
- export const closeOrder = async (
140
- params: CloseOrderParams,
141
- ): Promise<CloseOrderResult> => {
136
+ const fullCloseOrder = async (
137
+ params: Pick<CloseOrderParams, 'signer' | 'marginAccountId' | 'market'>,
138
+ ): Promise<TransactionExecutionOutput> => {
139
+ const result = await executeMarketOrderViaOrdersGateway({
140
+ signer: params.signer,
141
+ marginAccountId: params.marginAccountId,
142
+ counterpartyAccountIds: params.market.counterpartyAccountIds,
143
+ marketId: params.market.id,
144
+ exchangeId: params.market.exchangeId,
145
+ orderType: OrdersGatewayOrderType.FULL_CLOSE_ORDER,
146
+ orderInputs: AbiCoder.defaultAbiCoder().encode([], []),
147
+ });
148
+
149
+ return result;
150
+ };
151
+
152
+ const partialCloseOrder = async (
153
+ params: Pick<CloseOrderParams, 'signer' | 'marginAccountId' | 'market'> & {
154
+ orderBase: PositionEntity['base'];
155
+ closingOrderBase: number;
156
+ },
157
+ ): Promise<TransactionExecutionOutput> => {
142
158
  if (params.orderBase === 0) {
143
159
  throw new Error('Position is already closed.');
144
160
  }
145
161
 
146
- let closingBase: number = 0;
147
- if (params.type === 'full') {
148
- closingBase = params.orderBase;
149
- } else {
150
- closingBase = params.closingOrderBase;
151
-
152
- if (closingBase === 0) {
153
- throw new Error('Specified base to close is zero.');
154
- }
155
-
156
- if (closingBase * params.orderBase < 0) {
157
- throw new Error(
158
- 'Specified base to close is in different direction that the actual order.',
159
- );
160
- }
161
-
162
- if (Math.abs(closingBase) > Math.abs(params.orderBase)) {
163
- throw new Error(
164
- 'Specified base to close is larger than the actual order.',
165
- );
166
- }
162
+ if (params.closingOrderBase === 0) {
163
+ throw new Error('Specified base to close is zero.');
167
164
  }
168
165
 
169
- const orderPriceLimit = calculateMaxPriceLimit(closingBase < 0);
166
+ if (params.closingOrderBase * params.orderBase < 0) {
167
+ throw new Error(
168
+ 'Specified base to close is in different direction that the actual order.',
169
+ );
170
+ }
171
+
172
+ if (Math.abs(params.closingOrderBase) > Math.abs(params.orderBase)) {
173
+ throw new Error('Specified base to close is larger than the actual order.');
174
+ }
175
+
176
+ const orderPriceLimit = calculateMaxPriceLimit(params.closingOrderBase < 0);
177
+ const orderInputs = AbiCoder.defaultAbiCoder().encode(
178
+ ['int256', 'uint256'],
179
+ [-scale(18)(params.closingOrderBase), orderPriceLimit],
180
+ );
170
181
 
171
182
  const result = await executeMarketOrderViaOrdersGateway({
172
183
  signer: params.signer,
@@ -174,13 +185,21 @@ export const closeOrder = async (
174
185
  counterpartyAccountIds: params.market.counterpartyAccountIds,
175
186
  marketId: params.market.id,
176
187
  exchangeId: params.market.exchangeId,
177
- order: {
178
- baseE18: -scale(18)(closingBase),
179
- priceLimitE18: orderPriceLimit,
180
- type: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
181
- },
188
+ orderType: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
189
+ orderInputs,
182
190
  });
183
191
 
192
+ return result;
193
+ };
194
+
195
+ export const closeOrder = async (
196
+ params: CloseOrderParams,
197
+ ): Promise<CloseOrderResult> => {
198
+ const result =
199
+ params.type === 'full'
200
+ ? await fullCloseOrder(params)
201
+ : await partialCloseOrder(params);
202
+
184
203
  return {
185
204
  transactionHash: result?.txHash || null,
186
205
  coreSigNonce: Number(result?.coreSigNonce) || null,
@@ -28,7 +28,6 @@ export type MatchOrderParams = {
28
28
  export type CloseOrderParams = {
29
29
  signer: Signer | JsonRpcSigner;
30
30
  marginAccountId: number;
31
- orderBase: PositionEntity['base'];
32
31
  market: MarketParams;
33
32
  } & (
34
33
  | {
@@ -36,6 +35,7 @@ export type CloseOrderParams = {
36
35
  }
37
36
  | {
38
37
  type: 'partial';
38
+ orderBase: PositionEntity['base'];
39
39
  closingOrderBase: number;
40
40
  }
41
41
  );
@@ -76,12 +76,10 @@ export type OrdersGatewayMarketOrderParams = {
76
76
  marketId: MarketEntity['id'];
77
77
  counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];
78
78
  exchangeId: MarketEntity['orderInfo']['exchangeId'];
79
- order: {
80
- baseE18: bigint;
81
- priceLimitE18: bigint;
82
- type:
83
- | OrdersGatewayOrderType.MARKET_ORDER
84
- | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER;
85
- };
79
+ orderType:
80
+ | OrdersGatewayOrderType.MARKET_ORDER
81
+ | OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER
82
+ | OrdersGatewayOrderType.FULL_CLOSE_ORDER;
83
+ orderInputs: string;
86
84
  tradeSource?: 'reya' | 'rage' | 'other';
87
85
  };