@yuants/vendor-okx 0.23.24 → 0.23.25
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.js +9 -8
- package/dist/account.js.map +1 -1
- package/dist/api.js +444 -402
- package/dist/api.js.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/loan-account.js +3 -2
- package/dist/loan-account.js.map +1 -1
- package/dist/market-order.js +1 -1
- package/dist/market-order.js.map +1 -1
- package/dist/order-actions.js +211 -0
- package/dist/order-actions.js.map +1 -0
- package/dist/order.js +3 -3
- package/dist/order.js.map +1 -1
- package/dist/public-api.js.map +1 -1
- package/dist/quote.js.map +1 -1
- package/dist/services.js +3 -2
- package/dist/services.js.map +1 -1
- package/dist/strategy-account.js +5 -4
- package/dist/strategy-account.js.map +1 -1
- package/dist/trade.js +6 -5
- package/dist/trade.js.map +1 -1
- package/dist/transfer.js +279 -0
- package/dist/transfer.js.map +1 -0
- package/dist/websocket.js.map +1 -1
- package/lib/account.d.ts +2 -2
- package/lib/account.d.ts.map +1 -1
- package/lib/account.js +8 -7
- package/lib/account.js.map +1 -1
- package/lib/api.d.ts +1252 -1264
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +477 -404
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +5 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -4
- package/lib/index.js.map +1 -1
- package/lib/loan-account.js +2 -1
- package/lib/loan-account.js.map +1 -1
- package/lib/market-order.d.ts.map +1 -1
- package/lib/market-order.js +1 -1
- package/lib/market-order.js.map +1 -1
- package/lib/order-actions.d.ts +2 -0
- package/lib/order-actions.d.ts.map +1 -0
- package/lib/order-actions.js +213 -0
- package/lib/order-actions.js.map +1 -0
- package/lib/order.js +2 -2
- package/lib/order.js.map +1 -1
- package/lib/public-api.js.map +1 -1
- package/lib/quote.d.ts +1 -1
- package/lib/quote.d.ts.map +1 -1
- package/lib/quote.js.map +1 -1
- package/lib/services.js +2 -1
- package/lib/services.js.map +1 -1
- package/lib/strategy-account.d.ts +2 -2
- package/lib/strategy-account.d.ts.map +1 -1
- package/lib/strategy-account.js +4 -3
- package/lib/strategy-account.js.map +1 -1
- package/lib/trade.js +6 -5
- package/lib/trade.js.map +1 -1
- package/lib/transfer.d.ts +2 -0
- package/lib/transfer.d.ts.map +1 -0
- package/lib/transfer.js +281 -0
- package/lib/transfer.js.map +1 -0
- package/lib/websocket.js.map +1 -1
- package/package.json +13 -12
- package/temp/package-deps.json +30 -29
- package/dist/legacy_index.js +0 -563
- package/dist/legacy_index.js.map +0 -1
- package/dist/logger.js +0 -91
- package/dist/logger.js.map +0 -1
- package/lib/legacy_index.d.ts +0 -2
- package/lib/legacy_index.d.ts.map +0 -1
- package/lib/legacy_index.js +0 -565
- package/lib/legacy_index.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/dist/logger.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import cluster from 'cluster';
|
|
2
|
-
/**
|
|
3
|
-
* 集群模式下的日志管理器
|
|
4
|
-
* 在worker进程中将日志发送到主进程,在主进程中直接使用console
|
|
5
|
-
*/
|
|
6
|
-
class ClusterLogger {
|
|
7
|
-
constructor() {
|
|
8
|
-
this.isWorker = !cluster.isPrimary;
|
|
9
|
-
}
|
|
10
|
-
sendToMaster(level, message, ...args) {
|
|
11
|
-
if (this.isWorker && process.send) {
|
|
12
|
-
const formattedMessage = args.length > 0
|
|
13
|
-
? `${message} ${args
|
|
14
|
-
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
|
15
|
-
.join(' ')}`
|
|
16
|
-
: message;
|
|
17
|
-
process.send({
|
|
18
|
-
type: 'log',
|
|
19
|
-
level,
|
|
20
|
-
message: `[PID:${process.pid}] ${formattedMessage}`,
|
|
21
|
-
pid: process.pid,
|
|
22
|
-
label: process.env.WORKER_LABEL || 'unknown',
|
|
23
|
-
timestamp: Date.now(),
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
// 在主进程中直接输出
|
|
28
|
-
const formattedMessage = args.length > 0
|
|
29
|
-
? `${message} ${args
|
|
30
|
-
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
|
31
|
-
.join(' ')}`
|
|
32
|
-
: message;
|
|
33
|
-
const logMessage = `[PID:${process.pid}] ${formattedMessage}`;
|
|
34
|
-
// 控制台输出
|
|
35
|
-
switch (level) {
|
|
36
|
-
case 'error':
|
|
37
|
-
console.error(logMessage);
|
|
38
|
-
break;
|
|
39
|
-
case 'warn':
|
|
40
|
-
console.warn(logMessage);
|
|
41
|
-
break;
|
|
42
|
-
case 'debug':
|
|
43
|
-
console.debug(logMessage);
|
|
44
|
-
break;
|
|
45
|
-
default:
|
|
46
|
-
console.log(logMessage);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
info(message, ...args) {
|
|
51
|
-
this.sendToMaster('info', message, ...args);
|
|
52
|
-
}
|
|
53
|
-
warn(message, ...args) {
|
|
54
|
-
this.sendToMaster('warn', message, ...args);
|
|
55
|
-
}
|
|
56
|
-
error(message, ...args) {
|
|
57
|
-
this.sendToMaster('error', message, ...args);
|
|
58
|
-
}
|
|
59
|
-
debug(message, ...args) {
|
|
60
|
-
this.sendToMaster('debug', message, ...args);
|
|
61
|
-
}
|
|
62
|
-
log(message, ...args) {
|
|
63
|
-
this.info(message, ...args);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
// 默认配置的logger实例
|
|
67
|
-
export const logger = new ClusterLogger();
|
|
68
|
-
// 猛踩油门
|
|
69
|
-
export const overrideConsole = () => {
|
|
70
|
-
if (!cluster.isPrimary) {
|
|
71
|
-
const originalConsole = {
|
|
72
|
-
log: console.log,
|
|
73
|
-
info: console.info,
|
|
74
|
-
warn: console.warn,
|
|
75
|
-
error: console.error,
|
|
76
|
-
};
|
|
77
|
-
console.log = (...args) => logger.info(args.join(' '));
|
|
78
|
-
console.info = (...args) => logger.info(args.join(' '));
|
|
79
|
-
console.warn = (...args) => logger.warn(args.join(' '));
|
|
80
|
-
console.error = (...args) => logger.error(args.join(' '));
|
|
81
|
-
// 提供恢复方法
|
|
82
|
-
return () => {
|
|
83
|
-
console.log = originalConsole.log;
|
|
84
|
-
console.info = originalConsole.info;
|
|
85
|
-
console.warn = originalConsole.warn;
|
|
86
|
-
console.error = originalConsole.error;
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return () => { }; // 主进程中返回空函数
|
|
90
|
-
};
|
|
91
|
-
//# sourceMappingURL=logger.js.map
|
package/dist/logger.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"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","sourcesContent":["import cluster from 'cluster';\nimport { formatTime } from '@yuants/utils';\n\n/**\n * 日志级别\n */\nexport type LogLevel = 'info' | 'warn' | 'error' | 'debug';\n\n/**\n * 集群模式下的日志管理器\n * 在worker进程中将日志发送到主进程,在主进程中直接使用console\n */\nclass ClusterLogger {\n private isWorker = !cluster.isPrimary;\n\n private sendToMaster(level: LogLevel, message: string, ...args: any[]) {\n if (this.isWorker && process.send) {\n const formattedMessage =\n args.length > 0\n ? `${message} ${args\n .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))\n .join(' ')}`\n : message;\n\n process.send({\n type: 'log',\n level,\n message: `[PID:${process.pid}] ${formattedMessage}`,\n pid: process.pid,\n label: process.env.WORKER_LABEL || 'unknown',\n timestamp: Date.now(),\n });\n } else {\n // 在主进程中直接输出\n const formattedMessage =\n args.length > 0\n ? `${message} ${args\n .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))\n .join(' ')}`\n : message;\n\n const logMessage = `[PID:${process.pid}] ${formattedMessage}`;\n\n // 控制台输出\n switch (level) {\n case 'error':\n console.error(logMessage);\n break;\n case 'warn':\n console.warn(logMessage);\n break;\n case 'debug':\n console.debug(logMessage);\n break;\n default:\n console.log(logMessage);\n }\n }\n }\n\n info(message: string, ...args: any[]) {\n this.sendToMaster('info', message, ...args);\n }\n\n warn(message: string, ...args: any[]) {\n this.sendToMaster('warn', message, ...args);\n }\n\n error(message: string, ...args: any[]) {\n this.sendToMaster('error', message, ...args);\n }\n\n debug(message: string, ...args: any[]) {\n this.sendToMaster('debug', message, ...args);\n }\n\n log(message: string, ...args: any[]) {\n this.info(message, ...args);\n }\n}\n\n// 默认配置的logger实例\nexport const logger = new ClusterLogger();\n\n// 猛踩油门\nexport const overrideConsole = () => {\n if (!cluster.isPrimary) {\n const originalConsole = {\n log: console.log,\n info: console.info,\n warn: console.warn,\n error: console.error,\n };\n\n console.log = (...args: any[]) => logger.info(args.join(' '));\n console.info = (...args: any[]) => logger.info(args.join(' '));\n console.warn = (...args: any[]) => logger.warn(args.join(' '));\n console.error = (...args: any[]) => logger.error(args.join(' '));\n\n // 提供恢复方法\n return () => {\n console.log = originalConsole.log;\n console.info = originalConsole.info;\n console.warn = originalConsole.warn;\n console.error = originalConsole.error;\n };\n }\n return () => {}; // 主进程中返回空函数\n};\n"]}
|
package/lib/legacy_index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"legacy_index.d.ts","sourceRoot":"","sources":["../src/legacy_index.ts"],"names":[],"mappings":""}
|
package/lib/legacy_index.js
DELETED
|
@@ -1,565 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const protocol_1 = require("@yuants/protocol");
|
|
4
|
-
const transfer_1 = require("@yuants/transfer");
|
|
5
|
-
const utils_1 = require("@yuants/utils");
|
|
6
|
-
const rxjs_1 = require("rxjs");
|
|
7
|
-
const account_1 = require("./account");
|
|
8
|
-
const api_1 = require("./api");
|
|
9
|
-
const product_1 = require("./product");
|
|
10
|
-
const public_api_1 = require("./public-api");
|
|
11
|
-
const quote_1 = require("./quote");
|
|
12
|
-
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
13
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'Terminal', terminal.terminalInfo.terminal_id);
|
|
14
|
-
const resOfAssetCurrencies = (0, rxjs_1.defer)(() => api_1.client.getAssetCurrencies()).pipe((0, rxjs_1.repeat)({ delay: 3600000 }), (0, rxjs_1.retry)({ delay: 10000 }), (0, rxjs_1.shareReplay)(1));
|
|
15
|
-
resOfAssetCurrencies.subscribe(); // make it hot
|
|
16
|
-
const memoizeMap = (fn) => {
|
|
17
|
-
const cache = {};
|
|
18
|
-
return ((...params) => { var _a; var _b; return ((_a = cache[_b = (0, utils_1.encodePath)(params)]) !== null && _a !== void 0 ? _a : (cache[_b] = fn(...params))); });
|
|
19
|
-
};
|
|
20
|
-
const fundingRate$ = memoizeMap((product_id) => (0, rxjs_1.defer)(() => (0, public_api_1.getFundingRate)({ instId: (0, utils_1.decodePath)(product_id)[1] })).pipe((0, rxjs_1.mergeMap)((x) => x.data), (0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1)));
|
|
21
|
-
const interestRateLoanQuota$ = (0, rxjs_1.defer)(() => (0, public_api_1.getInterestRateLoanQuota)()).pipe((0, rxjs_1.repeat)({ delay: 60000 }), (0, rxjs_1.retry)({ delay: 60000 }), (0, rxjs_1.shareReplay)(1));
|
|
22
|
-
const interestRateByCurrency$ = memoizeMap((currency) => interestRateLoanQuota$.pipe((0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe((0, rxjs_1.mergeMap)((x) => x.basic), (0, rxjs_1.filter)((x) => x.ccy === currency), (0, rxjs_1.map)((x) => +x.rate))), (0, rxjs_1.shareReplay)(1)));
|
|
23
|
-
// provideTicks(terminal, 'OKX', (product_id) => {
|
|
24
|
-
// const [instType, instId] = decodePath(product_id);
|
|
25
|
-
// if (instType === 'SWAP') {
|
|
26
|
-
// return defer(async () => {
|
|
27
|
-
// const products = await firstValueFrom(usdtSwapProducts$);
|
|
28
|
-
// const theProduct = products.find((x) => x.product_id === product_id);
|
|
29
|
-
// if (!theProduct) throw `No Found ProductID ${product_id}`;
|
|
30
|
-
// const theTicker$ = swapMarketTickers$.pipe(
|
|
31
|
-
// map((x) => x[instId]),
|
|
32
|
-
// shareReplay(1),
|
|
33
|
-
// );
|
|
34
|
-
// return [of(theProduct), theTicker$, fundingRate$(product_id), swapOpenInterest$] as const;
|
|
35
|
-
// }).pipe(
|
|
36
|
-
// catchError(() => EMPTY),
|
|
37
|
-
// mergeMap((x) =>
|
|
38
|
-
// combineLatest(x).pipe(
|
|
39
|
-
// map(([theProduct, ticker, fundingRate, swapOpenInterest]): ITick => {
|
|
40
|
-
// return {
|
|
41
|
-
// datasource_id: 'OKX',
|
|
42
|
-
// product_id,
|
|
43
|
-
// updated_at: Date.now(),
|
|
44
|
-
// settlement_scheduled_at: +fundingRate.fundingTime,
|
|
45
|
-
// price: +ticker.last,
|
|
46
|
-
// ask: +ticker.askPx,
|
|
47
|
-
// bid: +ticker.bidPx,
|
|
48
|
-
// volume: +ticker.lastSz,
|
|
49
|
-
// interest_rate_for_long: -+fundingRate.fundingRate,
|
|
50
|
-
// interest_rate_for_short: +fundingRate.fundingRate,
|
|
51
|
-
// open_interest: swapOpenInterest.get(instId),
|
|
52
|
-
// };
|
|
53
|
-
// }),
|
|
54
|
-
// ),
|
|
55
|
-
// ),
|
|
56
|
-
// );
|
|
57
|
-
// }
|
|
58
|
-
// if (instType === 'MARGIN') {
|
|
59
|
-
// return defer(async () => {
|
|
60
|
-
// const products = await firstValueFrom(marginProducts$);
|
|
61
|
-
// const theProduct = products.find((x) => x.product_id === product_id);
|
|
62
|
-
// if (!theProduct) throw `No Found ProductID ${product_id}`;
|
|
63
|
-
// const theTicker$ = spotMarketTickers$.pipe(
|
|
64
|
-
// map((x) => x[instId]),
|
|
65
|
-
// shareReplay(1),
|
|
66
|
-
// );
|
|
67
|
-
// return [
|
|
68
|
-
// of(theProduct),
|
|
69
|
-
// theTicker$,
|
|
70
|
-
// interestRateByCurrency$(theProduct.base_currency!),
|
|
71
|
-
// interestRateByCurrency$(theProduct.quote_currency!),
|
|
72
|
-
// ] as const;
|
|
73
|
-
// }).pipe(
|
|
74
|
-
// catchError(() => EMPTY),
|
|
75
|
-
// mergeMap((x) =>
|
|
76
|
-
// combineLatest(x).pipe(
|
|
77
|
-
// map(
|
|
78
|
-
// ([theProduct, ticker, interestRateForBase, interestRateForQuote]): ITick => ({
|
|
79
|
-
// datasource_id: 'OKX',
|
|
80
|
-
// product_id,
|
|
81
|
-
// updated_at: Date.now(),
|
|
82
|
-
// price: +ticker.last,
|
|
83
|
-
// volume: +ticker.lastSz,
|
|
84
|
-
// // 在下一个整点扣除利息 See 如何计算利息 https://www.okx.com/zh-hans/help/how-to-calculate-borrowing-interest
|
|
85
|
-
// settlement_scheduled_at: new Date().setMinutes(0, 0, 0) + 3600_000,
|
|
86
|
-
// interest_rate_for_long: -interestRateForQuote / 24,
|
|
87
|
-
// interest_rate_for_short: -interestRateForBase / 24,
|
|
88
|
-
// }),
|
|
89
|
-
// ),
|
|
90
|
-
// ),
|
|
91
|
-
// ),
|
|
92
|
-
// );
|
|
93
|
-
// }
|
|
94
|
-
// return EMPTY;
|
|
95
|
-
// });
|
|
96
|
-
(0, rxjs_1.defer)(async () => {
|
|
97
|
-
const account_config = await (0, rxjs_1.firstValueFrom)(account_1.accountConfig$);
|
|
98
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'AccountConfig', JSON.stringify(account_config));
|
|
99
|
-
const { mainUid, uid } = account_config.data[0];
|
|
100
|
-
const isMainAccount = mainUid === uid;
|
|
101
|
-
const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;
|
|
102
|
-
const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;
|
|
103
|
-
const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
|
|
104
|
-
// BLOCK_CHAIN: only available for main account
|
|
105
|
-
if (isMainAccount) {
|
|
106
|
-
const depositAddressRes = await api_1.client.getAssetDepositAddress({ ccy: 'USDT' });
|
|
107
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'DepositAddress', JSON.stringify(depositAddressRes.data));
|
|
108
|
-
const addresses = depositAddressRes.data.filter((v) => v.chain === 'USDT-TRC20' && v.to === '6');
|
|
109
|
-
for (const address of addresses) {
|
|
110
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
111
|
-
terminal,
|
|
112
|
-
account_id: FUNDING_ACCOUNT_ID,
|
|
113
|
-
network_id: 'TRC20',
|
|
114
|
-
currency: 'USDT',
|
|
115
|
-
address: address.addr,
|
|
116
|
-
onApply: {
|
|
117
|
-
INIT: async (order) => {
|
|
118
|
-
var _a, _b;
|
|
119
|
-
if (!order.current_amount ||
|
|
120
|
-
order.current_amount < 3 // 最小提币额度
|
|
121
|
-
) {
|
|
122
|
-
return { state: 'ERROR', message: 'Amount too small' };
|
|
123
|
-
}
|
|
124
|
-
const res = await (0, rxjs_1.firstValueFrom)(resOfAssetCurrencies);
|
|
125
|
-
const theRes = (_a = res.data) === null || _a === void 0 ? void 0 : _a.find((x) => x.ccy === 'USDT' && x.chain === 'USDT-TRC20');
|
|
126
|
-
const _fee = theRes === null || theRes === void 0 ? void 0 : theRes.minFee;
|
|
127
|
-
if (!_fee)
|
|
128
|
-
return { state: 'ERROR', message: 'Currency Info not found, cannot get fee' };
|
|
129
|
-
const fee = +_fee;
|
|
130
|
-
const amt = Math.floor(order.current_amount - fee);
|
|
131
|
-
const transferResult = await api_1.client.postAssetWithdrawal({
|
|
132
|
-
amt: `${amt}`,
|
|
133
|
-
ccy: 'USDT',
|
|
134
|
-
chain: 'USDT-TRC20',
|
|
135
|
-
fee: `${fee}`,
|
|
136
|
-
dest: '4',
|
|
137
|
-
toAddr: order.current_rx_address,
|
|
138
|
-
});
|
|
139
|
-
if (transferResult.code !== '0') {
|
|
140
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
141
|
-
}
|
|
142
|
-
const wdId = (_b = transferResult.data[0]) === null || _b === void 0 ? void 0 : _b.wdId;
|
|
143
|
-
return { state: 'AWAIT_TX_ID', context: wdId };
|
|
144
|
-
},
|
|
145
|
-
AWAIT_TX_ID: async (transferOrder) => {
|
|
146
|
-
var _a, _b;
|
|
147
|
-
const wdId = transferOrder.current_tx_context;
|
|
148
|
-
const withdrawalHistory = await api_1.client.getAssetWithdrawalHistory({ wdId });
|
|
149
|
-
const txId = (_b = (_a = withdrawalHistory.data) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.txId;
|
|
150
|
-
if (!txId) {
|
|
151
|
-
return { state: 'AWAIT_TX_ID', context: wdId };
|
|
152
|
-
}
|
|
153
|
-
return { state: 'COMPLETE', transaction_id: txId };
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
|
-
onEval: async (transferOrder) => {
|
|
157
|
-
const checkResult = await api_1.client.getAssetDepositHistory({
|
|
158
|
-
ccy: 'USDT',
|
|
159
|
-
txId: transferOrder.current_transaction_id,
|
|
160
|
-
type: '4',
|
|
161
|
-
});
|
|
162
|
-
if (checkResult.code !== '0') {
|
|
163
|
-
return {
|
|
164
|
-
state: 'INIT',
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
if (checkResult.data[0].state !== '2') {
|
|
168
|
-
return { state: 'PENDING' };
|
|
169
|
-
}
|
|
170
|
-
const received_amount = +checkResult.data[0].amt;
|
|
171
|
-
return { state: 'COMPLETE', received_amount };
|
|
172
|
-
},
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
// Funding-Trading
|
|
177
|
-
{
|
|
178
|
-
const FUNDING_TRADING_NETWORK_ID = `OKX/${uid}/Funding-Trading`;
|
|
179
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
180
|
-
terminal,
|
|
181
|
-
account_id: FUNDING_ACCOUNT_ID,
|
|
182
|
-
network_id: FUNDING_TRADING_NETWORK_ID,
|
|
183
|
-
currency: 'USDT',
|
|
184
|
-
address: 'funding',
|
|
185
|
-
onApply: {
|
|
186
|
-
INIT: async (order) => {
|
|
187
|
-
const transferResult = await api_1.client.postAssetTransfer({
|
|
188
|
-
type: '0',
|
|
189
|
-
ccy: 'USDT',
|
|
190
|
-
amt: `${order.current_amount}`,
|
|
191
|
-
from: '6',
|
|
192
|
-
to: '18',
|
|
193
|
-
});
|
|
194
|
-
if (transferResult.code !== '0') {
|
|
195
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
196
|
-
}
|
|
197
|
-
const transaction_id = transferResult.data[0].transId;
|
|
198
|
-
return { state: 'COMPLETE', transaction_id };
|
|
199
|
-
},
|
|
200
|
-
},
|
|
201
|
-
onEval: async (transferOrder) => {
|
|
202
|
-
return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
206
|
-
terminal,
|
|
207
|
-
account_id: TRADING_ACCOUNT_ID,
|
|
208
|
-
network_id: FUNDING_TRADING_NETWORK_ID,
|
|
209
|
-
currency: 'USDT',
|
|
210
|
-
address: 'trading',
|
|
211
|
-
onApply: {
|
|
212
|
-
INIT: async (order) => {
|
|
213
|
-
const transferResult = await api_1.client.postAssetTransfer({
|
|
214
|
-
type: '0',
|
|
215
|
-
ccy: order.currency,
|
|
216
|
-
amt: `${order.current_amount}`,
|
|
217
|
-
from: '18',
|
|
218
|
-
to: '6',
|
|
219
|
-
});
|
|
220
|
-
if (transferResult.code !== '0') {
|
|
221
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
222
|
-
}
|
|
223
|
-
const transaction_id = transferResult.data[0].transId;
|
|
224
|
-
return { state: 'COMPLETE', transaction_id };
|
|
225
|
-
},
|
|
226
|
-
},
|
|
227
|
-
onEval: async (transferOrder) => {
|
|
228
|
-
return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
// Funding-Earning
|
|
233
|
-
{
|
|
234
|
-
const FUNDING_EARNING_NETWORK_ID = `OKX/${uid}/Funding-Earning`;
|
|
235
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
236
|
-
terminal,
|
|
237
|
-
account_id: FUNDING_ACCOUNT_ID,
|
|
238
|
-
network_id: FUNDING_EARNING_NETWORK_ID,
|
|
239
|
-
currency: 'USDT',
|
|
240
|
-
address: 'funding',
|
|
241
|
-
onApply: {
|
|
242
|
-
INIT: async (order) => {
|
|
243
|
-
const transferResult = await api_1.client.postFinanceSavingsPurchaseRedempt({
|
|
244
|
-
ccy: 'USDT',
|
|
245
|
-
amt: `${order.current_amount}`,
|
|
246
|
-
side: 'purchase',
|
|
247
|
-
rate: '0.01',
|
|
248
|
-
});
|
|
249
|
-
if (transferResult.code !== '0') {
|
|
250
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
251
|
-
}
|
|
252
|
-
return { state: 'COMPLETE', transaction_id: 'ok' };
|
|
253
|
-
},
|
|
254
|
-
},
|
|
255
|
-
onEval: async (transferOrder) => {
|
|
256
|
-
return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
|
|
257
|
-
},
|
|
258
|
-
});
|
|
259
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
260
|
-
terminal,
|
|
261
|
-
account_id: EARNING_ACCOUNT_ID,
|
|
262
|
-
network_id: FUNDING_EARNING_NETWORK_ID,
|
|
263
|
-
currency: 'USDT',
|
|
264
|
-
address: 'earning',
|
|
265
|
-
onApply: {
|
|
266
|
-
INIT: async (order) => {
|
|
267
|
-
const transferResult = await api_1.client.postFinanceSavingsPurchaseRedempt({
|
|
268
|
-
ccy: 'USDT',
|
|
269
|
-
amt: `${order.current_amount}`,
|
|
270
|
-
side: 'redempt',
|
|
271
|
-
rate: '0.01',
|
|
272
|
-
});
|
|
273
|
-
if (transferResult.code !== '0') {
|
|
274
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
275
|
-
}
|
|
276
|
-
return { state: 'COMPLETE', transaction_id: 'ok' };
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
onEval: async (transferOrder) => {
|
|
280
|
-
return { state: 'COMPLETE', received_amount: transferOrder.current_amount };
|
|
281
|
-
},
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
// SubAccount
|
|
285
|
-
{
|
|
286
|
-
const getSubAccountNetworkId = (subUid) => `OKX/${mainUid}/SubAccount/${subUid}`;
|
|
287
|
-
if (isMainAccount) {
|
|
288
|
-
const subAcctsRes = await api_1.client.getSubAccountList();
|
|
289
|
-
for (const item of subAcctsRes.data || []) {
|
|
290
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
291
|
-
terminal,
|
|
292
|
-
account_id: FUNDING_ACCOUNT_ID,
|
|
293
|
-
network_id: getSubAccountNetworkId(item.uid),
|
|
294
|
-
currency: 'USDT',
|
|
295
|
-
address: 'main',
|
|
296
|
-
onApply: {
|
|
297
|
-
INIT: async (order) => {
|
|
298
|
-
const transferResult = await api_1.client.postAssetTransfer({
|
|
299
|
-
type: '1',
|
|
300
|
-
ccy: 'USDT',
|
|
301
|
-
amt: `${order.current_amount}`,
|
|
302
|
-
from: '6',
|
|
303
|
-
to: '6',
|
|
304
|
-
subAcct: item.subAcct,
|
|
305
|
-
});
|
|
306
|
-
if (transferResult.code !== '0') {
|
|
307
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
308
|
-
}
|
|
309
|
-
const transaction_id = transferResult.data[0].transId;
|
|
310
|
-
return { state: 'COMPLETE', transaction_id };
|
|
311
|
-
},
|
|
312
|
-
},
|
|
313
|
-
onEval: async (order) => {
|
|
314
|
-
// ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’
|
|
315
|
-
// const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });
|
|
316
|
-
// const received_amount = checkResult?.data?.[0]?.amt;
|
|
317
|
-
// if (!received_amount) {
|
|
318
|
-
// return { state: 'INIT', message: checkResult.msg };
|
|
319
|
-
// }
|
|
320
|
-
// return { state: 'COMPLETE', received_amount: +received_amount };
|
|
321
|
-
return { state: 'COMPLETE', received_amount: order.current_amount };
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// SubAccount API
|
|
327
|
-
else {
|
|
328
|
-
(0, transfer_1.addAccountTransferAddress)({
|
|
329
|
-
terminal,
|
|
330
|
-
account_id: FUNDING_ACCOUNT_ID,
|
|
331
|
-
network_id: getSubAccountNetworkId(uid),
|
|
332
|
-
currency: 'USDT',
|
|
333
|
-
address: 'sub',
|
|
334
|
-
onApply: {
|
|
335
|
-
INIT: async (order) => {
|
|
336
|
-
const transferResult = await api_1.client.postAssetTransfer({
|
|
337
|
-
type: '3',
|
|
338
|
-
ccy: 'USDT',
|
|
339
|
-
amt: `${order.current_amount}`,
|
|
340
|
-
from: '6',
|
|
341
|
-
to: '6',
|
|
342
|
-
});
|
|
343
|
-
if (transferResult.code !== '0') {
|
|
344
|
-
return { state: 'INIT', message: transferResult.msg };
|
|
345
|
-
}
|
|
346
|
-
const transaction_id = transferResult.data[0].transId;
|
|
347
|
-
return { state: 'COMPLETE', transaction_id };
|
|
348
|
-
},
|
|
349
|
-
},
|
|
350
|
-
onEval: async (order) => {
|
|
351
|
-
// ISSUE: OKX API Issue: transId is incorrect or transId does not match with ‘ type’
|
|
352
|
-
// const checkResult = await client.getAssetTransferState({ transId: order.current_transaction_id });
|
|
353
|
-
// const received_amount = checkResult?.data?.[0]?.amt;
|
|
354
|
-
// if (!received_amount) {
|
|
355
|
-
// return { state: 'INIT', message: checkResult.msg };
|
|
356
|
-
// }
|
|
357
|
-
// return { state: 'COMPLETE', received_amount: +received_amount };
|
|
358
|
-
return { state: 'COMPLETE', received_amount: order.current_amount };
|
|
359
|
-
},
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}).subscribe();
|
|
364
|
-
(0, rxjs_1.defer)(async () => {
|
|
365
|
-
const tradingAccountId = await (0, rxjs_1.firstValueFrom)(account_1.tradingAccountId$);
|
|
366
|
-
terminal.server.provideService('SubmitOrder', {
|
|
367
|
-
required: ['account_id'],
|
|
368
|
-
properties: {
|
|
369
|
-
account_id: { const: tradingAccountId },
|
|
370
|
-
},
|
|
371
|
-
}, async (msg) => {
|
|
372
|
-
var _a, _b, _c;
|
|
373
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', JSON.stringify(msg));
|
|
374
|
-
const order = msg.req;
|
|
375
|
-
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
376
|
-
const mapOrderDirectionToSide = (direction) => {
|
|
377
|
-
switch (direction) {
|
|
378
|
-
case 'OPEN_LONG':
|
|
379
|
-
case 'CLOSE_SHORT':
|
|
380
|
-
return 'buy';
|
|
381
|
-
case 'OPEN_SHORT':
|
|
382
|
-
case 'CLOSE_LONG':
|
|
383
|
-
return 'sell';
|
|
384
|
-
}
|
|
385
|
-
throw new Error(`Unknown direction: ${direction}`);
|
|
386
|
-
};
|
|
387
|
-
const mapOrderDirectionToPosSide = (direction) => {
|
|
388
|
-
switch (direction) {
|
|
389
|
-
case 'OPEN_LONG':
|
|
390
|
-
case 'CLOSE_LONG':
|
|
391
|
-
return 'long';
|
|
392
|
-
case 'CLOSE_SHORT':
|
|
393
|
-
case 'OPEN_SHORT':
|
|
394
|
-
return 'short';
|
|
395
|
-
}
|
|
396
|
-
throw new Error(`Unknown direction: ${direction}`);
|
|
397
|
-
};
|
|
398
|
-
const mapOrderTypeToOrdType = (order_type) => {
|
|
399
|
-
switch (order_type) {
|
|
400
|
-
case 'LIMIT':
|
|
401
|
-
return 'limit';
|
|
402
|
-
case 'MARKET':
|
|
403
|
-
return 'market';
|
|
404
|
-
case 'MAKER':
|
|
405
|
-
return 'post_only';
|
|
406
|
-
}
|
|
407
|
-
throw new Error(`Unknown order type: ${order_type}`);
|
|
408
|
-
};
|
|
409
|
-
// 交易数量,表示要购买或者出售的数量。
|
|
410
|
-
// 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。
|
|
411
|
-
// 当币币杠杆以市价买入时,指计价货币的数量。
|
|
412
|
-
// 当币币杠杆以市价卖出时,指交易货币的数量。
|
|
413
|
-
// 对于币币市价单,单位由 tgtCcy 决定
|
|
414
|
-
// 当交割、永续、期权买入和卖出时,指合约张数。
|
|
415
|
-
const mapOrderVolumeToSz = async (order) => {
|
|
416
|
-
if (instType === 'SWAP') {
|
|
417
|
-
return order.volume;
|
|
418
|
-
}
|
|
419
|
-
if (instType === 'MARGIN') {
|
|
420
|
-
if (order.order_type === 'LIMIT') {
|
|
421
|
-
return order.volume;
|
|
422
|
-
}
|
|
423
|
-
if (order.order_type === 'MAKER') {
|
|
424
|
-
return order.volume;
|
|
425
|
-
}
|
|
426
|
-
if (order.order_type === 'MARKET') {
|
|
427
|
-
if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {
|
|
428
|
-
return order.volume;
|
|
429
|
-
}
|
|
430
|
-
//
|
|
431
|
-
const price = await (0, rxjs_1.firstValueFrom)(quote_1.spotMarketTickers$.pipe((0, rxjs_1.map)((x) => mapOrderDirectionToPosSide(order.order_direction) === 'long'
|
|
432
|
-
? +x[instId].askPx
|
|
433
|
-
: +x[instId].bidPx)));
|
|
434
|
-
if (!price) {
|
|
435
|
-
throw new Error(`invalid tick: ${price}`);
|
|
436
|
-
}
|
|
437
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'price', price);
|
|
438
|
-
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
439
|
-
if (!theProduct) {
|
|
440
|
-
throw new Error(`Unknown product: ${order.position_id}`);
|
|
441
|
-
}
|
|
442
|
-
return (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step);
|
|
443
|
-
}
|
|
444
|
-
return 0;
|
|
445
|
-
}
|
|
446
|
-
if (instType === 'SPOT') {
|
|
447
|
-
return order.volume;
|
|
448
|
-
}
|
|
449
|
-
throw new Error(`Unknown instType: ${instType}`);
|
|
450
|
-
};
|
|
451
|
-
const params = {
|
|
452
|
-
instId,
|
|
453
|
-
tdMode: instType === 'SPOT' ? 'cash' : 'cross',
|
|
454
|
-
side: mapOrderDirectionToSide(order.order_direction),
|
|
455
|
-
posSide: instType === 'MARGIN' || instType === 'SPOT'
|
|
456
|
-
? 'net'
|
|
457
|
-
: mapOrderDirectionToPosSide(order.order_direction),
|
|
458
|
-
ordType: mapOrderTypeToOrdType(order.order_type),
|
|
459
|
-
sz: (await mapOrderVolumeToSz(order)).toString(),
|
|
460
|
-
tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,
|
|
461
|
-
reduceOnly: instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes((_a = order.order_direction) !== null && _a !== void 0 ? _a : '')
|
|
462
|
-
? 'true'
|
|
463
|
-
: undefined,
|
|
464
|
-
px: order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price.toString() : undefined,
|
|
465
|
-
ccy: instType === 'MARGIN' ? 'USDT' : undefined,
|
|
466
|
-
};
|
|
467
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
|
|
468
|
-
const res = await api_1.client.postTradeOrder(params);
|
|
469
|
-
if (res.code === '0' && ((_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ordId)) {
|
|
470
|
-
return {
|
|
471
|
-
res: {
|
|
472
|
-
code: 0,
|
|
473
|
-
message: 'OK',
|
|
474
|
-
data: {
|
|
475
|
-
order_id: res.data[0].ordId,
|
|
476
|
-
},
|
|
477
|
-
},
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
return { res: { code: +res.code, message: res.msg } };
|
|
481
|
-
});
|
|
482
|
-
terminal.server.provideService('ModifyOrder', {
|
|
483
|
-
required: ['account_id'],
|
|
484
|
-
properties: {
|
|
485
|
-
account_id: { const: tradingAccountId },
|
|
486
|
-
},
|
|
487
|
-
}, async (msg) => {
|
|
488
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', JSON.stringify(msg));
|
|
489
|
-
const order = msg.req;
|
|
490
|
-
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
491
|
-
const params = {
|
|
492
|
-
instId,
|
|
493
|
-
ordId: order.order_id, // 使用现有订单ID
|
|
494
|
-
};
|
|
495
|
-
// 如果需要修改价格
|
|
496
|
-
if (order.price !== undefined) {
|
|
497
|
-
params.newPx = order.price.toString();
|
|
498
|
-
}
|
|
499
|
-
// 如果需要修改数量
|
|
500
|
-
if (order.volume !== undefined) {
|
|
501
|
-
// 处理数量修改,类似于 SubmitOrder 中的逻辑
|
|
502
|
-
if (instType === 'SWAP') {
|
|
503
|
-
params.newSz = order.volume.toString();
|
|
504
|
-
}
|
|
505
|
-
else if (instType === 'SPOT') {
|
|
506
|
-
params.newSz = order.volume.toString();
|
|
507
|
-
}
|
|
508
|
-
else if (instType === 'MARGIN') {
|
|
509
|
-
if (order.order_type === 'LIMIT') {
|
|
510
|
-
params.newSz = order.volume.toString();
|
|
511
|
-
}
|
|
512
|
-
if (order.order_type === 'MAKER') {
|
|
513
|
-
params.newSz = order.volume.toString();
|
|
514
|
-
}
|
|
515
|
-
if (order.order_type === 'MARKET') {
|
|
516
|
-
// 对于市价单,可能需要根据当前价格计算新的数量
|
|
517
|
-
const price = await (0, rxjs_1.firstValueFrom)(quote_1.spotMarketTickers$.pipe((0, rxjs_1.map)((x) => order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'
|
|
518
|
-
? +x[instId].askPx
|
|
519
|
-
: +x[instId].bidPx)));
|
|
520
|
-
if (!price) {
|
|
521
|
-
throw new Error(`invalid tick: ${price}`);
|
|
522
|
-
}
|
|
523
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'price', price);
|
|
524
|
-
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
525
|
-
if (!theProduct) {
|
|
526
|
-
throw new Error(`Unknown product: ${order.position_id}`);
|
|
527
|
-
}
|
|
528
|
-
params.newSz = (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step).toString();
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
throw new Error(`Unknown instType: ${instType}`);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));
|
|
536
|
-
const res = await api_1.client.postTradeAmendOrder(params);
|
|
537
|
-
if (res.code !== '0') {
|
|
538
|
-
return {
|
|
539
|
-
res: {
|
|
540
|
-
code: +res.code,
|
|
541
|
-
message: res.msg,
|
|
542
|
-
},
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
return { res: { code: 0, message: 'OK' } };
|
|
546
|
-
});
|
|
547
|
-
terminal.server.provideService('CancelOrder', {
|
|
548
|
-
required: ['account_id'],
|
|
549
|
-
properties: {
|
|
550
|
-
account_id: { const: tradingAccountId },
|
|
551
|
-
},
|
|
552
|
-
}, (msg) => (0, rxjs_1.defer)(async () => {
|
|
553
|
-
const order = msg.req;
|
|
554
|
-
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
555
|
-
const res = await api_1.client.postTradeCancelOrder({
|
|
556
|
-
instId,
|
|
557
|
-
ordId: order.order_id,
|
|
558
|
-
});
|
|
559
|
-
if (res.code !== '0') {
|
|
560
|
-
return { res: { code: +res.code, message: res.msg } };
|
|
561
|
-
}
|
|
562
|
-
return { res: { code: 0, message: 'OK' } };
|
|
563
|
-
}));
|
|
564
|
-
}).subscribe();
|
|
565
|
-
//# sourceMappingURL=legacy_index.js.map
|