@reyaxyz/sdk 0.151.4 → 0.152.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 +1 -1
- package/dist/services/orders/index.js +1 -1
- package/dist/services/orders/index.js.map +1 -1
- package/dist/services/orders/nonce.js +37 -0
- package/dist/services/orders/nonce.js.map +1 -0
- package/dist/services/orders/orderV2.js +416 -89
- package/dist/services/orders/orderV2.js.map +1 -1
- package/dist/services/orders/triggerSettlementHeadroom.js +51 -0
- package/dist/services/orders/triggerSettlementHeadroom.js.map +1 -0
- package/dist/services/orders/types.js.map +1 -1
- package/dist/types/services/orders/index.d.ts +1 -1
- package/dist/types/services/orders/index.d.ts.map +1 -1
- package/dist/types/services/orders/nonce.d.ts +2 -0
- package/dist/types/services/orders/nonce.d.ts.map +1 -0
- package/dist/types/services/orders/orderV2.d.ts +194 -14
- package/dist/types/services/orders/orderV2.d.ts.map +1 -1
- package/dist/types/services/orders/triggerSettlementHeadroom.d.ts +33 -0
- package/dist/types/services/orders/triggerSettlementHeadroom.d.ts.map +1 -0
- package/dist/types/services/orders/types.d.ts +1 -11
- package/dist/types/services/orders/types.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/services/orders/index.ts +1 -1
- package/src/services/orders/nonce.ts +34 -0
- package/src/services/orders/orderV2.ts +731 -122
- package/src/services/orders/triggerSettlementHeadroom.ts +56 -0
- package/src/services/orders/types.ts +0 -15
- package/dist/services/orders/order.js +0 -231
- package/dist/services/orders/order.js.map +0 -1
- package/dist/types/services/orders/order.d.ts +0 -4
- package/dist/types/services/orders/order.d.ts.map +0 -1
- package/src/services/orders/order.ts +0 -214
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side mirror of the one trigger-admission rule a client can evaluate on
|
|
3
|
+
* its own: the settlement headroom a `GTT` lifetime must outlast.
|
|
4
|
+
*
|
|
5
|
+
* The per-market limit band is deliberately NOT mirrored here. It is an
|
|
6
|
+
* engine-side admission rule derived from matching-engine config, it is not
|
|
7
|
+
* published on the API, and a client cannot compute it — a `limitPx` outside it
|
|
8
|
+
* is refused at admission with `TRIGGER_LIMIT_OUTSIDE_BAND_ERROR`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Settlement headroom of a production deployment, in seconds. A `GTT` lifetime
|
|
13
|
+
* must be STRICTLY greater than `now + headroom`, or the engine refuses the
|
|
14
|
+
* order with `ORDER_EXPIRES_TOO_SOON_ERROR`. It is a venue parameter and is not
|
|
15
|
+
* published on the API; a deployment that runs a different one is handled by
|
|
16
|
+
* the per-call `settlementHeadroomSeconds` override.
|
|
17
|
+
*/
|
|
18
|
+
export const DEFAULT_SETTLEMENT_HEADROOM_SECONDS = 60;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Refuse a `GTT` lifetime that does not outlast the settlement headroom.
|
|
22
|
+
*
|
|
23
|
+
* Independent of the `expiresAfter > deadline` coupling: a caller that pins a
|
|
24
|
+
* short deadline satisfies that check while still signing a lifetime the engine
|
|
25
|
+
* refuses. On a trigger the same deadline covers both phases, so the headroom
|
|
26
|
+
* bites there too.
|
|
27
|
+
*
|
|
28
|
+
* `expiresAfter` must be a positive safe integer (Unix seconds) and
|
|
29
|
+
* `headroomSeconds` a finite non-negative number; both are checked here rather
|
|
30
|
+
* than at the later `BigInt(expiresAfter)` conversion, so a `NaN`, `Infinity`,
|
|
31
|
+
* fractional or negative value fails at the validation boundary instead of
|
|
32
|
+
* after the signer has been resolved and a nonce minted.
|
|
33
|
+
*/
|
|
34
|
+
export const assertSettlementHeadroom = (
|
|
35
|
+
expiresAfter: number,
|
|
36
|
+
headroomSeconds: number = DEFAULT_SETTLEMENT_HEADROOM_SECONDS,
|
|
37
|
+
nowSeconds: number = Math.floor(Date.now() / 1000),
|
|
38
|
+
): void => {
|
|
39
|
+
if (!Number.isSafeInteger(expiresAfter) || expiresAfter <= 0) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`expiresAfter=${expiresAfter} must be a positive integer Unix timestamp in seconds`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isFinite(headroomSeconds) || headroomSeconds < 0) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`settlementHeadroomSeconds=${headroomSeconds} must be a finite, non-negative number of seconds`,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const earliestAdmissible = nowSeconds + headroomSeconds;
|
|
50
|
+
if (expiresAfter <= earliestAdmissible) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`expiresAfter=${expiresAfter} does not outlast the ${headroomSeconds}s settlement headroom ` +
|
|
53
|
+
`(needs > ${earliestAdmissible}). Pass settlementHeadroomSeconds if the target deployment runs a different one.`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
CoreCommandsEIP712SignatureAndPayload,
|
|
4
4
|
MarketEntity,
|
|
5
5
|
MethodParameters,
|
|
6
|
-
OrdersGatewayOrderType,
|
|
7
6
|
OwnerMetadataEntity,
|
|
8
7
|
PositionEntity,
|
|
9
8
|
TransactionExecutionOutput,
|
|
@@ -69,17 +68,3 @@ export type CloseOrderResult = {
|
|
|
69
68
|
|
|
70
69
|
export type MethodParametersAndEIP712Payload = MethodParameters &
|
|
71
70
|
CoreCommandsEIP712SignatureAndPayload;
|
|
72
|
-
|
|
73
|
-
export type OrdersGatewayMarketOrderParams = {
|
|
74
|
-
signer: Signer | JsonRpcSigner;
|
|
75
|
-
marginAccountId: number;
|
|
76
|
-
marketId: MarketEntity['id'];
|
|
77
|
-
counterpartyAccountIds: MarketEntity['orderInfo']['counterpartyAccountIds'];
|
|
78
|
-
exchangeId: MarketEntity['orderInfo']['exchangeId'];
|
|
79
|
-
orderType:
|
|
80
|
-
| OrdersGatewayOrderType.MARKET_ORDER
|
|
81
|
-
| OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER
|
|
82
|
-
| OrdersGatewayOrderType.FULL_CLOSE_ORDER;
|
|
83
|
-
orderInputs: string;
|
|
84
|
-
tradeSource?: 'reya' | 'rage' | 'other';
|
|
85
|
-
};
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
-
};
|
|
41
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
exports.closeOrder = exports.matchOrder = void 0;
|
|
43
|
-
var signAndBroadcastTransaction_1 = require("../signAndBroadcastTransaction");
|
|
44
|
-
var common_1 = require("@reyaxyz/common");
|
|
45
|
-
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
46
|
-
var common_2 = require("@reyaxyz/common");
|
|
47
|
-
var common_3 = require("@reyaxyz/common");
|
|
48
|
-
var network_1 = require("../../utils/network");
|
|
49
|
-
var ethers_1 = require("ethers");
|
|
50
|
-
var executeMarketOrderViaOrdersGateway = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
51
|
-
var reyaChainId, creationTimestampMs, creationTimestampSeconds, _a, signature, nonce, fnSig, fnArgs, _b, ogInterface, data, result;
|
|
52
|
-
return __generator(this, function (_c) {
|
|
53
|
-
switch (_c.label) {
|
|
54
|
-
case 0:
|
|
55
|
-
reyaChainId = (0, network_1.getReyaNetwork)();
|
|
56
|
-
creationTimestampMs = Date.now();
|
|
57
|
-
creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);
|
|
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)];
|
|
59
|
-
case 1:
|
|
60
|
-
_a = _c.sent(), signature = _a.signature, nonce = _a.nonce;
|
|
61
|
-
fnSig = 'execute';
|
|
62
|
-
_b = [params.marginAccountId,
|
|
63
|
-
params.marketId,
|
|
64
|
-
params.exchangeId,
|
|
65
|
-
params.counterpartyAccountIds,
|
|
66
|
-
params.orderType,
|
|
67
|
-
params.orderInputs];
|
|
68
|
-
return [4 /*yield*/, params.signer.getAddress()];
|
|
69
|
-
case 2:
|
|
70
|
-
fnArgs = [
|
|
71
|
-
_b.concat([
|
|
72
|
-
_c.sent(),
|
|
73
|
-
nonce
|
|
74
|
-
]),
|
|
75
|
-
[signature.v, signature.r, signature.s, signature.deadline]
|
|
76
|
-
];
|
|
77
|
-
ogInterface = new ethers_1.Interface(common_1.OrdersGatewayAbi);
|
|
78
|
-
data = ogInterface.encodeFunctionData(fnSig, fnArgs);
|
|
79
|
-
return [4 /*yield*/, (0, signAndBroadcastTransaction_1.signAndBroadcastTransaction)(data, reyaChainId, common_2.ContractType.ORDERS_GATEWAY_PROXY, {
|
|
80
|
-
accountId: params.marginAccountId,
|
|
81
|
-
marketId: params.marketId,
|
|
82
|
-
tradeSource: params.tradeSource,
|
|
83
|
-
action: 'ioc-order-ui',
|
|
84
|
-
})];
|
|
85
|
-
case 3:
|
|
86
|
-
result = _c.sent();
|
|
87
|
-
return [2 /*return*/, result];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}); };
|
|
91
|
-
var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
92
|
-
var snappedAmount, orderBaseE18, orderPriceLimitE18, orderInputs, result;
|
|
93
|
-
var _a, _b, _c, _d, _e, _f;
|
|
94
|
-
return __generator(this, function (_g) {
|
|
95
|
-
switch (_g.label) {
|
|
96
|
-
case 0:
|
|
97
|
-
if (params.amountInBase === 0) {
|
|
98
|
-
throw new Error('Order cannot be of size zero.');
|
|
99
|
-
}
|
|
100
|
-
snappedAmount = (0, bignumber_js_1.default)(params.amountInBase)
|
|
101
|
-
.abs()
|
|
102
|
-
.dividedBy(params.market.baseSpacing)
|
|
103
|
-
.integerValue(bignumber_js_1.default.ROUND_FLOOR)
|
|
104
|
-
.multipliedBy(params.market.baseSpacing)
|
|
105
|
-
.toNumber();
|
|
106
|
-
if (params.amountInBase < 0) {
|
|
107
|
-
snappedAmount = -snappedAmount;
|
|
108
|
-
}
|
|
109
|
-
if (Math.abs(snappedAmount) < params.market.minOrderSizeBase) {
|
|
110
|
-
console.error("Minimum trade amount ".concat(params.market.minOrderSizeBase, ". Entered amount ").concat(snappedAmount, ", ").concat(params.amountInBase));
|
|
111
|
-
throw new Error("Minimum trade amount ".concat(params.market.minOrderSizeBase));
|
|
112
|
-
}
|
|
113
|
-
orderBaseE18 = (0, common_3.scale)(18)(snappedAmount);
|
|
114
|
-
orderPriceLimitE18 = (0, common_1.calculateMaxPriceLimit)(snappedAmount > 0);
|
|
115
|
-
orderInputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['int256', 'uint256'], [orderBaseE18, orderPriceLimitE18]);
|
|
116
|
-
return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
|
|
117
|
-
signer: params.signer,
|
|
118
|
-
marginAccountId: params.marginAccountId,
|
|
119
|
-
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
120
|
-
marketId: params.market.id,
|
|
121
|
-
exchangeId: params.market.exchangeId,
|
|
122
|
-
tradeSource: params.tradeSource,
|
|
123
|
-
orderType: common_1.OrdersGatewayOrderType.MARKET_ORDER,
|
|
124
|
-
orderInputs: orderInputs,
|
|
125
|
-
})];
|
|
126
|
-
case 1:
|
|
127
|
-
result = _g.sent();
|
|
128
|
-
return [2 /*return*/, {
|
|
129
|
-
transactionHash: (result === null || result === void 0 ? void 0 : result.txHash) || null,
|
|
130
|
-
coreSigNonce: Number(result === null || result === void 0 ? void 0 : result.coreSigNonce) || null,
|
|
131
|
-
xpBoost: ((_a = result.miscellaneous) === null || _a === void 0 ? void 0 : _a.tradeXpBoost) || 0,
|
|
132
|
-
lotteryXpBoost: ((_b = result.miscellaneous) === null || _b === void 0 ? void 0 : _b.lotteryXpBoost) || 0,
|
|
133
|
-
isNftWon: ((_c = result.miscellaneous) === null || _c === void 0 ? void 0 : _c.xpNftAwarded) || false,
|
|
134
|
-
marginAccountId: params.marginAccountId,
|
|
135
|
-
xpEarned: ((_d = result.miscellaneous) === null || _d === void 0 ? void 0 : _d.xpEarned) || 0,
|
|
136
|
-
executionPrice: ((_e = result.tradeDetails) === null || _e === void 0 ? void 0 : _e.executionPrice) || 0,
|
|
137
|
-
base: ((_f = result.tradeDetails) === null || _f === void 0 ? void 0 : _f.base) || 0,
|
|
138
|
-
positions: result.positions,
|
|
139
|
-
orders: result.orders,
|
|
140
|
-
accounts: result.accounts,
|
|
141
|
-
}];
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}); };
|
|
145
|
-
exports.matchOrder = matchOrder;
|
|
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) {
|
|
169
|
-
case 0:
|
|
170
|
-
if (params.orderBase === 0) {
|
|
171
|
-
throw new Error('Position is already closed.');
|
|
172
|
-
}
|
|
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.');
|
|
178
|
-
}
|
|
179
|
-
if (Math.abs(params.closingOrderBase) > Math.abs(params.orderBase)) {
|
|
180
|
-
throw new Error('Specified base to close is larger than the actual order.');
|
|
181
|
-
}
|
|
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]);
|
|
184
|
-
return [4 /*yield*/, executeMarketOrderViaOrdersGateway({
|
|
185
|
-
signer: params.signer,
|
|
186
|
-
marginAccountId: params.marginAccountId,
|
|
187
|
-
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
188
|
-
marketId: params.market.id,
|
|
189
|
-
exchangeId: params.market.exchangeId,
|
|
190
|
-
orderType: common_1.OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
191
|
-
orderInputs: orderInputs,
|
|
192
|
-
})];
|
|
193
|
-
case 1:
|
|
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;
|
|
216
|
-
return [2 /*return*/, {
|
|
217
|
-
transactionHash: (result === null || result === void 0 ? void 0 : result.txHash) || null,
|
|
218
|
-
coreSigNonce: Number(result === null || result === void 0 ? void 0 : result.coreSigNonce) || null,
|
|
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,
|
|
222
|
-
marginAccountId: params.marginAccountId,
|
|
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,
|
|
226
|
-
}];
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
}); };
|
|
230
|
-
exports.closeOrder = closeOrder;
|
|
231
|
-
//# sourceMappingURL=order.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBACjC,wBAAwB,GAAG,IAAI,CAAC,KAAK,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;gBAE3C,qBAAM,IAAA,+BAAsB,EACvD,MAAM,CAAC,MAAM,EACb,WAAW,EACX,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,sBAAsB,EAC7B,MAAM,CAAC,SAAS,EAChB,MAAM,CAAC,WAAW,EAClB,wBAAwB,GAAG,2CAAkC,EAC7D,mBAAmB,CACpB,EAAA;;gBAXK,KAAuB,SAW5B,EAXO,SAAS,eAAA,EAAE,KAAK,WAAA;gBAalB,KAAK,GAAG,SAAS,CAAC;sBAGpB,MAAM,CAAC,eAAe;oBACtB,MAAM,CAAC,QAAQ;oBACf,MAAM,CAAC,UAAU;oBACjB,MAAM,CAAC,sBAAsB;oBAC7B,MAAM,CAAC,SAAS;oBAChB,MAAM,CAAC,WAAW;gBAClB,qBAAM,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,EAAA;;gBAR9B,MAAM;;wBA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,EAAtB,wBAAsB;gBAClB,qBAAM,cAAc,CAAC,MAAM,CAAC,EAAA;;gBAA5B,KAAA,SAA4B,CAAA;;oBAC5B,qBAAM,iBAAiB,CAAC,MAAM,CAAC,EAAA;;gBAA/B,KAAA,SAA+B,CAAA;;;gBAH/B,MAAM,KAGyB;gBAErC,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,KAAI,IAAI;wBACvC,YAAY,EAAE,MAAM,CAAC,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,YAAY,CAAC,IAAI,IAAI;wBAClD,OAAO,EAAE,CAAA,MAAA,MAAM,CAAC,aAAa,0CAAE,YAAY,KAAI,CAAC;wBAChD,cAAc,EAAE,CAAA,MAAA,MAAM,CAAC,aAAa,0CAAE,cAAc,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,4 +0,0 @@
|
|
|
1
|
-
import { CloseOrderParams, CloseOrderResult, MatchOrderParams, MatchOrderResult } from './types';
|
|
2
|
-
export declare const matchOrder: (params: MatchOrderParams) => Promise<MatchOrderResult>;
|
|
3
|
-
export declare const closeOrder: (params: CloseOrderParams) => Promise<CloseOrderResult>;
|
|
4
|
-
//# sourceMappingURL=order.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CloseOrderParams,
|
|
3
|
-
CloseOrderResult,
|
|
4
|
-
MatchOrderParams,
|
|
5
|
-
MatchOrderResult,
|
|
6
|
-
OrdersGatewayMarketOrderParams,
|
|
7
|
-
} from './types';
|
|
8
|
-
import { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';
|
|
9
|
-
import {
|
|
10
|
-
calculateMaxPriceLimit,
|
|
11
|
-
ORDERS_GATEWAY_DEADLINE_IN_SECONDS,
|
|
12
|
-
OrdersGatewayAbi,
|
|
13
|
-
OrdersGatewayOrderType,
|
|
14
|
-
PositionEntity,
|
|
15
|
-
signOrdersGatewayOrder,
|
|
16
|
-
TransactionExecutionOutput,
|
|
17
|
-
} from '@reyaxyz/common';
|
|
18
|
-
import BigNumber from 'bignumber.js';
|
|
19
|
-
import { ContractType } from '@reyaxyz/common';
|
|
20
|
-
import { scale } from '@reyaxyz/common';
|
|
21
|
-
import { getReyaNetwork } from '../../utils/network';
|
|
22
|
-
import { AbiCoder, Interface } from 'ethers';
|
|
23
|
-
|
|
24
|
-
const executeMarketOrderViaOrdersGateway = async (
|
|
25
|
-
params: OrdersGatewayMarketOrderParams,
|
|
26
|
-
): Promise<TransactionExecutionOutput> => {
|
|
27
|
-
const reyaChainId = getReyaNetwork();
|
|
28
|
-
const creationTimestampMs = Date.now();
|
|
29
|
-
const creationTimestampSeconds = Math.floor(creationTimestampMs / 1000);
|
|
30
|
-
|
|
31
|
-
const { signature, nonce } = await signOrdersGatewayOrder(
|
|
32
|
-
params.signer,
|
|
33
|
-
reyaChainId,
|
|
34
|
-
params.marginAccountId,
|
|
35
|
-
params.marketId,
|
|
36
|
-
params.exchangeId,
|
|
37
|
-
params.counterpartyAccountIds,
|
|
38
|
-
params.orderType,
|
|
39
|
-
params.orderInputs,
|
|
40
|
-
creationTimestampSeconds + ORDERS_GATEWAY_DEADLINE_IN_SECONDS,
|
|
41
|
-
creationTimestampMs,
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const fnSig = 'execute';
|
|
45
|
-
const fnArgs = [
|
|
46
|
-
[
|
|
47
|
-
params.marginAccountId,
|
|
48
|
-
params.marketId,
|
|
49
|
-
params.exchangeId,
|
|
50
|
-
params.counterpartyAccountIds,
|
|
51
|
-
params.orderType,
|
|
52
|
-
params.orderInputs,
|
|
53
|
-
await params.signer.getAddress(),
|
|
54
|
-
nonce,
|
|
55
|
-
],
|
|
56
|
-
[signature.v, signature.r, signature.s, signature.deadline],
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
const ogInterface = new Interface(OrdersGatewayAbi);
|
|
60
|
-
const data = ogInterface.encodeFunctionData(fnSig, fnArgs);
|
|
61
|
-
|
|
62
|
-
const result = await signAndBroadcastTransaction(
|
|
63
|
-
data,
|
|
64
|
-
reyaChainId,
|
|
65
|
-
ContractType.ORDERS_GATEWAY_PROXY,
|
|
66
|
-
{
|
|
67
|
-
accountId: params.marginAccountId,
|
|
68
|
-
marketId: params.marketId,
|
|
69
|
-
tradeSource: params.tradeSource,
|
|
70
|
-
action: 'ioc-order-ui',
|
|
71
|
-
},
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
return result;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const matchOrder = async (
|
|
78
|
-
params: MatchOrderParams,
|
|
79
|
-
): Promise<MatchOrderResult> => {
|
|
80
|
-
if (params.amountInBase === 0) {
|
|
81
|
-
throw new Error('Order cannot be of size zero.');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let snappedAmount = BigNumber(params.amountInBase)
|
|
85
|
-
.abs()
|
|
86
|
-
.dividedBy(params.market.baseSpacing)
|
|
87
|
-
.integerValue(BigNumber.ROUND_FLOOR)
|
|
88
|
-
.multipliedBy(params.market.baseSpacing)
|
|
89
|
-
.toNumber();
|
|
90
|
-
|
|
91
|
-
if (params.amountInBase < 0) {
|
|
92
|
-
snappedAmount = -snappedAmount;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (Math.abs(snappedAmount) < params.market.minOrderSizeBase) {
|
|
96
|
-
console.error(
|
|
97
|
-
`Minimum trade amount ${params.market.minOrderSizeBase}. Entered amount ${snappedAmount}, ${params.amountInBase}`,
|
|
98
|
-
);
|
|
99
|
-
throw new Error(`Minimum trade amount ${params.market.minOrderSizeBase}`);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const orderBaseE18 = scale(18)(snappedAmount);
|
|
103
|
-
const orderPriceLimitE18 = calculateMaxPriceLimit(snappedAmount > 0);
|
|
104
|
-
const orderInputs = AbiCoder.defaultAbiCoder().encode(
|
|
105
|
-
['int256', 'uint256'],
|
|
106
|
-
[orderBaseE18, orderPriceLimitE18],
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const result = await executeMarketOrderViaOrdersGateway({
|
|
110
|
-
signer: params.signer,
|
|
111
|
-
marginAccountId: params.marginAccountId,
|
|
112
|
-
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
113
|
-
marketId: params.market.id,
|
|
114
|
-
exchangeId: params.market.exchangeId,
|
|
115
|
-
tradeSource: params.tradeSource,
|
|
116
|
-
orderType: OrdersGatewayOrderType.MARKET_ORDER,
|
|
117
|
-
orderInputs,
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
transactionHash: result?.txHash || null,
|
|
122
|
-
coreSigNonce: Number(result?.coreSigNonce) || null,
|
|
123
|
-
xpBoost: result.miscellaneous?.tradeXpBoost || 0,
|
|
124
|
-
lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,
|
|
125
|
-
isNftWon: result.miscellaneous?.xpNftAwarded || false,
|
|
126
|
-
marginAccountId: params.marginAccountId,
|
|
127
|
-
xpEarned: result.miscellaneous?.xpEarned || 0,
|
|
128
|
-
executionPrice: result.tradeDetails?.executionPrice || 0,
|
|
129
|
-
base: result.tradeDetails?.base || 0,
|
|
130
|
-
positions: result.positions,
|
|
131
|
-
orders: result.orders,
|
|
132
|
-
accounts: result.accounts,
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
|
|
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> => {
|
|
158
|
-
if (params.orderBase === 0) {
|
|
159
|
-
throw new Error('Position is already closed.');
|
|
160
|
-
}
|
|
161
|
-
|
|
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
|
-
}
|
|
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
|
-
);
|
|
181
|
-
|
|
182
|
-
const result = await executeMarketOrderViaOrdersGateway({
|
|
183
|
-
signer: params.signer,
|
|
184
|
-
marginAccountId: params.marginAccountId,
|
|
185
|
-
counterpartyAccountIds: params.market.counterpartyAccountIds,
|
|
186
|
-
marketId: params.market.id,
|
|
187
|
-
exchangeId: params.market.exchangeId,
|
|
188
|
-
orderType: OrdersGatewayOrderType.REDUCE_ONLY_MARKET_ORDER,
|
|
189
|
-
orderInputs,
|
|
190
|
-
});
|
|
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
|
-
|
|
203
|
-
return {
|
|
204
|
-
transactionHash: result?.txHash || null,
|
|
205
|
-
coreSigNonce: Number(result?.coreSigNonce) || null,
|
|
206
|
-
xpBoost: result.miscellaneous?.tradeXpBoost || 0,
|
|
207
|
-
lotteryXpBoost: result.miscellaneous?.lotteryXpBoost || 0,
|
|
208
|
-
isNftWon: result.miscellaneous?.xpNftAwarded || false,
|
|
209
|
-
marginAccountId: params.marginAccountId,
|
|
210
|
-
xpEarned: result.miscellaneous?.xpEarned || 0,
|
|
211
|
-
executionPrice: result.tradeDetails?.executionPrice || 0,
|
|
212
|
-
base: result.tradeDetails?.base || 0,
|
|
213
|
-
};
|
|
214
|
-
};
|