Skip to main content

sync_example/
sync_example.rs

1use gateio_rs::{
2    api::spot::{
3        Order, create_batch_orders, create_order, get_account, get_account_book,
4        get_batch_user_fee, get_currency_pair, get_currency_pairs, get_ticker,
5    },
6    http::Credentials,
7    ureq::GateHttpClient,
8};
9use serde_json::Value;
10
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    // 1) Create Credentials
13    // TODO: Replace with your actual API credentials or use environment variables
14    let api_key = "YOUR_GATE_API_KEY";
15    let api_secret = "YOUR_GATE_API_SECRET";
16    let credentials = Credentials::new(api_key.to_owned(), api_secret.to_owned());
17
18    // 2) Configure Client
19    let client = GateHttpClient::default().credentials(credentials);
20
21    // 3) Send sync request examples
22
23    // Example 1: Get ticker
24    println!("Getting ticker for BTC_USDT...");
25    let req = get_ticker().currency_pair("BTC_USDT").timezone("utc8");
26
27    let resp = client.send(req)?;
28    let body = resp.into_body_str()?;
29    let ticker_data: Value = serde_json::from_str(&body)?;
30    println!("Ticker: {}\n", serde_json::to_string_pretty(&ticker_data)?);
31
32    // Example 2: Get account information
33    println!("Getting account information...");
34    let req = get_account();
35
36    let resp = client.send(req)?;
37    let body = resp.into_body_str()?;
38    let account_data: Value = serde_json::from_str(&body)?;
39    println!(
40        "Account: {}\n",
41        serde_json::to_string_pretty(&account_data)?
42    );
43
44    // Example 3: Get currency pairs
45    println!("Getting currency pairs...");
46    let req = get_currency_pairs();
47
48    let resp = client.send(req)?;
49    let body = resp.into_body_str()?;
50    let pairs_data: Value = serde_json::from_str(&body)?;
51    println!(
52        "Found {} currency pairs\n",
53        pairs_data.as_array().map(|a| a.len()).unwrap_or(0)
54    );
55
56    // Example 4: Get specific currency pair
57    println!("Getting LTC_USDT currency pair info...");
58    let req = get_currency_pair("LTC_USDT");
59
60    let resp = client.send(req)?;
61    let body = resp.into_body_str()?;
62    let pair_data: Value = serde_json::from_str(&body)?;
63    println!(
64        "LTC_USDT info: {}\n",
65        serde_json::to_string_pretty(&pair_data)?
66    );
67
68    // Example 5: Create order
69    println!("Creating order...");
70    let req = create_order("LTC_USDT", "buy", "0.04").price("84.2");
71
72    let resp = client.send(req)?;
73    let body = resp.into_body_str()?;
74    let order_data: Value = serde_json::from_str(&body)?;
75    println!(
76        "Order created: {}\n",
77        serde_json::to_string_pretty(&order_data)?
78    );
79
80    // Example 6: Batch user fee
81    println!("Getting batch user fee...");
82    let req = get_batch_user_fee("BTC_USDT,ETH_USDT");
83
84    let resp = client.send(req)?;
85    let body = resp.into_body_str()?;
86    let fee_data: Value = serde_json::from_str(&body)?;
87    println!("Fees: {}\n", serde_json::to_string_pretty(&fee_data)?);
88
89    // Example 7: Account book
90    println!("Getting account book...");
91    let req = get_account_book().book_type("new_order");
92
93    let resp = client.send(req)?;
94    let body = resp.into_body_str()?;
95    let book_data: Value = serde_json::from_str(&body)?;
96    println!(
97        "Account book: {}\n",
98        serde_json::to_string_pretty(&book_data)?
99    );
100
101    // Example 8: Batch orders
102    println!("Creating batch orders...");
103    let order1 = Order::new("BTC_USDT", "buy", "0.001")
104        .text("t-abc123")
105        .order_type("limit")
106        .account("unified")
107        .price("65000")
108        .time_in_force("gtc")
109        .iceberg("0");
110
111    let order2 = Order::new("ETH_USDT", "buy", "0.01")
112        .text("t-def456")
113        .order_type("limit")
114        .account("unified")
115        .price("3000")
116        .time_in_force("gtc")
117        .iceberg("0");
118
119    let orders = vec![order1, order2];
120    let req = create_batch_orders(orders);
121
122    let resp = client.send(req)?;
123    let body = resp.into_body_str()?;
124    let batch_data: Value = serde_json::from_str(&body)?;
125    println!(
126        "Batch orders: {}\n",
127        serde_json::to_string_pretty(&batch_data)?
128    );
129
130    println!("All sync examples completed successfully!");
131    Ok(())
132}