-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.ts
709 lines (623 loc) · 18.9 KB
/
auth.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import { UnsuccessfulFetch } from "rpc-request";
import {
DefaultCurrency,
PublicClient,
PublicClientOptions,
SymbolFilter,
ApiLimit,
} from "./public.js";
import { SignRequest } from "./signer.js";
import type { RequestInit } from "node-fetch";
export interface AccountName {
account?: string;
}
export type OrderEexecutionOptions =
| "maker-or-cancel"
| "immediate-or-cancel"
| "fill-or-kill"
| "auction-only"
| "indication-of-interest";
export type BasicOrderOptions = AccountName & {
client_order_id?: string;
amount: number;
min_amount?: number;
price: number;
type: "exchange limit" | "exchange stop limit";
options?: [OrderEexecutionOptions];
stop_price?: number;
} & SymbolFilter;
export interface OrderOptions extends BasicOrderOptions {
side: "buy" | "sell";
}
export interface OrderID extends AccountName {
order_id: number;
client_order_id?: string;
include_trades?: boolean;
}
export type PastTradesFilter = SymbolFilter & {
limit_trades?: number;
timestamp?: number;
} & AccountName;
export interface BaseClearingOrder extends SymbolFilter {
amount: number;
price: number;
side: "buy" | "sell";
expires_in_hrs: number;
}
export interface ClearingOrderOptions extends BaseClearingOrder {
counterparty_id?: string;
}
export interface BrokerOrderOptions extends BaseClearingOrder {
target_counterparty_id: string;
source_counterparty_id: string;
}
export interface ClearingOrderID {
clearing_id: string;
}
export interface ConfirmClearingOptions {
clearing_id: string;
symbol?: string;
amount: number;
price: number;
side: "buy" | "sell";
}
export interface NotionalBalancesOptions extends AccountName {
currency?: string;
}
export interface TransferFilter extends AccountName {
timestamp?: number;
limit_transfers?: number;
}
export interface DepositAddressesFilter extends AccountName {
network: string;
}
export interface NewAddressFilter extends AccountName {
currency: string;
label?: string;
legacy?: boolean;
}
export interface WithdrawCryptoFilter extends AccountName {
currency: string;
address: string;
amount: number;
}
export interface InternalTransferFilter {
currency: string;
sourceAccount: string;
targetAccount: string;
amount: number;
}
export interface AddBankOptions extends AccountName {
/** Account number of bank account to be added */
accountnumber: string;
/** Routing number of bank account to be added */
routing: string;
/** Type of bank account to be added */
type: "checking" | "savings";
/** The name of the bank account as shown on your account statements */
name: string;
}
export interface SENWithdrawOptions extends AccountName {
/** Unique ID for your SEN bank account */
bankId: string;
/** Amount (USD) to transfer to your account */
amount: string;
}
export interface AccountDetails {
account: {
accountName: string;
shortName: string;
type: string;
created: string;
};
users: {
name: string;
lastSignIn: string;
status: string;
countryCode: string;
isVerified: boolean;
}[];
memo_reference_code: string;
}
export interface Account {
name: string;
type?: "exchange" | "custody";
}
export interface WithdrawGUSDFilter extends AccountName {
address: string;
amount: number;
}
export type OrderType =
| "exchange limit"
| "stop-limit"
| "auction-only exchange limit"
| "market buy"
| "market sell"
| "indication-of-interest";
export interface BaseOrder {
side: "buy" | "sell";
price?: string;
symbol: string;
order_id: string;
client_order_id?: string;
timestamp: string;
timestampms: number;
is_live: boolean;
is_cancelled: boolean;
is_hidden: boolean;
remaining_amount?: string;
original_amount?: string;
executed_amount?: string;
}
export interface OrderStatus extends BaseOrder {
id: string;
exchange: "gemini";
avg_execution_price: string;
type: OrderType;
options: [OrderEexecutionOptions] | [];
reason?: string;
trades?: PastTrade[];
was_forced: boolean;
stop_price?: string;
}
export interface CancelOrdersResponse {
result: "ok";
details: { cancelledOrders: number[]; cancelRejects: number[] };
}
export interface PastTrade {
price: string;
amount: string;
timestamp: number;
timestampms: number;
type: "Buy" | "Sell";
aggressor: boolean;
fee_currency: string;
fee_amount: string;
tid: number;
order_id: string;
client_order_id?: string;
exchange?: "gemini";
is_auction_fill: boolean;
break?: string;
}
export interface NotionalVolume {
account_id?: number;
date: string;
last_updated_ms: number;
web_maker_fee_bps: number;
web_taker_fee_bps: number;
web_auction_fee_bps: number;
api_maker_fee_bps: number;
api_taker_fee_bps: number;
api_auction_fee_bps: number;
fix_maker_fee_bps: number;
fix_taker_fee_bps: number;
fix_auction_fee_bps: number;
block_maker_fee_bps: number;
block_taker_fee_bps: number;
notional_30d_volume: number;
notional_1d_volume: { date: string; notional_volume: number }[];
}
export interface TradeVolume {
account_id?: number;
symbol: string;
base_currency: string;
notional_currency: string;
data_date: string;
total_volume_base: number;
maker_buy_sell_ratio: number;
buy_maker_base: number;
buy_maker_notional: number;
buy_maker_count: number;
sell_maker_base: number;
sell_maker_notional: number;
sell_maker_count: number;
buy_taker_base: number;
buy_taker_notional: number;
buy_taker_count: number;
sell_taker_base: number;
sell_taker_notional: number;
sell_taker_count: number;
}
export interface NewClearingOrderResponse {
result: string;
clearing_id: string;
}
export interface ClearingOrderStatus {
result: "ok";
status:
| "AwaitConfirm"
| "AwaitSourceTargetConfirm"
| "AwaitTargetConfirm"
| "AwaitSourceConfirm"
| "Confirmed"
| "AttemptSettlement"
| "Settled"
| "Expired"
| "Canceled"
| "Not Found";
}
export interface CancelClearingOrderResponse {
result: "ok" | "failed";
details: string;
}
export interface ConfirmClearingOptionsResponse {
result: "confirmed" | "error";
reason?: string;
message?: string;
}
export interface Balance {
type: "exchange";
currency: string;
amount: string;
available: string;
availableForWithdrawal: string;
}
export interface NotionalBalance {
currency: string;
amount: string;
amountNotional: string;
available: string;
availableNotional: string;
availableForWithdrawal: string;
availableForWithdrawalNotional: string;
}
export interface Transfer {
type: "Deposit" | "Withdrawal";
status: "Advanced" | "Complete";
timestampms: number;
eid: number;
currency: string;
amount: string;
method?: "ACH" | "Wire";
txHash?: string;
outputIdx?: number;
destination?: string;
purpose?: string;
}
export interface DepositAddress {
address: string;
timestamp: number;
label?: string;
}
export interface NewAddress {
currency: string;
address: string;
label?: string;
}
export interface Withdrawal {
address: string;
amount: string;
txHash?: string;
withdrawalId?: string;
message?: string;
}
export interface InternalTransferResponse {
uuid: string;
}
export interface AddBankResponse {
/** Reference ID for the new bank addition request. Once received, send in a wire from the requested bank account to verify it and enable withdrawals to that account. */
referenceId: string;
}
export interface PaymentBalance {
/** Account type. Will always be `exchange`. */
type: "exchange";
/** Symbol for fiat balance. */
currency: "USD";
/** Total account balance for currency. */
amount: string;
/** Total amount available for trading */
available: string;
/** Total amount available for withdrawal */
availableForWithdrawal: string;
}
export interface PaymentBank {
/** Name of bank account */
bank: string;
/** Unique identifier for bank account */
bankId: string;
}
export interface PaymentMethods {
balances: PaymentBalance[];
banks: PaymentBank[];
}
export interface SENWithdrawal {
amount: string;
withdrawalId: string;
message: string;
}
export interface AccountInfo {
name: string;
account: string;
type: "exchange" | "custody";
counterparty_id: string;
created: number;
}
export interface GUSDWithdrawal {
destination: string;
amount: string;
txHash: string;
}
export interface Heartbeat {
result: "ok";
}
export interface AuthenticatedClientOptions extends PublicClientOptions {
key: string;
secret: string;
}
export class AuthenticatedClient extends PublicClient {
readonly #key: string;
readonly #secret: string;
#nonce: () => number;
public constructor({ key, secret, ...rest }: AuthenticatedClientOptions) {
super(rest);
this.#key = key;
this.#secret = secret;
this.#nonce = (): number => Date.now();
}
public post<T = unknown>(
path: string | undefined,
_options: RequestInit | undefined,
body: { request: string } & Record<string, unknown> = { request: "/" }
): Promise<T> {
body.nonce = this.nonce();
const payload = Buffer.from(JSON.stringify(body)).toString("base64");
const request = { key: this.#key, secret: this.#secret, payload };
const headers = { ...SignRequest(request) };
return new Promise<T>((resolve, reject) => {
super
.post(path ?? body.request, { headers })
.then((data) => {
resolve(data as T);
})
.catch((error: unknown) => {
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);
});
}
/** Submit a new order. */
public newOrder({
symbol = this.symbol,
...rest
}: OrderOptions): Promise<OrderStatus> {
const request = "/v1/order/new";
const body = { request, symbol, ...rest };
return this.post<OrderStatus>(request, {}, body);
}
/** Submit a new buy order. */
public buy({
symbol = this.symbol,
...rest
}: BasicOrderOptions): Promise<OrderStatus> {
const request = "/v1/order/new";
const body = { request, symbol, ...rest, side: "buy" };
return this.post<OrderStatus>(request, {}, body);
}
/** Submit a new sell order. */
public sell({
symbol = this.symbol,
...rest
}: BasicOrderOptions): Promise<OrderStatus> {
const request = "/v1/order/new";
const body = { request, symbol, ...rest, side: "sell" };
return this.post<OrderStatus>(request, {}, body);
}
/** Cancel an order. */
public cancelOrder(params: OrderID): Promise<OrderStatus> {
const request = "/v1/order/cancel";
const body = { request, ...params };
return this.post<OrderStatus>(request, {}, body);
}
/** Cancel all orders opened by this session. */
public cancelSession(account?: AccountName): Promise<CancelOrdersResponse> {
const request = "/v1/order/cancel/session";
const body = { request, ...account };
return this.post<CancelOrdersResponse>(request, {}, body);
}
/** Cancel all outstanding orders created by all sessions owned by this account. */
public cancelAll(account?: AccountName): Promise<CancelOrdersResponse> {
const request = "/v1/order/cancel/all";
const body = { request, ...account };
return this.post<CancelOrdersResponse>(request, {}, body);
}
/** Get an order status. */
public getOrderStatus(params: OrderID): Promise<OrderStatus> {
const request = "/v1/order/status";
const body = { request, ...params };
return this.post<OrderStatus>(request, {}, body);
}
/** Get all your live orders. */
public getActiveOrders(account?: AccountName): Promise<OrderStatus[]> {
const request = "/v1/orders";
const body = { request, ...account };
return this.post<OrderStatus[]>(request, {}, body);
}
/** Get your past trades. */
public getPastTrades({
symbol = this.symbol,
limit_trades = ApiLimit,
...rest
}: PastTradesFilter = {}): Promise<PastTrade[]> {
const request = "/v1/mytrades";
const body = { request, symbol, limit_trades, ...rest };
return this.post<PastTrade[]>(request, {}, body);
}
/** Get the volume in price currency that has been traded across all pairs over a period of 30 days. */
public getNotionalVolume(account?: AccountName): Promise<NotionalVolume> {
const request = "/v1/notionalvolume";
const body = { request, ...account };
return this.post<NotionalVolume>(request, {}, body);
}
/** Get the trade volume for each symbol. */
public getTradeVolume(account?: AccountName): Promise<TradeVolume[][]> {
const request = "/v1/tradevolume";
const body = { request, ...account };
return this.post<TradeVolume[][]>(request, {}, body);
}
/** Submit a new clearing order. */
public newClearingOrder({
symbol = this.symbol,
...rest
}: ClearingOrderOptions): Promise<NewClearingOrderResponse> {
const request = "/v1/clearing/new";
const body = { request, symbol, ...rest };
return this.post<NewClearingOrderResponse>(request, {}, body);
}
/** Submit a new broker clearing order. */
public newBrokerOrder({
symbol = this.symbol,
...rest
}: BrokerOrderOptions): Promise<NewClearingOrderResponse> {
const request = "/v1/clearing/broker/new";
const body = { request, symbol, ...rest };
return this.post<NewClearingOrderResponse>(request, {}, body);
}
/** Get a clearing order status. */
public getClearingOrderStatus(
order: ClearingOrderID
): Promise<ClearingOrderStatus> {
const request = "/v1/clearing/status";
const body = { request, ...order };
return this.post<ClearingOrderStatus>(request, {}, body);
}
/** Cancel a clearing order. */
public cancelClearingOrder(
order: ClearingOrderID
): Promise<CancelClearingOrderResponse> {
const request = "/v1/clearing/cancel";
const body = { request, ...order };
return this.post<CancelClearingOrderResponse>(request, {}, body);
}
/** Confirm a clearing order. */
public confirmClearingOrder({
symbol = this.symbol,
...rest
}: ConfirmClearingOptions): Promise<ConfirmClearingOptionsResponse> {
const request = "/v1/clearing/confirm";
const body = { request, symbol, ...rest };
return this.post<ConfirmClearingOptionsResponse>(request, {}, body);
}
/** Get the available balances in the supported currencies. */
public getAvailableBalances(account?: AccountName): Promise<Balance[]> {
const request = "/v1/balances";
const body = { request, ...account };
return this.post<Balance[]>(request, {}, body);
}
/** Get the available balances in the supported currencies as well as in notional USD. */
public getNotionalBalances({
currency = DefaultCurrency,
...account
}: NotionalBalancesOptions = {}): Promise<NotionalBalance[]> {
const request = `/v1/notionalbalances/${currency}`;
const body = { request, ...account };
return this.post<NotionalBalance[]>(request, {}, body);
}
/** Get deposits and withdrawals in the supported currencies. */
public getTransfers(rest?: TransferFilter): Promise<Transfer[]> {
const request = "/v1/transfers";
const body = { request, ...rest };
return this.post<Transfer[]>(request, {}, body);
}
/** Get deposit addresses. */
public getDepositAddresses({
network,
...rest
}: DepositAddressesFilter): Promise<DepositAddress[]> {
const request = `/v1/addresses/${network}`;
const body = { request, ...rest };
return this.post<DepositAddress[]>(request, {}, body);
}
/** Get a new deposit address. */
public getNewAddress({
currency,
...rest
}: NewAddressFilter): Promise<NewAddress> {
const request = `/v1/deposit/${currency}/newAddress`;
const body = { request, ...rest };
return this.post<NewAddress>(request, {}, body);
}
/** Withdraw cryptocurrency funds to a whitelisted address. */
public withdrawCrypto({
currency,
...rest
}: WithdrawCryptoFilter): Promise<Withdrawal> {
const request = `/v1/withdraw/${currency}`;
const body = { request, ...rest };
return this.post<Withdrawal>(request, {}, body);
}
/** Make an internal transfer between any two exchange accounts within the Group. */
public internalTransfer({
currency,
...rest
}: InternalTransferFilter): Promise<InternalTransferResponse> {
const request = `/v1/account/transfer/${currency}`;
const body = { request, ...rest };
return this.post<InternalTransferResponse>(request, {}, body);
}
/** The add bank API allows for banking information to be sent in via API. However, for the bank to be verified, you must still send in a wire for any amount from the bank account. */
public addBank(bank: AddBankOptions): Promise<AddBankResponse> {
const request = `/v1/payments/addbank`;
const body = { request, ...bank };
return this.post<AddBankResponse>(request, {}, body);
}
/** Get data on balances in the account and linked banks */
public getPaymentMethods(account?: AccountName): Promise<PaymentMethods> {
const request = `/v1/payments/methods`;
const body = { request, ...account };
return this.post<PaymentMethods>(request, {}, body);
}
/** The SEN Withdrawals API allows you to withdraw USD from your Gemini account via SEN */
public withdrawSEN(options: SENWithdrawOptions): Promise<SENWithdrawal> {
const request = `/v1/payments/sen/withdraw`;
const body = { request, ...options };
return this.post<SENWithdrawal>(request, {}, body);
}
/** Get details about the specific account requested such as users, country codes, etc. */
public getAccountDetails(account?: AccountName): Promise<AccountDetails> {
const request = "/v1/account";
const body = { request, ...account };
return this.post<AccountDetails>(request, {}, body);
}
/** Create a new exchange account within the group. */
public createAccount(account: Account): Promise<Account> {
const request = "/v1/account/create";
const body = { request, ...account };
return this.post<Account>(request, {}, body);
}
/** Get the accounts within the group. */
public getAccounts(): Promise<AccountInfo[]> {
const request = "/v1/account/list";
return this.post<AccountInfo[]>(request, {}, { request });
}
/** Withdraw `USD` as `GUSD`. */
public withdrawGUSD(options: WithdrawGUSDFilter): Promise<GUSDWithdrawal> {
const request = "/v1/withdraw/usd";
const body = { request, ...options };
return this.post<GUSDWithdrawal>(request, {}, body);
}
/** Prevent a session from timing out and canceling orders if the require heartbeat flag has been set. */
public heartbeat(): Promise<Heartbeat> {
const request = "/v1/heartbeat";
return this.post<Heartbeat>(request, {}, { request });
}
public set nonce(nonce: () => number) {
this.#nonce = nonce;
}
public get nonce(): () => number {
return this.#nonce;
}
}