-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CBAppClient.ts
495 lines (458 loc) · 12.5 KB
/
CBAppClient.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
import { AxiosRequestConfig } from 'axios';
import { BaseRestClient } from './lib/BaseRestClient.js';
import {
REST_CLIENT_TYPE_ENUM,
RestClientOptions,
RestClientType,
} from './lib/requestUtils.js';
import {
CBAppDepositFundsRequest,
CBAppSendMoneyRequest,
CBAppTransferMoneyRequest,
CBAppWithdrawFundsRequest,
} from './types/request/coinbase-app-client.js';
import {
CBAppAccount,
CBAppAddress,
CBAppCryptocurrency,
CBAppDepositWithdrawal,
CBAppFiatCurrency,
CBAppPagination,
CBAppTransaction,
} from './types/response/coinbase-app-client.js';
/**
* REST client for Coinbase's Coinbase App API:
* https://docs.cdp.coinbase.com/coinbase-app/docs/welcome
*/
export class CBAppClient extends BaseRestClient {
constructor(
restClientOptions: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {},
) {
super(restClientOptions, {
...requestOptions,
headers: {
// Some endpoints return a warning if a version header isn't included: https://docs.cdp.coinbase.com/coinbase-app/docs/versioning
// Currently set to a date from the changelog: https://docs.cdp.coinbase.com/coinbase-app/docs/changelog
'CB-VERSION': '2024-09-13',
...requestOptions.headers,
},
});
return this;
}
getClientType(): RestClientType {
return REST_CLIENT_TYPE_ENUM.coinbaseApp;
}
/**
*
* Account Endpoints
*
*/
/**
* List Accounts
*
* List a current user's accounts to which the authentication method has access to.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of accounts.
*/
getAccounts(params?: { paginationURL?: string }): Promise<{
data: CBAppAccount[];
pagination: CBAppPagination;
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate('/v2/accounts');
}
/**
* Show Account
*
* Get a current user's account by account ID or currency string.
*/
getAccount(params: { account_id: string }): Promise<{
data: CBAppAccount;
}> {
return this.getPrivate(`/v2/accounts/${params.account_id}`);
}
/**
*
* Address Endpoints
*
*/
/**
* Create Address
*
* Creates a new address for an account. Addresses can be created for wallet account types.
*/
createAddress(params: { account_id: string; name?: string }): Promise<{
data: CBAppAddress;
}> {
return this.postPrivate(`/v2/accounts/${params.account_id}/addresses`, {
body: params,
});
}
/**
* List Addresses
*
* Lists addresses for an account.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of addresses.
*/
getAddresses(params: {
account_id: string;
paginationURL?: string;
}): Promise<{
pagination: CBAppPagination;
data: CBAppAddress[];
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate(`/v2/accounts/${params.account_id}/addresses`);
}
/**
* Show Address
*
* Get an single address for an account.
* A regular cryptocurrency address can be used in place of address_id but the address must be associated with the correct account.
*
* !! An address can only be associated with one account. See Create Address to create new addresses.
*/
getAddress(params: { account_id: string; addressId: string }): Promise<{
data: CBAppAddress;
}> {
return this.getPrivate(
`/v2/accounts/${params.account_id}/addresses/${params.addressId}`,
);
}
/**
* List Address Transactions
*
* List transactions that have been sent to a specific address.
* A regular cryptocurrency address can be used in place of address_id but the address must be associated with the correct account.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of transactions.
*/
getAddressTransactions(params: {
account_id: string;
addressId: string;
paginationURL?: string;
}): Promise<{
pagination: CBAppPagination;
data: CBAppTransaction[];
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate(
`/v2/accounts/${params.account_id}/addresses/${params.addressId}/transactions`,
);
}
/**
*
* Transactions Endpoints
*
*/
/**
* Send Money
*
* Send funds to a network address for any Coinbase supported asset, or email address of the recipient.
* No transaction fees are required for off-blockchain cryptocurrency transactions.
*/
sendMoney(
params: CBAppSendMoneyRequest,
): Promise<{ data: CBAppTransaction }> {
const { account_id, ...restParams } = params;
return this.postPrivate(`/v2/accounts/${account_id}/transactions`, {
body: restParams,
});
}
/**
* Transfer Money
*
* Transfer any Coinbase supported digital asset between two of a single user's accounts.
* Accounts must support the same currency for transfers to be successful.
*/
transferMoney(
params: CBAppTransferMoneyRequest,
): Promise<{ data: CBAppTransaction }> {
const { account_id, ...restParams } = params;
return this.postPrivate(`/v2/accounts/${account_id}/transactions`, {
body: restParams,
});
}
/**
* List Transactions
*
* Lists the transactions of an account by account ID.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of transactions.
*/
getTransactions(params: {
account_id: string;
paginationURL?: string;
}): Promise<{
pagination: CBAppPagination;
data: CBAppTransaction[];
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate(`/v2/accounts/${params.account_id}/transactions`);
}
/**
* Show Transaction
*
* Get a single transaction for an account.
*/
getTransaction(params: {
account_id: string;
transactionId: string;
}): Promise<{
data: CBAppTransaction;
}> {
return this.getPrivate(
`/v2/accounts/${params.account_id}/transactions/${params.transactionId}`,
);
}
/**
*
* Deposits Endpoints
*
*/
/**
* Deposit Funds
*
* Deposits user-defined amount of funds to a fiat account.
*/
depositFunds(
params: CBAppDepositFundsRequest,
): Promise<{ data: CBAppDepositWithdrawal }> {
const { account_id, ...restParams } = params;
return this.postPrivate(`/v2/accounts/${account_id}/deposits`, {
body: restParams,
});
}
/**
* Commit Deposit
*
* Completes a deposit that is created in commit: false state.
*/
commitDeposit(params: {
account_id: string;
deposit_id: string;
}): Promise<{ data: CBAppDepositWithdrawal }> {
return this.postPrivate(
`/v2/accounts/${params.account_id}/deposits/${params.deposit_id}/commit`,
);
}
/**
* List Deposits
*
* Lists fiat deposits for an account.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of deposits.
*/
getDeposits(params: { account_id: string; paginationURL?: string }): Promise<{
pagination: CBAppPagination;
data: CBAppDepositWithdrawal[];
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate(`/v2/accounts/${params.account_id}/deposits`);
}
/**
* Show Deposit
*
* Get one deposit by deposit Id.
*/
getDeposit(params: { account_id: string; deposit_id: string }): Promise<{
data: CBAppDepositWithdrawal;
}> {
return this.getPrivate(
`/v2/accounts/${params.account_id}/deposits/${params.deposit_id}`,
);
}
/**
*
* Withdrawals Endpoints
*
*/
/**
* Withdraw Funds
*
* Withdraws a user-defined amount of funds from a fiat account.
*/
withdrawFunds(
params: CBAppWithdrawFundsRequest,
): Promise<{ data: CBAppDepositWithdrawal }> {
const { account_id, ...restParams } = params;
return this.postPrivate(`/v2/accounts/${account_id}/withdrawals`, {
body: restParams,
});
}
/**
* Commit Withdrawal
*
* Completes a withdrawal that is created in commit: false state.
*/
commitWithdrawal(params: {
account_id: string;
withdrawal_id: string;
}): Promise<{ data: CBAppDepositWithdrawal }> {
return this.postPrivate(
`/v2/accounts/${params.account_id}/withdrawals/${params.withdrawal_id}/commit`,
);
}
/**
* List Withdrawals
*
* Lists withdrawals for an account.
*
* This endpoint is paginated. In case you are calling it first time, leave paginationURL empty.
* If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of withdrawals.
*/
getWithdrawals(params: {
account_id: string;
paginationURL?: string;
}): Promise<{
pagination: CBAppPagination;
data: CBAppDepositWithdrawal[];
}> {
if (params?.paginationURL) {
return this.getPrivate(params.paginationURL);
}
return this.getPrivate(`/v2/accounts/${params.account_id}/withdrawals`);
}
/**
* Show Withdrawal
*
* Get a single withdrawal.
*/
getWithdrawal(params: {
account_id: string;
withdrawal_id: string;
}): Promise<{
data: CBAppDepositWithdrawal;
}> {
return this.getPrivate(
`/v2/accounts/${params.account_id}/withdrawals/${params.withdrawal_id}`,
);
}
/**
*
* DATA - Currencies Endpoints
*
*/
/**
* Get Fiat Currencies
*
* Lists known fiat currencies. Currency codes conform to the ISO 4217 standard where possible.
* Currencies with no representation in ISO 4217 may use a custom code.
*/
getFiatCurrencies(): Promise<{
data: CBAppFiatCurrency[];
}> {
return this.get('/v2/currencies');
}
/**
* Get Cryptocurrencies
*
* Lists known cryptocurrencies.
*/
getCryptocurrencies(): Promise<CBAppCryptocurrency[]> {
return this.get('/v2/currencies/crypto');
}
/**
*
* DATA- Exchange rates Endpoints
*
*/
/**
* Get Exchange Rates
*
* Get current exchange rates. Default base currency is USD but it can be defined as any supported currency.
* Returned rates will define the exchange rate for one unit of the base currency.
*/
getExchangeRates(params?: { currency?: string }): Promise<{
data: {
currency: string;
rates: { [key: string]: string };
};
}> {
return this.get(`/v2/exchange-rates`, params);
}
/**
*
* DATA - Prices Endpoints
*
*/
/**
* Get Buy Price
*
* Get the total price to buy one bitcoin or ether.
* This endpoint doesn't require authentication.
*/
getBuyPrice(params: { currencyPair: string }): Promise<{
data: {
amount: string;
currency: string;
};
}> {
return this.get(`/v2/prices/${params.currencyPair}/buy`);
}
/**
* Get Sell Price
*
* Get the total price to sell one bitcoin or ether.
* This endpoint doesn't require authentication.
*/
getSellPrice(params: { currencyPair: string }): Promise<{
data: {
amount: string;
currency: string;
};
}> {
return this.get(`/v2/prices/${params.currencyPair}/sell`);
}
/**
* Get Spot Price
*
* Get the current market price for bitcoin. This is usually somewhere in between the buy and sell price.
* This endpoint doesn't require authentication.
*/
getSpotPrice(params: { currencyPair: string; date?: string }): Promise<{
data: {
amount: string;
currency: string;
};
}> {
const { currencyPair, ...query } = params;
return this.get(`/v2/prices/${currencyPair}/spot`, query);
}
/**
*
* DATA - Time Endpoints
*
*/
/**
* Get Current Time
*
* Get the API server time.
* This endpoint doesn't require authentication.
*/
getCurrentTime(): Promise<{
data: {
iso: string;
epoch: number;
};
}> {
return this.get('/v2/time');
}
}