@yuants/vendor-huobi 0.11.41 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/account-info.js +235 -0
- package/dist/account-info.js.map +1 -0
- package/dist/api/private-api.js +251 -0
- package/dist/api/private-api.js.map +1 -0
- package/dist/api/public-api.js +145 -0
- package/dist/api/public-api.js.map +1 -0
- package/dist/api.js +0 -279
- package/dist/api.js.map +1 -1
- package/dist/index.js +28 -524
- package/dist/index.js.map +1 -1
- package/dist/interest_rate.js +2 -2
- package/dist/interest_rate.js.map +1 -1
- package/dist/order-actions-with-credentials.js +31 -0
- package/dist/order-actions-with-credentials.js.map +1 -0
- package/dist/order-actions.js +29 -0
- package/dist/order-actions.js.map +1 -0
- package/dist/orders/submitOrder.js +82 -0
- package/dist/orders/submitOrder.js.map +1 -0
- package/dist/product.js +4 -4
- package/dist/product.js.map +1 -1
- package/dist/quote.js +5 -5
- package/dist/quote.js.map +1 -1
- package/dist/transfer.js +248 -0
- package/dist/transfer.js.map +1 -0
- package/dist/uid.js +22 -0
- package/dist/uid.js.map +1 -0
- package/lib/account-info.d.ts +35 -0
- package/lib/account-info.d.ts.map +1 -0
- package/lib/account-info.js +243 -0
- package/lib/account-info.js.map +1 -0
- package/lib/api/private-api.d.ts +471 -0
- package/lib/api/private-api.d.ts.map +1 -0
- package/lib/api/private-api.js +280 -0
- package/lib/api/private-api.js.map +1 -0
- package/lib/api/public-api.d.ts +308 -0
- package/lib/api/public-api.d.ts.map +1 -0
- package/lib/api/public-api.js +158 -0
- package/lib/api/public-api.js.map +1 -0
- package/lib/api.d.ts +2 -621
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +0 -279
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +26 -522
- package/lib/index.js.map +1 -1
- package/lib/interest_rate.js +2 -2
- package/lib/interest_rate.js.map +1 -1
- package/lib/order-actions-with-credentials.d.ts +2 -0
- package/lib/order-actions-with-credentials.d.ts.map +1 -0
- package/lib/order-actions-with-credentials.js +33 -0
- package/lib/order-actions-with-credentials.js.map +1 -0
- package/lib/order-actions.d.ts +7 -0
- package/lib/order-actions.d.ts.map +1 -0
- package/lib/order-actions.js +33 -0
- package/lib/order-actions.js.map +1 -0
- package/lib/orders/submitOrder.d.ts +11 -0
- package/lib/orders/submitOrder.d.ts.map +1 -0
- package/lib/orders/submitOrder.js +87 -0
- package/lib/orders/submitOrder.js.map +1 -0
- package/lib/product.js +4 -4
- package/lib/product.js.map +1 -1
- package/lib/quote.js +5 -5
- package/lib/quote.js.map +1 -1
- package/lib/transfer.d.ts +19 -0
- package/lib/transfer.d.ts.map +1 -0
- package/lib/transfer.js +255 -0
- package/lib/transfer.js.map +1 -0
- package/lib/uid.d.ts +13 -0
- package/lib/uid.d.ts.map +1 -0
- package/lib/uid.js +26 -0
- package/lib/uid.js.map +1 -0
- package/package.json +1 -1
- package/temp/package-deps.json +16 -9
- package/dist/logger.js +0 -91
- package/dist/logger.js.map +0 -1
- package/lib/logger.d.ts +0 -21
- package/lib/logger.d.ts.map +0 -1
- package/lib/logger.js +0 -98
- package/lib/logger.js.map +0 -1
package/lib/index.js
CHANGED
|
@@ -2,548 +2,52 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const data_account_1 = require("@yuants/data-account");
|
|
4
4
|
const protocol_1 = require("@yuants/protocol");
|
|
5
|
-
const transfer_1 = require("@yuants/transfer");
|
|
6
5
|
const utils_1 = require("@yuants/utils");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
6
|
+
const account_info_1 = require("./account-info");
|
|
7
|
+
const private_api_1 = require("./api/private-api");
|
|
9
8
|
require("./interest_rate");
|
|
10
|
-
const
|
|
9
|
+
const order_actions_1 = require("./order-actions");
|
|
10
|
+
require("./order-actions-with-credentials");
|
|
11
11
|
require("./quote");
|
|
12
|
+
const transfer_1 = require("./transfer");
|
|
13
|
+
const uid_1 = require("./uid");
|
|
12
14
|
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
15
|
+
const credential = (0, private_api_1.getDefaultCredential)();
|
|
13
16
|
(async () => {
|
|
14
|
-
var _a
|
|
15
|
-
|
|
17
|
+
var _a;
|
|
18
|
+
// 账户类型切换
|
|
19
|
+
const swapAccountTypeRes = await (0, private_api_1.getSwapUnifiedAccountType)(credential);
|
|
16
20
|
if (((_a = swapAccountTypeRes.data) === null || _a === void 0 ? void 0 : _a.account_type) === 1) {
|
|
17
21
|
console.info((0, utils_1.formatTime)(Date.now()), 'SwitchingAccountType', `previous: ${swapAccountTypeRes.data.account_type}, switching to 2 (unified account)`);
|
|
18
|
-
const switchRes = await
|
|
22
|
+
const switchRes = await (0, private_api_1.postSwapSwitchAccountType)(credential, { account_type: 2 });
|
|
19
23
|
console.info((0, utils_1.formatTime)(Date.now()), 'SwitchingAccountType', `current: ${switchRes.data.account_type}`);
|
|
20
24
|
}
|
|
21
|
-
const huobiUid = (await
|
|
25
|
+
const huobiUid = (await (0, private_api_1.getUid)(credential)).data;
|
|
22
26
|
console.info((0, utils_1.formatTime)(Date.now()), 'UID', huobiUid);
|
|
23
|
-
const huobiAccounts = await
|
|
24
|
-
const
|
|
25
|
-
const spotAccountUid = (_c = huobiAccounts.data.find((v) => v.type === 'spot')) === null || _c === void 0 ? void 0 : _c.id;
|
|
27
|
+
const huobiAccounts = await (0, private_api_1.getAccount)(credential);
|
|
28
|
+
const spotAccountUid = (await uid_1.spotAccountUidCache.query(JSON.stringify(credential)));
|
|
26
29
|
console.info((0, utils_1.formatTime)(Date.now()), 'huobiAccount', JSON.stringify(huobiAccounts));
|
|
27
30
|
const account_id = `huobi/${huobiUid}`;
|
|
28
31
|
const SPOT_ACCOUNT_ID = `${account_id}/spot/usdt`;
|
|
29
32
|
const SUPER_MARGIN_ACCOUNT_ID = `${account_id}/super-margin`;
|
|
30
33
|
const SWAP_ACCOUNT_ID = `${account_id}/swap`;
|
|
31
|
-
const subUsersRes = await
|
|
34
|
+
const subUsersRes = await (0, private_api_1.getSubUserList)(credential);
|
|
32
35
|
const subAccounts = subUsersRes.data;
|
|
33
36
|
const isMainAccount = subUsersRes.ok;
|
|
34
37
|
console.info((0, utils_1.formatTime)(Date.now()), 'subAccounts', JSON.stringify(subAccounts));
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
throw new Error('Failed to get unified account info');
|
|
40
|
-
}
|
|
41
|
-
const balanceData = balance.data.find((v) => v.margin_asset === 'USDT');
|
|
42
|
-
if (!balanceData) {
|
|
43
|
-
throw new Error('No USDT balance found in unified account');
|
|
44
|
-
}
|
|
45
|
-
const equity = balanceData.margin_balance;
|
|
46
|
-
const free = balanceData.withdraw_available;
|
|
47
|
-
// positions
|
|
48
|
-
const positionsRes = await api_1.client.getSwapCrossPositionInfo();
|
|
49
|
-
const mapProductIdToPerpetualProduct = await (0, rxjs_1.firstValueFrom)(product_1.swapProductService.mapProductIdToProduct$);
|
|
50
|
-
const positions = (positionsRes.data || []).map((v) => {
|
|
51
|
-
const product_id = v.contract_code;
|
|
52
|
-
const theProduct = mapProductIdToPerpetualProduct === null || mapProductIdToPerpetualProduct === void 0 ? void 0 : mapProductIdToPerpetualProduct.get(product_id);
|
|
53
|
-
const valuation = v.volume * v.last_price * ((theProduct === null || theProduct === void 0 ? void 0 : theProduct.value_scale) || 1);
|
|
54
|
-
return {
|
|
55
|
-
position_id: `${v.contract_code}/${v.contract_type}/${v.direction}/${v.margin_mode}`,
|
|
56
|
-
datasource_id: 'HUOBI-SWAP',
|
|
57
|
-
product_id,
|
|
58
|
-
direction: v.direction === 'buy' ? 'LONG' : 'SHORT',
|
|
59
|
-
volume: v.volume,
|
|
60
|
-
free_volume: v.available,
|
|
61
|
-
position_price: v.cost_hold,
|
|
62
|
-
closable_price: v.last_price,
|
|
63
|
-
floating_profit: v.profit_unreal,
|
|
64
|
-
valuation,
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
|
-
// orders
|
|
68
|
-
// const orders: IOrder[] = [];
|
|
69
|
-
// let page_index = 1;
|
|
70
|
-
// const page_size = 50;
|
|
71
|
-
// while (true) {
|
|
72
|
-
// const ordersRes = await client.getSwapOpenOrders({ page_index, page_size });
|
|
73
|
-
// if (!ordersRes.data?.orders || ordersRes.data.orders.length === 0) {
|
|
74
|
-
// break;
|
|
75
|
-
// }
|
|
76
|
-
// const pageOrders: IOrder[] = ordersRes.data.orders.map((v): IOrder => {
|
|
77
|
-
// return {
|
|
78
|
-
// order_id: v.order_id_str,
|
|
79
|
-
// account_id: SWAP_ACCOUNT_ID,
|
|
80
|
-
// product_id: v.contract_code,
|
|
81
|
-
// order_type: ['lightning'].includes(v.order_price_type)
|
|
82
|
-
// ? 'MARKET'
|
|
83
|
-
// : ['limit', 'opponent', 'post_only', 'optimal_5', 'optimal_10', 'optimal_20'].includes(
|
|
84
|
-
// v.order_price_type,
|
|
85
|
-
// )
|
|
86
|
-
// ? 'LIMIT'
|
|
87
|
-
// : ['fok'].includes(v.order_price_type)
|
|
88
|
-
// ? 'FOK'
|
|
89
|
-
// : v.order_price_type.includes('ioc')
|
|
90
|
-
// ? 'IOC'
|
|
91
|
-
// : 'STOP', // unreachable code
|
|
92
|
-
// order_direction:
|
|
93
|
-
// v.direction === 'open'
|
|
94
|
-
// ? v.offset === 'buy'
|
|
95
|
-
// ? 'OPEN_LONG'
|
|
96
|
-
// : 'OPEN_SHORT'
|
|
97
|
-
// : v.offset === 'buy'
|
|
98
|
-
// ? 'CLOSE_SHORT'
|
|
99
|
-
// : 'CLOSE_LONG',
|
|
100
|
-
// volume: v.volume,
|
|
101
|
-
// submit_at: v.created_at,
|
|
102
|
-
// price: v.price,
|
|
103
|
-
// traded_volume: v.trade_volume,
|
|
104
|
-
// };
|
|
105
|
-
// });
|
|
106
|
-
// orders.push(...pageOrders);
|
|
107
|
-
// page_index++;
|
|
108
|
-
// }
|
|
109
|
-
return {
|
|
110
|
-
money: {
|
|
111
|
-
currency: 'USDT',
|
|
112
|
-
equity,
|
|
113
|
-
free,
|
|
114
|
-
},
|
|
115
|
-
positions,
|
|
116
|
-
};
|
|
117
|
-
}, { auto_refresh_interval: 1000 });
|
|
118
|
-
const superMarginUnifiedRawAccountBalance$ = (0, rxjs_1.defer)(() => api_1.client.getSpotAccountBalance(superMarginAccountUid)).pipe(
|
|
119
|
-
//
|
|
120
|
-
(0, rxjs_1.map)((res) => res.data), (0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.tap)({
|
|
121
|
-
error: (e) => {
|
|
122
|
-
console.error((0, utils_1.formatTime)(Date.now()), 'unifiedRaw', e);
|
|
123
|
-
},
|
|
124
|
-
}), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
|
|
125
|
-
const subscriptions = new Set();
|
|
126
|
-
(0, rxjs_1.from)(api_1.client.spot_ws.connection$).subscribe(() => {
|
|
127
|
-
subscriptions.clear();
|
|
128
|
-
});
|
|
129
|
-
// subscribe the symbols of positions we held
|
|
130
|
-
superMarginUnifiedRawAccountBalance$
|
|
131
|
-
.pipe(
|
|
132
|
-
//
|
|
133
|
-
(0, rxjs_1.mergeMap)((res) => (0, rxjs_1.from)((res === null || res === void 0 ? void 0 : res.list) || []).pipe((0, rxjs_1.filter)((v) => v.currency !== 'usdt'), (0, rxjs_1.map)((v) => v.currency), (0, rxjs_1.distinct)(), (0, rxjs_1.toArray)(), (0, rxjs_1.map)((v) => new Set(v)))))
|
|
134
|
-
.subscribe((v) => {
|
|
135
|
-
const toUnsubscribe = [...subscriptions].filter((x) => !v.has(x));
|
|
136
|
-
const toSubscribe = [...v].filter((x) => !subscriptions.has(x));
|
|
137
|
-
for (const symbol of toUnsubscribe) {
|
|
138
|
-
api_1.client.spot_ws.output$.next({
|
|
139
|
-
unsub: `market.${symbol}usdt.ticker`,
|
|
140
|
-
});
|
|
141
|
-
subscriptions.delete(symbol);
|
|
142
|
-
}
|
|
143
|
-
for (const symbol of toSubscribe) {
|
|
144
|
-
api_1.client.spot_ws.output$.next({
|
|
145
|
-
sub: `market.${symbol}usdt.ticker`,
|
|
146
|
-
});
|
|
147
|
-
subscriptions.add(symbol);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
(0, data_account_1.provideAccountInfoService)(terminal, SUPER_MARGIN_ACCOUNT_ID, async () => {
|
|
151
|
-
var _a;
|
|
152
|
-
// get account balance
|
|
153
|
-
const accountBalance = await api_1.client.getSpotAccountBalance(superMarginAccountUid);
|
|
154
|
-
const balanceList = ((_a = accountBalance.data) === null || _a === void 0 ? void 0 : _a.list) || [];
|
|
155
|
-
// calculate usdt balance
|
|
156
|
-
const usdtBalance = balanceList
|
|
157
|
-
.filter((v) => v.currency === 'usdt')
|
|
158
|
-
.reduce((acc, cur) => acc + +cur.balance, 0);
|
|
159
|
-
// get positions (non-usdt currencies)
|
|
160
|
-
const positions = [];
|
|
161
|
-
const nonUsdtCurrencies = balanceList
|
|
162
|
-
.filter((v) => v.currency !== 'usdt')
|
|
163
|
-
.reduce((acc, cur) => {
|
|
164
|
-
const existing = acc.find((item) => item.currency === cur.currency);
|
|
165
|
-
if (existing) {
|
|
166
|
-
existing.balance += +cur.balance;
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
acc.push({ currency: cur.currency, balance: +cur.balance });
|
|
170
|
-
}
|
|
171
|
-
return acc;
|
|
172
|
-
}, []);
|
|
173
|
-
// get prices and create positions
|
|
174
|
-
for (const currencyData of nonUsdtCurrencies) {
|
|
175
|
-
if (currencyData.balance > 0) {
|
|
176
|
-
try {
|
|
177
|
-
// get current price from websocket or fallback to REST API
|
|
178
|
-
let price;
|
|
179
|
-
try {
|
|
180
|
-
const tickPrice = await (0, rxjs_1.firstValueFrom)(api_1.client.spot_ws.input$.pipe(
|
|
181
|
-
//
|
|
182
|
-
(0, rxjs_1.first)((v) => { var _a, _b; return ((_a = v.ch) === null || _a === void 0 ? void 0 : _a.includes('ticker')) && ((_b = v.ch) === null || _b === void 0 ? void 0 : _b.includes(currencyData.currency)) && v.tick; }), (0, rxjs_1.map)((v) => v.tick.bid), (0, rxjs_1.timeout)(5000), (0, rxjs_1.tap)({
|
|
183
|
-
error: (e) => {
|
|
184
|
-
subscriptions.clear();
|
|
185
|
-
},
|
|
186
|
-
})));
|
|
187
|
-
price = tickPrice;
|
|
188
|
-
}
|
|
189
|
-
catch (_b) {
|
|
190
|
-
// fallback to REST API
|
|
191
|
-
const tickerRes = await api_1.client.getSpotTick({ symbol: `${currencyData.currency}usdt` });
|
|
192
|
-
price = tickerRes.tick.close;
|
|
193
|
-
}
|
|
194
|
-
positions.push({
|
|
195
|
-
position_id: `${currencyData.currency}/usdt/spot`,
|
|
196
|
-
product_id: `${currencyData.currency}usdt`,
|
|
197
|
-
direction: 'LONG',
|
|
198
|
-
volume: currencyData.balance,
|
|
199
|
-
free_volume: currencyData.balance,
|
|
200
|
-
position_price: price,
|
|
201
|
-
closable_price: price,
|
|
202
|
-
floating_profit: 0,
|
|
203
|
-
valuation: currencyData.balance * price,
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
catch (error) {
|
|
207
|
-
console.warn((0, utils_1.formatTime)(Date.now()), `Failed to get price for ${currencyData.currency}:`, error);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
// calculate equity
|
|
212
|
-
const equity = positions.reduce((acc, cur) => acc + cur.closable_price * cur.volume, 0) + usdtBalance;
|
|
213
|
-
return {
|
|
214
|
-
money: {
|
|
215
|
-
currency: 'USDT',
|
|
216
|
-
equity,
|
|
217
|
-
free: equity,
|
|
218
|
-
},
|
|
219
|
-
positions,
|
|
220
|
-
};
|
|
221
|
-
}, { auto_refresh_interval: 1000 });
|
|
222
|
-
(0, data_account_1.provideAccountInfoService)(terminal, SPOT_ACCOUNT_ID, async () => {
|
|
223
|
-
var _a, _b;
|
|
224
|
-
const spotBalance = await api_1.client.getSpotAccountBalance(spotAccountUid);
|
|
225
|
-
const equity = +((_b = (_a = spotBalance.data.list.find((v) => v.currency === 'usdt')) === null || _a === void 0 ? void 0 : _a.balance) !== null && _b !== void 0 ? _b : 0);
|
|
226
|
-
const free = equity;
|
|
227
|
-
return {
|
|
228
|
-
money: {
|
|
229
|
-
currency: 'USDT',
|
|
230
|
-
equity,
|
|
231
|
-
free,
|
|
232
|
-
},
|
|
233
|
-
positions: [],
|
|
234
|
-
};
|
|
235
|
-
}, { auto_refresh_interval: 1000 });
|
|
38
|
+
// 设置账户信息服务
|
|
39
|
+
(0, account_info_1.provideSwapAccountInfoService)(terminal, SWAP_ACCOUNT_ID, credential);
|
|
40
|
+
(0, account_info_1.provideSpotAccountInfoService)(terminal, SPOT_ACCOUNT_ID, credential, spotAccountUid);
|
|
41
|
+
// 设置账户市场关联
|
|
236
42
|
(0, data_account_1.addAccountMarket)(terminal, { account_id: SPOT_ACCOUNT_ID, market_id: 'HUOBI/SPOT' });
|
|
237
43
|
(0, data_account_1.addAccountMarket)(terminal, { account_id: SUPER_MARGIN_ACCOUNT_ID, market_id: 'HUOBI/SUPER-MARGIN' });
|
|
238
44
|
(0, data_account_1.addAccountMarket)(terminal, { account_id: SWAP_ACCOUNT_ID, market_id: 'HUOBI/SWAP' });
|
|
239
|
-
//
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
},
|
|
247
|
-
}, (msg) => {
|
|
248
|
-
const { account_id: req_account_id } = msg.req;
|
|
249
|
-
console.info((0, utils_1.formatTime)(Date.now()), `SubmitOrder for ${account_id}`, JSON.stringify(msg));
|
|
250
|
-
if (req_account_id === SWAP_ACCOUNT_ID) {
|
|
251
|
-
return (0, rxjs_1.defer)(() => api_1.client.getSwapCrossPositionInfo()).pipe((0, rxjs_1.mergeMap)((res) => res.data), (0, rxjs_1.map)((v) => [v.contract_code, v.lever_rate]), (0, rxjs_1.toArray)(), (0, rxjs_1.map)((v) => Object.fromEntries(v)), (0, rxjs_1.mergeMap)((mapContractCodeToRate) => {
|
|
252
|
-
var _a;
|
|
253
|
-
const lever_rate = (_a = mapContractCodeToRate[msg.req.product_id]) !== null && _a !== void 0 ? _a : 20;
|
|
254
|
-
const params = {
|
|
255
|
-
contract_code: msg.req.product_id,
|
|
256
|
-
contract_type: 'swap',
|
|
257
|
-
price: msg.req.price,
|
|
258
|
-
volume: msg.req.volume,
|
|
259
|
-
offset: msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'OPEN_SHORT'
|
|
260
|
-
? 'open'
|
|
261
|
-
: 'close',
|
|
262
|
-
direction: msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'CLOSE_SHORT'
|
|
263
|
-
? 'buy'
|
|
264
|
-
: 'sell',
|
|
265
|
-
// dynamically adjust the leverage
|
|
266
|
-
lever_rate,
|
|
267
|
-
order_price_type: msg.req.order_type === 'MARKET' ? 'market' : 'limit',
|
|
268
|
-
};
|
|
269
|
-
return api_1.client.postSwapOrder(params).then((v) => {
|
|
270
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', JSON.stringify(v), JSON.stringify(params));
|
|
271
|
-
return v;
|
|
272
|
-
});
|
|
273
|
-
}), (0, rxjs_1.map)((v) => {
|
|
274
|
-
if (v.status !== 'ok') {
|
|
275
|
-
return { res: { code: 500, message: v.status } };
|
|
276
|
-
}
|
|
277
|
-
return { res: { code: 0, message: 'OK' } };
|
|
278
|
-
}), (0, rxjs_1.catchError)((e) => {
|
|
279
|
-
console.error((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', e);
|
|
280
|
-
return (0, rxjs_1.of)({ res: { code: 500, message: `${e}` } });
|
|
281
|
-
}));
|
|
282
|
-
}
|
|
283
|
-
// for super-margin orders, we need to denote the amount of usdt to borrow, therefore we need to:
|
|
284
|
-
// 1. get the loanable amount
|
|
285
|
-
// 2. get the current balance
|
|
286
|
-
// 3. get the current price
|
|
287
|
-
// 4. combine the information to submit the order
|
|
288
|
-
return (0, rxjs_1.defer)(() => api_1.client.getCrossMarginLoanInfo()).pipe(
|
|
289
|
-
//
|
|
290
|
-
(0, rxjs_1.mergeMap)((res) => res.data), (0, rxjs_1.first)((v) => v.currency === 'usdt'), (0, rxjs_1.map)((v) => +v['loanable-amt']), (0, rxjs_1.combineLatestWith)(superMarginUnifiedRawAccountBalance$.pipe((0, rxjs_1.first)(), (0, rxjs_1.mergeMap)((res) => (0, rxjs_1.from)(res.list).pipe(
|
|
291
|
-
// we only need the amount of usdt that can be used to trade
|
|
292
|
-
(0, rxjs_1.filter)((v) => v.currency === 'usdt' && v.type === 'trade'), (0, rxjs_1.reduce)((acc, cur) => acc + +cur.balance, 0))))), (0, rxjs_1.combineLatestWith)(product_1.spotProductService.mapProductIdToProduct$.pipe((0, rxjs_1.first)())), (0, rxjs_1.mergeMap)(async ([[loanable, balance], mapProductIdToProduct]) => {
|
|
293
|
-
const priceRes = await api_1.client.getSpotTick({ symbol: msg.req.product_id });
|
|
294
|
-
const theProduct = mapProductIdToProduct.get(msg.req.product_id);
|
|
295
|
-
const price = priceRes.tick.close;
|
|
296
|
-
const borrow_amount = msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'CLOSE_SHORT'
|
|
297
|
-
? Math.max(Math.min(loanable, msg.req.volume * price - balance), 0)
|
|
298
|
-
: undefined;
|
|
299
|
-
const params = {
|
|
300
|
-
symbol: msg.req.product_id,
|
|
301
|
-
'account-id': '' + superMarginAccountUid,
|
|
302
|
-
// amount: msg.req.type === OrderType.MARKET ? 0 : '' + msg.req.volume,
|
|
303
|
-
// 'market-amount': msg.req.type === OrderType.MARKET ? '' + msg.req.volume : undefined,
|
|
304
|
-
amount: '' +
|
|
305
|
-
(msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'CLOSE_SHORT'
|
|
306
|
-
? (0, utils_1.roundToStep)(msg.req.volume * price, theProduct === null || theProduct === void 0 ? void 0 : theProduct.volume_step)
|
|
307
|
-
: msg.req.volume),
|
|
308
|
-
'borrow-amount': '' + borrow_amount,
|
|
309
|
-
type: `${msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'CLOSE_SHORT'
|
|
310
|
-
? 'buy'
|
|
311
|
-
: 'sell'}-${'LIMIT' === msg.req.order_type ? 'limit' : 'market'}`,
|
|
312
|
-
'trade-purpose': msg.req.order_direction === 'OPEN_LONG' || msg.req.order_direction === 'CLOSE_SHORT'
|
|
313
|
-
? '1' // auto borrow
|
|
314
|
-
: '2',
|
|
315
|
-
price: msg.req.order_type === 'MARKET' ? undefined : '' + msg.req.price,
|
|
316
|
-
source: 'super-margin-api',
|
|
317
|
-
};
|
|
318
|
-
return api_1.client.postSpotOrder(params).then((v) => {
|
|
319
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', JSON.stringify(v), JSON.stringify(params));
|
|
320
|
-
return v;
|
|
321
|
-
});
|
|
322
|
-
}), (0, rxjs_1.map)((v) => {
|
|
323
|
-
if (v.success === false) {
|
|
324
|
-
return { res: { code: v.code, message: v.message } };
|
|
325
|
-
}
|
|
326
|
-
return { res: { code: 0, message: 'OK' } };
|
|
327
|
-
}), (0, rxjs_1.catchError)((e) => {
|
|
328
|
-
console.error((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', e);
|
|
329
|
-
return (0, rxjs_1.of)({ res: { code: 500, message: `${e}` } });
|
|
330
|
-
}));
|
|
331
|
-
});
|
|
332
|
-
// Update Spot TRC20 Addresses (Only Main Account)
|
|
333
|
-
if (isMainAccount) {
|
|
334
|
-
const res = await api_1.client.getSpotAccountDepositAddresses({ currency: 'usdt' });
|
|
335
|
-
const addresses = res.data.filter((v) => v.chain === 'trc20usdt').map((v) => v.address);
|
|
336
|
-
for (const address of addresses) {
|
|
337
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
338
|
-
terminal,
|
|
339
|
-
account_id: SPOT_ACCOUNT_ID,
|
|
340
|
-
currency: 'USDT',
|
|
341
|
-
address: address,
|
|
342
|
-
network_id: 'TRC20',
|
|
343
|
-
onApply: {
|
|
344
|
-
INIT: async (order) => {
|
|
345
|
-
var _a, _b;
|
|
346
|
-
const res0 = await api_1.client.getV2ReferenceCurrencies({ currency: 'usdt' });
|
|
347
|
-
const fee = (_b = (_a = res0.data
|
|
348
|
-
.find((v) => v.currency === 'usdt')) === null || _a === void 0 ? void 0 : _a.chains.find((v) => v.chain === 'trc20usdt')) === null || _b === void 0 ? void 0 : _b.transactFeeWithdraw;
|
|
349
|
-
if (!fee) {
|
|
350
|
-
return { state: 'ERROR', message: 'MISSING FEE' };
|
|
351
|
-
}
|
|
352
|
-
const res = await api_1.client.postWithdraw({
|
|
353
|
-
address: order.current_rx_address,
|
|
354
|
-
amount: '' + (order.expected_amount - +fee),
|
|
355
|
-
currency: 'usdt',
|
|
356
|
-
fee: fee,
|
|
357
|
-
chain: 'trc20usdt',
|
|
358
|
-
});
|
|
359
|
-
if (res.status != 'ok') {
|
|
360
|
-
return { state: 'INIT', message: `${res.status}` };
|
|
361
|
-
}
|
|
362
|
-
return { state: 'PENDING', context: `${res.data}` };
|
|
363
|
-
},
|
|
364
|
-
PENDING: async (order) => {
|
|
365
|
-
var _a;
|
|
366
|
-
if (!order.current_tx_context) {
|
|
367
|
-
return { state: 'ERROR', message: 'MISSING CONTEXT' };
|
|
368
|
-
}
|
|
369
|
-
const wdId = +order.current_tx_context;
|
|
370
|
-
const res = await api_1.client.getDepositWithdrawHistory({
|
|
371
|
-
currency: 'usdt',
|
|
372
|
-
type: 'withdraw',
|
|
373
|
-
from: `${wdId}`,
|
|
374
|
-
});
|
|
375
|
-
const txId = (_a = res.data.find((v) => v.id === wdId)) === null || _a === void 0 ? void 0 : _a['tx-hash'];
|
|
376
|
-
if (!txId) {
|
|
377
|
-
return { state: 'PENDING', context: `${wdId}` };
|
|
378
|
-
}
|
|
379
|
-
return {
|
|
380
|
-
state: 'COMPLETE',
|
|
381
|
-
transaction_id: txId,
|
|
382
|
-
};
|
|
383
|
-
},
|
|
384
|
-
},
|
|
385
|
-
onEval: async (order) => {
|
|
386
|
-
const res = await api_1.client.getDepositWithdrawHistory({
|
|
387
|
-
currency: 'usdt',
|
|
388
|
-
type: 'deposit',
|
|
389
|
-
direct: 'next',
|
|
390
|
-
});
|
|
391
|
-
const theItem = res.data.find((v) => v['tx-hash'] === order.current_transaction_id && v.state === 'safe');
|
|
392
|
-
if (!theItem) {
|
|
393
|
-
return { state: 'PENDING' };
|
|
394
|
-
}
|
|
395
|
-
return { received_amount: +theItem.amount, state: 'COMPLETE' };
|
|
396
|
-
},
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
401
|
-
terminal,
|
|
402
|
-
account_id: SPOT_ACCOUNT_ID,
|
|
403
|
-
currency: 'USDT',
|
|
404
|
-
network_id: `Huobi/${huobiUid}/SPOT-SUPER_MARGIN`,
|
|
405
|
-
address: 'SPOT',
|
|
406
|
-
onApply: {
|
|
407
|
-
INIT: async (order) => {
|
|
408
|
-
const transferInResult = await api_1.client.postSuperMarginAccountTransferIn({
|
|
409
|
-
currency: 'usdt',
|
|
410
|
-
amount: '' + (order.current_amount || order.expected_amount),
|
|
411
|
-
});
|
|
412
|
-
if (transferInResult.status !== 'ok') {
|
|
413
|
-
return { state: 'INIT' };
|
|
414
|
-
}
|
|
415
|
-
return { state: 'COMPLETE' };
|
|
416
|
-
},
|
|
417
|
-
},
|
|
418
|
-
onEval: async (order) => {
|
|
419
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
420
|
-
},
|
|
421
|
-
});
|
|
422
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
423
|
-
terminal,
|
|
424
|
-
account_id: SUPER_MARGIN_ACCOUNT_ID,
|
|
425
|
-
currency: 'USDT',
|
|
426
|
-
network_id: `Huobi/${huobiUid}/SPOT-SUPER_MARGIN`,
|
|
427
|
-
address: 'SUPER_MARGIN',
|
|
428
|
-
onApply: {
|
|
429
|
-
INIT: async (order) => {
|
|
430
|
-
const transferOutResult = await api_1.client.postSuperMarginAccountTransferOut({
|
|
431
|
-
currency: 'usdt',
|
|
432
|
-
amount: '' + (order.current_amount || order.expected_amount),
|
|
433
|
-
});
|
|
434
|
-
if (transferOutResult.status !== 'ok') {
|
|
435
|
-
return { state: 'INIT' };
|
|
436
|
-
}
|
|
437
|
-
return { state: 'COMPLETE' };
|
|
438
|
-
},
|
|
439
|
-
},
|
|
440
|
-
onEval: async (order) => {
|
|
441
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
442
|
-
},
|
|
443
|
-
});
|
|
444
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
445
|
-
terminal,
|
|
446
|
-
account_id: SPOT_ACCOUNT_ID,
|
|
447
|
-
currency: 'USDT',
|
|
448
|
-
network_id: `Huobi/${huobiUid}/SPOT-SWAP`,
|
|
449
|
-
address: 'SPOT',
|
|
450
|
-
onApply: {
|
|
451
|
-
INIT: async (order) => {
|
|
452
|
-
const transferResult = await api_1.client.postSpotAccountTransfer({
|
|
453
|
-
from: 'spot',
|
|
454
|
-
to: 'linear-swap',
|
|
455
|
-
currency: 'usdt',
|
|
456
|
-
amount: order.current_amount || order.expected_amount,
|
|
457
|
-
'margin-account': 'USDT',
|
|
458
|
-
});
|
|
459
|
-
if (!transferResult.success) {
|
|
460
|
-
return { state: 'INIT' };
|
|
461
|
-
}
|
|
462
|
-
return { state: 'COMPLETE' };
|
|
463
|
-
},
|
|
464
|
-
},
|
|
465
|
-
onEval: async (order) => {
|
|
466
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
467
|
-
},
|
|
468
|
-
});
|
|
469
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
470
|
-
terminal,
|
|
471
|
-
account_id: SWAP_ACCOUNT_ID,
|
|
472
|
-
currency: 'USDT',
|
|
473
|
-
network_id: `Huobi/${huobiUid}/SPOT-SWAP`,
|
|
474
|
-
address: 'SWAP',
|
|
475
|
-
onApply: {
|
|
476
|
-
INIT: async (order) => {
|
|
477
|
-
const transferResult = await api_1.client.postSpotAccountTransfer({
|
|
478
|
-
from: 'linear-swap',
|
|
479
|
-
to: 'spot',
|
|
480
|
-
currency: 'usdt',
|
|
481
|
-
amount: order.current_amount || order.expected_amount,
|
|
482
|
-
'margin-account': 'USDT',
|
|
483
|
-
});
|
|
484
|
-
if (!transferResult.success) {
|
|
485
|
-
return { state: 'INIT' };
|
|
486
|
-
}
|
|
487
|
-
return { state: 'COMPLETE' };
|
|
488
|
-
},
|
|
489
|
-
},
|
|
490
|
-
onEval: async (order) => {
|
|
491
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
492
|
-
},
|
|
493
|
-
});
|
|
494
|
-
if (isMainAccount) {
|
|
495
|
-
for (const subAccount of subAccounts) {
|
|
496
|
-
const SPOT_SUB_ACCOUNT_ID = `huobi/${subAccount.uid}/spot/usdt`;
|
|
497
|
-
const SUB_ACCOUNT_NETWORK_ID = `Huobi/${huobiUid}/SubAccount/${subAccount.uid}`;
|
|
498
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
499
|
-
terminal,
|
|
500
|
-
account_id: SPOT_ACCOUNT_ID,
|
|
501
|
-
currency: 'USDT',
|
|
502
|
-
network_id: SUB_ACCOUNT_NETWORK_ID,
|
|
503
|
-
address: '#main',
|
|
504
|
-
onApply: {
|
|
505
|
-
INIT: async (order) => {
|
|
506
|
-
const transferResult = await api_1.client.postSubUserTransfer({
|
|
507
|
-
'sub-uid': +order.current_rx_address,
|
|
508
|
-
currency: 'usdt',
|
|
509
|
-
amount: order.current_amount || order.expected_amount,
|
|
510
|
-
type: 'master-transfer-out',
|
|
511
|
-
});
|
|
512
|
-
if (transferResult.status !== 'ok') {
|
|
513
|
-
return { state: 'INIT' };
|
|
514
|
-
}
|
|
515
|
-
return { state: 'COMPLETE' };
|
|
516
|
-
},
|
|
517
|
-
},
|
|
518
|
-
onEval: async (order) => {
|
|
519
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
520
|
-
},
|
|
521
|
-
});
|
|
522
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
523
|
-
terminal,
|
|
524
|
-
account_id: SPOT_SUB_ACCOUNT_ID,
|
|
525
|
-
currency: 'USDT',
|
|
526
|
-
network_id: SUB_ACCOUNT_NETWORK_ID,
|
|
527
|
-
address: `${subAccount.uid}`,
|
|
528
|
-
onApply: {
|
|
529
|
-
INIT: async (order) => {
|
|
530
|
-
const transferResult = await api_1.client.postSubUserTransfer({
|
|
531
|
-
'sub-uid': +order.current_tx_address,
|
|
532
|
-
currency: 'usdt',
|
|
533
|
-
amount: order.current_amount || order.expected_amount,
|
|
534
|
-
type: 'master-transfer-in',
|
|
535
|
-
});
|
|
536
|
-
if (transferResult.status !== 'ok') {
|
|
537
|
-
return { state: 'INIT' };
|
|
538
|
-
}
|
|
539
|
-
return { state: 'COMPLETE' };
|
|
540
|
-
},
|
|
541
|
-
},
|
|
542
|
-
onEval: async (order) => {
|
|
543
|
-
return { received_amount: order.current_amount || order.expected_amount, state: 'COMPLETE' };
|
|
544
|
-
},
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
}
|
|
45
|
+
// 设置订单提交服务
|
|
46
|
+
(0, order_actions_1.provideOrderSubmitService)(terminal, SWAP_ACCOUNT_ID, SUPER_MARGIN_ACCOUNT_ID, credential);
|
|
47
|
+
// 设置转账功能
|
|
48
|
+
await (0, transfer_1.setupTrc20WithdrawalAddresses)(terminal, SPOT_ACCOUNT_ID, credential, isMainAccount);
|
|
49
|
+
(0, transfer_1.setupSpotSuperMarginTransfer)(terminal, SPOT_ACCOUNT_ID, SUPER_MARGIN_ACCOUNT_ID, credential, huobiUid);
|
|
50
|
+
(0, transfer_1.setupSpotSwapTransfer)(terminal, SPOT_ACCOUNT_ID, SWAP_ACCOUNT_ID, credential, huobiUid);
|
|
51
|
+
(0, transfer_1.setupSubAccountTransfers)(terminal, SPOT_ACCOUNT_ID, credential, huobiUid, subAccounts, isMainAccount);
|
|
548
52
|
})();
|
|
549
53
|
//# sourceMappingURL=index.js.map
|