coinbase_pro 0.1.1

A Rust API for retriving data and placing trades on Coinbase Pro
Documentation
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
use std::fmt::{
    Display,
    Formatter,
};

use chrono::NaiveDateTime;
use serde::{
    Deserialize,
    Serialize,
};
use serde_json::Value;

use crate::deserialization::{
    iso_date_time,
    string_as_float,
    transfer_date,
};

/// # Account Data
/// A strongly typed representation of the account data returned by [/accounts](https://api.exchange.coinbase.com/accounts).
/// CBPro API reference: [Profiles](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounts).
///
/// # JSON Input Example
///
/// ```ignore
///{
///     "id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "currency":"BTC",
///     "balance":"0.0000000000000000",
///     "hold":"0.0000000000000000",
///     "available":"0",
///     "profile_id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "trading_enabled":true
///}
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Account {
    pub id: String,
    pub currency: String,
    #[serde(with = "string_as_float")]
    pub balance: f64,
    #[serde(with = "string_as_float")]
    pub hold: f64,
    #[serde(with = "string_as_float")]
    pub available: f64,
    pub profile_id: String,
    pub trading_enabled: bool,
}

impl PartialEq<&str> for Account {
    fn eq(&self, other: &&str) -> bool {
        self.id.as_str() == *other
    }
}

///# Hold Data
/// A strongly typed representation of the hold data returned by [/accounts/{account_id}/holds](https://api.exchange.coinbase.com/accounts/{account_id}/holds).
/// CBPro API reference: [Holds](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccountholds).
///
/// # JSON Input Example
///
/// ```ignore
///{
///     "id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "created_at":"2022-01-20T00:00:00.000000Z",
///     "amount":"0.0000000000000000",
///     "ref":"deadbeef-dead-beef-dead-beefdeadbeex",
///     "type":"order"
///}
///```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Hold {
    pub id: String,
    #[serde(with = "iso_date_time")]
    pub created_at: NaiveDateTime,
    pub amount: String,
    #[serde(rename = "ref")]
    pub ref_string: String,
    #[serde(rename = "type")]
    pub type_string: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Ledger {
    pub id: String,
    #[serde(with = "string_as_float")]
    pub amount: f64,
    #[serde(with = "iso_date_time")]
    pub created_at: NaiveDateTime,
    #[serde(with = "string_as_float")]
    pub balance: f64,
    #[serde(flatten)]
    pub details: LedgerDetail,
}

/// todo!(LedgerDetail Variants) Needs an example for Fee, Rebate, and Conversion
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "details", rename_all = "lowercase")]
pub enum LedgerDetail {
    Match(MatchDetail),
    Transfer(TransferDetail),
    Fee(Value),
    Rebate(Value),
    Conversion(Value),
}

