@reyaxyz/sdk 0.3.0 → 0.4.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/dist/services/orders/order.js +27 -1
- 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 +2 -1
- package/dist/types/services/orders/order.d.ts.map +1 -1
- package/dist/types/services/orders/types.d.ts +9 -0
- package/dist/types/services/orders/types.d.ts.map +1 -1
- package/dist/types/utils/trade.d.ts +1 -1
- package/dist/types/utils/trade.d.ts.map +1 -1
- package/dist/utils/trade.js +3 -3
- package/dist/utils/trade.js.map +1 -1
- package/package.json +2 -2
- package/src/services/orders/order.ts +45 -1
- package/src/services/orders/types.ts +11 -0
- package/src/utils/trade.ts +3 -3
|
@@ -35,12 +35,16 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
35
35
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
38
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.matchOrder = void 0;
|
|
42
|
+
exports.closeOrder = exports.matchOrder = void 0;
|
|
40
43
|
var encode_1 = require("./encode");
|
|
41
44
|
var executeTransaction_1 = require("../executeTransaction");
|
|
42
45
|
var consts_1 = require("../../utils/consts");
|
|
43
46
|
var trade_1 = require("../../utils/trade");
|
|
47
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
44
48
|
var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
45
49
|
var orderPriceLimit, orderBase, _a, data, value, chainId, result;
|
|
46
50
|
return __generator(this, function (_b) {
|
|
@@ -63,4 +67,26 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
63
67
|
});
|
|
64
68
|
}); };
|
|
65
69
|
exports.matchOrder = matchOrder;
|
|
70
|
+
var closeOrder = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
71
|
+
var base, orderPriceLimit, _a, data, value, chainId, result;
|
|
72
|
+
return __generator(this, function (_b) {
|
|
73
|
+
switch (_b.label) {
|
|
74
|
+
case 0:
|
|
75
|
+
if (params.orderBase === 0) {
|
|
76
|
+
throw new Error('Position base can not be 0');
|
|
77
|
+
}
|
|
78
|
+
base = (0, bignumber_js_1.default)(params.orderBase).negated().toNumber();
|
|
79
|
+
orderPriceLimit = (0, trade_1.calculatePriceLimitForTrade)(params.market.currentPrice, base);
|
|
80
|
+
_a = (0, encode_1.encodeMatchOrder)(params.marginAccountId, BigInt(base), orderPriceLimit, params.market.counterpartyAccountIds, params.market.id, params.market.exchangeId), data = _a.calldata, value = _a.value;
|
|
81
|
+
chainId = 80001;
|
|
82
|
+
return [4 /*yield*/, (0, executeTransaction_1.executeTransaction)(params.signer, data, value, chainId, consts_1.TransactionType.TRADE)];
|
|
83
|
+
case 1:
|
|
84
|
+
result = _b.sent();
|
|
85
|
+
return [2 /*return*/, {
|
|
86
|
+
transactionHash: (result === null || result === void 0 ? void 0 : result.hash) || null,
|
|
87
|
+
}];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}); };
|
|
91
|
+
exports.closeOrder = closeOrder;
|
|
66
92
|
//# sourceMappingURL=order.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,mCAA4C;AAC5C,4DAA2D;AAC3D,6CAAqD;AACrD,2CAG2B;AAC3B,8DAAqC;AAE9B,IAAM,UAAU,GAAG,UACxB,MAAwB;;;;;gBAExB,IAAI,MAAM,CAAC,SAAS,KAAK,CAAC,EAAE,CAAC;oBAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;gBAChD,CAAC;gBACK,eAAe,GAAG,IAAA,mCAA2B,EACjD,MAAM,CAAC,MAAM,CAAC,YAAY,EAC1B,MAAM,CAAC,SAAS,CACjB,CAAC;gBAEI,SAAS,GAAG,IAAA,uCAA+B,EAC/C,MAAM,CAAC,SAAS,EAChB,MAAM,CAAC,MAAM,CAAC,YAAY,CAC3B,CAAC;gBAEI,KAA4B,IAAA,yBAAgB,EAChD,MAAM,CAAC,eAAe,EACtB,SAAS,EACT,eAAe,EACf,MAAM,CAAC,MAAM,CAAC,sBAAsB,EACpC,MAAM,CAAC,MAAM,CAAC,EAAE,EAChB,MAAM,CAAC,MAAM,CAAC,UAAU,CACzB,EAPiB,IAAI,cAAA,EAAE,KAAK,WAAA,CAO3B;gBAGI,OAAO,GAAG,KAAK,CAAC;gBACP,qBAAM,IAAA,uCAAkB,EACrC,MAAM,CAAC,MAAM,EACb,IAAI,EACJ,KAAK,EACL,OAAO,EACP,wBAAe,CAAC,KAAK,CACtB,EAAA;;gBANK,MAAM,GAAG,SAMd;gBAED,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,KAAI,IAAI;qBACtC,EAAC;;;KACH,CAAC;AAtCW,QAAA,UAAU,cAsCrB;AAEK,IAAM,UAAU,GAAG,UACxB,MAAwB;;;;;gBAExB,IAAI,MAAM,CAAC,SAAS,KAAK,CAAC,EAAE,CAAC;oBAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;gBAChD,CAAC;gBAEK,IAAI,GAAG,IAAA,sBAAS,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC;gBAExD,eAAe,GAAG,IAAA,mCAA2B,EACjD,MAAM,CAAC,MAAM,CAAC,YAAY,EAC1B,IAAI,CACL,CAAC;gBAEI,KAA4B,IAAA,yBAAgB,EAChD,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,IAAI,CAAC,EACZ,eAAe,EACf,MAAM,CAAC,MAAM,CAAC,sBAAsB,EACpC,MAAM,CAAC,MAAM,CAAC,EAAE,EAChB,MAAM,CAAC,MAAM,CAAC,UAAU,CACzB,EAPiB,IAAI,cAAA,EAAE,KAAK,WAAA,CAO3B;gBAGI,OAAO,GAAG,KAAK,CAAC;gBACP,qBAAM,IAAA,uCAAkB,EACrC,MAAM,CAAC,MAAM,EACb,IAAI,EACJ,KAAK,EACL,OAAO,EACP,wBAAe,CAAC,KAAK,CACtB,EAAA;;gBANK,MAAM,GAAG,SAMd;gBAED,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,KAAI,IAAI;qBACtC,EAAC;;;KACH,CAAC;AApCW,QAAA,UAAU,cAoCrB","sourcesContent":["import {\n CloseOrderParams,\n CloseOrderResult,\n MatchOrderParams,\n MatchOrderResult,\n} from './types';\nimport { encodeMatchOrder } from './encode';\nimport { executeTransaction } from '../executeTransaction';\nimport { TransactionType } from '../../utils/consts';\nimport {\n calculateOrderBaseFromOrderSize,\n calculatePriceLimitForTrade,\n} from '../../utils/trade';\nimport BigNumber from 'bignumber.js';\n\nexport const matchOrder = async (\n params: MatchOrderParams,\n): Promise<MatchOrderResult> => {\n if (params.orderSize === 0) {\n throw new Error('Position size can not be 0');\n }\n const orderPriceLimit = calculatePriceLimitForTrade(\n params.market.currentPrice,\n params.orderSize,\n );\n\n const orderBase = calculateOrderBaseFromOrderSize(\n params.orderSize,\n params.market.currentPrice,\n );\n\n const { calldata: data, value } = encodeMatchOrder(\n params.marginAccountId,\n orderBase,\n orderPriceLimit,\n params.market.counterpartyAccountIds,\n params.market.id,\n params.market.exchangeId,\n );\n\n // @todo update it\n const chainId = 80001;\n const result = await executeTransaction(\n params.signer,\n data,\n value,\n chainId,\n TransactionType.TRADE,\n );\n\n return {\n transactionHash: result?.hash || null,\n };\n};\n\nexport const closeOrder = async (\n params: CloseOrderParams,\n): Promise<CloseOrderResult> => {\n if (params.orderBase === 0) {\n throw new Error('Position base can not be 0');\n }\n\n const base = BigNumber(params.orderBase).negated().toNumber(); // when closing position we are doing trade in the opposite direction\n\n const orderPriceLimit = calculatePriceLimitForTrade(\n params.market.currentPrice,\n base,\n );\n\n const { calldata: data, value } = encodeMatchOrder(\n params.marginAccountId,\n BigInt(base),\n orderPriceLimit,\n params.market.counterpartyAccountIds,\n params.market.id,\n params.market.exchangeId,\n );\n\n // @todo update it\n const chainId = 80001;\n const result = await executeTransaction(\n params.signer,\n data,\n value,\n chainId,\n TransactionType.TRADE,\n );\n\n return {\n transactionHash: result?.hash || null,\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';\n\nexport type MarketParams = {\n id: number;\n exchangeId: number;\n counterpartyAccountIds: number[];\n currentPrice: number;\n};\n\nexport type MatchOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n orderSize: number;\n market: MarketParams;\n};\n\nexport type MatchOrderResult = {\n transactionHash: string | null;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"/","sources":["services/orders/types.ts"],"names":[],"mappings":"","sourcesContent":["import { Signer, JsonRpcSigner } from 'ethers';\n\nexport type MarketParams = {\n id: number;\n exchangeId: number;\n counterpartyAccountIds: number[];\n currentPrice: number;\n};\n\nexport type MatchOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n orderSize: number;\n market: MarketParams;\n};\n\nexport type CloseOrderParams = {\n signer: Signer | JsonRpcSigner;\n marginAccountId: number;\n orderBase: number; // PositionEntity[base]\n market: MarketParams;\n};\n\nexport type MatchOrderResult = {\n transactionHash: string | null;\n};\n\nexport type CloseOrderResult = {\n transactionHash: string | null;\n};\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import { MatchOrderParams, MatchOrderResult } from './types';
|
|
1
|
+
import { CloseOrderParams, CloseOrderResult, MatchOrderParams, MatchOrderResult } from './types';
|
|
2
2
|
export declare const matchOrder: (params: MatchOrderParams) => Promise<MatchOrderResult>;
|
|
3
|
+
export declare const closeOrder: (params: CloseOrderParams) => Promise<CloseOrderResult>;
|
|
3
4
|
//# sourceMappingURL=order.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.d.ts","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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,EACjB,MAAM,SAAS,CAAC;AAUjB,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAoC1B,CAAC;AAEF,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAkC1B,CAAC"}
|
|
@@ -11,7 +11,16 @@ export type MatchOrderParams = {
|
|
|
11
11
|
orderSize: number;
|
|
12
12
|
market: MarketParams;
|
|
13
13
|
};
|
|
14
|
+
export type CloseOrderParams = {
|
|
15
|
+
signer: Signer | JsonRpcSigner;
|
|
16
|
+
marginAccountId: number;
|
|
17
|
+
orderBase: number;
|
|
18
|
+
market: MarketParams;
|
|
19
|
+
};
|
|
14
20
|
export type MatchOrderResult = {
|
|
15
21
|
transactionHash: string | null;
|
|
16
22
|
};
|
|
23
|
+
export type CloseOrderResult = {
|
|
24
|
+
transactionHash: string | null;
|
|
25
|
+
};
|
|
17
26
|
//# 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;AAE/C,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,sBAAsB,EAAE,MAAM,EAAE,CAAC;IACjC,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,CAAC"}
|
|
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;AAE/C,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,sBAAsB,EAAE,MAAM,EAAE,CAAC;IACjC,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare function calculatePriceLimitForTrade(orderPrice: number,
|
|
1
|
+
export declare function calculatePriceLimitForTrade(orderPrice: number, amount: number): bigint;
|
|
2
2
|
export declare function calculateOrderBaseFromOrderSize(orderSize: number, currentPrice: number): bigint;
|
|
3
3
|
//# sourceMappingURL=trade.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trade.d.ts","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":"AAEA,wBAAgB,2BAA2B,CACzC,UAAU,EAAE,MAAM,EAClB,
|
|
1
|
+
{"version":3,"file":"trade.d.ts","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":"AAEA,wBAAgB,2BAA2B,CACzC,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,GACb,MAAM,CAkBR;AAED,wBAAgB,+BAA+B,CAC7C,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,GACnB,MAAM,CAUR"}
|
package/dist/utils/trade.js
CHANGED
|
@@ -5,13 +5,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.calculateOrderBaseFromOrderSize = exports.calculatePriceLimitForTrade = void 0;
|
|
7
7
|
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
-
function calculatePriceLimitForTrade(orderPrice,
|
|
8
|
+
function calculatePriceLimitForTrade(orderPrice, amount) {
|
|
9
9
|
// Ensure orderPrice is BigInt
|
|
10
10
|
var orderPriceBigInt = (0, bignumber_js_1.default)(orderPrice);
|
|
11
11
|
// Calculate 50% of the order price
|
|
12
12
|
var offset = orderPriceBigInt.div(2);
|
|
13
|
-
// Determine if the
|
|
14
|
-
if (
|
|
13
|
+
// Determine if the amount is positive or negative and adjust the price accordingly
|
|
14
|
+
if (amount > 0) {
|
|
15
15
|
// Trade is long, so add
|
|
16
16
|
return BigInt((0, bignumber_js_1.default)(orderPriceBigInt).plus(offset).times(1e18).toFixed(0));
|
|
17
17
|
}
|
package/dist/utils/trade.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trade.js","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,SAAgB,2BAA2B,CACzC,UAAkB,EAClB,
|
|
1
|
+
{"version":3,"file":"trade.js","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,SAAgB,2BAA2B,CACzC,UAAkB,EAClB,MAAc;IAEd,8BAA8B;IAC9B,IAAM,gBAAgB,GAAG,IAAA,sBAAS,EAAC,UAAU,CAAC,CAAC;IAE/C,mCAAmC;IACnC,IAAM,MAAM,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAEvC,mFAAmF;IACnF,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QACf,wBAAwB;QACxB,OAAO,MAAM,CACX,IAAA,sBAAS,EAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAChE,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CACX,IAAA,sBAAS,EAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CACjE,CAAC;IACJ,CAAC;AACH,CAAC;AArBD,kEAqBC;AAED,SAAgB,+BAA+B,CAC7C,SAAiB,EACjB,YAAoB;IAEpB,IAAM,aAAa,GAAG,IAAA,sBAAS,EAAC,SAAS,CAAC,CAAC,CAAC,OAAO;IACnD,yEAAyE;IAEzE,IAAM,SAAS,GAAG,IAAA,sBAAS,EAAC,SAAS,CAAC;SACnC,KAAK,CAAC,aAAa,CAAC;SACpB,GAAG,CAAC,YAAY,CAAC;SACjB,OAAO,CAAC,CAAC,CAAC,CAAC;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,CAAC;AAC3B,CAAC;AAbD,0EAaC","sourcesContent":["import BigNumber from 'bignumber.js';\n\nexport function calculatePriceLimitForTrade(\n orderPrice: number,\n amount: number,\n): bigint {\n // Ensure orderPrice is BigInt\n const orderPriceBigInt = BigNumber(orderPrice);\n\n // Calculate 50% of the order price\n const offset = orderPriceBigInt.div(2);\n\n // Determine if the amount is positive or negative and adjust the price accordingly\n if (amount > 0) {\n // Trade is long, so add\n return BigInt(\n BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),\n );\n } else {\n return BigInt(\n BigNumber(orderPriceBigInt).minus(offset).times(1e18).toFixed(0),\n );\n }\n}\n\nexport function calculateOrderBaseFromOrderSize(\n orderSize: number,\n currentPrice: number,\n): bigint {\n const decimalFactor = BigNumber('1000000'); // 10^6\n // @todo check decimal places handling if this should stay hardcoded to 6\n\n const orderBase = BigNumber(orderSize)\n .times(decimalFactor)\n .div(currentPrice)\n .toFixed(0);\n\n return BigInt(orderBase);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"ethers": "6.9.0"
|
|
34
34
|
},
|
|
35
35
|
"packageManager": "pnpm@8.10.4",
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "87e449e615b82a9bcd2e38296f2c2c0daa09a61b"
|
|
37
37
|
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CloseOrderParams,
|
|
3
|
+
CloseOrderResult,
|
|
4
|
+
MatchOrderParams,
|
|
5
|
+
MatchOrderResult,
|
|
6
|
+
} from './types';
|
|
2
7
|
import { encodeMatchOrder } from './encode';
|
|
3
8
|
import { executeTransaction } from '../executeTransaction';
|
|
4
9
|
import { TransactionType } from '../../utils/consts';
|
|
@@ -6,6 +11,7 @@ import {
|
|
|
6
11
|
calculateOrderBaseFromOrderSize,
|
|
7
12
|
calculatePriceLimitForTrade,
|
|
8
13
|
} from '../../utils/trade';
|
|
14
|
+
import BigNumber from 'bignumber.js';
|
|
9
15
|
|
|
10
16
|
export const matchOrder = async (
|
|
11
17
|
params: MatchOrderParams,
|
|
@@ -46,3 +52,41 @@ export const matchOrder = async (
|
|
|
46
52
|
transactionHash: result?.hash || null,
|
|
47
53
|
};
|
|
48
54
|
};
|
|
55
|
+
|
|
56
|
+
export const closeOrder = async (
|
|
57
|
+
params: CloseOrderParams,
|
|
58
|
+
): Promise<CloseOrderResult> => {
|
|
59
|
+
if (params.orderBase === 0) {
|
|
60
|
+
throw new Error('Position base can not be 0');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const base = BigNumber(params.orderBase).negated().toNumber(); // when closing position we are doing trade in the opposite direction
|
|
64
|
+
|
|
65
|
+
const orderPriceLimit = calculatePriceLimitForTrade(
|
|
66
|
+
params.market.currentPrice,
|
|
67
|
+
base,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const { calldata: data, value } = encodeMatchOrder(
|
|
71
|
+
params.marginAccountId,
|
|
72
|
+
BigInt(base),
|
|
73
|
+
orderPriceLimit,
|
|
74
|
+
params.market.counterpartyAccountIds,
|
|
75
|
+
params.market.id,
|
|
76
|
+
params.market.exchangeId,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// @todo update it
|
|
80
|
+
const chainId = 80001;
|
|
81
|
+
const result = await executeTransaction(
|
|
82
|
+
params.signer,
|
|
83
|
+
data,
|
|
84
|
+
value,
|
|
85
|
+
chainId,
|
|
86
|
+
TransactionType.TRADE,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
transactionHash: result?.hash || null,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
@@ -14,6 +14,17 @@ export type MatchOrderParams = {
|
|
|
14
14
|
market: MarketParams;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
export type CloseOrderParams = {
|
|
18
|
+
signer: Signer | JsonRpcSigner;
|
|
19
|
+
marginAccountId: number;
|
|
20
|
+
orderBase: number; // PositionEntity[base]
|
|
21
|
+
market: MarketParams;
|
|
22
|
+
};
|
|
23
|
+
|
|
17
24
|
export type MatchOrderResult = {
|
|
18
25
|
transactionHash: string | null;
|
|
19
26
|
};
|
|
27
|
+
|
|
28
|
+
export type CloseOrderResult = {
|
|
29
|
+
transactionHash: string | null;
|
|
30
|
+
};
|
package/src/utils/trade.ts
CHANGED
|
@@ -2,7 +2,7 @@ import BigNumber from 'bignumber.js';
|
|
|
2
2
|
|
|
3
3
|
export function calculatePriceLimitForTrade(
|
|
4
4
|
orderPrice: number,
|
|
5
|
-
|
|
5
|
+
amount: number,
|
|
6
6
|
): bigint {
|
|
7
7
|
// Ensure orderPrice is BigInt
|
|
8
8
|
const orderPriceBigInt = BigNumber(orderPrice);
|
|
@@ -10,8 +10,8 @@ export function calculatePriceLimitForTrade(
|
|
|
10
10
|
// Calculate 50% of the order price
|
|
11
11
|
const offset = orderPriceBigInt.div(2);
|
|
12
12
|
|
|
13
|
-
// Determine if the
|
|
14
|
-
if (
|
|
13
|
+
// Determine if the amount is positive or negative and adjust the price accordingly
|
|
14
|
+
if (amount > 0) {
|
|
15
15
|
// Trade is long, so add
|
|
16
16
|
return BigInt(
|
|
17
17
|
BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),
|