Skip to main content

grpc_client/
grpc_client.rs

1use openapi_rs::{OpenApiSdkClient, OrderSide};
2use rust_decimal::Decimal;
3
4fn d(value: i64, scale: u32) -> Decimal {
5    Decimal::new(value, scale)
6}
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let grpc_url = std::env::var("OPENAPI_GRPC_URL")?;
11    let mut client = OpenApiSdkClient::connect(grpc_url.clone()).await?;
12    println!("connected to {grpc_url}");
13
14    let token = client.login("alice", "secret").await?;
15
16    let quote = client.quote_volume("EURUSD", d(15, 2)).await?;
17    println!(
18        "quote => symbol={}, lot={}, volume={}",
19        quote.symbol, quote.lot_size, quote.volume
20    );
21
22    let execution = client
23        .place_market_order(token.clone(), "EURUSD", d(1, 2), OrderSide::Sell)
24        .await?;
25    println!(
26        "order => id={}, symbol={}, volume={}, margin={}",
27        execution.order_id, execution.symbol, execution.volume, execution.margin_used
28    );
29
30    let positions = client.fetch_open_positions(token.clone()).await?;
31    println!("open positions => {}", positions.len());
32
33    let balance = client.fetch_balance(token.clone()).await?;
34    println!(
35        "balance => total={}, used_margin={}, free_margin={}",
36        balance.balance, balance.used_margin, balance.free_margin
37    );
38
39    client.logout(token).await?;
40
41    Ok(())
42}