impl LedgerDetail {
    pub fn variant_name(&self) -> &str {
        match self {
            LedgerDetail::Match(_) => "match",
            LedgerDetail::Transfer(_) => "transfer",
            LedgerDetail::Fee(_) => "fee",
            LedgerDetail::Rebate(_) => "rebate",
            LedgerDetail::Conversion(_) => "conversion",
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MatchDetail {
    pub order_id: String,
    pub product_id: String,
    pub trade_id: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TransferDetail {
    pub transfer_id: String,
    pub transfer_type: String,
}

impl Display for Ledger {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{}", self.id)?;
        writeln!(f, "{}", self.amount)?;
        writeln!(f, "{}", self.created_at)?;
        writeln!(f, "{}", self.balance)?;
        writeln!(f, "{:?}", self.details)
    }
}

///# Transfer Data
/// A strongly typed representation of the transfer data returned by [/accounts/{account_id}/transfers](https://api.exchange.coinbase.com/accounts/{account_id}/transfers).
///
/// CBPro API reference: [Transfers](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounttransfers).
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "type":"withdraw",
///     "created_at":"2021-09-13 00:00:00.000000+00",
///     "completed_at":"2021-09-13 00:00:00.000000+00",
///     "canceled_at":null,
///     "processed_at":"2021-09-13 00:00:00.00000+00",
///     "user_nonce":"1234567891011",
///     "amount":"0.00000000",
///     "details":
///     {
///		    "fee":"0.000000",
///		    "subtotal":"0.00",
///		    "sent_to_address":"0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
///		    "coinbase_account_id":"deadbeef-dead-beef-dead-beefdeadbeef",
///		    "coinbase_withdrawal_id":"bofadeeznutsdeadbeefligm",
///		    "coinbase_transaction_id":"bofadeeznutsdeadbeefligm",
///		    "crypto_transaction_hash":"bofadeeznutsdeadbeefligmaballzsugmamikehuntjennytaliabofadeeznut",
///		    "coinbase_payment_method_id":""
///     }
/// }
///```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Transfer {
    pub id: String,
    #[serde(rename = "type")]
    pub type_string: String,
    #[serde(with = "transfer_date")]
    pub created_at: NaiveDateTime,
    #[serde(with = "transfer_date")]
    pub completed_at: NaiveDateTime,
    pub canceled_at: Option<String>,
    #[serde(with = "transfer_date")]
    pub processed_at: NaiveDateTime,
    pub user_nonce: Option<String>,
    #[serde(with = "string_as_float")]
    pub amount: f64,
    pub details: Details,
    pub idem: Option<String>,
}

///# Details Data
/// A strongly typed representation of the detail data within the [Transfer] struct.
///
/// CBPro API reference: [Transfers](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounttransfers).
/// These details come in 1 of 2 forms, a withdrawal detail and a deposit detail struct.
///
/// todo!("Represent this enum as a single struct with optionals.")
///
/// # JSON Input Example
///
///```ignore
/// {
///     "fee":"0.000000",
///     "subtotal":"0.00",
///     "sent_to_address":"0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
///     "coinbase_account_id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "coinbase_withdrawal_id":"bofadeeznutsdeadbeefligm",
///     "coinbase_transaction_id":"bofadeeznutsdeadbeefligm",
///     "crypto_transaction_hash":"bofadeeznutsdeadbeefligmaballzsugmamikehuntjennytaliabofadeeznut",
///     "coinbase_payment_method_id":""
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Details {
    Withdraw(WithdrawDetails),
    Deposit(DepositDetails),
}

///# Withdrawal Details Data
/// A strongly typed representation of the withdrawal detail data within the [Transfer] struct.
///
/// CBPro API reference: [Transfers](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounttransfers).
///
///
/// # JSON Input Example
///
///```ignore
/// {
///     "fee":"0.000000",
///     "subtotal":"0.00",
///     "sent_to_address":"0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
///     "coinbase_account_id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "coinbase_withdrawal_id":"bofadeeznutsdeadbeefligm",
///     "coinbase_transaction_id":"bofadeeznutsdeadbeefligm",
///     "crypto_transaction_hash":"bofadeeznutsdeadbeefligmaballzsugmamikehuntjennytaliabofadeeznut",
///     "coinbase_payment_method_id":""
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WithdrawDetails {
    #[serde(with = "string_as_float")]
    pub fee: f64,
    #[serde(with = "string_as_float")]
    pub subtotal: f64,
    pub sent_to_address: String,
    pub coinbase_account_id: String,
    pub coinbase_withdrawal_id: String,
    pub coinbase_transaction_id: String,
    pub crypto_transaction_hash: String,
    pub coinbase_payment_method_id: String,
}

///# Deposit Details Data
/// A strongly typed representation of the deposit detail data within the [Transfer] struct.
///
/// CBPro API reference: [Transfers](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounttransfers).
///
///
/// # JSON Input Example
///
///```ignore
/// {
///     "fee":"0.000000",
///     "subtotal":"0.00",
///     "sent_to_address":"0xDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
///     "coinbase_account_id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "coinbase_withdrawal_id":"bofadeeznutsdeadbeefligm",
///     "coinbase_transaction_id":"bofadeeznutsdeadbeefligm",
///     "crypto_transaction_hash":"bofadeeznutsdeadbeefligmaballzsugmamikehuntjennytaliabofadeeznut",
///     "coinbase_payment_method_id":""
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DepositDetails {
    pub crypto_address: String,
    pub destination_tag: Option<String>,
    pub coinbase_account_id: String,
    pub destination_tag_name: String,
    pub crypto_transaction_id: String,
    pub coinbase_transaction_id: String,
    pub crypto_transaction_hash: String,
}

///# Wallet Data
/// A strongly typed representation of the data returned by [/coinbase-accounts](https://api.exchange.coinbase.com/coinbase-accounts).
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
///```ignore
/// {
///     "id":"deadbeef-dead-beef-dead-beefdeadbeef",
///     "name":"USD Wallet",
///     "balance":"0.00",
///     "currency":"USD",
///     "type":"fiat",
///     "primary":false,
///     "active":true,
///     "available_on_consumer":true,
///     "destination_tag_name":"STX Memo",
///     "destination_tag_regex":"^.{0,34}$",
///     "wire_deposit_information":
///     {
///         "account_number":null,
///         "routing_number":"021214891",
///         "bank_name":"Cross River Bank",
///         "bank_address":"885 Teaneck Road, Teaneck, NJ 07666",
///         "bank_country":
///         {
///             "code":"US",
///             "name":"United States"
///         },
///         "account_name":"Coinbase Inc",
///         "account_address":"100 Pine Street, Suite 1250, San Francisco, CA 94111",
///         "reference":"AIXNTKRQEXC"
///     },
///     "swift_deposit_information":null,
///     "hold_balance":"0.00",
///     "hold_currency":"USD"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct Wallet {
    pub id: String,
    pub name: String,
    pub balance: String,
    pub currency: String,
    #[serde(rename = "type")]
    pub type_string: String,
    pub primary: bool,
    pub active: bool,
    pub available_on_consumer: bool,
    pub ready: Option<bool>,
    pub destination_tag_name: Option<String>,
    pub destination_tag_regex: Option<String>,
    pub wire_deposit_information: Option<WireDepositInformation>,
    pub swift_deposit_information: Option<SwiftDepositInformation>,
    pub sepa_deposit_information: Option<SepaDepositInformation>,
    pub uk_deposit_information: Option<UKDepositInformation>,
    #[serde(with = "string_as_float")]
    pub hold_balance: f64,
    pub hold_currency: String,
}

///# Wire Deposit Information Data
/// A strongly typed representation of the wire deposit detail data within the [Wallet] struct.
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "account_number":null,
///     "routing_number":"021214891",
///     "bank_name":"Cross River Bank",
///     "bank_address":"885 Teaneck Road, Teaneck, NJ 07666",
///     "bank_country":
///     {
///         "code":"US",
///         "name":"United States"
///     },
///     "account_name":"Coinbase Inc",
///     "account_address":"100 Pine Street, Suite 1250, San Francisco, CA 94111",
///     "reference":"AIXNTKRQEXC"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct WireDepositInformation {
    pub account_number: Option<String>,
    pub routing_number: String,
    pub bank_name: String,
    pub bank_address: String,
    pub bank_country: BankCountry,
    pub account_name: String,
    pub account_address: String,
    pub reference: String,
}

