@reyaxyz/sdk 0.139.4 → 0.140.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/order.js +71 -45
- package/dist/services/orders/order.js.map +1 -1
- package/dist/services/orders/types.js.map +1 -1
- package/dist/types/services/orders/order.d.ts.map +1 -1
- package/dist/types/services/orders/types.d.ts +3 -6
- package/dist/types/services/orders/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/services/orders/order.ts +64 -48
- package/src/services/orders/types.ts +6 -8
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
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
|
|
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.
|
|
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.
|
|
68
|
-
|
|
66
|
+
params.orderType,
|
|
67
|
+
params.orderInputs];
|
|
69
68
|
return [4 /*yield*/, params.signer.getAddress()];
|
|
70
69
|
case 2:
|
|
71
70
|
fnArgs = [
|
|
@@ -77,12 +76,11 @@ var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(vo
|
|
|
77
76
|
];
|
|
78
77
|
ogInterface = new ethers_1.Interface(common_1.OrdersGatewayAbi);
|
|
79
78
|
data = ogInterface.encodeFunctionData(fnSig, fnArgs);
|
|
80
|
-
isFastExecutionEnabled = process.env.FAST_ORDER_EXECUTION === 'true' || false;
|
|
81
79
|
return [4 /*yield*/, (0, signAndBroadcastTransaction_1.signAndBroadcastTransaction)(data, reyaChainId, common_2.ContractType.ORDERS_GATEWAY_PROXY, {
|
|
82
80
|
accountId: params.marginAccountId,
|
|
83
81
|
marketId: params.marketId,
|
|
84
82
|
tradeSource: params.tradeSource,
|
|
85
|
-
action:
|
|
83
|
+
action: 'ioc-order-ui',
|
|
86
84
|
})];
|
|
87
85
|
case 3:
|
|
88
86
|
result = _c.sent();
|
|
@@ -91,7 +89,7 @@ var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(vo
|
|
|
91
89
|
});
|
|
92
90
|
}); };
|
|
93
91
|
var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
94
|
-
var snappedAmount, orderBaseE18, orderPriceLimitE18, result;
|
|
92
|
+
var snappedAmount, orderBaseE18, orderPriceLimitE18, orderInputs, result;
|
|
95
93
|
var _a, _b, _c, _d, _e, _f;
|
|
96
94
|
return __generator(this, function (_g) {
|
|
97
95
|
switch (_g.label) {
|
|
@@ -114,6 +112,7 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
114
112
|
}
|
|
115
113
|
orderBaseE18 = (0, common_3.scale)(18)(snappedAmount);
|
|
116
114
|
orderPriceLimitE18 = (0, common_1.calculateMaxPriceLimit)(snappedAmount > 0);
|
|
115
|
+
orderInputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [orderBaseE18, orderPriceLimitE18]);
|
|
117
116
|
return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
|
|
118
117
|
signer: params.signer,
|
|
119
118
|
marginAccountId: params.marginAccountId,
|
|
@@ -121,11 +120,8 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
121
120
|
marketId: params.market.id,
|
|
122
121
|
exchangeId: params.market.exchangeId,
|
|
123
122
|
tradeSource: params.tradeSource,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
priceLimitE18: orderPriceLimitE18,
|
|
127
|
-
type: common_1.OrdersGatewayOrderType.MARKET_ORDER,
|
|
128
|
-
},
|
|
123
|
+
orderType: common_1.OrdersGatewayOrderType.MARKET_ORDER,
|
|
124
|
+
orderInputs: orderInputs,
|
|
129
125
|
})];
|
|
130
126
|
case 1:
|
|
131
127
|
result = _g.sent();
|
|
@@ -147,56 +143,86 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
147
143
|
});
|
|
148
144
|
}); };
|
|
149
145
|
exports.matchOrder = matchOrder;
|
|
150
|
-
var
|
|
151
|
-
var
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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) {
|
|
155
169
|
case 0:
|
|
156
170
|
if (params.orderBase === 0) {
|
|
157
171
|
throw new Error('Position is already closed.');
|
|
158
172
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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.');
|
|
162
178
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (closingBase === 0) {
|
|
166
|
-
throw new Error('Specified base to close is zero.');
|
|
167
|
-
}
|
|
168
|
-
if (closingBase * params.orderBase < 0) {
|
|
169
|
-
throw new Error('Specified base to close is in different direction that the actual order.');
|
|
170
|
-
}
|
|
171
|
-
if (Math.abs(closingBase) > Math.abs(params.orderBase)) {
|
|
172
|
-
throw new Error('Specified base to close is larger than the actual order.');
|
|
173
|
-
}
|
|
179
|
+
if (Math.abs(params.closingOrderBase) > Math.abs(params.orderBase)) {
|
|
180
|
+
throw new Error('Specified base to close is larger than the actual order.');
|
|
174
181
|
}
|
|
175
|
-
orderPriceLimit = (0, common_1.calculateMaxPriceLimit)(
|
|
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]);
|
|
176
184
|
return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
|
|
177
185
|
signer: params.signer,
|
|
178
186
|
marginAccountId: params.marginAccountId,
|
|
179
187
|
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
180
188
|
marketId: params.market.id,
|
|
181
189
|
exchangeId: params.market.exchangeId,
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
priceLimitE18: orderPriceLimit,
|
|
185
|
-
type: common_1.OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
186
|
-
},
|
|
190
|
+
orderType: common_1.OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
191
|
+
orderInputs: orderInputs,
|
|
187
192
|
})];
|
|
188
193
|
case 1:
|
|
189
|
-
result =
|
|
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;
|
|
190
216
|
return [2 /*return*/, {
|
|
191
217
|
transactionHash: (result === null || result === void 0 ? void 0 : result.txHash) || null,
|
|
192
218
|
coreSigNonce: Number(result === null || result === void 0 ? void 0 : result.coreSigNonce) || null,
|
|
193
|
-
xpBoost: ((
|
|
194
|
-
lotteryXpBoost: ((
|
|
195
|
-
isNftWon: ((
|
|
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,
|
|
196
222
|
marginAccountId: params.marginAccountId,
|
|
197
|
-
xpEarned: ((
|
|
198
|
-
executionPrice: ((
|
|
199
|
-
base: ((
|
|
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,
|
|
200
226
|
}];
|
|
201
227
|
}
|
|
202
228
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,8EAA6E;AAC7E,0CAOyB;AACzB,8DAAqC;AACrC,0CAA+C;AAC/C,0CA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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 isFastExecutionEnabled =\n process.env.FAST_ORDER_EXECUTION === 'true' || false;\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: isFastExecutionEnabled ? 'ioc-order-ui' : undefined,\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/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,KAAI,CAAC;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
|
|
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;
|
|
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
|
-
|
|
61
|
-
|
|
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":"types.d.ts","sourceRoot":"/","sources":["services/orders/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,EACL,qCAAqC,EACrC,YAAY,EACZ,gBAAgB,EAChB,sBAAsB,EACtB,mBAAmB,EACnB,cAAc,EACd,0BAA0B,EAC3B,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC;IACvB,UAAU,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,YAAY,CAAC,CAAC;IACpD,sBAAsB,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,wBAAwB,CAAC,CAAC;IAC5E,gBAAgB,EAAE,YAAY,CAAC,kBAAkB,CAAC,CAAC;IACnD,WAAW,EAAE,YAAY,CAAC,aAAa,CAAC,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,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AC,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.
|
|
3
|
+
"version": "0.140.0",
|
|
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.
|
|
32
|
+
"@reyaxyz/common": "0.322.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": "
|
|
38
|
+
"gitHead": "980210f2ff4043bf832eb6fa138886082f0114b3"
|
|
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.
|
|
43
|
-
|
|
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.
|
|
56
|
-
|
|
51
|
+
params.orderType,
|
|
52
|
+
params.orderInputs,
|
|
57
53
|
await params.signer.getAddress(),
|
|
58
54
|
nonce,
|
|
59
55
|
],
|
|
@@ -63,9 +59,6 @@ const executeMarketOrderViaOrdersGateway = async (
|
|
|
63
59
|
const ogInterface = new Interface(OrdersGatewayAbi);
|
|
64
60
|
const data = ogInterface.encodeFunctionData(fnSig, fnArgs);
|
|
65
61
|
|
|
66
|
-
const isFastExecutionEnabled =
|
|
67
|
-
process.env.FAST_ORDER_EXECUTION === 'true' || false;
|
|
68
|
-
|
|
69
62
|
const result = await signAndBroadcastTransaction(
|
|
70
63
|
data,
|
|
71
64
|
reyaChainId,
|
|
@@ -74,7 +67,7 @@ const executeMarketOrderViaOrdersGateway = async (
|
|
|
74
67
|
accountId: params.marginAccountId,
|
|
75
68
|
marketId: params.marketId,
|
|
76
69
|
tradeSource: params.tradeSource,
|
|
77
|
-
action:
|
|
70
|
+
action: 'ioc-order-ui',
|
|
78
71
|
},
|
|
79
72
|
);
|
|
80
73
|
|
|
@@ -108,6 +101,10 @@ export const matchOrder = async (
|
|
|
108
101
|
|
|
109
102
|
const orderBaseE18 = scale(18)(snappedAmount);
|
|
110
103
|
const orderPriceLimitE18 = calculateMaxPriceLimit(snappedAmount > 0);
|
|
104
|
+
const orderInputs = AbiCoder.defaultAbiCoder().encode(
|
|
105
|
+
['int256', 'uint256'],
|
|
106
|
+
[orderBaseE18, orderPriceLimitE18],
|
|
107
|
+
);
|
|
111
108
|
|
|
112
109
|
const result = await executeMarketOrderViaOrdersGateway({
|
|
113
110
|
signer: params.signer,
|
|
@@ -116,11 +113,8 @@ export const matchOrder = async (
|
|
|
116
113
|
marketId: params.market.id,
|
|
117
114
|
exchangeId: params.market.exchangeId,
|
|
118
115
|
tradeSource: params.tradeSource,
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
priceLimitE18: orderPriceLimitE18,
|
|
122
|
-
type: OrdersGatewayOrderType.MARKET_ORDER,
|
|
123
|
-
},
|
|
116
|
+
orderType: OrdersGatewayOrderType.MARKET_ORDER,
|
|
117
|
+
orderInputs,
|
|
124
118
|
});
|
|
125
119
|
|
|
126
120
|
return {
|
|
@@ -139,37 +133,51 @@ export const matchOrder = async (
|
|
|
139
133
|
};
|
|
140
134
|
};
|
|
141
135
|
|
|
142
|
-
|
|
143
|
-
params: CloseOrderParams,
|
|
144
|
-
): Promise<
|
|
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> => {
|
|
145
158
|
if (params.orderBase === 0) {
|
|
146
159
|
throw new Error('Position is already closed.');
|
|
147
160
|
}
|
|
148
161
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (closingBase * params.orderBase < 0) {
|
|
160
|
-
throw new Error(
|
|
161
|
-
'Specified base to close is in different direction that the actual order.',
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (Math.abs(closingBase) > Math.abs(params.orderBase)) {
|
|
166
|
-
throw new Error(
|
|
167
|
-
'Specified base to close is larger than the actual order.',
|
|
168
|
-
);
|
|
169
|
-
}
|
|
162
|
+
if (params.closingOrderBase === 0) {
|
|
163
|
+
throw new Error('Specified base to close is zero.');
|
|
164
|
+
}
|
|
165
|
+
|
|
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
170
|
}
|
|
171
171
|
|
|
172
|
-
|
|
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
|
+
);
|
|
173
181
|
|
|
174
182
|
const result = await executeMarketOrderViaOrdersGateway({
|
|
175
183
|
signer: params.signer,
|
|
@@ -177,13 +185,21 @@ export const closeOrder = async (
|
|
|
177
185
|
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
178
186
|
marketId: params.market.id,
|
|
179
187
|
exchangeId: params.market.exchangeId,
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
priceLimitE18: orderPriceLimit,
|
|
183
|
-
type: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
184
|
-
},
|
|
188
|
+
orderType: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
189
|
+
orderInputs,
|
|
185
190
|
});
|
|
186
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
|
+
|
|
187
203
|
return {
|
|
188
204
|
transactionHash: result?.txHash || null,
|
|
189
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
};
|