-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwsAPI.RAW.ts
More file actions
85 lines (74 loc) · 2.14 KB
/
Copy pathwsAPI.RAW.ts
File metadata and controls
85 lines (74 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
DefaultLogger,
LogParams,
WebsocketClient,
WS_KEY_MAP,
} from '../../../src/index.js';
// Install from npm in your own project:
// import { WebsocketClient, WS_KEY_MAP } from '@siebly/htx-api';
const customLogger: DefaultLogger = {
trace: (..._params: LogParams): void => {
// console.log('trace', ...params);
},
info: (...params: LogParams): void => {
console.log('info', ...params);
},
error: (...params: LogParams): void => {
console.error('error', ...params);
},
};
async function start() {
const account = {
key: process.env.API_SPOT_KEY || 'keyHere',
secret: process.env.API_SPOT_SECRET || 'secretHere',
};
/**
* WebsocketClient can send low-level WS API request/response commands with
* sendWSAPIRequest(). Use WebsocketAPIClient if you prefer typed wrapper
* methods for the same trading commands.
*/
const client = new WebsocketClient(
{
apiKey: account.key,
apiSecret: account.secret,
},
customLogger,
);
client
.on('open', (data) => console.log('open:', data.wsKey))
.on('authenticated', (data) => console.log('authenticated:', data.wsKey))
.on('response', (data) => console.info('response:', JSON.stringify(data)))
.on('exception', (data) => console.error('exception:', data));
/**
* Optional: connect/authenticate before the first request.
*/
// await client.connectWSAPI(WS_KEY_MAP.spotTrade);
const runLiveTradingExamples = false;
if (!runLiveTradingExamples) {
console.log('Set runLiveTradingExamples=true after reviewing the params.');
return;
}
const submitOrderResponse = await client.sendWSAPIRequest(
WS_KEY_MAP.spotTrade,
'create-order',
{
'account-id': 123456,
symbol: 'btcusdt',
type: 'buy-limit',
amount: '0.001',
price: '20000',
source: 'spot-api',
'client-order-id': client.generateNewOrderID(),
},
);
console.log('create-order:', submitOrderResponse);
const cancelResponse = await client.sendWSAPIRequest(
WS_KEY_MAP.spotTrade,
'cancel',
{
'order-ids': ['123456789'],
},
);
console.log('cancel:', cancelResponse);
}
start();