run_all_examples/
get_orders.rs

1use futures::{pin_mut, stream::StreamExt};
2
3use coinbase_v3::{
4    basic_oauth::OAuthCbClient, client::CbClient, orders, products, utils, DateTime,
5};
6
7#[allow(dead_code)]
8#[tokio::main]
9async fn main() {
10    let (client_id, client_secret, redirect_url) = utils::get_env_variables();
11    let oauth_cb_client = OAuthCbClient::new(&client_id, &client_secret, &redirect_url)
12        .add_scope("wallet:transactions:read") // NOT wallet:orders:read as CB's doc says.
13        .authorize_once()
14        .await;
15
16    let cb_client = CbClient::new(&oauth_cb_client);
17    run_list_orders(&cb_client).await;
18    run_list_fills(&cb_client).await;
19
20    oauth_cb_client.revoke_access().await;
21}
22
23pub async fn run_list_orders(cb_client: &CbClient<'_>) {
24    let product_id: Option<String> = None;
25    let order_status: Option<Vec<orders::Status>> = None;
26    let limit: Option<i32> = Some(10);
27    let start_date: Option<DateTime> = None;
28    let end_date: Option<DateTime> = None;
29    let deprecated_user_native_currency: Option<String> = None;
30    let order_type: Option<orders::OrderType> = None;
31    let order_side: Option<orders::OrderSide> = Some(orders::OrderSide::Buy);
32    let cursor: Option<String> = None;
33    let product_type: Option<products::ProductType> = Some(products::ProductType::Spot);
34    let order_placement_source: Option<orders::OrderPlacementSource> = None;
35    let contract_expiry_type: Option<products::ContractExpiryType> = None;
36
37    let orders_stream = cb_client.list_orders(
38        product_id,
39        order_status,
40        limit,
41        start_date,
42        end_date,
43        deprecated_user_native_currency,
44        order_type,
45        order_side,
46        cursor,
47        product_type,
48        order_placement_source,
49        contract_expiry_type,
50    );
51    pin_mut!(orders_stream);
52
53    let mut orders = Vec::<orders::Order>::new();
54    while let Some(orders_result) = orders_stream.next().await {
55        let mut partial_orders = orders_result.unwrap();
56        println!("Got {} orders", partial_orders.len());
57        orders.append(&mut partial_orders);
58
59        if partial_orders.len() > 30 {
60            break;
61        }
62    }
63    println!("Got {} orders in total.", orders.len());
64
65    let order = cb_client.get_order(&orders[0].order_id).await.unwrap();
66    assert_eq!(orders[0], order);
67}
68
69pub async fn run_list_fills(cb_client: &CbClient<'_>) {
70    let limit = Some(10);
71    let fills_stream = cb_client.list_fills(None, None, None, None, limit, None);
72    pin_mut!(fills_stream);
73
74    let mut fills = Vec::<orders::Fill>::new();
75    while let Some(fills_result) = fills_stream.next().await {
76        let mut partial_fills = fills_result.unwrap();
77        println!("Got {} fills", partial_fills.len());
78        fills.append(&mut partial_fills);
79
80        if fills.len() > 30 {
81            break;
82        }
83    }
84    println!("\nFills\n------------\n{:#?}", fills.len());
85}