@yuants/vendor-aster 0.11.8 → 0.11.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/services/orders/listOrders.js +17 -2
- package/dist/services/orders/listOrders.js.map +1 -1
- package/dist/services/orders/order-type-mapping.test.js +82 -0
- package/dist/services/orders/order-type-mapping.test.js.map +1 -0
- package/dist/services/orders/submitOrder.js +21 -4
- package/dist/services/orders/submitOrder.js.map +1 -1
- package/lib/services/orders/listOrders.d.ts.map +1 -1
- package/lib/services/orders/listOrders.js +17 -2
- package/lib/services/orders/listOrders.js.map +1 -1
- package/lib/services/orders/order-type-mapping.test.d.ts +2 -0
- package/lib/services/orders/order-type-mapping.test.d.ts.map +1 -0
- package/lib/services/orders/order-type-mapping.test.js +84 -0
- package/lib/services/orders/order-type-mapping.test.js.map +1 -0
- package/lib/services/orders/submitOrder.d.ts.map +1 -1
- package/lib/services/orders/submitOrder.js +21 -4
- package/lib/services/orders/submitOrder.js.map +1 -1
- package/package.json +5 -5
- package/temp/build/typescript/ts_Ro1DJWVV.json +1 -1
- package/temp/package-deps.json +8 -7
- package/temp/test/jest/haste-map-0df2c1646479030bb486acc96d7e140a-62fc6fbbf847e4167de25f4822a8f953-1b0100527d0283bc2ce2729ac44cdebc +0 -0
- package/temp/test/jest/perf-cache-0df2c1646479030bb486acc96d7e140a-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import { getApiV1OpenOrders, getFApiV1OpenOrders, } from '../../api/private-api';
|
|
2
2
|
import { encodePath } from '@yuants/utils';
|
|
3
|
+
const mapAsterOrderTypeToYuants = (type, timeInForce) => {
|
|
4
|
+
if (type === 'MARKET') {
|
|
5
|
+
return 'MARKET';
|
|
6
|
+
}
|
|
7
|
+
if (type === 'LIMIT') {
|
|
8
|
+
if (timeInForce === 'GTX')
|
|
9
|
+
return 'MAKER';
|
|
10
|
+
if (timeInForce === 'IOC')
|
|
11
|
+
return 'IOC';
|
|
12
|
+
if (timeInForce === 'FOK')
|
|
13
|
+
return 'FOK';
|
|
14
|
+
return 'LIMIT';
|
|
15
|
+
}
|
|
16
|
+
return 'LIMIT';
|
|
17
|
+
};
|
|
3
18
|
const resolvePerpOrderDirection = (asterOrder) => {
|
|
4
19
|
const reduceOnly = asterOrder.reduceOnly || asterOrder.closePosition;
|
|
5
20
|
if (asterOrder.positionSide === 'LONG') {
|
|
@@ -25,7 +40,7 @@ export const mapPerpOrder = (order, account_id) => {
|
|
|
25
40
|
order_id: `${order.orderId}`,
|
|
26
41
|
account_id,
|
|
27
42
|
product_id: encodePath('ASTER', 'PERP', order.symbol),
|
|
28
|
-
order_type: order.type,
|
|
43
|
+
order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),
|
|
29
44
|
order_direction: resolvePerpOrderDirection(order),
|
|
30
45
|
volume: Number.isFinite(volume) ? volume : 0,
|
|
31
46
|
price: Number.isFinite(price) ? price : undefined,
|
|
@@ -45,7 +60,7 @@ export const mapSpotOrder = (order, account_id) => {
|
|
|
45
60
|
order_id: `${order.orderId}`,
|
|
46
61
|
account_id,
|
|
47
62
|
product_id: encodePath('ASTER', 'SPOT', order.symbol),
|
|
48
|
-
order_type: order.type,
|
|
63
|
+
order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),
|
|
49
64
|
order_direction: resolveSpotOrderDirection(order.side),
|
|
50
65
|
volume: Number.isFinite(volume) ? volume : 0,
|
|
51
66
|
price: Number.isFinite(price) ? price : undefined,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listOrders.js","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,mBAAmB,GAIpB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAI3C,MAAM,yBAAyB,GAAG,CAAC,UAAiC,EAAkB,EAAE;IACtF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AC,UAAU,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,mBAAmB,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IAC9E,OAAO,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfListOrders, IOrder } from '@yuants/data-order';\nimport {\n getApiV1OpenOrders,\n getFApiV1OpenOrders,\n IAsterFutureOpenOrder,\n IAsterSpotOpenOrder,\n ICredential,\n} from '../../api/private-api';\nimport { encodePath } from '@yuants/utils';\n\ntype OrderDirection = 'OPEN_LONG' | 'OPEN_SHORT' | 'CLOSE_LONG' | 'CLOSE_SHORT';\n\nconst mapAsterOrderTypeToYuants = (type?: string, timeInForce?: string): IOrder['order_type'] => {\n if (type === 'MARKET') {\n return 'MARKET';\n }\n if (type === 'LIMIT') {\n if (timeInForce === 'GTX') return 'MAKER';\n if (timeInForce === 'IOC') return 'IOC';\n if (timeInForce === 'FOK') return 'FOK';\n return 'LIMIT';\n }\n return 'LIMIT';\n};\n\nconst resolvePerpOrderDirection = (asterOrder: IAsterFutureOpenOrder): OrderDirection => {\n const reduceOnly = asterOrder.reduceOnly || asterOrder.closePosition;\n\n if (asterOrder.positionSide === 'LONG') {\n return asterOrder.side === 'BUY' ? 'OPEN_LONG' : 'CLOSE_LONG';\n }\n if (asterOrder.positionSide === 'SHORT') {\n return asterOrder.side === 'BUY' ? 'CLOSE_SHORT' : 'OPEN_SHORT';\n }\n if (reduceOnly) {\n return asterOrder.side === 'BUY' ? 'CLOSE_SHORT' : 'CLOSE_LONG';\n }\n return asterOrder.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT';\n};\n\nconst resolveSpotOrderDirection = (side: 'BUY' | 'SELL'): OrderDirection => {\n return side === 'SELL' ? 'CLOSE_LONG' : 'OPEN_LONG';\n};\n\nexport const mapPerpOrder = (order: IAsterFutureOpenOrder, account_id: string): IOrder => {\n const volume = Number(order.origQty);\n const tradedVolume = Number(order.executedQty);\n const price = Number(order.price);\n const avgPrice = Number(order.avgPrice);\n\n return {\n order_id: `${order.orderId}`,\n account_id,\n product_id: encodePath('ASTER', 'PERP', order.symbol),\n order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: resolvePerpOrderDirection(order),\n volume: Number.isFinite(volume) ? volume : 0,\n price: Number.isFinite(price) ? price : undefined,\n submit_at: order.updateTime,\n traded_volume: Number.isFinite(tradedVolume) ? tradedVolume : undefined,\n traded_price: Number.isFinite(avgPrice) && avgPrice > 0 ? avgPrice : undefined,\n order_status: order.status,\n };\n};\n\nexport const mapSpotOrder = (order: IAsterSpotOpenOrder, account_id: string): IOrder => {\n const volume = Number(order.origQty);\n const tradedVolume = Number(order.executedQty);\n const price = Number(order.price);\n const avgPrice = Number(order.avgPrice ?? order.price);\n\n return {\n order_id: `${order.orderId}`,\n account_id,\n product_id: encodePath('ASTER', 'SPOT', order.symbol),\n order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: resolveSpotOrderDirection(order.side),\n volume: Number.isFinite(volume) ? volume : 0,\n price: Number.isFinite(price) ? price : undefined,\n submit_at: order.updateTime ?? order.time,\n traded_volume: Number.isFinite(tradedVolume) ? tradedVolume : undefined,\n traded_price: Number.isFinite(avgPrice) && avgPrice > 0 ? avgPrice : undefined,\n order_status: order.status,\n };\n};\n\nexport const listOrders = async (credential: ICredential) => {\n const [prepOrders, spotOrders] = await Promise.all([\n getFApiV1OpenOrders(credential, {}),\n getApiV1OpenOrders(credential, {}),\n ]);\n return [\n ...prepOrders.map((order) => mapPerpOrder(order, '')),\n ...spotOrders.map((order) => mapSpotOrder(order, '')),\n ];\n};\n\nexport const listSpotOrders = async (credential: ICredential) => {\n const [spotOrders] = await Promise.all([getApiV1OpenOrders(credential, {})]);\n return [...spotOrders.map((order) => mapSpotOrder(order, ''))];\n};\n\nexport const listPrepOrders = async (credential: ICredential) => {\n const [prepOrders] = await Promise.all([getFApiV1OpenOrders(credential, {})]);\n return [...prepOrders.map((order) => mapPerpOrder(order, ''))];\n};\n"]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
jest.mock('../../api/private-api', () => ({
|
|
2
|
+
getApiV1TickerPrice: jest.fn(),
|
|
3
|
+
postApiV1Order: jest.fn(),
|
|
4
|
+
postFApiV1Order: jest.fn(),
|
|
5
|
+
}));
|
|
6
|
+
jest.mock('../trade-history', () => ({
|
|
7
|
+
fetchTradeHistory: jest.fn(),
|
|
8
|
+
}));
|
|
9
|
+
import { postApiV1Order, postFApiV1Order } from '../../api/private-api';
|
|
10
|
+
import { mapPerpOrder, mapSpotOrder } from './listOrders';
|
|
11
|
+
import { handleSubmitOrder } from './submitOrder';
|
|
12
|
+
const mockedPostApiV1Order = jest.mocked(postApiV1Order);
|
|
13
|
+
const mockedPostFApiV1Order = jest.mocked(postFApiV1Order);
|
|
14
|
+
const baseSpotOrder = {
|
|
15
|
+
symbol: 'BTCUSDT',
|
|
16
|
+
orderId: 11,
|
|
17
|
+
clientOrderId: 'spot-order',
|
|
18
|
+
price: '12345',
|
|
19
|
+
origQty: '1',
|
|
20
|
+
executedQty: '0',
|
|
21
|
+
status: 'NEW',
|
|
22
|
+
timeInForce: 'GTC',
|
|
23
|
+
type: 'LIMIT',
|
|
24
|
+
side: 'BUY',
|
|
25
|
+
time: 1,
|
|
26
|
+
updateTime: 2,
|
|
27
|
+
};
|
|
28
|
+
const basePerpOrder = {
|
|
29
|
+
symbol: 'BTCUSDT',
|
|
30
|
+
orderId: 21,
|
|
31
|
+
clientOrderId: 'perp-order',
|
|
32
|
+
price: '23456',
|
|
33
|
+
origQty: '2',
|
|
34
|
+
executedQty: '0',
|
|
35
|
+
status: 'NEW',
|
|
36
|
+
timeInForce: 'GTC',
|
|
37
|
+
type: 'LIMIT',
|
|
38
|
+
side: 'BUY',
|
|
39
|
+
updateTime: 3,
|
|
40
|
+
avgPrice: '0',
|
|
41
|
+
};
|
|
42
|
+
describe('Aster order type mappings', () => {
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
jest.resetAllMocks();
|
|
45
|
+
mockedPostApiV1Order.mockResolvedValue({ orderId: 1 });
|
|
46
|
+
mockedPostFApiV1Order.mockResolvedValue({ orderId: 2 });
|
|
47
|
+
});
|
|
48
|
+
test.each([
|
|
49
|
+
['ASTER/SPOT/BTCUSDT', 'IOC', 'IOC'],
|
|
50
|
+
['ASTER/SPOT/BTCUSDT', 'FOK', 'FOK'],
|
|
51
|
+
['ASTER/PERP/BTCUSDT', 'IOC', 'IOC'],
|
|
52
|
+
['ASTER/PERP/BTCUSDT', 'FOK', 'FOK'],
|
|
53
|
+
])('submitOrder sends %s %s with timeInForce=%s', async (product_id, order_type, tif) => {
|
|
54
|
+
const result = await handleSubmitOrder({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, {
|
|
55
|
+
account_id: 'ASTER/test',
|
|
56
|
+
product_id,
|
|
57
|
+
order_type,
|
|
58
|
+
order_direction: 'OPEN_LONG',
|
|
59
|
+
volume: 1,
|
|
60
|
+
price: 12345,
|
|
61
|
+
});
|
|
62
|
+
expect(result).toEqual({ order_id: product_id.includes('/SPOT/') ? '1' : '2' });
|
|
63
|
+
if (product_id.includes('/SPOT/')) {
|
|
64
|
+
expect(mockedPostApiV1Order).toHaveBeenCalledWith({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
expect(mockedPostFApiV1Order).toHaveBeenCalledWith({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
68
|
+
});
|
|
69
|
+
test('mapSpotOrder restores LIMIT plus timeInForce variants', () => {
|
|
70
|
+
expect(mapSpotOrder(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'GTC' }), '')).toMatchObject({ order_type: 'LIMIT' });
|
|
71
|
+
expect(mapSpotOrder(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'GTX' }), '')).toMatchObject({ order_type: 'MAKER' });
|
|
72
|
+
expect(mapSpotOrder(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'IOC' }), '')).toMatchObject({ order_type: 'IOC' });
|
|
73
|
+
expect(mapSpotOrder(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'FOK' }), '')).toMatchObject({ order_type: 'FOK' });
|
|
74
|
+
});
|
|
75
|
+
test('mapPerpOrder restores LIMIT plus timeInForce variants', () => {
|
|
76
|
+
expect(mapPerpOrder(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'GTC' }), '')).toMatchObject({ order_type: 'LIMIT' });
|
|
77
|
+
expect(mapPerpOrder(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'GTX' }), '')).toMatchObject({ order_type: 'MAKER' });
|
|
78
|
+
expect(mapPerpOrder(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'IOC' }), '')).toMatchObject({ order_type: 'IOC' });
|
|
79
|
+
expect(mapPerpOrder(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'FOK' }), '')).toMatchObject({ order_type: 'FOK' });
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
//# sourceMappingURL=order-type-mapping.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-type-mapping.test.js","sourceRoot":"","sources":["../../../src/services/orders/order-type-mapping.test.ts"],"names":[],"mappings":"AAEA,IAAI,CAAC,IAAI,CAAC,uBAAuB,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,mBAAmB,EAAE,IAAI,CAAC,EAAE,EAAE;IAC9B,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;IACzB,eAAe,EA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AAE,CAAC,CAAC;QAC1G,MAAM,CAAC,YAAY,iCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;QAC1G,MAAM,CAAC,YAAY,iCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;QACxG,MAAM,CAAC,YAAY,iCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;IAC1G,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import type { IAsterFutureOpenOrder, IAsterSpotOpenOrder } from '../../api/private-api';\n\njest.mock('../../api/private-api', () => ({\n getApiV1TickerPrice: jest.fn(),\n postApiV1Order: jest.fn(),\n postFApiV1Order: jest.fn(),\n}));\n\njest.mock('../trade-history', () => ({\n fetchTradeHistory: jest.fn(),\n}));\n\nimport { postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { mapPerpOrder, mapSpotOrder } from './listOrders';\nimport { handleSubmitOrder } from './submitOrder';\n\nconst mockedPostApiV1Order = jest.mocked(postApiV1Order);\nconst mockedPostFApiV1Order = jest.mocked(postFApiV1Order);\n\nconst baseSpotOrder: IAsterSpotOpenOrder = {\n symbol: 'BTCUSDT',\n orderId: 11,\n clientOrderId: 'spot-order',\n price: '12345',\n origQty: '1',\n executedQty: '0',\n status: 'NEW',\n timeInForce: 'GTC',\n type: 'LIMIT',\n side: 'BUY',\n time: 1,\n updateTime: 2,\n};\n\nconst basePerpOrder: IAsterFutureOpenOrder = {\n symbol: 'BTCUSDT',\n orderId: 21,\n clientOrderId: 'perp-order',\n price: '23456',\n origQty: '2',\n executedQty: '0',\n status: 'NEW',\n timeInForce: 'GTC',\n type: 'LIMIT',\n side: 'BUY',\n updateTime: 3,\n avgPrice: '0',\n};\n\ndescribe('Aster order type mappings', () => {\n beforeEach(() => {\n jest.resetAllMocks();\n mockedPostApiV1Order.mockResolvedValue({ orderId: 1 });\n mockedPostFApiV1Order.mockResolvedValue({ orderId: 2 } as never);\n });\n\n test.each([\n ['ASTER/SPOT/BTCUSDT', 'IOC', 'IOC'],\n ['ASTER/SPOT/BTCUSDT', 'FOK', 'FOK'],\n ['ASTER/PERP/BTCUSDT', 'IOC', 'IOC'],\n ['ASTER/PERP/BTCUSDT', 'FOK', 'FOK'],\n ] as const)('submitOrder sends %s %s with timeInForce=%s', async (product_id, order_type, tif) => {\n const result = await handleSubmitOrder(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n {\n account_id: 'ASTER/test',\n product_id,\n order_type,\n order_direction: 'OPEN_LONG',\n volume: 1,\n price: 12345,\n },\n );\n\n expect(result).toEqual({ order_id: product_id.includes('/SPOT/') ? '1' : '2' });\n\n if (product_id.includes('/SPOT/')) {\n expect(mockedPostApiV1Order).toHaveBeenCalledWith(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n return;\n }\n\n expect(mockedPostFApiV1Order).toHaveBeenCalledWith(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n });\n\n test('mapSpotOrder restores LIMIT plus timeInForce variants', () => {\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'GTC' }, '')).toMatchObject({ order_type: 'LIMIT' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'GTX' }, '')).toMatchObject({ order_type: 'MAKER' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'IOC' }, '')).toMatchObject({ order_type: 'IOC' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'FOK' }, '')).toMatchObject({ order_type: 'FOK' });\n });\n\n test('mapPerpOrder restores LIMIT plus timeInForce variants', () => {\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'GTC' }, '')).toMatchObject({ order_type: 'LIMIT' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'GTX' }, '')).toMatchObject({ order_type: 'MAKER' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'IOC' }, '')).toMatchObject({ order_type: 'IOC' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'FOK' }, '')).toMatchObject({ order_type: 'FOK' });\n });\n});\n"]}
|
|
@@ -11,6 +11,23 @@ const parseProductId = (productId) => {
|
|
|
11
11
|
}
|
|
12
12
|
return { category: undefined, symbol: parts[0] };
|
|
13
13
|
};
|
|
14
|
+
const mapOrderTypeToAsterType = (order_type) => {
|
|
15
|
+
return { MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT', IOC: 'LIMIT', FOK: 'LIMIT' }[order_type];
|
|
16
|
+
};
|
|
17
|
+
const mapOrderTypeToTimeInForce = (order_type) => {
|
|
18
|
+
switch (order_type) {
|
|
19
|
+
case 'MAKER':
|
|
20
|
+
return 'GTX';
|
|
21
|
+
case 'LIMIT':
|
|
22
|
+
return 'GTC';
|
|
23
|
+
case 'IOC':
|
|
24
|
+
return 'IOC';
|
|
25
|
+
case 'FOK':
|
|
26
|
+
return 'FOK';
|
|
27
|
+
default:
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
14
31
|
const handleSubmitOrderOfSpot = async (credential, order) => {
|
|
15
32
|
var _a;
|
|
16
33
|
const { symbol } = parseProductId(order.product_id);
|
|
@@ -18,13 +35,13 @@ const handleSubmitOrderOfSpot = async (credential, order) => {
|
|
|
18
35
|
if (!resolvedSymbol) {
|
|
19
36
|
throw new Error(`Invalid product_id: unable to resolve spot symbol from "${order.product_id}"`);
|
|
20
37
|
}
|
|
21
|
-
const type =
|
|
38
|
+
const type = mapOrderTypeToAsterType(order.order_type);
|
|
22
39
|
if (!type)
|
|
23
40
|
throw new Error(`Unsupported order_type: ${order.order_type}`);
|
|
24
41
|
const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
|
|
25
42
|
if (!side)
|
|
26
43
|
throw new Error(`Unsupported order_direction: ${order.order_direction}`);
|
|
27
|
-
const timeInForce = order.order_type
|
|
44
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
28
45
|
const price = order.price;
|
|
29
46
|
let quantity = order.volume;
|
|
30
47
|
let quoteOrderQty;
|
|
@@ -62,7 +79,7 @@ const handleSubmitOrderOfPerp = async (credential, order) => {
|
|
|
62
79
|
const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
|
|
63
80
|
if (!side)
|
|
64
81
|
throw new Error(`Unsupported order_direction: ${order.order_direction}`);
|
|
65
|
-
const type =
|
|
82
|
+
const type = mapOrderTypeToAsterType(order.order_type);
|
|
66
83
|
if (!type)
|
|
67
84
|
throw new Error(`Unsupported order_type: ${order.order_type}`);
|
|
68
85
|
const quantity = order.volume;
|
|
@@ -76,7 +93,7 @@ const handleSubmitOrderOfPerp = async (credential, order) => {
|
|
|
76
93
|
? 'SHORT'
|
|
77
94
|
: undefined;
|
|
78
95
|
const reduceOnly = order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;
|
|
79
|
-
const timeInForce = order.order_type
|
|
96
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
80
97
|
//
|
|
81
98
|
const res = await postFApiV1Order(credential, {
|
|
82
99
|
symbol,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOr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CAAE,QAAQ,CAAC;IAE/B,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAC3E,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACtB,iBAAiB,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,EAAE,CAAC;AACpC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAA6C,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE;IACrG,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,8BAA8B;IAClF,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;AACjF,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfSubmitOrder } from '@yuants/data-order';\nimport { decodePath, roundToStep } from '@yuants/utils';\nimport { getApiV1TickerPrice, ICredential, postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { fetchTradeHistory } from '../trade-history';\n\nconst parseProductId = (productId?: string) => {\n if (!productId) {\n return { category: undefined as string | undefined, symbol: undefined as string | undefined };\n }\n const parts = decodePath(productId);\n if (parts.length >= 3) {\n return { category: parts[1], symbol: parts.slice(2).join('/') };\n }\n return { category: undefined, symbol: parts[0] };\n};\n\nconst handleSubmitOrderOfSpot: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n const resolvedSymbol = symbol ?? order.product_id;\n if (!resolvedSymbol) {\n throw new Error(`Invalid product_id: unable to resolve spot symbol from \"${order.product_id}\"`);\n }\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice(credential, {});\n const thePrice = spotPrice.find((x) => x.symbol === resolvedSymbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${resolvedSymbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n const res = await postApiV1Order(credential, {\n symbol: resolvedSymbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n if (!res.orderId) {\n throw new Error('Failed to retrieve order ID from response');\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, resolvedSymbol);\n }\n return { order_id: '' + res.orderId };\n};\n\nconst handleSubmitOrderOfPerp: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n if (!symbol) {\n throw new Error(`Invalid product_id: unable to decode symbol from \"${order.product_id}\"`);\n }\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const quantity = order.volume;\n const price = order.price;\n\n const isPositionSingleSide = true; // FIXME: Aster 永续合约仅支持单向持仓模式\n\n const positionSide = isPositionSingleSide\n ? undefined\n : order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_LONG'\n ? 'LONG'\n : order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_SHORT'\n ? 'SHORT'\n : undefined;\n\n const reduceOnly =\n order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;\n\n const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n //\n const res = await postFApiV1Order(credential, {\n symbol,\n side,\n type,\n quantity,\n price,\n timeInForce,\n positionSide,\n reduceOnly,\n });\n\n const orderId =\n (res as any)?.orderId ??\n (res as any)?.order_id ??\n (res as any)?.data?.orderId ??\n (res as any)?.data?.order_id;\n\n if (!orderId) throw new Error('Failed to retrieve order ID from response');\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${orderId}` };\n};\n\nexport const handleSubmitOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const [_, instType] = decodePath(order.product_id); // BITGET/USDT-FUTURES/BTCUSDT\n if (instType === 'SPOT') {\n return handleSubmitOrderOfSpot(credential, order);\n }\n if (instType === 'PERP') {\n return handleSubmitOrderOfPerp(credential, order);\n }\n throw new Error(`Unsupported account_id for SubmitOrder: ${order.account_id}`);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,mBAAmB,E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O,mCAC3B,MAAC,GAAW,aAAX,GAAG,uBAAH,GAAG,CAAU,IAAI,0CAAE,QAAQ,CAAC;IAE/B,IAAI,CAAC,OAAO;QAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAC3E,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACtB,iBAAiB,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,EAAE,CAAC;AACpC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAA6C,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE;IACrG,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,8BAA8B;IAClF,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;AACjF,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfSubmitOrder } from '@yuants/data-order';\nimport { decodePath, roundToStep } from '@yuants/utils';\nimport { getApiV1TickerPrice, ICredential, postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { fetchTradeHistory } from '../trade-history';\n\nconst parseProductId = (productId?: string) => {\n if (!productId) {\n return { category: undefined as string | undefined, symbol: undefined as string | undefined };\n }\n const parts = decodePath(productId);\n if (parts.length >= 3) {\n return { category: parts[1], symbol: parts.slice(2).join('/') };\n }\n return { category: undefined, symbol: parts[0] };\n};\n\nconst mapOrderTypeToAsterType = (order_type?: string) => {\n return ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT', IOC: 'LIMIT', FOK: 'LIMIT' } as const)[\n order_type!\n ];\n};\n\nconst mapOrderTypeToTimeInForce = (order_type?: string) => {\n switch (order_type) {\n case 'MAKER':\n return 'GTX';\n case 'LIMIT':\n return 'GTC';\n case 'IOC':\n return 'IOC';\n case 'FOK':\n return 'FOK';\n default:\n return undefined;\n }\n};\n\nconst handleSubmitOrderOfSpot: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n const resolvedSymbol = symbol ?? order.product_id;\n if (!resolvedSymbol) {\n throw new Error(`Invalid product_id: unable to resolve spot symbol from \"${order.product_id}\"`);\n }\n\n const type = mapOrderTypeToAsterType(order.order_type);\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice(credential, {});\n const thePrice = spotPrice.find((x) => x.symbol === resolvedSymbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${resolvedSymbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n const res = await postApiV1Order(credential, {\n symbol: resolvedSymbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n if (!res.orderId) {\n throw new Error('Failed to retrieve order ID from response');\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, resolvedSymbol);\n }\n return { order_id: '' + res.orderId };\n};\n\nconst handleSubmitOrderOfPerp: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n if (!symbol) {\n throw new Error(`Invalid product_id: unable to decode symbol from \"${order.product_id}\"`);\n }\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const type = mapOrderTypeToAsterType(order.order_type);\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const quantity = order.volume;\n const price = order.price;\n\n const isPositionSingleSide = true; // FIXME: Aster 永续合约仅支持单向持仓模式\n\n const positionSide = isPositionSingleSide\n ? undefined\n : order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_LONG'\n ? 'LONG'\n : order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_SHORT'\n ? 'SHORT'\n : undefined;\n\n const reduceOnly =\n order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;\n\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n\n //\n const res = await postFApiV1Order(credential, {\n symbol,\n side,\n type,\n quantity,\n price,\n timeInForce,\n positionSide,\n reduceOnly,\n });\n\n const orderId =\n (res as any)?.orderId ??\n (res as any)?.order_id ??\n (res as any)?.data?.orderId ??\n (res as any)?.data?.order_id;\n\n if (!orderId) throw new Error('Failed to retrieve order ID from response');\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${orderId}` };\n};\n\nexport const handleSubmitOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const [_, instType] = decodePath(order.product_id); // BITGET/USDT-FUTURES/BTCUSDT\n if (instType === 'SPOT') {\n return handleSubmitOrderOfSpot(credential, order);\n }\n if (instType === 'PERP') {\n return handleSubmitOrderOfPerp(credential, order);\n }\n throw new Error(`Unsupported account_id for SubmitOrder: ${order.account_id}`);\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listOrders.d.ts","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAGL,qBAAqB,EACrB,mBAAmB,EACnB,WAAW,EACZ,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"listOrders.d.ts","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,EAGL,qBAAqB,EACrB,mBAAmB,EACnB,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAqC/B,eAAO,MAAM,YAAY,GAAI,OAAO,qBAAqB,EAAE,YAAY,MAAM,KAAG,MAmB/E,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,OAAO,mBAAmB,EAAE,YAAY,MAAM,KAAG,MAmB7E,CAAC;AAEF,eAAO,MAAM,UAAU,GAAU,YAAY,WAAW,sBASvD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAU,YAAY,WAAW,sBAG3D,CAAC;AAEF,eAAO,MAAM,cAAc,GAAU,YAAY,WAAW,sBAG3D,CAAC"}
|
|
@@ -3,6 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.listPrepOrders = exports.listSpotOrders = exports.listOrders = exports.mapSpotOrder = exports.mapPerpOrder = void 0;
|
|
4
4
|
const private_api_1 = require("../../api/private-api");
|
|
5
5
|
const utils_1 = require("@yuants/utils");
|
|
6
|
+
const mapAsterOrderTypeToYuants = (type, timeInForce) => {
|
|
7
|
+
if (type === 'MARKET') {
|
|
8
|
+
return 'MARKET';
|
|
9
|
+
}
|
|
10
|
+
if (type === 'LIMIT') {
|
|
11
|
+
if (timeInForce === 'GTX')
|
|
12
|
+
return 'MAKER';
|
|
13
|
+
if (timeInForce === 'IOC')
|
|
14
|
+
return 'IOC';
|
|
15
|
+
if (timeInForce === 'FOK')
|
|
16
|
+
return 'FOK';
|
|
17
|
+
return 'LIMIT';
|
|
18
|
+
}
|
|
19
|
+
return 'LIMIT';
|
|
20
|
+
};
|
|
6
21
|
const resolvePerpOrderDirection = (asterOrder) => {
|
|
7
22
|
const reduceOnly = asterOrder.reduceOnly || asterOrder.closePosition;
|
|
8
23
|
if (asterOrder.positionSide === 'LONG') {
|
|
@@ -28,7 +43,7 @@ const mapPerpOrder = (order, account_id) => {
|
|
|
28
43
|
order_id: `${order.orderId}`,
|
|
29
44
|
account_id,
|
|
30
45
|
product_id: (0, utils_1.encodePath)('ASTER', 'PERP', order.symbol),
|
|
31
|
-
order_type: order.type,
|
|
46
|
+
order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),
|
|
32
47
|
order_direction: resolvePerpOrderDirection(order),
|
|
33
48
|
volume: Number.isFinite(volume) ? volume : 0,
|
|
34
49
|
price: Number.isFinite(price) ? price : undefined,
|
|
@@ -49,7 +64,7 @@ const mapSpotOrder = (order, account_id) => {
|
|
|
49
64
|
order_id: `${order.orderId}`,
|
|
50
65
|
account_id,
|
|
51
66
|
product_id: (0, utils_1.encodePath)('ASTER', 'SPOT', order.symbol),
|
|
52
|
-
order_type: order.type,
|
|
67
|
+
order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),
|
|
53
68
|
order_direction: resolveSpotOrderDirection(order.side),
|
|
54
69
|
volume: Number.isFinite(volume) ? volume : 0,
|
|
55
70
|
price: Number.isFinite(price) ? price : undefined,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listOrders.js","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":";;;AACA,uDAM+B;AAC/B,yCAA2C;AAI3C,MAAM,yBAAyB,GAAG,CAAC,UAAiC,EAAkB,EAAE;IACtF,MAAM,UAAU,GAAG,UAAU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AC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAA,iCAAmB,EAAC,UAAU,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IAC9E,OAAO,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,oBAAY,EAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AACjE,CAAC,CAAC;AAHW,QAAA,cAAc,kBAGzB","sourcesContent":["import { IActionHandlerOfListOrders, IOrder } from '@yuants/data-order';\nimport {\n getApiV1OpenOrders,\n getFApiV1OpenOrders,\n IAsterFutureOpenOrder,\n IAsterSpotOpenOrder,\n ICredential,\n} from '../../api/private-api';\nimport { encodePath } from '@yuants/utils';\n\ntype OrderDirection = 'OPEN_LONG' | 'OPEN_SHORT' | 'CLOSE_LONG' | 'CLOSE_SHORT';\n\nconst mapAsterOrderTypeToYuants = (type?: string, timeInForce?: string): IOrder['order_type'] => {\n if (type === 'MARKET') {\n return 'MARKET';\n }\n if (type === 'LIMIT') {\n if (timeInForce === 'GTX') return 'MAKER';\n if (timeInForce === 'IOC') return 'IOC';\n if (timeInForce === 'FOK') return 'FOK';\n return 'LIMIT';\n }\n return 'LIMIT';\n};\n\nconst resolvePerpOrderDirection = (asterOrder: IAsterFutureOpenOrder): OrderDirection => {\n const reduceOnly = asterOrder.reduceOnly || asterOrder.closePosition;\n\n if (asterOrder.positionSide === 'LONG') {\n return asterOrder.side === 'BUY' ? 'OPEN_LONG' : 'CLOSE_LONG';\n }\n if (asterOrder.positionSide === 'SHORT') {\n return asterOrder.side === 'BUY' ? 'CLOSE_SHORT' : 'OPEN_SHORT';\n }\n if (reduceOnly) {\n return asterOrder.side === 'BUY' ? 'CLOSE_SHORT' : 'CLOSE_LONG';\n }\n return asterOrder.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT';\n};\n\nconst resolveSpotOrderDirection = (side: 'BUY' | 'SELL'): OrderDirection => {\n return side === 'SELL' ? 'CLOSE_LONG' : 'OPEN_LONG';\n};\n\nexport const mapPerpOrder = (order: IAsterFutureOpenOrder, account_id: string): IOrder => {\n const volume = Number(order.origQty);\n const tradedVolume = Number(order.executedQty);\n const price = Number(order.price);\n const avgPrice = Number(order.avgPrice);\n\n return {\n order_id: `${order.orderId}`,\n account_id,\n product_id: encodePath('ASTER', 'PERP', order.symbol),\n order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: resolvePerpOrderDirection(order),\n volume: Number.isFinite(volume) ? volume : 0,\n price: Number.isFinite(price) ? price : undefined,\n submit_at: order.updateTime,\n traded_volume: Number.isFinite(tradedVolume) ? tradedVolume : undefined,\n traded_price: Number.isFinite(avgPrice) && avgPrice > 0 ? avgPrice : undefined,\n order_status: order.status,\n };\n};\n\nexport const mapSpotOrder = (order: IAsterSpotOpenOrder, account_id: string): IOrder => {\n const volume = Number(order.origQty);\n const tradedVolume = Number(order.executedQty);\n const price = Number(order.price);\n const avgPrice = Number(order.avgPrice ?? order.price);\n\n return {\n order_id: `${order.orderId}`,\n account_id,\n product_id: encodePath('ASTER', 'SPOT', order.symbol),\n order_type: mapAsterOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: resolveSpotOrderDirection(order.side),\n volume: Number.isFinite(volume) ? volume : 0,\n price: Number.isFinite(price) ? price : undefined,\n submit_at: order.updateTime ?? order.time,\n traded_volume: Number.isFinite(tradedVolume) ? tradedVolume : undefined,\n traded_price: Number.isFinite(avgPrice) && avgPrice > 0 ? avgPrice : undefined,\n order_status: order.status,\n };\n};\n\nexport const listOrders = async (credential: ICredential) => {\n const [prepOrders, spotOrders] = await Promise.all([\n getFApiV1OpenOrders(credential, {}),\n getApiV1OpenOrders(credential, {}),\n ]);\n return [\n ...prepOrders.map((order) => mapPerpOrder(order, '')),\n ...spotOrders.map((order) => mapSpotOrder(order, '')),\n ];\n};\n\nexport const listSpotOrders = async (credential: ICredential) => {\n const [spotOrders] = await Promise.all([getApiV1OpenOrders(credential, {})]);\n return [...spotOrders.map((order) => mapSpotOrder(order, ''))];\n};\n\nexport const listPrepOrders = async (credential: ICredential) => {\n const [prepOrders] = await Promise.all([getFApiV1OpenOrders(credential, {})]);\n return [...prepOrders.map((order) => mapPerpOrder(order, ''))];\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-type-mapping.test.d.ts","sourceRoot":"","sources":["../../../src/services/orders/order-type-mapping.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
jest.mock('../../api/private-api', () => ({
|
|
4
|
+
getApiV1TickerPrice: jest.fn(),
|
|
5
|
+
postApiV1Order: jest.fn(),
|
|
6
|
+
postFApiV1Order: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
jest.mock('../trade-history', () => ({
|
|
9
|
+
fetchTradeHistory: jest.fn(),
|
|
10
|
+
}));
|
|
11
|
+
const private_api_1 = require("../../api/private-api");
|
|
12
|
+
const listOrders_1 = require("./listOrders");
|
|
13
|
+
const submitOrder_1 = require("./submitOrder");
|
|
14
|
+
const mockedPostApiV1Order = jest.mocked(private_api_1.postApiV1Order);
|
|
15
|
+
const mockedPostFApiV1Order = jest.mocked(private_api_1.postFApiV1Order);
|
|
16
|
+
const baseSpotOrder = {
|
|
17
|
+
symbol: 'BTCUSDT',
|
|
18
|
+
orderId: 11,
|
|
19
|
+
clientOrderId: 'spot-order',
|
|
20
|
+
price: '12345',
|
|
21
|
+
origQty: '1',
|
|
22
|
+
executedQty: '0',
|
|
23
|
+
status: 'NEW',
|
|
24
|
+
timeInForce: 'GTC',
|
|
25
|
+
type: 'LIMIT',
|
|
26
|
+
side: 'BUY',
|
|
27
|
+
time: 1,
|
|
28
|
+
updateTime: 2,
|
|
29
|
+
};
|
|
30
|
+
const basePerpOrder = {
|
|
31
|
+
symbol: 'BTCUSDT',
|
|
32
|
+
orderId: 21,
|
|
33
|
+
clientOrderId: 'perp-order',
|
|
34
|
+
price: '23456',
|
|
35
|
+
origQty: '2',
|
|
36
|
+
executedQty: '0',
|
|
37
|
+
status: 'NEW',
|
|
38
|
+
timeInForce: 'GTC',
|
|
39
|
+
type: 'LIMIT',
|
|
40
|
+
side: 'BUY',
|
|
41
|
+
updateTime: 3,
|
|
42
|
+
avgPrice: '0',
|
|
43
|
+
};
|
|
44
|
+
describe('Aster order type mappings', () => {
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
jest.resetAllMocks();
|
|
47
|
+
mockedPostApiV1Order.mockResolvedValue({ orderId: 1 });
|
|
48
|
+
mockedPostFApiV1Order.mockResolvedValue({ orderId: 2 });
|
|
49
|
+
});
|
|
50
|
+
test.each([
|
|
51
|
+
['ASTER/SPOT/BTCUSDT', 'IOC', 'IOC'],
|
|
52
|
+
['ASTER/SPOT/BTCUSDT', 'FOK', 'FOK'],
|
|
53
|
+
['ASTER/PERP/BTCUSDT', 'IOC', 'IOC'],
|
|
54
|
+
['ASTER/PERP/BTCUSDT', 'FOK', 'FOK'],
|
|
55
|
+
])('submitOrder sends %s %s with timeInForce=%s', async (product_id, order_type, tif) => {
|
|
56
|
+
const result = await (0, submitOrder_1.handleSubmitOrder)({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, {
|
|
57
|
+
account_id: 'ASTER/test',
|
|
58
|
+
product_id,
|
|
59
|
+
order_type,
|
|
60
|
+
order_direction: 'OPEN_LONG',
|
|
61
|
+
volume: 1,
|
|
62
|
+
price: 12345,
|
|
63
|
+
});
|
|
64
|
+
expect(result).toEqual({ order_id: product_id.includes('/SPOT/') ? '1' : '2' });
|
|
65
|
+
if (product_id.includes('/SPOT/')) {
|
|
66
|
+
expect(mockedPostApiV1Order).toHaveBeenCalledWith({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
expect(mockedPostFApiV1Order).toHaveBeenCalledWith({ address: 'addr', api_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
70
|
+
});
|
|
71
|
+
test('mapSpotOrder restores LIMIT plus timeInForce variants', () => {
|
|
72
|
+
expect((0, listOrders_1.mapSpotOrder)(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'GTC' }), '')).toMatchObject({ order_type: 'LIMIT' });
|
|
73
|
+
expect((0, listOrders_1.mapSpotOrder)(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'GTX' }), '')).toMatchObject({ order_type: 'MAKER' });
|
|
74
|
+
expect((0, listOrders_1.mapSpotOrder)(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'IOC' }), '')).toMatchObject({ order_type: 'IOC' });
|
|
75
|
+
expect((0, listOrders_1.mapSpotOrder)(Object.assign(Object.assign({}, baseSpotOrder), { timeInForce: 'FOK' }), '')).toMatchObject({ order_type: 'FOK' });
|
|
76
|
+
});
|
|
77
|
+
test('mapPerpOrder restores LIMIT plus timeInForce variants', () => {
|
|
78
|
+
expect((0, listOrders_1.mapPerpOrder)(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'GTC' }), '')).toMatchObject({ order_type: 'LIMIT' });
|
|
79
|
+
expect((0, listOrders_1.mapPerpOrder)(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'GTX' }), '')).toMatchObject({ order_type: 'MAKER' });
|
|
80
|
+
expect((0, listOrders_1.mapPerpOrder)(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'IOC' }), '')).toMatchObject({ order_type: 'IOC' });
|
|
81
|
+
expect((0, listOrders_1.mapPerpOrder)(Object.assign(Object.assign({}, basePerpOrder), { timeInForce: 'FOK' }), '')).toMatchObject({ order_type: 'FOK' });
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=order-type-mapping.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-type-mapping.test.js","sourceRoot":"","sources":["../../../src/services/orders/order-type-mapping.test.ts"],"names":[],"mappings":";;AAEA,IAAI,CAAC,IAAI,CAAC,uBAAuB,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,mBAAmB,EAAE,IAAI,CAAC,EAAE,EAAE;IAC9B,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;IACzB,eAAe,EAAE,IAAI,CAAC,EAAE,EAAE;CAC3B,CAAC,CAAC,CAAC;AAEJ,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,EAAE,CAAC,CAAC;IACnC,iBAAiB,EAAE,IAAI,CAAC,EAAE,EAAE;CAC7B,CAAC,CAAC,CAAC;AAEJ,uDAAwE;AACxE,6CAA0D;AAC1D,+CAAkD;AAElD,MAAM,oBAAoB,GAAG,IAAI,CAAC,MAAM,CAAC,4BAAc,CAAC,CAAC;AACzD,MAAM,qBAAqB,GAAG,IAAI,CAAC,MAAM,CAAC,6BAAe,CAAC,CAAC;AAE3D,MAAM,aAAa,GAAwB;IACzC,MAAM,EAAE,SAAS;IACjB,OAAO,EAAE,EAAE;IACX,aAAa,EAAE,YAAY;IAC3B,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,GAAG;IACZ,WAAW,EAAE,GAAG;IAChB,MAAM,EAAE,KAAK;IACb,WAAW,EAAE,KAAK;IAClB,IAAI,EAAE,OAAO;IACb,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,CAAC;IACP,UAAU,EAAE,CAAC;CACd,CAAC;AAEF,MAAM,aAAa,GAA0B;IAC3C,MAAM,EAAE,SAAS;IACjB,OAAO,EAAE,EAAE;IACX,aAAa,EAAE,YAAY;IAC3B,KAAK,EAAE,OAAO;IACd,OAAO,EAAE,GAAG;IACZ,WAAW,EAAE,GAAG;IAChB,MAAM,EAAE,KAAK;IACb,WAAW,EAAE,KAAK;IACl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AAK,EAAE,CAAC,CAAC;IAC1G,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uDAAuD,EAAE,GAAG,EAAE;QACjE,MAAM,CAAC,IAAA,yBAAY,kCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;QAC1G,MAAM,CAAC,IAAA,yBAAY,kCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;QAC1G,MAAM,CAAC,IAAA,yBAAY,kCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;QACxG,MAAM,CAAC,IAAA,yBAAY,kCAAM,aAAa,KAAE,WAAW,EAAE,KAAK,KAAI,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;IAC1G,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import type { IAsterFutureOpenOrder, IAsterSpotOpenOrder } from '../../api/private-api';\n\njest.mock('../../api/private-api', () => ({\n getApiV1TickerPrice: jest.fn(),\n postApiV1Order: jest.fn(),\n postFApiV1Order: jest.fn(),\n}));\n\njest.mock('../trade-history', () => ({\n fetchTradeHistory: jest.fn(),\n}));\n\nimport { postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { mapPerpOrder, mapSpotOrder } from './listOrders';\nimport { handleSubmitOrder } from './submitOrder';\n\nconst mockedPostApiV1Order = jest.mocked(postApiV1Order);\nconst mockedPostFApiV1Order = jest.mocked(postFApiV1Order);\n\nconst baseSpotOrder: IAsterSpotOpenOrder = {\n symbol: 'BTCUSDT',\n orderId: 11,\n clientOrderId: 'spot-order',\n price: '12345',\n origQty: '1',\n executedQty: '0',\n status: 'NEW',\n timeInForce: 'GTC',\n type: 'LIMIT',\n side: 'BUY',\n time: 1,\n updateTime: 2,\n};\n\nconst basePerpOrder: IAsterFutureOpenOrder = {\n symbol: 'BTCUSDT',\n orderId: 21,\n clientOrderId: 'perp-order',\n price: '23456',\n origQty: '2',\n executedQty: '0',\n status: 'NEW',\n timeInForce: 'GTC',\n type: 'LIMIT',\n side: 'BUY',\n updateTime: 3,\n avgPrice: '0',\n};\n\ndescribe('Aster order type mappings', () => {\n beforeEach(() => {\n jest.resetAllMocks();\n mockedPostApiV1Order.mockResolvedValue({ orderId: 1 });\n mockedPostFApiV1Order.mockResolvedValue({ orderId: 2 } as never);\n });\n\n test.each([\n ['ASTER/SPOT/BTCUSDT', 'IOC', 'IOC'],\n ['ASTER/SPOT/BTCUSDT', 'FOK', 'FOK'],\n ['ASTER/PERP/BTCUSDT', 'IOC', 'IOC'],\n ['ASTER/PERP/BTCUSDT', 'FOK', 'FOK'],\n ] as const)('submitOrder sends %s %s with timeInForce=%s', async (product_id, order_type, tif) => {\n const result = await handleSubmitOrder(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n {\n account_id: 'ASTER/test',\n product_id,\n order_type,\n order_direction: 'OPEN_LONG',\n volume: 1,\n price: 12345,\n },\n );\n\n expect(result).toEqual({ order_id: product_id.includes('/SPOT/') ? '1' : '2' });\n\n if (product_id.includes('/SPOT/')) {\n expect(mockedPostApiV1Order).toHaveBeenCalledWith(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n return;\n }\n\n expect(mockedPostFApiV1Order).toHaveBeenCalledWith(\n { address: 'addr', api_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n });\n\n test('mapSpotOrder restores LIMIT plus timeInForce variants', () => {\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'GTC' }, '')).toMatchObject({ order_type: 'LIMIT' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'GTX' }, '')).toMatchObject({ order_type: 'MAKER' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'IOC' }, '')).toMatchObject({ order_type: 'IOC' });\n expect(mapSpotOrder({ ...baseSpotOrder, timeInForce: 'FOK' }, '')).toMatchObject({ order_type: 'FOK' });\n });\n\n test('mapPerpOrder restores LIMIT plus timeInForce variants', () => {\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'GTC' }, '')).toMatchObject({ order_type: 'LIMIT' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'GTX' }, '')).toMatchObject({ order_type: 'MAKER' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'IOC' }, '')).toMatchObject({ order_type: 'IOC' });\n expect(mapPerpOrder({ ...basePerpOrder, timeInForce: 'FOK' }, '')).toMatchObject({ order_type: 'FOK' });\n });\n});\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.d.ts","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,EAAuB,WAAW,EAAmC,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"submitOrder.d.ts","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAEjE,OAAO,EAAuB,WAAW,EAAmC,MAAM,uBAAuB,CAAC;AA6I1G,eAAO,MAAM,iBAAiB,EAAE,2BAA2B,CAAC,WAAW,CAStE,CAAC"}
|
|
@@ -14,6 +14,23 @@ const parseProductId = (productId) => {
|
|
|
14
14
|
}
|
|
15
15
|
return { category: undefined, symbol: parts[0] };
|
|
16
16
|
};
|
|
17
|
+
const mapOrderTypeToAsterType = (order_type) => {
|
|
18
|
+
return { MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT', IOC: 'LIMIT', FOK: 'LIMIT' }[order_type];
|
|
19
|
+
};
|
|
20
|
+
const mapOrderTypeToTimeInForce = (order_type) => {
|
|
21
|
+
switch (order_type) {
|
|
22
|
+
case 'MAKER':
|
|
23
|
+
return 'GTX';
|
|
24
|
+
case 'LIMIT':
|
|
25
|
+
return 'GTC';
|
|
26
|
+
case 'IOC':
|
|
27
|
+
return 'IOC';
|
|
28
|
+
case 'FOK':
|
|
29
|
+
return 'FOK';
|
|
30
|
+
default:
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
17
34
|
const handleSubmitOrderOfSpot = async (credential, order) => {
|
|
18
35
|
var _a;
|
|
19
36
|
const { symbol } = parseProductId(order.product_id);
|
|
@@ -21,13 +38,13 @@ const handleSubmitOrderOfSpot = async (credential, order) => {
|
|
|
21
38
|
if (!resolvedSymbol) {
|
|
22
39
|
throw new Error(`Invalid product_id: unable to resolve spot symbol from "${order.product_id}"`);
|
|
23
40
|
}
|
|
24
|
-
const type =
|
|
41
|
+
const type = mapOrderTypeToAsterType(order.order_type);
|
|
25
42
|
if (!type)
|
|
26
43
|
throw new Error(`Unsupported order_type: ${order.order_type}`);
|
|
27
44
|
const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
|
|
28
45
|
if (!side)
|
|
29
46
|
throw new Error(`Unsupported order_direction: ${order.order_direction}`);
|
|
30
|
-
const timeInForce = order.order_type
|
|
47
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
31
48
|
const price = order.price;
|
|
32
49
|
let quantity = order.volume;
|
|
33
50
|
let quoteOrderQty;
|
|
@@ -65,7 +82,7 @@ const handleSubmitOrderOfPerp = async (credential, order) => {
|
|
|
65
82
|
const side = { OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' }[order.order_direction];
|
|
66
83
|
if (!side)
|
|
67
84
|
throw new Error(`Unsupported order_direction: ${order.order_direction}`);
|
|
68
|
-
const type =
|
|
85
|
+
const type = mapOrderTypeToAsterType(order.order_type);
|
|
69
86
|
if (!type)
|
|
70
87
|
throw new Error(`Unsupported order_type: ${order.order_type}`);
|
|
71
88
|
const quantity = order.volume;
|
|
@@ -79,7 +96,7 @@ const handleSubmitOrderOfPerp = async (credential, order) => {
|
|
|
79
96
|
? 'SHORT'
|
|
80
97
|
: undefined;
|
|
81
98
|
const reduceOnly = order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;
|
|
82
|
-
const timeInForce = order.order_type
|
|
99
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
83
100
|
//
|
|
84
101
|
const res = await (0, private_api_1.postFApiV1Order)(credential, {
|
|
85
102
|
symbol,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":";;;AACA,yCAAwD;AACxD,uDAA0G;AAC1G,oDAAqD;AAErD,MAAM,cAAc,GAAG,CAAC,SAAkB,EAAE,EAAE;IAC5C,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,EAAE,QAAQ,EAAE,SAA+B,EAAE,MAAM,EAAE,SAA+B,EAAE,CAAC;IAChG,CAAC;IACD,MAAM,KAAK,GAAG,IAAA,kBAAU,EAAC,SAAS,CAAC,CAAC;IACpC,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,G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C,CAAC;IACpD,CAAC;IACD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;AACjF,CAAC,CAAC;AATW,QAAA,iBAAiB,qBAS5B","sourcesContent":["import { IActionHandlerOfSubmitOrder } from '@yuants/data-order';\nimport { decodePath, roundToStep } from '@yuants/utils';\nimport { getApiV1TickerPrice, ICredential, postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { fetchTradeHistory } from '../trade-history';\n\nconst parseProductId = (productId?: string) => {\n if (!productId) {\n return { category: undefined as string | undefined, symbol: undefined as string | undefined };\n }\n const parts = decodePath(productId);\n if (parts.length >= 3) {\n return { category: parts[1], symbol: parts.slice(2).join('/') };\n }\n return { category: undefined, symbol: parts[0] };\n};\n\nconst handleSubmitOrderOfSpot: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n const resolvedSymbol = symbol ?? order.product_id;\n if (!resolvedSymbol) {\n throw new Error(`Invalid product_id: unable to resolve spot symbol from \"${order.product_id}\"`);\n }\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice(credential, {});\n const thePrice = spotPrice.find((x) => x.symbol === resolvedSymbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${resolvedSymbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n const res = await postApiV1Order(credential, {\n symbol: resolvedSymbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n if (!res.orderId) {\n throw new Error('Failed to retrieve order ID from response');\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, resolvedSymbol);\n }\n return { order_id: '' + res.orderId };\n};\n\nconst handleSubmitOrderOfPerp: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n if (!symbol) {\n throw new Error(`Invalid product_id: unable to decode symbol from \"${order.product_id}\"`);\n }\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const type = ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT' } as const)[order.order_type!];\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const quantity = order.volume;\n const price = order.price;\n\n const isPositionSingleSide = true; // FIXME: Aster 永续合约仅支持单向持仓模式\n\n const positionSide = isPositionSingleSide\n ? undefined\n : order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_LONG'\n ? 'LONG'\n : order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_SHORT'\n ? 'SHORT'\n : undefined;\n\n const reduceOnly =\n order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;\n\n const timeInForce = order.order_type === 'MAKER' ? 'GTX' : order.order_type === 'LIMIT' ? 'GTC' : undefined;\n\n //\n const res = await postFApiV1Order(credential, {\n symbol,\n side,\n type,\n quantity,\n price,\n timeInForce,\n positionSide,\n reduceOnly,\n });\n\n const orderId =\n (res as any)?.orderId ??\n (res as any)?.order_id ??\n (res as any)?.data?.orderId ??\n (res as any)?.data?.order_id;\n\n if (!orderId) throw new Error('Failed to retrieve order ID from response');\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${orderId}` };\n};\n\nexport const handleSubmitOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const [_, instType] = decodePath(order.product_id); // BITGET/USDT-FUTURES/BTCUSDT\n if (instType === 'SPOT') {\n return handleSubmitOrderOfSpot(credential, order);\n }\n if (instType === 'PERP') {\n return handleSubmitOrderOfPerp(credential, order);\n }\n throw new Error(`Unsupported account_id for SubmitOrder: ${order.account_id}`);\n};\n"]}
|
|
1
|
+
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":";;;AACA,yCAAwD;AACxD,uDAA0G;AAC1G,oDAAqD;AAErD,MAAM,cAAc,GAAG,CAAC,SAAkB,EAAE,EAAE;IAC5C,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,EAAE,QAAQ,EAAE,SAA+B,EAAE,MAAM,EAAE,SAA+B,EAAE,CAAC;IAChG,CAAC;IACD,MAAM,KAAK,GAAG,IAAA,kBAAU,EAAC,SAAS,CAAC,CAAC;IACpC,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;QACtB,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;IAClE,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AACnD,CAAC,CAAC;AAEF,MAAM,uBAAuB,GAAG,CAAC,UAAmB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,CAAC;IACD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,OAAO,uBAAuB,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IACpD,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;AACjF,CAAC,CAAC;AATW,QAAA,iBAAiB,qBAS5B","sourcesContent":["import { IActionHandlerOfSubmitOrder } from '@yuants/data-order';\nimport { decodePath, roundToStep } from '@yuants/utils';\nimport { getApiV1TickerPrice, ICredential, postApiV1Order, postFApiV1Order } from '../../api/private-api';\nimport { fetchTradeHistory } from '../trade-history';\n\nconst parseProductId = (productId?: string) => {\n if (!productId) {\n return { category: undefined as string | undefined, symbol: undefined as string | undefined };\n }\n const parts = decodePath(productId);\n if (parts.length >= 3) {\n return { category: parts[1], symbol: parts.slice(2).join('/') };\n }\n return { category: undefined, symbol: parts[0] };\n};\n\nconst mapOrderTypeToAsterType = (order_type?: string) => {\n return ({ MARKET: 'MARKET', LIMIT: 'LIMIT', MAKER: 'LIMIT', IOC: 'LIMIT', FOK: 'LIMIT' } as const)[\n order_type!\n ];\n};\n\nconst mapOrderTypeToTimeInForce = (order_type?: string) => {\n switch (order_type) {\n case 'MAKER':\n return 'GTX';\n case 'LIMIT':\n return 'GTC';\n case 'IOC':\n return 'IOC';\n case 'FOK':\n return 'FOK';\n default:\n return undefined;\n }\n};\n\nconst handleSubmitOrderOfSpot: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n const resolvedSymbol = symbol ?? order.product_id;\n if (!resolvedSymbol) {\n throw new Error(`Invalid product_id: unable to resolve spot symbol from \"${order.product_id}\"`);\n }\n\n const type = mapOrderTypeToAsterType(order.order_type);\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n\n const price = order.price;\n\n let quantity: number | undefined = order.volume;\n let quoteOrderQty: number | undefined;\n\n if (type === 'MARKET' && side === 'BUY') {\n const spotPrice = await getApiV1TickerPrice(credential, {});\n const thePrice = spotPrice.find((x) => x.symbol === resolvedSymbol)?.price;\n if (!thePrice) throw new Error(`Cannot get price for symbol ${resolvedSymbol}`);\n quantity = undefined;\n quoteOrderQty = roundToStep(order.volume * +thePrice, 0.01);\n }\n\n const res = await postApiV1Order(credential, {\n symbol: resolvedSymbol,\n type,\n side,\n timeInForce,\n price,\n quantity,\n quoteOrderQty,\n });\n\n if (!res.orderId) {\n throw new Error('Failed to retrieve order ID from response');\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, resolvedSymbol);\n }\n return { order_id: '' + res.orderId };\n};\n\nconst handleSubmitOrderOfPerp: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const { symbol } = parseProductId(order.product_id);\n if (!symbol) {\n throw new Error(`Invalid product_id: unable to decode symbol from \"${order.product_id}\"`);\n }\n\n const side = ({ OPEN_LONG: 'BUY', OPEN_SHORT: 'SELL', CLOSE_LONG: 'SELL', CLOSE_SHORT: 'BUY' } as const)[\n order.order_direction!\n ];\n if (!side) throw new Error(`Unsupported order_direction: ${order.order_direction}`);\n\n const type = mapOrderTypeToAsterType(order.order_type);\n if (!type) throw new Error(`Unsupported order_type: ${order.order_type}`);\n\n const quantity = order.volume;\n const price = order.price;\n\n const isPositionSingleSide = true; // FIXME: Aster 永续合约仅支持单向持仓模式\n\n const positionSide = isPositionSingleSide\n ? undefined\n : order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_LONG'\n ? 'LONG'\n : order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_SHORT'\n ? 'SHORT'\n : undefined;\n\n const reduceOnly =\n order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT' ? 'true' : undefined;\n\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n\n //\n const res = await postFApiV1Order(credential, {\n symbol,\n side,\n type,\n quantity,\n price,\n timeInForce,\n positionSide,\n reduceOnly,\n });\n\n const orderId =\n (res as any)?.orderId ??\n (res as any)?.order_id ??\n (res as any)?.data?.orderId ??\n (res as any)?.data?.order_id;\n\n if (!orderId) throw new Error('Failed to retrieve order ID from response');\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${orderId}` };\n};\n\nexport const handleSubmitOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const [_, instType] = decodePath(order.product_id); // BITGET/USDT-FUTURES/BTCUSDT\n if (instType === 'SPOT') {\n return handleSubmitOrderOfSpot(credential, order);\n }\n if (instType === 'PERP') {\n return handleSubmitOrderOfPerp(credential, order);\n }\n throw new Error(`Unsupported account_id for SubmitOrder: ${order.account_id}`);\n};\n"]}
|