@yuants/vendor-binance 0.14.13 → 0.14.14
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 +2 -2
- package/dist/services/orders/listOrders.js.map +1 -1
- package/dist/services/orders/order-type-mapping.test.js +166 -0
- package/dist/services/orders/order-type-mapping.test.js.map +1 -0
- package/dist/services/orders/order-utils.js +29 -2
- package/dist/services/orders/order-utils.js.map +1 -1
- package/dist/services/orders/submitOrder.js +3 -3
- package/dist/services/orders/submitOrder.js.map +1 -1
- package/lib/services/orders/listOrders.js +2 -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 +168 -0
- package/lib/services/orders/order-type-mapping.test.js.map +1 -0
- package/lib/services/orders/order-utils.d.ts +2 -1
- package/lib/services/orders/order-utils.d.ts.map +1 -1
- package/lib/services/orders/order-utils.js +31 -3
- package/lib/services/orders/order-utils.js.map +1 -1
- package/lib/services/orders/submitOrder.d.ts.map +1 -1
- package/lib/services/orders/submitOrder.js +2 -2
- package/lib/services/orders/submitOrder.js.map +1 -1
- package/package.json +9 -9
- package/temp/build/typescript/ts_tQj2bmGB.json +1 -1
- package/temp/package-deps.json +8 -7
- package/temp/test/jest/haste-map-f37bc94525b36254e573de220443d250-dc922b21f2d35e7315abf4c3159e25a5-63b40dbcff306581344a73f9073894a1 +0 -0
- package/temp/test/jest/perf-cache-f37bc94525b36254e573de220443d250-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
|
@@ -13,7 +13,7 @@ export const listUnifiedUmOrders = async (credential) => {
|
|
|
13
13
|
order_id: `${order.orderId}`,
|
|
14
14
|
account_id: '',
|
|
15
15
|
product_id: encodePath('BINANCE', 'USDT-FUTURE', order.symbol),
|
|
16
|
-
order_type: mapBinanceOrderTypeToYuants(order.type),
|
|
16
|
+
order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),
|
|
17
17
|
order_direction: (_a = mapBinanceSideToYuantsDirection(order.side, order.positionSide)) !== null && _a !== void 0 ? _a : (order.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT'),
|
|
18
18
|
volume: +order.origQty,
|
|
19
19
|
traded_volume: +order.executedQty,
|
|
@@ -33,7 +33,7 @@ export const listSpotOrders = async (credential) => {
|
|
|
33
33
|
order_id: `${order.orderId}`,
|
|
34
34
|
account_id: '',
|
|
35
35
|
product_id: encodePath('BINANCE', 'SPOT', order.symbol),
|
|
36
|
-
order_type: mapBinanceOrderTypeToYuants(order.type),
|
|
36
|
+
order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),
|
|
37
37
|
order_direction: mapSpotSideToOrderDirection(order.side),
|
|
38
38
|
volume: +order.origQty,
|
|
39
39
|
traded_volume: +order.executedQty,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listOrders.js","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAe,MAAM,uBAAuB,CAAC;AAC/F,OAAO,EACL,qBAAqB,EACrB,2BAA2B,EAC3B,+BAA+B,EAC/B,2BAA2B,GAC5B,MAAM,eAAe,CAAC;AAEvB,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,UAAuB,EAAqB,EAAE;IACtF,MAAM,GAAG,GAAG,MAAM,sBAAsB,CAAC,UAAU,CAAC,CAAC;IACrD,IAAI,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,GAAG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AAE,CAAC,KAAK,CAAC,WAAW;QACjC,KAAK,EAAE,KAAK,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK;QAC3D,SAAS,EAAE,KAAK,CAAC,IAAI;QACrB,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC;QACxC,YAAY,EAAE,qBAAqB,CAAC,KAAK,CAAC,MAAM,CAAC;KAClD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,WACvC,UAAuB,EACvB,UAAkB;IAElB,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,8BAA8B;IAC5E,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,UAAU,CAAC,CAAC;QAChD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IACnE,CAAC;IACD,IAAI,QAAQ,KAAK,aAAa,EAAE,CAAC;QAC/B,MAAM,MAAM,GAAG,MAAM,mBAAmB,CAAC,UAAU,CAAC,CAAC;QACrD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IACnE,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAC;AACvD,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfListOrders, IOrder } from '@yuants/data-order';\nimport { decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { isApiError } from '../../api/client';\nimport { getSpotOpenOrders, getUnifiedUmOpenOrders, ICredential } from '../../api/private-api';\nimport {\n mapBinanceOrderStatus,\n mapBinanceOrderTypeToYuants,\n mapBinanceSideToYuantsDirection,\n mapSpotSideToOrderDirection,\n} from './order-utils';\n\nexport const listUnifiedUmOrders = async (credential: ICredential): Promise<IOrder[]> => {\n const res = await getUnifiedUmOpenOrders(credential);\n if (isApiError(res)) {\n throw new Error(res.msg);\n }\n return res.map(\n (order): IOrder => ({\n order_id: `${order.orderId}`,\n account_id: '',\n product_id: encodePath('BINANCE', 'USDT-FUTURE', order.symbol),\n order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),\n order_direction:\n mapBinanceSideToYuantsDirection(order.side, order.positionSide) ??\n (order.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT'),\n volume: +order.origQty,\n traded_volume: +order.executedQty,\n price: order.price === undefined ? undefined : +order.price,\n submit_at: order.time,\n updated_at: formatTime(order.updateTime),\n order_status: mapBinanceOrderStatus(order.status),\n }),\n );\n};\n\nexport const listSpotOrders = async (credential: ICredential): Promise<IOrder[]> => {\n const res = await getSpotOpenOrders(credential);\n if (isApiError(res)) {\n throw new Error(res.msg);\n }\n return res.map(\n (order): IOrder => ({\n order_id: `${order.orderId}`,\n account_id: '',\n product_id: encodePath('BINANCE', 'SPOT', order.symbol),\n order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: mapSpotSideToOrderDirection(order.side),\n volume: +order.origQty,\n traded_volume: +order.executedQty,\n price: order.price === undefined ? undefined : +order.price,\n submit_at: order.time,\n updated_at: formatTime(order.updateTime),\n order_status: mapBinanceOrderStatus(order.status),\n }),\n );\n};\n\nexport const getOrdersByProductId = async function (\n credential: ICredential,\n product_id: string,\n): Promise<IOrder[]> {\n const [_, instType] = decodePath(product_id); // BINANCE/USDT-FUTURE/ADAUSDT\n if (instType === 'SPOT') {\n const orders = await listSpotOrders(credential);\n return orders.filter((order) => order.product_id === product_id);\n }\n if (instType === 'USDT-FUTURE') {\n const orders = await listUnifiedUmOrders(credential);\n return orders.filter((order) => order.product_id === product_id);\n }\n throw new Error(`Unsupported instType: ${instType}`);\n};\n"]}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { encodePath } from '@yuants/utils';
|
|
2
|
+
jest.mock('../../api/private-api', () => ({
|
|
3
|
+
getSpotOpenOrders: jest.fn(),
|
|
4
|
+
getUnifiedUmOpenOrders: jest.fn(),
|
|
5
|
+
postSpotOrder: jest.fn(),
|
|
6
|
+
postUmOrder: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
jest.mock('../../api/client', () => ({
|
|
9
|
+
isApiError: jest.fn(() => false),
|
|
10
|
+
}));
|
|
11
|
+
jest.mock('../trade-history', () => ({
|
|
12
|
+
fetchTradeHistory: jest.fn(),
|
|
13
|
+
}));
|
|
14
|
+
import { getSpotOpenOrders, getUnifiedUmOpenOrders, postSpotOrder, postUmOrder } from '../../api/private-api';
|
|
15
|
+
import { mapBinanceOrderTypeToYuants, mapOrderTypeToOrdType, mapOrderTypeToTimeInForce } from './order-utils';
|
|
16
|
+
import { listSpotOrders, listUnifiedUmOrders } from './listOrders';
|
|
17
|
+
import { submitOrder } from './submitOrder';
|
|
18
|
+
const mockedGetSpotOpenOrders = jest.mocked(getSpotOpenOrders);
|
|
19
|
+
const mockedGetUnifiedUmOpenOrders = jest.mocked(getUnifiedUmOpenOrders);
|
|
20
|
+
const mockedPostSpotOrder = jest.mocked(postSpotOrder);
|
|
21
|
+
const mockedPostUmOrder = jest.mocked(postUmOrder);
|
|
22
|
+
describe('Binance order type mappings', () => {
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.resetAllMocks();
|
|
25
|
+
mockedPostSpotOrder.mockResolvedValue({
|
|
26
|
+
symbol: 'BTCUSDT',
|
|
27
|
+
orderId: 1,
|
|
28
|
+
clientOrderId: 'spot-order',
|
|
29
|
+
transactTime: 1,
|
|
30
|
+
});
|
|
31
|
+
mockedPostUmOrder.mockResolvedValue({
|
|
32
|
+
orderId: 2,
|
|
33
|
+
symbol: 'BTCUSDT',
|
|
34
|
+
status: 'NEW',
|
|
35
|
+
clientOrderId: 'um-order',
|
|
36
|
+
price: '12345',
|
|
37
|
+
avgPrice: '0',
|
|
38
|
+
origQty: '1',
|
|
39
|
+
executedQty: '0',
|
|
40
|
+
cumQty: '0',
|
|
41
|
+
cumQuote: '0',
|
|
42
|
+
timeInForce: 'GTC',
|
|
43
|
+
type: 'LIMIT',
|
|
44
|
+
reduceOnly: false,
|
|
45
|
+
side: 'BUY',
|
|
46
|
+
positionSide: 'LONG',
|
|
47
|
+
selfTradePreventionMode: 'NONE',
|
|
48
|
+
goodTillDate: 0,
|
|
49
|
+
updateTime: 1,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
test('maps IOC and FOK order types to Binance LIMIT orders', () => {
|
|
53
|
+
expect(mapOrderTypeToOrdType('IOC')).toBe('LIMIT');
|
|
54
|
+
expect(mapOrderTypeToOrdType('FOK')).toBe('LIMIT');
|
|
55
|
+
});
|
|
56
|
+
test('maps Yuan order types to Binance timeInForce values', () => {
|
|
57
|
+
expect(mapOrderTypeToTimeInForce('LIMIT')).toBe('GTC');
|
|
58
|
+
expect(mapOrderTypeToTimeInForce('MAKER')).toBe('GTX');
|
|
59
|
+
expect(mapOrderTypeToTimeInForce('IOC')).toBe('IOC');
|
|
60
|
+
expect(mapOrderTypeToTimeInForce('FOK')).toBe('FOK');
|
|
61
|
+
expect(mapOrderTypeToTimeInForce('MARKET')).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
test('maps Binance type and timeInForce back to Yuan order types', () => {
|
|
64
|
+
expect(mapBinanceOrderTypeToYuants('MARKET')).toBe('MARKET');
|
|
65
|
+
expect(mapBinanceOrderTypeToYuants('LIMIT', 'GTC')).toBe('LIMIT');
|
|
66
|
+
expect(mapBinanceOrderTypeToYuants('LIMIT', 'GTX')).toBe('MAKER');
|
|
67
|
+
expect(mapBinanceOrderTypeToYuants('LIMIT', 'IOC')).toBe('IOC');
|
|
68
|
+
expect(mapBinanceOrderTypeToYuants('LIMIT', 'FOK')).toBe('FOK');
|
|
69
|
+
});
|
|
70
|
+
test.each([
|
|
71
|
+
['SPOT', 'IOC', 'IOC'],
|
|
72
|
+
['SPOT', 'FOK', 'FOK'],
|
|
73
|
+
['USDT-FUTURE', 'IOC', 'IOC'],
|
|
74
|
+
['USDT-FUTURE', 'FOK', 'FOK'],
|
|
75
|
+
])('submitOrder sends %s %s orders with timeInForce=%s', async (marketType, orderType, tif) => {
|
|
76
|
+
const productId = encodePath('BINANCE', marketType, 'BTCUSDT');
|
|
77
|
+
await expect(submitOrder({ access_key: 'ak', secret_key: 'sk' }, {
|
|
78
|
+
account_id: 'BINANCE/test',
|
|
79
|
+
product_id: productId,
|
|
80
|
+
order_type: orderType,
|
|
81
|
+
order_direction: 'OPEN_LONG',
|
|
82
|
+
volume: 1,
|
|
83
|
+
price: 12345,
|
|
84
|
+
})).resolves.toEqual({ order_id: marketType === 'SPOT' ? '1' : '2' });
|
|
85
|
+
if (marketType === 'SPOT') {
|
|
86
|
+
expect(mockedPostSpotOrder).toHaveBeenCalledWith({ access_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
expect(mockedPostUmOrder).toHaveBeenCalledWith({ access_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
90
|
+
});
|
|
91
|
+
test('listOrders maps LIMIT+IOC/FOK/GTX readback values correctly', async () => {
|
|
92
|
+
mockedGetSpotOpenOrders.mockResolvedValue([
|
|
93
|
+
{
|
|
94
|
+
symbol: 'BTCUSDT',
|
|
95
|
+
orderId: 11,
|
|
96
|
+
orderListId: -1,
|
|
97
|
+
clientOrderId: 'spot-maker',
|
|
98
|
+
price: '12345',
|
|
99
|
+
origQty: '1',
|
|
100
|
+
executedQty: '0',
|
|
101
|
+
origQuoteOrderQty: '0',
|
|
102
|
+
cummulativeQuoteQty: '0',
|
|
103
|
+
status: 'NEW',
|
|
104
|
+
timeInForce: 'GTX',
|
|
105
|
+
type: 'LIMIT',
|
|
106
|
+
side: 'BUY',
|
|
107
|
+
stopPrice: '0',
|
|
108
|
+
icebergQty: '0',
|
|
109
|
+
time: 1,
|
|
110
|
+
updateTime: 2,
|
|
111
|
+
isWorking: true,
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
symbol: 'ETHUSDT',
|
|
115
|
+
orderId: 12,
|
|
116
|
+
orderListId: -1,
|
|
117
|
+
clientOrderId: 'spot-ioc',
|
|
118
|
+
price: '2345',
|
|
119
|
+
origQty: '2',
|
|
120
|
+
executedQty: '0',
|
|
121
|
+
origQuoteOrderQty: '0',
|
|
122
|
+
cummulativeQuoteQty: '0',
|
|
123
|
+
status: 'NEW',
|
|
124
|
+
timeInForce: 'IOC',
|
|
125
|
+
type: 'LIMIT',
|
|
126
|
+
side: 'BUY',
|
|
127
|
+
stopPrice: '0',
|
|
128
|
+
icebergQty: '0',
|
|
129
|
+
time: 3,
|
|
130
|
+
updateTime: 4,
|
|
131
|
+
isWorking: true,
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
mockedGetUnifiedUmOpenOrders.mockResolvedValue([
|
|
135
|
+
{
|
|
136
|
+
avgPrice: '0',
|
|
137
|
+
clientOrderId: 'um-fok',
|
|
138
|
+
cumQuote: '0',
|
|
139
|
+
executedQty: '0',
|
|
140
|
+
orderId: 21,
|
|
141
|
+
origQty: '3',
|
|
142
|
+
origType: 'LIMIT',
|
|
143
|
+
price: '3456',
|
|
144
|
+
reduceOnly: false,
|
|
145
|
+
side: 'SELL',
|
|
146
|
+
positionSide: 'SHORT',
|
|
147
|
+
status: 'NEW',
|
|
148
|
+
symbol: 'BTCUSDT',
|
|
149
|
+
time: 5,
|
|
150
|
+
timeInForce: 'FOK',
|
|
151
|
+
type: 'LIMIT',
|
|
152
|
+
updateTime: 6,
|
|
153
|
+
selfTradePreventionMode: 'NONE',
|
|
154
|
+
goodTillDate: 0,
|
|
155
|
+
},
|
|
156
|
+
]);
|
|
157
|
+
await expect(listSpotOrders({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([
|
|
158
|
+
{ order_id: '11', order_type: 'MAKER' },
|
|
159
|
+
{ order_id: '12', order_type: 'IOC' },
|
|
160
|
+
]);
|
|
161
|
+
await expect(listUnifiedUmOrders({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([
|
|
162
|
+
{ order_id: '21', order_type: 'FOK' },
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
//# 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":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,IAAI,CAAC,IAAI,CAAC,uBAAuB,EAAE,GAAG,EAAE,CAAC,CAAC;IACxC,iBAAiB,EAAE,IAAI,CAAC,EAAE,EAAE;IAC5B,sBAAsB,EAAE,IAAI,CAAC,EAAE,EAAE;IACjC,aAAa,EAAE,IAAI,CAAC,EAAE,EAAE;IACxB,WAAW,EAAE,IAAI,CAAC,EAAE,EAAE;CACvB,CAAC,CAAC,CAAC;AAEJ,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,EAAE,CAAC,CAAC;IACnC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC;CACjC,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,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,aAAa,EAAE,WAAW,EAAE,MAA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,SAAS,EAAE,IAAI;aAChB;SACF,CAAC,CAAC;QAEH,4BAA4B,CAAC,iBAAiB,CAAC;YAC7C;gBACE,QAAQ,EAAE,GAAG;gBACb,aAAa,EAAE,QAAQ;gBACvB,QAAQ,EAAE,GAAG;gBACb,WAAW,EAAE,GAAG;gBAChB,OAAO,EAAE,EAAE;gBACX,OAAO,EAAE,GAAG;gBACZ,QAAQ,EAAE,OAAO;gBACjB,KAAK,EAAE,MAAM;gBACb,UAAU,EAAE,KAAK;gBACjB,IAAI,EAAE,MAAM;gBACZ,YAAY,EAAE,OAAO;gBACrB,MAAM,EAAE,KAAK;gBACb,MAAM,EAAE,SAAS;gBACjB,IAAI,EAAE,CAAC;gBACP,WAAW,EAAE,KAAK;gBAClB,IAAI,EAAE,OAAO;gBACb,UAAU,EAAE,CAAC;gBACb,uBAAuB,EAAE,MAAM;gBAC/B,YAAY,EAAE,CAAC;aAChB;SACF,CAAC,CAAC;QAEH,MAAM,MAAM,CAAC,cAAc,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC;YAC1F,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE;YACvC,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE;SACtC,CAAC,CAAC;QAEH,MAAM,MAAM,CAAC,mBAAmB,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,aAAa,CAAC;YAC/F,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE;SACtC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { encodePath } from '@yuants/utils';\n\njest.mock('../../api/private-api', () => ({\n getSpotOpenOrders: jest.fn(),\n getUnifiedUmOpenOrders: jest.fn(),\n postSpotOrder: jest.fn(),\n postUmOrder: jest.fn(),\n}));\n\njest.mock('../../api/client', () => ({\n isApiError: jest.fn(() => false),\n}));\n\njest.mock('../trade-history', () => ({\n fetchTradeHistory: jest.fn(),\n}));\n\nimport { getSpotOpenOrders, getUnifiedUmOpenOrders, postSpotOrder, postUmOrder } from '../../api/private-api';\nimport { mapBinanceOrderTypeToYuants, mapOrderTypeToOrdType, mapOrderTypeToTimeInForce } from './order-utils';\nimport { listSpotOrders, listUnifiedUmOrders } from './listOrders';\nimport { submitOrder } from './submitOrder';\n\nconst mockedGetSpotOpenOrders = jest.mocked(getSpotOpenOrders);\nconst mockedGetUnifiedUmOpenOrders = jest.mocked(getUnifiedUmOpenOrders);\nconst mockedPostSpotOrder = jest.mocked(postSpotOrder);\nconst mockedPostUmOrder = jest.mocked(postUmOrder);\n\ndescribe('Binance order type mappings', () => {\n beforeEach(() => {\n jest.resetAllMocks();\n\n mockedPostSpotOrder.mockResolvedValue({\n symbol: 'BTCUSDT',\n orderId: 1,\n clientOrderId: 'spot-order',\n transactTime: 1,\n });\n\n mockedPostUmOrder.mockResolvedValue({\n orderId: 2,\n symbol: 'BTCUSDT',\n status: 'NEW',\n clientOrderId: 'um-order',\n price: '12345',\n avgPrice: '0',\n origQty: '1',\n executedQty: '0',\n cumQty: '0',\n cumQuote: '0',\n timeInForce: 'GTC',\n type: 'LIMIT',\n reduceOnly: false,\n side: 'BUY',\n positionSide: 'LONG',\n selfTradePreventionMode: 'NONE',\n goodTillDate: 0,\n updateTime: 1,\n });\n });\n\n test('maps IOC and FOK order types to Binance LIMIT orders', () => {\n expect(mapOrderTypeToOrdType('IOC')).toBe('LIMIT');\n expect(mapOrderTypeToOrdType('FOK')).toBe('LIMIT');\n });\n\n test('maps Yuan order types to Binance timeInForce values', () => {\n expect(mapOrderTypeToTimeInForce('LIMIT')).toBe('GTC');\n expect(mapOrderTypeToTimeInForce('MAKER')).toBe('GTX');\n expect(mapOrderTypeToTimeInForce('IOC')).toBe('IOC');\n expect(mapOrderTypeToTimeInForce('FOK')).toBe('FOK');\n expect(mapOrderTypeToTimeInForce('MARKET')).toBeUndefined();\n });\n\n test('maps Binance type and timeInForce back to Yuan order types', () => {\n expect(mapBinanceOrderTypeToYuants('MARKET')).toBe('MARKET');\n expect(mapBinanceOrderTypeToYuants('LIMIT', 'GTC')).toBe('LIMIT');\n expect(mapBinanceOrderTypeToYuants('LIMIT', 'GTX')).toBe('MAKER');\n expect(mapBinanceOrderTypeToYuants('LIMIT', 'IOC')).toBe('IOC');\n expect(mapBinanceOrderTypeToYuants('LIMIT', 'FOK')).toBe('FOK');\n });\n\n test.each([\n ['SPOT', 'IOC', 'IOC'],\n ['SPOT', 'FOK', 'FOK'],\n ['USDT-FUTURE', 'IOC', 'IOC'],\n ['USDT-FUTURE', 'FOK', 'FOK'],\n ])('submitOrder sends %s %s orders with timeInForce=%s', async (marketType, orderType, tif) => {\n const productId = encodePath('BINANCE', marketType, 'BTCUSDT');\n\n await expect(\n submitOrder(\n { access_key: 'ak', secret_key: 'sk' },\n {\n account_id: 'BINANCE/test',\n product_id: productId,\n order_type: orderType as 'IOC' | 'FOK',\n order_direction: 'OPEN_LONG',\n volume: 1,\n price: 12345,\n },\n ),\n ).resolves.toEqual({ order_id: marketType === 'SPOT' ? '1' : '2' });\n\n if (marketType === 'SPOT') {\n expect(mockedPostSpotOrder).toHaveBeenCalledWith(\n { access_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n return;\n }\n\n expect(mockedPostUmOrder).toHaveBeenCalledWith(\n { access_key: 'ak', secret_key: 'sk' },\n expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }),\n );\n });\n\n test('listOrders maps LIMIT+IOC/FOK/GTX readback values correctly', async () => {\n mockedGetSpotOpenOrders.mockResolvedValue([\n {\n symbol: 'BTCUSDT',\n orderId: 11,\n orderListId: -1,\n clientOrderId: 'spot-maker',\n price: '12345',\n origQty: '1',\n executedQty: '0',\n origQuoteOrderQty: '0',\n cummulativeQuoteQty: '0',\n status: 'NEW',\n timeInForce: 'GTX',\n type: 'LIMIT',\n side: 'BUY',\n stopPrice: '0',\n icebergQty: '0',\n time: 1,\n updateTime: 2,\n isWorking: true,\n },\n {\n symbol: 'ETHUSDT',\n orderId: 12,\n orderListId: -1,\n clientOrderId: 'spot-ioc',\n price: '2345',\n origQty: '2',\n executedQty: '0',\n origQuoteOrderQty: '0',\n cummulativeQuoteQty: '0',\n status: 'NEW',\n timeInForce: 'IOC',\n type: 'LIMIT',\n side: 'BUY',\n stopPrice: '0',\n icebergQty: '0',\n time: 3,\n updateTime: 4,\n isWorking: true,\n },\n ]);\n\n mockedGetUnifiedUmOpenOrders.mockResolvedValue([\n {\n avgPrice: '0',\n clientOrderId: 'um-fok',\n cumQuote: '0',\n executedQty: '0',\n orderId: 21,\n origQty: '3',\n origType: 'LIMIT',\n price: '3456',\n reduceOnly: false,\n side: 'SELL',\n positionSide: 'SHORT',\n status: 'NEW',\n symbol: 'BTCUSDT',\n time: 5,\n timeInForce: 'FOK',\n type: 'LIMIT',\n updateTime: 6,\n selfTradePreventionMode: 'NONE',\n goodTillDate: 0,\n },\n ]);\n\n await expect(listSpotOrders({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([\n { order_id: '11', order_type: 'MAKER' },\n { order_id: '12', order_type: 'IOC' },\n ]);\n\n await expect(listUnifiedUmOrders({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([\n { order_id: '21', order_type: 'FOK' },\n ]);\n });\n});\n"]}
|
|
@@ -72,6 +72,8 @@ export const mapOrderTypeToOrdType = (order_type) => {
|
|
|
72
72
|
switch (order_type) {
|
|
73
73
|
case 'LIMIT':
|
|
74
74
|
case 'MAKER':
|
|
75
|
+
case 'IOC':
|
|
76
|
+
case 'FOK':
|
|
75
77
|
return 'LIMIT';
|
|
76
78
|
case 'MARKET':
|
|
77
79
|
return 'MARKET';
|
|
@@ -79,10 +81,35 @@ export const mapOrderTypeToOrdType = (order_type) => {
|
|
|
79
81
|
throw new Error(`Unsupported order_type: ${order_type}`);
|
|
80
82
|
}
|
|
81
83
|
};
|
|
82
|
-
export const
|
|
84
|
+
export const mapOrderTypeToTimeInForce = (order_type) => {
|
|
85
|
+
switch (order_type) {
|
|
86
|
+
case 'LIMIT':
|
|
87
|
+
return 'GTC';
|
|
88
|
+
case 'MAKER':
|
|
89
|
+
return 'GTX';
|
|
90
|
+
case 'IOC':
|
|
91
|
+
return 'IOC';
|
|
92
|
+
case 'FOK':
|
|
93
|
+
return 'FOK';
|
|
94
|
+
case 'MARKET':
|
|
95
|
+
return undefined;
|
|
96
|
+
default:
|
|
97
|
+
throw new Error(`Unsupported order_type: ${order_type}`);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
export const mapBinanceOrderTypeToYuants = (binanceType, timeInForce) => {
|
|
83
101
|
switch (binanceType) {
|
|
84
102
|
case 'LIMIT':
|
|
85
|
-
|
|
103
|
+
switch (timeInForce) {
|
|
104
|
+
case 'GTX':
|
|
105
|
+
return 'MAKER';
|
|
106
|
+
case 'IOC':
|
|
107
|
+
return 'IOC';
|
|
108
|
+
case 'FOK':
|
|
109
|
+
return 'FOK';
|
|
110
|
+
default:
|
|
111
|
+
return 'LIMIT';
|
|
112
|
+
}
|
|
86
113
|
case 'MARKET':
|
|
87
114
|
return 'MARKET';
|
|
88
115
|
default:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order-utils.js","sourceRoot":"","sources":["../../../src/services/orders/order-utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,UAAkB,EAAE,EAAE;IACvD,MAAM,KAAK,GAAG,UAAU,CAAC,UAAU,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AC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;AACpF,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAA2C;IACpE,GAAG,EAAE,UAAU;IACf,gBAAgB,EAAE,QAAQ;IAC1B,MAAM,EAAE,QAAQ;IAChB,WAAW,EAAE,UAAU;IACvB,cAAc,EAAE,WAAW;IAC3B,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,WAAW;IACrB,OAAO,EAAE,WAAW;CACrB,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,MAAe,EAA0B,EAAE;;IAC/E,IAAI,CAAC,MAAM;QAAE,OAAO,UAAU,CAAC;IAC/B,OAAO,MAAA,qBAAqB,CAAC,MAAM,CAAC,mCAAI,UAAU,CAAC;AACrD,CAAC,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { decodePath } from '@yuants/utils';\nimport { createHash } from 'crypto';\n\nexport const decodeFutureSymbol = (product_id: string) => {\n const parts = decodePath(product_id);\n // Support both old format (usdt-future/symbol) and new format (BINANCE/USDT-FUTURE/symbol)\n if (parts.length === 2) {\n const [instType, symbol] = parts;\n if (instType !== 'usdt-future' || !symbol) {\n throw new Error(`Unsupported product_id for Binance futures: ${product_id}`);\n }\n return symbol;\n } else if (parts.length === 3) {\n const [datasource, instType, symbol] = parts;\n if (datasource !== 'BINANCE' || instType !== 'USDT-FUTURE' || !symbol) {\n throw new Error(`Unsupported product_id for Binance futures: ${product_id}`);\n }\n return symbol;\n }\n throw new Error(`Unsupported product_id for Binance futures: ${product_id}`);\n};\n\nexport const decodeSpotSymbol = (product_id: string) => {\n try {\n const parts = decodePath(product_id);\n // Support both old format (spot/symbol) and new format (BINANCE/SPOT/symbol)\n if (parts.length === 2) {\n const [instType, symbol] = parts;\n if (instType?.toLowerCase() === 'spot' && symbol) {\n return symbol;\n }\n } else if (parts.length === 3) {\n const [datasource, instType, symbol] = parts;\n if (datasource === 'BINANCE' && instType === 'SPOT' && symbol) {\n return symbol;\n }\n }\n } catch (err) {\n // ignore decode errors and fall back to raw product id\n }\n if (!product_id.includes('/')) {\n return product_id;\n }\n throw new Error(`Unsupported product_id for Binance spot: ${product_id}`);\n};\n\nexport const mapOrderDirectionToSide = (direction?: IOrder['order_direction']) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_SHORT':\n return 'BUY';\n case 'OPEN_SHORT':\n case 'CLOSE_LONG':\n return 'SELL';\n default:\n throw new Error(`Unsupported order_direction: ${direction}`);\n }\n};\n\nexport const mapOrderDirectionToPositionSide = (direction?: IOrder['order_direction']) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_LONG':\n return 'LONG';\n case 'OPEN_SHORT':\n case 'CLOSE_SHORT':\n return 'SHORT';\n default:\n throw new Error(`Unsupported order_direction for positionSide: ${direction}`);\n }\n};\n\nexport const mapOrderTypeToOrdType = (order_type?: IOrder['order_type']) => {\n switch (order_type) {\n case 'LIMIT':\n case 'MAKER':\n case 'IOC':\n case 'FOK':\n return 'LIMIT';\n case 'MARKET':\n return 'MARKET';\n default:\n throw new Error(`Unsupported order_type: ${order_type}`);\n }\n};\n\nexport const mapOrderTypeToTimeInForce = (order_type?: IOrder['order_type']) => {\n switch (order_type) {\n case 'LIMIT':\n return 'GTC';\n case 'MAKER':\n return 'GTX';\n case 'IOC':\n return 'IOC';\n case 'FOK':\n return 'FOK';\n case 'MARKET':\n return undefined;\n default:\n throw new Error(`Unsupported order_type: ${order_type}`);\n }\n};\n\nexport const mapBinanceOrderTypeToYuants = (\n binanceType?: string,\n timeInForce?: string,\n): IOrder['order_type'] => {\n switch (binanceType) {\n case 'LIMIT':\n switch (timeInForce) {\n case 'GTX':\n return 'MAKER';\n case 'IOC':\n return 'IOC';\n case 'FOK':\n return 'FOK';\n default:\n return 'LIMIT';\n }\n case 'MARKET':\n return 'MARKET';\n default:\n return 'LIMIT';\n }\n};\n\nexport const mapBinanceSideToYuantsDirection = (\n side?: string,\n positionSide?: string,\n): IOrder['order_direction'] | undefined => {\n if (!side || !positionSide) {\n return undefined;\n }\n if (positionSide === 'LONG') {\n return side === 'BUY' ? 'OPEN_LONG' : 'CLOSE_LONG';\n }\n if (positionSide === 'SHORT') {\n return side === 'SELL' ? 'OPEN_SHORT' : 'CLOSE_SHORT';\n }\n return undefined;\n};\n\nexport const mapSpotSideToOrderDirection = (side?: string): IOrder['order_direction'] => {\n if (side === 'SELL') {\n return 'CLOSE_LONG';\n }\n return 'OPEN_LONG';\n};\n\nexport const deriveClientOrderId = (order: IOrder) => {\n if (order.order_id) return `${order.order_id}`;\n const payload = JSON.stringify({\n account_id: order.account_id,\n product_id: order.product_id,\n order_direction: order.order_direction,\n order_type: order.order_type,\n price: order.price,\n volume: order.volume,\n });\n return `YUANTS${createHash('sha256').update(payload).digest('hex').slice(0, 24)}`;\n};\n\nconst BinanceOrderStatusMap: Record<string, IOrder['order_status']> = {\n NEW: 'ACCEPTED',\n PARTIALLY_FILLED: 'TRADED',\n FILLED: 'TRADED',\n PENDING_NEW: 'ACCEPTED',\n PENDING_CANCEL: 'CANCELLED',\n CANCELED: 'CANCELLED',\n CANCELLED: 'CANCELLED',\n REJECTED: 'CANCELLED',\n EXPIRED: 'CANCELLED',\n};\n\nexport const mapBinanceOrderStatus = (status?: string): IOrder['order_status'] => {\n if (!status) return 'ACCEPTED';\n return BinanceOrderStatusMap[status] ?? 'ACCEPTED';\n};\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { decodePath, newError } from '@yuants/utils';
|
|
2
2
|
import { isApiError } from '../../api/client';
|
|
3
3
|
import { postSpotOrder, postUmOrder } from '../../api/private-api';
|
|
4
|
-
import { decodeFutureSymbol, decodeSpotSymbol, deriveClientOrderId, mapOrderDirectionToPositionSide, mapOrderDirectionToSide, mapOrderTypeToOrdType, } from './order-utils';
|
|
4
|
+
import { decodeFutureSymbol, decodeSpotSymbol, deriveClientOrderId, mapOrderDirectionToPositionSide, mapOrderDirectionToSide, mapOrderTypeToOrdType, mapOrderTypeToTimeInForce, } from './order-utils';
|
|
5
5
|
import { fetchTradeHistory } from '../trade-history';
|
|
6
6
|
const submitUnifiedOrder = async (credential, order) => {
|
|
7
7
|
const symbol = decodeFutureSymbol(order.product_id);
|
|
@@ -13,7 +13,7 @@ const submitUnifiedOrder = async (credential, order) => {
|
|
|
13
13
|
const side = mapOrderDirectionToSide(order.order_direction);
|
|
14
14
|
const positionSide = isSingleSideMode ? undefined : mapOrderDirectionToPositionSide(order.order_direction);
|
|
15
15
|
const type = mapOrderTypeToOrdType(order.order_type);
|
|
16
|
-
const timeInForce = order.order_type
|
|
16
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
17
17
|
const reduceOnly = isSingleSideMode
|
|
18
18
|
? order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT'
|
|
19
19
|
? 'true'
|
|
@@ -45,7 +45,7 @@ const submitSpotOrder = async (credential, order) => {
|
|
|
45
45
|
}
|
|
46
46
|
const side = mapOrderDirectionToSide(order.order_direction);
|
|
47
47
|
const type = mapOrderTypeToOrdType(order.order_type);
|
|
48
|
-
const timeInForce = order.order_type
|
|
48
|
+
const timeInForce = mapOrderTypeToTimeInForce(order.order_type);
|
|
49
49
|
const params = {
|
|
50
50
|
symbol,
|
|
51
51
|
side,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../../src/services/orders/submitOrder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAe,aAAa,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAChF,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,mBAAmB,EACnB,+BAA+B,EAC/B,uBAAuB,EACvB,qBAAqB,
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IAAI,KAAK,MAAM,EAAE,CAAC;QACpB,OAAO,eAAe,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAC5C,CAAC;IAED,MAAM,QAAQ,CAAC,+CAA+C,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;AAC7E,CAAC,CAAC","sourcesContent":["import { IActionHandlerOfSubmitOrder } from '@yuants/data-order';\nimport { decodePath, newError } from '@yuants/utils';\nimport { isApiError } from '../../api/client';\nimport { ICredential, postSpotOrder, postUmOrder } from '../../api/private-api';\nimport {\n decodeFutureSymbol,\n decodeSpotSymbol,\n deriveClientOrderId,\n mapOrderDirectionToPositionSide,\n mapOrderDirectionToSide,\n mapOrderTypeToOrdType,\n mapOrderTypeToTimeInForce,\n} from './order-utils';\nimport { fetchTradeHistory } from '../trade-history';\n\nconst submitUnifiedOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const symbol = decodeFutureSymbol(order.product_id);\n if (!order.volume) {\n throw new Error('Binance submitOrder requires order.volume to be set');\n }\n // 单向持仓模式 (single side mode) 下\n const isSingleSideMode = false; // TODO: fetch from account info API and cache it\n const side = mapOrderDirectionToSide(order.order_direction);\n const positionSide = isSingleSideMode ? undefined : mapOrderDirectionToPositionSide(order.order_direction);\n const type = mapOrderTypeToOrdType(order.order_type);\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n const reduceOnly = isSingleSideMode\n ? order.order_direction === 'CLOSE_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? 'true'\n : undefined\n : undefined;\n\n const res = await postUmOrder(credential, {\n symbol,\n side,\n positionSide,\n type,\n quantity: order.volume,\n price: order.price,\n timeInForce,\n reduceOnly,\n newClientOrderId: deriveClientOrderId(order),\n });\n if (isApiError(res)) {\n throw new Error(`Binance submit unified order failed: ${res.code} ${res.msg}`);\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${res.orderId}` };\n};\n\nconst submitSpotOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const symbol = decodeSpotSymbol(order.product_id);\n if (!order.volume) {\n throw new Error('Binance submitOrder requires order.volume to be set');\n }\n const side = mapOrderDirectionToSide(order.order_direction);\n const type = mapOrderTypeToOrdType(order.order_type);\n const timeInForce = mapOrderTypeToTimeInForce(order.order_type);\n const params: Parameters<typeof postSpotOrder>[1] = {\n symbol,\n side,\n type,\n quantity: order.volume,\n newClientOrderId: deriveClientOrderId(order),\n };\n if (type === 'LIMIT') {\n if (order.price === undefined) {\n throw new Error('Binance spot LIMIT order requires price');\n }\n params.price = order.price;\n params.timeInForce = timeInForce;\n }\n if (order.price !== undefined && type === 'MARKET') {\n params.price = order.price;\n }\n const res = await postSpotOrder(credential, params);\n if (isApiError(res)) {\n throw new Error(`Binance submit spot order failed: ${res.code} ${res.msg}`);\n }\n if (type === 'MARKET') {\n fetchTradeHistory(credential, order.product_id, symbol);\n }\n return { order_id: `${res.orderId}` };\n};\n\nexport const submitOrder: IActionHandlerOfSubmitOrder<ICredential> = async (credential, order) => {\n const [, TYPE] = decodePath(order.product_id);\n\n if (TYPE === 'USDT-FUTURE') {\n return submitUnifiedOrder(credential, order);\n }\n if (TYPE === 'SPOT') {\n return submitSpotOrder(credential, order);\n }\n\n throw newError('BINANCE_SUBMIT_ORDER_UNSUPPORTED_PRODUCT_TYPE', { order });\n};\n"]}
|
|
@@ -16,7 +16,7 @@ const listUnifiedUmOrders = async (credential) => {
|
|
|
16
16
|
order_id: `${order.orderId}`,
|
|
17
17
|
account_id: '',
|
|
18
18
|
product_id: (0, utils_1.encodePath)('BINANCE', 'USDT-FUTURE', order.symbol),
|
|
19
|
-
order_type: (0, order_utils_1.mapBinanceOrderTypeToYuants)(order.type),
|
|
19
|
+
order_type: (0, order_utils_1.mapBinanceOrderTypeToYuants)(order.type, order.timeInForce),
|
|
20
20
|
order_direction: (_a = (0, order_utils_1.mapBinanceSideToYuantsDirection)(order.side, order.positionSide)) !== null && _a !== void 0 ? _a : (order.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT'),
|
|
21
21
|
volume: +order.origQty,
|
|
22
22
|
traded_volume: +order.executedQty,
|
|
@@ -37,7 +37,7 @@ const listSpotOrders = async (credential) => {
|
|
|
37
37
|
order_id: `${order.orderId}`,
|
|
38
38
|
account_id: '',
|
|
39
39
|
product_id: (0, utils_1.encodePath)('BINANCE', 'SPOT', order.symbol),
|
|
40
|
-
order_type: (0, order_utils_1.mapBinanceOrderTypeToYuants)(order.type),
|
|
40
|
+
order_type: (0, order_utils_1.mapBinanceOrderTypeToYuants)(order.type, order.timeInForce),
|
|
41
41
|
order_direction: (0, order_utils_1.mapSpotSideToOrderDirection)(order.side),
|
|
42
42
|
volume: +order.origQty,
|
|
43
43
|
traded_volume: +order.executedQty,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"listOrders.js","sourceRoot":"","sources":["../../../src/services/orders/listOrders.ts"],"names":[],"mappings":";;;AACA,yCAAmE;AACnE,6CAA8C;AAC9C,uDAA+F;AAC/F,+CAKuB;AAEhB,MAAM,mBAAmB,GAAG,KAAK,EAAE,UAAuB,EAAqB,EAAE;IACtF,MAAM,GAAG,GAAG,MAAM,IAAA,oCAAsB,EAAC,UAAU,CAAC,CAAC;IACrD,IAAI,IAAA,mBAAU,EAAC,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,GAAG,CAAC,GAAG,CACZ,CAAC,KAAK,EAAU,EAAE;;QAAC,OAAA,CAAC;YAClB,QAAQ,EAAE,GAAG,KAAK,CAAC,OAAO,EAAE;YAC5B,UAAU,EAAE,EAAE;YACd,UAAU,EAAE,IAAA,kBAAU,EAAC,SAAS,EAAE,aAAa,EAAE,KAAK,CAAC,MAAM,CAAC;YAC9D,UAAU,EAAE,IAAA,yCAA2B,EAAC,KAAK,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3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IAElB,MAAM,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG,IAAA,kBAAU,EAAC,UAAU,CAAC,CAAC,CAAC,8BAA8B;IAC5E,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,MAAM,MAAM,GAAG,MAAM,IAAA,sBAAc,EAAC,UAAU,CAAC,CAAC;QAChD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IACnE,CAAC;IACD,IAAI,QAAQ,KAAK,aAAa,EAAE,CAAC;QAC/B,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAmB,EAAC,UAAU,CAAC,CAAC;QACrD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,KAAK,UAAU,CAAC,CAAC;IACnE,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAC;AACvD,CAAC,CAAC;AAdW,QAAA,oBAAoB,wBAc/B","sourcesContent":["import { IActionHandlerOfListOrders, IOrder } from '@yuants/data-order';\nimport { decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { isApiError } from '../../api/client';\nimport { getSpotOpenOrders, getUnifiedUmOpenOrders, ICredential } from '../../api/private-api';\nimport {\n mapBinanceOrderStatus,\n mapBinanceOrderTypeToYuants,\n mapBinanceSideToYuantsDirection,\n mapSpotSideToOrderDirection,\n} from './order-utils';\n\nexport const listUnifiedUmOrders = async (credential: ICredential): Promise<IOrder[]> => {\n const res = await getUnifiedUmOpenOrders(credential);\n if (isApiError(res)) {\n throw new Error(res.msg);\n }\n return res.map(\n (order): IOrder => ({\n order_id: `${order.orderId}`,\n account_id: '',\n product_id: encodePath('BINANCE', 'USDT-FUTURE', order.symbol),\n order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),\n order_direction:\n mapBinanceSideToYuantsDirection(order.side, order.positionSide) ??\n (order.side === 'BUY' ? 'OPEN_LONG' : 'OPEN_SHORT'),\n volume: +order.origQty,\n traded_volume: +order.executedQty,\n price: order.price === undefined ? undefined : +order.price,\n submit_at: order.time,\n updated_at: formatTime(order.updateTime),\n order_status: mapBinanceOrderStatus(order.status),\n }),\n );\n};\n\nexport const listSpotOrders = async (credential: ICredential): Promise<IOrder[]> => {\n const res = await getSpotOpenOrders(credential);\n if (isApiError(res)) {\n throw new Error(res.msg);\n }\n return res.map(\n (order): IOrder => ({\n order_id: `${order.orderId}`,\n account_id: '',\n product_id: encodePath('BINANCE', 'SPOT', order.symbol),\n order_type: mapBinanceOrderTypeToYuants(order.type, order.timeInForce),\n order_direction: mapSpotSideToOrderDirection(order.side),\n volume: +order.origQty,\n traded_volume: +order.executedQty,\n price: order.price === undefined ? undefined : +order.price,\n submit_at: order.time,\n updated_at: formatTime(order.updateTime),\n order_status: mapBinanceOrderStatus(order.status),\n }),\n );\n};\n\nexport const getOrdersByProductId = async function (\n credential: ICredential,\n product_id: string,\n): Promise<IOrder[]> {\n const [_, instType] = decodePath(product_id); // BINANCE/USDT-FUTURE/ADAUSDT\n if (instType === 'SPOT') {\n const orders = await listSpotOrders(credential);\n return orders.filter((order) => order.product_id === product_id);\n }\n if (instType === 'USDT-FUTURE') {\n const orders = await listUnifiedUmOrders(credential);\n return orders.filter((order) => order.product_id === product_id);\n }\n throw new Error(`Unsupported instType: ${instType}`);\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,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@yuants/utils");
|
|
4
|
+
jest.mock('../../api/private-api', () => ({
|
|
5
|
+
getSpotOpenOrders: jest.fn(),
|
|
6
|
+
getUnifiedUmOpenOrders: jest.fn(),
|
|
7
|
+
postSpotOrder: jest.fn(),
|
|
8
|
+
postUmOrder: jest.fn(),
|
|
9
|
+
}));
|
|
10
|
+
jest.mock('../../api/client', () => ({
|
|
11
|
+
isApiError: jest.fn(() => false),
|
|
12
|
+
}));
|
|
13
|
+
jest.mock('../trade-history', () => ({
|
|
14
|
+
fetchTradeHistory: jest.fn(),
|
|
15
|
+
}));
|
|
16
|
+
const private_api_1 = require("../../api/private-api");
|
|
17
|
+
const order_utils_1 = require("./order-utils");
|
|
18
|
+
const listOrders_1 = require("./listOrders");
|
|
19
|
+
const submitOrder_1 = require("./submitOrder");
|
|
20
|
+
const mockedGetSpotOpenOrders = jest.mocked(private_api_1.getSpotOpenOrders);
|
|
21
|
+
const mockedGetUnifiedUmOpenOrders = jest.mocked(private_api_1.getUnifiedUmOpenOrders);
|
|
22
|
+
const mockedPostSpotOrder = jest.mocked(private_api_1.postSpotOrder);
|
|
23
|
+
const mockedPostUmOrder = jest.mocked(private_api_1.postUmOrder);
|
|
24
|
+
describe('Binance order type mappings', () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
jest.resetAllMocks();
|
|
27
|
+
mockedPostSpotOrder.mockResolvedValue({
|
|
28
|
+
symbol: 'BTCUSDT',
|
|
29
|
+
orderId: 1,
|
|
30
|
+
clientOrderId: 'spot-order',
|
|
31
|
+
transactTime: 1,
|
|
32
|
+
});
|
|
33
|
+
mockedPostUmOrder.mockResolvedValue({
|
|
34
|
+
orderId: 2,
|
|
35
|
+
symbol: 'BTCUSDT',
|
|
36
|
+
status: 'NEW',
|
|
37
|
+
clientOrderId: 'um-order',
|
|
38
|
+
price: '12345',
|
|
39
|
+
avgPrice: '0',
|
|
40
|
+
origQty: '1',
|
|
41
|
+
executedQty: '0',
|
|
42
|
+
cumQty: '0',
|
|
43
|
+
cumQuote: '0',
|
|
44
|
+
timeInForce: 'GTC',
|
|
45
|
+
type: 'LIMIT',
|
|
46
|
+
reduceOnly: false,
|
|
47
|
+
side: 'BUY',
|
|
48
|
+
positionSide: 'LONG',
|
|
49
|
+
selfTradePreventionMode: 'NONE',
|
|
50
|
+
goodTillDate: 0,
|
|
51
|
+
updateTime: 1,
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
test('maps IOC and FOK order types to Binance LIMIT orders', () => {
|
|
55
|
+
expect((0, order_utils_1.mapOrderTypeToOrdType)('IOC')).toBe('LIMIT');
|
|
56
|
+
expect((0, order_utils_1.mapOrderTypeToOrdType)('FOK')).toBe('LIMIT');
|
|
57
|
+
});
|
|
58
|
+
test('maps Yuan order types to Binance timeInForce values', () => {
|
|
59
|
+
expect((0, order_utils_1.mapOrderTypeToTimeInForce)('LIMIT')).toBe('GTC');
|
|
60
|
+
expect((0, order_utils_1.mapOrderTypeToTimeInForce)('MAKER')).toBe('GTX');
|
|
61
|
+
expect((0, order_utils_1.mapOrderTypeToTimeInForce)('IOC')).toBe('IOC');
|
|
62
|
+
expect((0, order_utils_1.mapOrderTypeToTimeInForce)('FOK')).toBe('FOK');
|
|
63
|
+
expect((0, order_utils_1.mapOrderTypeToTimeInForce)('MARKET')).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
test('maps Binance type and timeInForce back to Yuan order types', () => {
|
|
66
|
+
expect((0, order_utils_1.mapBinanceOrderTypeToYuants)('MARKET')).toBe('MARKET');
|
|
67
|
+
expect((0, order_utils_1.mapBinanceOrderTypeToYuants)('LIMIT', 'GTC')).toBe('LIMIT');
|
|
68
|
+
expect((0, order_utils_1.mapBinanceOrderTypeToYuants)('LIMIT', 'GTX')).toBe('MAKER');
|
|
69
|
+
expect((0, order_utils_1.mapBinanceOrderTypeToYuants)('LIMIT', 'IOC')).toBe('IOC');
|
|
70
|
+
expect((0, order_utils_1.mapBinanceOrderTypeToYuants)('LIMIT', 'FOK')).toBe('FOK');
|
|
71
|
+
});
|
|
72
|
+
test.each([
|
|
73
|
+
['SPOT', 'IOC', 'IOC'],
|
|
74
|
+
['SPOT', 'FOK', 'FOK'],
|
|
75
|
+
['USDT-FUTURE', 'IOC', 'IOC'],
|
|
76
|
+
['USDT-FUTURE', 'FOK', 'FOK'],
|
|
77
|
+
])('submitOrder sends %s %s orders with timeInForce=%s', async (marketType, orderType, tif) => {
|
|
78
|
+
const productId = (0, utils_1.encodePath)('BINANCE', marketType, 'BTCUSDT');
|
|
79
|
+
await expect((0, submitOrder_1.submitOrder)({ access_key: 'ak', secret_key: 'sk' }, {
|
|
80
|
+
account_id: 'BINANCE/test',
|
|
81
|
+
product_id: productId,
|
|
82
|
+
order_type: orderType,
|
|
83
|
+
order_direction: 'OPEN_LONG',
|
|
84
|
+
volume: 1,
|
|
85
|
+
price: 12345,
|
|
86
|
+
})).resolves.toEqual({ order_id: marketType === 'SPOT' ? '1' : '2' });
|
|
87
|
+
if (marketType === 'SPOT') {
|
|
88
|
+
expect(mockedPostSpotOrder).toHaveBeenCalledWith({ access_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
expect(mockedPostUmOrder).toHaveBeenCalledWith({ access_key: 'ak', secret_key: 'sk' }, expect.objectContaining({ symbol: 'BTCUSDT', type: 'LIMIT', timeInForce: tif, price: 12345 }));
|
|
92
|
+
});
|
|
93
|
+
test('listOrders maps LIMIT+IOC/FOK/GTX readback values correctly', async () => {
|
|
94
|
+
mockedGetSpotOpenOrders.mockResolvedValue([
|
|
95
|
+
{
|
|
96
|
+
symbol: 'BTCUSDT',
|
|
97
|
+
orderId: 11,
|
|
98
|
+
orderListId: -1,
|
|
99
|
+
clientOrderId: 'spot-maker',
|
|
100
|
+
price: '12345',
|
|
101
|
+
origQty: '1',
|
|
102
|
+
executedQty: '0',
|
|
103
|
+
origQuoteOrderQty: '0',
|
|
104
|
+
cummulativeQuoteQty: '0',
|
|
105
|
+
status: 'NEW',
|
|
106
|
+
timeInForce: 'GTX',
|
|
107
|
+
type: 'LIMIT',
|
|
108
|
+
side: 'BUY',
|
|
109
|
+
stopPrice: '0',
|
|
110
|
+
icebergQty: '0',
|
|
111
|
+
time: 1,
|
|
112
|
+
updateTime: 2,
|
|
113
|
+
isWorking: true,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
symbol: 'ETHUSDT',
|
|
117
|
+
orderId: 12,
|
|
118
|
+
orderListId: -1,
|
|
119
|
+
clientOrderId: 'spot-ioc',
|
|
120
|
+
price: '2345',
|
|
121
|
+
origQty: '2',
|
|
122
|
+
executedQty: '0',
|
|
123
|
+
origQuoteOrderQty: '0',
|
|
124
|
+
cummulativeQuoteQty: '0',
|
|
125
|
+
status: 'NEW',
|
|
126
|
+
timeInForce: 'IOC',
|
|
127
|
+
type: 'LIMIT',
|
|
128
|
+
side: 'BUY',
|
|
129
|
+
stopPrice: '0',
|
|
130
|
+
icebergQty: '0',
|
|
131
|
+
time: 3,
|
|
132
|
+
updateTime: 4,
|
|
133
|
+
isWorking: true,
|
|
134
|
+
},
|
|
135
|
+
]);
|
|
136
|
+
mockedGetUnifiedUmOpenOrders.mockResolvedValue([
|
|
137
|
+
{
|
|
138
|
+
avgPrice: '0',
|
|
139
|
+
clientOrderId: 'um-fok',
|
|
140
|
+
cumQuote: '0',
|
|
141
|
+
executedQty: '0',
|
|
142
|
+
orderId: 21,
|
|
143
|
+
origQty: '3',
|
|
144
|
+
origType: 'LIMIT',
|
|
145
|
+
price: '3456',
|
|
146
|
+
reduceOnly: false,
|
|
147
|
+
side: 'SELL',
|
|
148
|
+
positionSide: 'SHORT',
|
|
149
|
+
status: 'NEW',
|
|
150
|
+
symbol: 'BTCUSDT',
|
|
151
|
+
time: 5,
|
|
152
|
+
timeInForce: 'FOK',
|
|
153
|
+
type: 'LIMIT',
|
|
154
|
+
updateTime: 6,
|
|
155
|
+
selfTradePreventionMode: 'NONE',
|
|
156
|
+
goodTillDate: 0,
|
|
157
|
+
},
|
|
158
|
+
]);
|
|
159
|
+
await expect((0, listOrders_1.listSpotOrders)({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([
|
|
160
|
+
{ order_id: '11', order_type: 'MAKER' },
|
|
161
|
+
{ order_id: '12', order_type: 'IOC' },
|
|
162
|
+
]);
|
|
163
|
+
await expect((0, listOrders_1.listUnifiedUmOrders)({ access_key: 'ak', secret_key: 'sk' })).resolves.toMatchObject([
|
|
164
|
+
{ order_id: '21', order_type: 'FOK' },
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
//# sourceMappingURL=order-type-mapping.test.js.map
|