///# Swift Deposit Information Data
/// A strongly typed representation of the SWIFT deposit detail data within the [Wallet] struct.
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "account_number":null,
///     "bank_name":"Cross River Bank",
///     "bank_address":"885 Teaneck Road, Teaneck, NJ 07666",
///     "bank_country":
///     {
///         "code":"US",
///         "name":"United States"
///     },
///     "account_name":"Coinbase Inc",
///     "account_address":"100 Pine Street, Suite 1250, San Francisco, CA 94111",
///     "reference":"AIXNTKRQEXC"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct SwiftDepositInformation {
    pub account_number: String,
    pub bank_name: String,
    pub bank_address: String,
    pub bank_country: BankCountry,
    pub account_name: String,
    pub account_address: String,
    pub reference: String,
}

///# Sepa Deposit Information Data
/// A strongly typed representation of the Sepa deposit detail data within the [Wallet] struct.
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "iban": "iban-string",
///     "swift": "swift-string",
///     "bank_name":"Cross River Bank",
///     "bank_address":"885 Teaneck Road, Teaneck, NJ 07666",
///     "bank_country":
///     {
///         "code":"US",
///         "name":"United States"
///     },
///     "account_name":"Coinbase Inc",
///     "account_address":"100 Pine Street, Suite 1250, San Francisco, CA 94111",
///     "reference":"AIXNTKRQEXC"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct SepaDepositInformation {
    pub iban: String,
    pub swift: String,
    pub bank_name: String,
    pub bank_address: String,
    pub bank_country: BankCountry,
    pub account_name: String,
    pub account_address: String,
    pub reference: String,
}

///# UK Deposit Information Data
/// A strongly typed representation of the Sepa deposit detail data within the [Wallet] struct.
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "sort_code": "sort_code-string",
///     "account_number": "account_number-string",
///     "bank_name":"Cross River Bank",
///     "account_name":"Coinbase Inc",
///     "reference":"AIXNTKRQEXC"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct UKDepositInformation {
    pub sort_code: String,
    pub account_number: String,
    pub bank_name: String,
    pub account_name: String,
    pub reference: String,
}

///# Bank Country Data
/// A strongly typed representation of the bank country data within the [Wallet] struct.
///
/// CBPro API reference: [Wallets](https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcoinbaseaccounts).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     "code":"US",
///     "name":"United States"
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct BankCountry {
    pub code: String,
    pub name: String,
}

/// # Fee Data
/// A strongly typed representation of the data returned by [/fees](https://api.exchange.coinbase.com/fees).
///
/// CBPro API reference: [Fees](https://api.exchange.coinbase.com/fees).
///
///
/// # JSON Input Example
///
/// ```ignore
/// {
///     taker_fee_rate: "0.006",
///     maker_fee_rate: "0.004",
///     usd_volume: "2854.82",
/// }
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Fees {
    #[serde(with = "string_as_float")]
    pub taker_fee_rate: f64,
    #[serde(with = "string_as_float")]
    pub maker_fee_rate: f64,
    #[serde(with = "string_as_float")]
    pub usd_volume: f64,
}