coinbase_v3/
lib.rs

1//! This crates implements Rust bindings for Coinbase Advanced Trade API (v3).
2//!
3//! This library mostly provides data bindings for the json responses and
4//! the GET/POST functions defined in the
5//! [API Reference](https://docs.cloud.coinbase.com/advanced-trade-api/reference).
6//! Coinbase's Advanced Trade API description can be found
7//! [there](https://docs.cloud.coinbase.com/advanced-trade-api/docs/welcome).
8//!
9//! In addition this crates provides:
10//!   - A client based on [reqwest](https://docs.rs/reqwest/latest/reqwest/)
11//!   expecting a Oauth2 token provider.
12//!   - A basic OAuth2 token provider based on [oauth2](https://docs.rs/oauth2/4.4.1/oauth2/).
13//!
14//! Notes:
15//!   - The OAuth2 token provider is basic and it may be replaced
16//!   by a fancier one implementing the [`basic_oauth::AccessTokenProvider`] trait.
17//!   - In particular, it is not taking care of the Access Token by periodically
18//!   sending the Refresh Token to the server.
19//!
20//!  ## Warning
21//!
22//! Use these bindings at your own risk. You may want to review the source code
23//! before.
24//!
25//! # Usage
26//!
27//! Most if not all API calls have examples attached to them. They can be found
28//! on the [github repo](https://github.com/mpbl/coinbase-v3/tree/main/examples),
29//! following more or less the structure of Coinbase's Advanced API documentation
30//! (accounts, products, orders, fees), splitted between GET and POST request to
31//! avoid creating orders you may regret.
32//!
33//! ## Example: Getting a single product
34//!
35//! ```no_run
36//! # use coinbase_v3::{basic_oauth, client, utils};
37//!
38//! #[tokio::main]
39//! async fn main() {
40//!     // Set up the Oauth2 Client -- can be a different one
41//!     let (client_id, client_secret, redirect_url) = utils::get_env_variables();
42//!     let oauth_cb_client = basic_oauth::OAuthCbClient::new(&client_id, &client_secret, &redirect_url)
43//!         .add_scope("wallet:user:read")
44//!         .authorize_once()
45//!         .await;
46//!
47//!     // Create the client
48//!     let cb_client = client::CbClient::new(&oauth_cb_client);
49//!
50//!     // Make the request for the product of interest
51//!     let product_id = "OGN-BTC";
52//!     let product = cb_client.get_product(product_id).await.unwrap();
53//!
54//!     // Use the result in some fashion
55//!     //....
56//!
57//!     // You may want to revoke the token access for increased security
58//!     // by default it should have a lifetime of 2 hours.
59//!     oauth_cb_client.revoke_access().await;
60//! }
61//!```
62//!
63//! ## Example: Getting a list of accounts
64//!
65//! Some API calls allow for pagination, for instance when listing accounts.
66//!
67//! ```no_run
68//! use coinbase_v3::{accounts, basic_oauth, client, utils};
69//! pub(crate) use futures::{pin_mut, stream::StreamExt};
70//!
71//! #[tokio::main]
72//! async fn main() {
73//!     // Same as above
74//!     let (client_id, client_secret, redirect_url) = utils::get_env_variables();
75//!     let oauth_cb_client = basic_oauth::OAuthCbClient::new(&client_id, &client_secret, &redirect_url)
76//!         .add_scope("wallet:accounts:read")
77//!         .authorize_once()
78//!         .await;
79//!     let cb_client = client::CbClient::new(&oauth_cb_client);
80//!
81//!     // Request to list accounts
82//!     let limit = Some(4); // only 4 accounts at a time to better demonstrate pagination
83//!     let accounts_stream = cb_client.list_accounts(limit, None);
84//!     pin_mut!(accounts_stream);
85//!
86//!     // Here we store all accounts in a Vector, but it is not mandatory.
87//!     let mut accounts = Vec::<accounts::Account>::new();
88//!
89//!     // Next step: iterate, 4 accounts at a time until there are no more.
90//!     while let Some(account_result) = accounts_stream.next().await {
91//!         let mut partial_accounts = account_result.unwrap();
92//!         println!("Got {} accounts", partial_accounts.len()); // Should give you 4 or less
93//!         // store the retrieved accounts, or do whatever.
94//!         accounts.append(&mut partial_accounts);
95//!     }
96//!     
97//!     println!("Got {} accounts in total.", accounts.len());
98//!
99//!     // Same
100//!     oauth_cb_client.revoke_access().await;
101//! }
102
103// ================ Libary modules ============================================
104pub mod accounts;
105pub mod basic_oauth;
106pub mod client;
107pub mod error;
108pub mod fees;
109pub mod orders;
110pub mod products;
111pub mod scopes;
112pub mod utils;
113
114// ================ Libary wide variables =====================================
115/// Base URL for Coinbase's v3 API.
116pub const MAIN_URL: &str = "https://api.coinbase.com/api/v3";
117
118/// Ensure DateTime is exclusively referring to UTC.
119pub type DateTime = chrono::DateTime<chrono::Utc>;