/
public.ts
217 lines (193 loc) · 5.65 KB
/
public.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import http from 'http';
import https from 'https';
import Axios, { AxiosInstance, AxiosResponse } from 'axios';
import * as types from '../../types';
import * as constants from '../../constants';
import { isNode } from '../../utils';
/**
* Public REST API client options
*
* @typedef {Object} RestPublicClientOptions
* @property {boolean} sandbox - Must be set to true
* @property {string} [apiKey] - Increases rate limits if provided
*/
export interface RestPublicClientOptions {
sandbox?: boolean;
baseURL?: string;
apiKey?: string;
}
/**
* Public REST API client
*
* @example
* import { v1 as uuidv1 } from 'uuid';
* import { RestPublicClient } from '@idexio/idex-sdk';
*
* const publicClient = new RestPublicClient({
* sandbox: true,
* // Optionally provide an API key to increase rate limits
* apiKey: '1f7c4f52-4af7-4e1b-aa94-94fac8d931aa',
* });
* console.log(await publicClient.getTickers('IDEX-ETH'));
*
* @param {RestPublicClientOptions} options
*/
export class RestPublicClient {
public baseURL: string;
private axios: AxiosInstance;
public constructor(options: RestPublicClientOptions) {
const baseURL =
options.baseURL ??
(options.sandbox
? constants.SANDBOX_REST_API_BASE_URL
: constants.LIVE_REST_API_BASE_URL);
this.baseURL = baseURL;
const headers = options.apiKey
? { [constants.REST_API_KEY_HEADER]: options.apiKey }
: {};
this.axios = isNode
? Axios.create({
headers,
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
})
: Axios.create({
headers,
});
}
// Public Data Endpoints
/**
* Test connectivity to the REST API
*
* @see https://docs.idex.io/#get-ping
* @returns {{}}
*/
public async ping(): Promise<{ [key: string]: never }> {
return this.get('/ping');
}
/**
* Returns the current server time
*
* @see https://docs.idex.io/#get-time
*
* @returns {Promise<number>} Current server time as milliseconds since UNIX epoch
*/
public async getServerTime(): Promise<number> {
const { serverTime } = await this.get('/time');
return serverTime;
}
/**
* Returns basic information about the exchange.
*
* @see https://docs.idex.io/#get-exchange
*
* @returns {Promise<RestResponseExchangeInfo>}
*/
public async getExchangeInfo(): Promise<types.RestResponseExchangeInfo> {
return this.get('/exchange');
}
/**
* Returns information about assets supported by the exchange
*
* @see https://docs.idex.io/#get-assets
*
* @returns {Promise<RestResponseAsset[]>}
*/
public async getAssets(): Promise<types.RestResponseAsset[]> {
return this.get('/assets');
}
/**
* Returns information about the currently listed markets
*
* @see https://docs.idex.io/#get-markets
*
* @param {RestRequestFindMarkets} findMarkets
* @returns {Promise<RestResponseMarket[]>}
*/
public async getMarkets(
findMarkets: types.RestRequestFindMarkets,
): Promise<types.RestResponseMarket[]> {
return this.get('/markets', findMarkets);
}
// Market Data Endpoints
/**
* Returns market statistics for the trailing 24-hour period
*
* @see https://docs.idex.io/#get-tickers
*
* @param {string} [market] - Base-quote pair e.g. 'IDEX-ETH', if provided limits ticker data to a single market
* @returns {Promise<RestResponseTicker[]>}
*/
public async getTickers(
market?: string,
): Promise<types.RestResponseTicker[]> {
return this.get('/tickers', market ? { market } : undefined);
}
/**
* Returns candle (OHLCV) data for a market
*
* @see https://docs.idex.io/#get-candles
*
* @param {RestRequestFindCandles} findCandles
* @returns {Promise<RestResponseCandle[]>}
*/
public async getCandles(
findCandles: types.RestRequestFindCandles,
): Promise<types.RestResponseCandle[]> {
return this.get('/candles', findCandles);
}
/**
* Returns public trade data for a market
*
* @see https://docs.idex.io/#get-trades
*
* @param {RestRequestFindTrades} findTrades
* @returns {Promise<RestResponseTrade[]>}
*/
public async getTrades(
findTrades: types.RestRequestFindTrades,
): Promise<types.RestResponseTrade[]> {
return this.get('/trades', findTrades);
}
/**
* Get current top bid/ask price levels of order book for a market
*
* @see https://docs.idex.io/#get-order-books
*
* @param {string} market - Base-quote pair e.g. 'IDEX-ETH'
* @returns {Promise<RestResponseOrderBookLevel1>}
*/
public async getOrderBookLevel1(
market: string,
): Promise<types.RestResponseOrderBookLevel1> {
return this.get('/orderbook', { level: 1, market });
}
/**
* Get current order book price levels for a market
*
* @see https://docs.idex.io/#get-order-books
*
* @param {string} market - Base-quote pair e.g. 'IDEX-ETH'
* @param {number} [limit=50] - Number of bids and asks to return. Default is 50, 0 returns the entire book
* @returns {Promise<RestResponseOrderBookLevel2>}
*/
public async getOrderBookLevel2(
market: string,
limit = 50,
): Promise<types.RestResponseOrderBookLevel2> {
return this.get('/orderbook', { level: 2, market, limit });
}
// Internal methods exposed for advanced usage
protected async get(
endpoint: string,
RestRequestParams: Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any
): Promise<AxiosResponse['data']> {
return (
await this.axios({
method: 'GET',
url: `${this.baseURL}${endpoint}`,
params: RestRequestParams,
})
).data;
}
}