-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
ftxus.ts
78 lines (64 loc) · 2.15 KB
/
ftxus.ts
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
import { RestClient } from "../src/rest-client";
import { RestClientOptions, WSClientConfigurableOptions } from "../src/util/requestUtils";
import { WebsocketClient } from "../src/websocket-client";
/*
FTX.us uses a different API domain for both REST and Websockets. Headers are also slightly different.
Set the domain in all client options to 'ftxus' to connect to ftx.us. Examples for REST and Websockets are below.
Note: some API calls may be unavailable due to limitations in the FTX.us APIs.
*/
(async () => {
// Optional, but required for private endpoints
const key = 'apiKeyHere';
const secret = 'apiSecretHere';
const restClientOptions: RestClientOptions = {
domain: 'ftxus'
};
const wsClientOptions: WSClientConfigurableOptions = {
key: 'apikeyhere',
secret: 'apisecrethere',
// subAccountName: 'sub1',
domain: 'ftxus',
restOptions: restClientOptions
};
// Prepare websocket connection
const ws = new WebsocketClient(wsClientOptions);
ws.on('response', msg => console.log('response: ', msg));
ws.on('update', msg => console.log('update: ', msg));
ws.on('error', msg => console.log('err: ', msg));
// Prepare rest client and trigger API calls as desired
const client = new RestClient(key, secret, restClientOptions);
// Try some public API calls
try {
console.log('getBalances: ', await client.getBalances());
console.log('getSubaccountBalances: ', await client.getSubaccountBalances('sub1'));
console.log('getMarkets: ', await client.getMarket('ABNB-0326'));
} catch (e) {
console.error('public get method failed: ', e);
}
// Try some authenticated API calls
const market = 'BTC/USD';
try {
console.log('buysome: ', await client.placeOrder({
market,
side: 'buy',
type: 'market',
size: 0.001,
price: null
}));
} catch (e) {
console.error('buy failed: ', e);
}
try {
console.log('sellsome: ', await client.placeOrder({
market,
side: 'sell',
type: 'market',
size: 0.001,
price: null
}));
} catch (e) {
console.error('sell failed: ', e);
}
// Nothing left - close the process
process.exit();
})();