-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
public.ts
271 lines (241 loc) · 6.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { FetchClient, UnsuccessfulFetch } from "rpc-bluebird";
import Bluebird from "bluebird";
export const ApiLimit = 500;
export const DefaultSymbol = "btcusd";
export const ApiUri = "https://api.gemini.com";
export const SandboxApiUri = "https://api.sandbox.gemini.com";
export const Headers = {
"User-Agent": "gemini-node-api",
"Content-Type": "text/plain",
Accept: "application/json",
"Content-Length": "0",
"Cache-Control": "no-cache",
};
export interface SymbolFilter {
symbol?: string;
}
export interface TickerFilter extends SymbolFilter {
v?: "v1" | "v2";
}
export interface CandlesFilter extends SymbolFilter {
time_frame?: "1m" | "5m" | "15m" | "30m" | "1hr" | "6hr" | "1day";
}
export interface BookFilter extends SymbolFilter {
limit_bids?: number;
limit_asks?: number;
}
export interface TradeHistoryFilter extends SymbolFilter {
timestamp?: number;
limit_trades?: number;
include_breaks?: boolean;
}
export interface AuctionHistoryFilter extends SymbolFilter {
timestamp?: number;
limit_auction_results?: number;
include_indicative?: boolean;
}
export interface TickerV1 {
bid: string;
ask: string;
last: string;
volume: { [key: string]: string | number };
}
export interface TickerV2 {
symbol: string;
open: string;
high: string;
low: string;
close: string;
changes: string[];
bid: string;
ask: string;
}
export type Ticker = TickerV1 | TickerV2;
export type Candle = [number, number, number, number, number, number];
export interface BookEntry {
price: string;
amount: string;
timestamp: string;
}
export interface OrderBook {
bids: BookEntry[];
asks: BookEntry[];
}
export interface Trade {
timestamp: number;
timestampms: number;
tid: number;
price: string;
amount: string;
exchange: "gemini";
type: "buy" | "sell" | "auction" | "block";
broken?: boolean;
}
export interface AuctionInfo {
closed_until_ms?: number;
last_auction_eid?: number;
last_auction_price?: string;
last_auction_quantity?: string;
last_highest_bid_price?: string;
last_lowest_ask_price?: string;
last_collar_price?: string;
most_recent_indicative_price?: string;
most_recent_indicative_quantity?: string;
most_recent_highest_bid_price?: string;
most_recent_lowest_ask_price?: string;
most_recent_collar_price?: string;
next_update_ms?: number;
next_auction_ms?: number;
}
export interface AuctionHistory {
timestamp: number;
timestampms: number;
auction_id: number;
eid: number;
event_type: "indicative" | "auction";
auction_result: "success" | "failure";
auction_price?: string;
auction_quantity?: string;
highest_bid_price?: string;
lowest_ask_price?: string;
collar_price?: string;
unmatched_collar_quantity?: string;
}
export interface PriceFeedItem {
pair: string;
price: string;
percentChange24h: string;
}
export interface PublicClientOptions {
symbol?: string;
sandbox?: boolean;
apiUri?: string;
}
export class PublicClient extends FetchClient<unknown> {
public readonly symbol: string;
public readonly apiUri: string;
public constructor({
symbol = DefaultSymbol,
sandbox = false,
apiUri = sandbox ? SandboxApiUri : ApiUri,
}: PublicClientOptions = {}) {
super({ headers: { ...Headers } }, { transform: "json", baseUrl: apiUri });
this.apiUri = apiUri;
this.symbol = symbol;
}
public get(path: string): Bluebird<unknown> {
return new Bluebird<unknown>((resolve, reject) => {
super
.get(path)
.then(resolve)
.catch((error) => {
if (error instanceof UnsuccessfulFetch) {
error.response
.json()
.then((data) => {
const { reason, message } = data as {
reason: string;
message?: string;
};
reject(new Error(message ?? reason));
})
.catch(reject);
} else {
reject(error);
}
})
.catch(reject);
});
}
/**
* Get all available symbols for trading.
*/
public getSymbols(): Bluebird<string[]> {
return this.get("v1/symbols") as Bluebird<string[]>;
}
/**
* Get information about recent trading activity for the symbol.
*/
public getTicker(options: { symbol?: string; v: "v2" }): Bluebird<TickerV2>;
public getTicker(options?: { symbol?: string; v?: "v1" }): Bluebird<TickerV1>;
public getTicker({
symbol = this.symbol,
v = "v1",
}: TickerFilter = {}): Bluebird<Ticker> {
if (v === "v2") {
return this.get(`/${v}/ticker/${symbol}`) as Bluebird<TickerV2>;
}
return this.get(`/${v}/pubticker/${symbol}`) as Bluebird<TickerV1>;
}
/**
* Get time-intervaled data for the provided symbol.
*/
public getCandles({
symbol = this.symbol,
time_frame = "1day",
}: CandlesFilter = {}): Bluebird<Candle[]> {
return this.get(`/v2/candles/${symbol}/${time_frame}`) as Bluebird<
Candle[]
>;
}
/**
* Get the current order book.
*/
public getOrderBook({
symbol = this.symbol,
...qs
}: BookFilter = {}): Bluebird<OrderBook> {
const url = new URL(`/v1/book/${symbol}`, this.apiUri);
PublicClient.addOptions(url, { ...qs });
return this.get(url.toString()) as Bluebird<OrderBook>;
}
/**
* Get the trades that have executed since the specified timestamp.
*/
public getTradeHistory({
symbol = this.symbol,
limit_trades = ApiLimit,
...qs
}: TradeHistoryFilter = {}): Bluebird<Trade[]> {
const url = new URL(`/v1/trades/${symbol}`, this.apiUri);
PublicClient.addOptions(url, { limit_trades, ...qs });
return this.get(url.toString()) as Bluebird<Trade[]>;
}
/**
* Get current auction information.
*/
public getCurrentAuction({
symbol = this.symbol,
}: SymbolFilter = {}): Bluebird<AuctionInfo> {
return this.get(`v1/auction/${symbol}`) as Bluebird<AuctionInfo>;
}
/**
* Get the auction events.
*/
public getAuctionHistory({
symbol = this.symbol,
limit_auction_results = ApiLimit,
...qs
}: AuctionHistoryFilter = {}): Bluebird<AuctionHistory[]> {
const url = new URL(`/v1/auction/${symbol}/history`, this.apiUri);
PublicClient.addOptions(url, { limit_auction_results, ...qs });
return this.get(url.toString()) as Bluebird<AuctionHistory[]>;
}
/**
* Get the price feed.
*/
public getPriceFeed(): Bluebird<PriceFeedItem[]> {
return this.get("v1/pricefeed") as Bluebird<PriceFeedItem[]>;
}
private static addOptions(
target: URL,
data: Record<string, string | number | boolean | undefined>
): void {
for (const key in data) {
const value = data[key];
if (typeof value !== "undefined") {
target.searchParams.append(key, value.toString());
}
}
}
}