ostium-rust-sdk 0.1.0

Rust SDK for interacting with the Ostium trading platform on Arbitrum
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Account API Reference

This document provides complete reference documentation for account management operations in the Ostium Rust SDK.

## Overview

The account API enables you to:
- Query account balances (USDC and other tokens)
- Retrieve open positions with PnL calculations
- Access pending orders and order history
- Monitor account performance metrics
- Check margin requirements and utilization

All account operations can be performed with or without authentication, depending on whether you're querying your own account or a public address.

## Balance Operations

### Getting Account Balance

#### `get_balance`

Retrieves comprehensive account balance information.

```rust
pub async fn get_balance(&self, address: Option<Address>) -> Result<Balance>
```

**Parameters:**
- `address: Option<Address>` - Account address to query (uses signer address if None)

**Returns:**
- `Result<Balance>` - Complete balance information

**Balance Structure:**
```rust
pub struct Balance {
    pub asset: String,      // Asset symbol (e.g., "USDC")
    pub available: Decimal, // Available for trading
    pub locked: Decimal,    // Locked in orders/positions
    pub total: Decimal,     // Total balance (available + locked)
}
```

**Example:**
```rust
// Query your own balance (requires authenticated client)
let my_balance = client.get_balance(None).await?;
println!("My USDC Balance:");
println!("  Total: ${}", my_balance.total);
println!("  Available: ${}", my_balance.available);
println!("  Locked: ${}", my_balance.locked);

// Query another account's balance
let other_address = "0x742d35Cc6634C0532925a3b8D53A2e1c5Bc2D8db".parse()?;
let other_balance = client.get_balance(Some(other_address)).await?;
println!("Other account balance: ${}", other_balance.total);
```

**Error Handling:**
```rust
match client.get_balance(None).await {
    Ok(balance) => {
        println!("Balance: ${}", balance.total);
    }
    Err(OstiumError::Wallet(msg)) => {
        eprintln!("No signer configured: {}", msg);
        // Need to provide address explicitly or configure signer
    }
    Err(OstiumError::Network(msg)) => {
        eprintln!("Network error: {}", msg);
        // Retry or check connection
    }
    Err(e) => eprintln!("Error: {}", e),
}
```

## Position Management

### Getting Open Positions

#### `get_positions`

Retrieves all open positions for an account with current market values.

```rust
pub async fn get_positions(&self, address: Option<Address>) -> Result<Vec<Position>>
```

**Parameters:**
- `address: Option<Address>` - Account address (uses signer address if None)

**Returns:**
- `Result<Vec<Position>>` - List of open positions

**Position Structure:**
```rust
pub struct Position {
    pub id: String,                     // Unique position identifier
    pub symbol: String,                 // Trading pair (e.g., "BTC/USD")
    pub side: PositionSide,            // Long or Short
    pub size: Decimal,                 // Position size
    pub entry_price: Decimal,          // Average entry price
    pub mark_price: Decimal,           // Current mark price
    pub unrealized_pnl: Decimal,       // Unrealized profit/loss
    pub realized_pnl: Decimal,         // Realized profit/loss
    pub margin: Decimal,               // Margin used
    pub leverage: Decimal,             // Position leverage
    pub liquidation_price: Option<Decimal>, // Liquidation price
    pub take_profit: Option<Decimal>,  // Take profit price
    pub stop_loss: Option<Decimal>,    // Stop loss price
    pub created_at: DateTime<Utc>,     // Position creation time
    pub updated_at: DateTime<Utc>,     // Last update time
}
```

**Example:**
```rust
let positions = client.get_positions(None).await?;

if positions.is_empty() {
    println!("No open positions");
} else {
    println!("Open Positions:");
    for position in positions {
        println!("  {} {} {} @ ${} ({}x leverage)", 
                 position.symbol,
                 match position.side {
                     PositionSide::Long => "Long",
                     PositionSide::Short => "Short",
                 },
                 position.size,
                 position.entry_price,
                 position.leverage);
        
        // Show PnL with color coding (conceptual)
        let pnl_status = if position.unrealized_pnl > Decimal::ZERO {
            "🟢 PROFIT"
        } else if position.unrealized_pnl < Decimal::ZERO {
            "🔴 LOSS"
        } else {
            "⚪ BREAKEVEN"
        };
        
        println!("    Current Price: ${}", position.mark_price);
        println!("    Unrealized PnL: ${} ({})", position.unrealized_pnl, pnl_status);
        
        if let Some(liq_price) = position.liquidation_price {
            println!("    Liquidation Price: ${}", liq_price);
        }
        
        if let Some(tp) = position.take_profit {
            println!("    Take Profit: ${}", tp);
        }
        
        if let Some(sl) = position.stop_loss {
            println!("    Stop Loss: ${}", sl);
        }
        
        println!(); // Empty line for readability
    }
}
```

### Position Analysis

```rust
// Calculate total portfolio metrics
fn analyze_portfolio(positions: &[Position]) -> PortfolioMetrics {
    let total_margin = positions.iter()
        .map(|p| p.margin)
        .sum::<Decimal>();
    
    let total_unrealized_pnl = positions.iter()
        .map(|p| p.unrealized_pnl)
        .sum::<Decimal>();
    
    let total_realized_pnl = positions.iter()
        .map(|p| p.realized_pnl)
        .sum::<Decimal>();
    
    let long_positions = positions.iter()
        .filter(|p| p.side == PositionSide::Long)
        .count();
    
    let short_positions = positions.iter()
        .filter(|p| p.side == PositionSide::Short)
        .count();
    
    PortfolioMetrics {
        total_margin,
        total_unrealized_pnl,
        total_realized_pnl,
        position_count: positions.len(),
        long_positions,
        short_positions,
    }
}

struct PortfolioMetrics {
    total_margin: Decimal,
    total_unrealized_pnl: Decimal,
    total_realized_pnl: Decimal,
    position_count: usize,
    long_positions: usize,
    short_positions: usize,
}
```

## Order Management

### Getting Open Orders

#### `get_orders`

Retrieves all pending orders for an account.

```rust
pub async fn get_orders(&self, address: Option<Address>) -> Result<Vec<Order>>
```

**Parameters:**
- `address: Option<Address>` - Account address (uses signer address if None)

**Returns:**
- `Result<Vec<Order>>` - List of pending orders

**Order Structure:**
```rust
pub struct Order {
    pub id: String,                    // Unique order identifier
    pub symbol: String,                // Trading pair
    pub order_type: OrderType,         // Order type (Market, Limit, etc.)
    pub side: PositionSide,           // Long or Short
    pub size: Decimal,                // Order size
    pub price: Option<Decimal>,       // Order price (for limit orders)
    pub stop_price: Option<Decimal>,  // Stop price (for stop orders)
    pub status: OrderStatus,          // Current status
    pub filled_size: Decimal,         // Amount already filled
    pub avg_fill_price: Option<Decimal>, // Average fill price
    pub created_at: DateTime<Utc>,    // Order creation time
    pub updated_at: DateTime<Utc>,    // Last update time
}
```

**Example:**
```rust
let orders = client.get_orders(None).await?;

if orders.is_empty() {
    println!("No pending orders");
} else {
    println!("Pending Orders:");
    for order in orders {
        println!("  {} {} {} {} @ ${}", 
                 order.symbol,
                 match order.order_type {
                     OrderType::Market => "Market",
                     OrderType::Limit => "Limit",
                     OrderType::StopMarket => "Stop Market",
                     OrderType::StopLimit => "Stop Limit",
                 },
                 match order.side {
                     PositionSide::Long => "Long",
                     PositionSide::Short => "Short",
                 },
                 order.size,
                 order.price.unwrap_or_default());
        
        println!("    Status: {:?}", order.status);
        
        if order.filled_size > Decimal::ZERO {
            println!("    Filled: {} / {}", order.filled_size, order.size);
        }
        
        if let Some(avg_price) = order.avg_fill_price {
            println!("    Avg Fill Price: ${}", avg_price);
        }
        
        println!(); // Empty line
    }
}
```

## Account Monitoring

### Real-time Account Updates

```rust
// Monitor account changes
async fn monitor_account_changes(client: &OstiumClient) -> Result<()> {
    let mut last_balance = client.get_balance(None).await?;
    let mut last_position_count = client.get_positions(None).await?.len();
    
    loop {
        tokio::time::sleep(Duration::from_secs(10)).await;
        
        // Check balance changes
        let current_balance = client.get_balance(None).await?;
        if current_balance.total != last_balance.total {
            println!("💰 Balance changed: ${} -> ${}", 
                     last_balance.total, current_balance.total);
            last_balance = current_balance;
        }
        
        // Check position changes
        let current_positions = client.get_positions(None).await?;
        if current_positions.len() != last_position_count {
            println!("📊 Position count changed: {} -> {}", 
                     last_position_count, current_positions.len());
            last_position_count = current_positions.len();
        }
        
        // Check for positions near liquidation
        for position in &current_positions {
            if let Some(liq_price) = position.liquidation_price {
                let distance_to_liq = ((position.mark_price - liq_price).abs() / position.mark_price) * Decimal::from(100);
                
                if distance_to_liq < Decimal::from(5) { // Within 5% of liquidation
                    println!("⚠️  WARNING: {} position near liquidation! Distance: {}%", 
                             position.symbol, distance_to_liq);
                }
            }
        }
    }
}
```

### Portfolio Summary

```rust
// Generate comprehensive portfolio summary
async fn get_portfolio_summary(client: &OstiumClient) -> Result<PortfolioSummary> {
    let balance = client.get_balance(None).await?;
    let positions = client.get_positions(None).await?;
    let orders = client.get_orders(None).await?;
    
    let total_margin_used = positions.iter()
        .map(|p| p.margin)
        .sum::<Decimal>();
    
    let total_unrealized_pnl = positions.iter()
        .map(|p| p.unrealized_pnl)
        .sum::<Decimal>();
    
    let margin_utilization = if balance.total > Decimal::ZERO {
        (total_margin_used / balance.total) * Decimal::from(100)
    } else {
        Decimal::ZERO
    };
    
    Ok(PortfolioSummary {
        account_value: balance.total + total_unrealized_pnl,
        available_balance: balance.available,
        margin_used: total_margin_used,
        unrealized_pnl: total_unrealized_pnl,
        margin_utilization,
        open_positions: positions.len(),
        pending_orders: orders.len(),
        positions,
        orders,
    })
}

struct PortfolioSummary {
    account_value: Decimal,
    available_balance: Decimal,
    margin_used: Decimal,
    unrealized_pnl: Decimal,
    margin_utilization: Decimal, // Percentage
    open_positions: usize,
    pending_orders: usize,
    positions: Vec<Position>,
    orders: Vec<Order>,
}
```

## Risk Management

### Margin Calculations

```rust
// Calculate margin requirements
fn calculate_margin_requirement(
    position_size: Decimal,
    price: Decimal,
    leverage: Decimal,
) -> Decimal {
    (position_size * price) / leverage
}

// Check if account can open new position
async fn can_open_position(
    client: &OstiumClient,
    params: &OpenPositionParams,
) -> Result<bool> {
    let balance = client.get_balance(None).await?;
    let current_price = client.get_price(&params.symbol).await?.mark_price;
    
    let required_margin = calculate_margin_requirement(
        params.size,
        current_price,
        params.leverage,
    );
    
    Ok(balance.available >= required_margin)
}
```

### Account Health Monitoring

```rust
// Monitor account health metrics
#[derive(Debug)]
struct AccountHealth {
    margin_ratio: Decimal,        // Available margin / Used margin
    liquidation_distance: Decimal, // Average distance to liquidation
    position_concentration: Decimal, // Largest position as % of account
    risk_score: RiskLevel,       // Overall risk assessment
}

enum RiskLevel {
    Low,     // < 25% margin utilization
    Medium,  // 25-50% margin utilization
    High,    // 50-75% margin utilization
    Critical, // > 75% margin utilization
}

async fn assess_account_health(client: &OstiumClient) -> Result<AccountHealth> {
    let balance = client.get_balance(None).await?;
    let positions = client.get_positions(None).await?;
    
    let total_margin = positions.iter().map(|p| p.margin).sum::<Decimal>();
    let margin_ratio = if total_margin > Decimal::ZERO {
        balance.available / total_margin
    } else {
        Decimal::MAX
    };
    
    // Calculate average distance to liquidation
    let mut total_distance = Decimal::ZERO;
    let mut count = 0;
    
    for position in &positions {
        if let Some(liq_price) = position.liquidation_price {
            let distance = ((position.mark_price - liq_price).abs() / position.mark_price) * Decimal::from(100);
            total_distance += distance;
            count += 1;
        }
    }
    
    let liquidation_distance = if count > 0 {
        total_distance / Decimal::from(count)
    } else {
        Decimal::from(100) // No positions = no liquidation risk
    };
    
    // Find largest position as percentage of account
    let largest_position = positions.iter()
        .map(|p| p.margin)
        .max()
        .unwrap_or_default();
    
    let position_concentration = if balance.total > Decimal::ZERO {
        (largest_position / balance.total) * Decimal::from(100)
    } else {
        Decimal::ZERO
    };
    
    // Assess overall risk
    let margin_utilization = (total_margin / balance.total) * Decimal::from(100);
    let risk_score = match margin_utilization {
        u if u < Decimal::from(25) => RiskLevel::Low,
        u if u < Decimal::from(50) => RiskLevel::Medium,
        u if u < Decimal::from(75) => RiskLevel::High,
        _ => RiskLevel::Critical,
    };
    
    Ok(AccountHealth {
        margin_ratio,
        liquidation_distance,
        position_concentration,
        risk_score,
    })
}
```

## Best Practices

### 1. Regular Balance Monitoring

```rust
// Check balance before every trade
async fn safe_open_position(
    client: &OstiumClient,
    params: OpenPositionParams,
) -> Result<()> {
    // Check balance first
    let balance = client.get_balance(None).await?;
    let current_price = client.get_price(&params.symbol).await?.mark_price;
    let required_margin = (params.size * current_price) / params.leverage;
    
    if balance.available < required_margin {
        return Err(OstiumError::validation(format!(
            "Insufficient balance: need ${}, have ${}",
            required_margin, balance.available
        )));
    }
    
    // Proceed with trade
    client.open_position(params).await?;
    Ok(())
}
```

### 2. Position Size Management

```rust
// Calculate safe position size based on account balance
fn calculate_safe_position_size(
    account_balance: Decimal,
    price: Decimal,
    leverage: Decimal,
    max_risk_percent: Decimal, // e.g., 2% = 0.02
) -> Decimal {
    let max_margin = account_balance * max_risk_percent;
    (max_margin * leverage) / price
}
```

### 3. Error Handling

```rust
// Comprehensive error handling for account operations
async fn robust_account_query(client: &OstiumClient) -> Result<()> {
    match client.get_positions(None).await {
        Ok(positions) => {
            println!("Successfully retrieved {} positions", positions.len());
        }
        Err(OstiumError::Wallet(msg)) => {
            eprintln!("Authentication required: {}", msg);
            // Prompt user to configure private key
        }
        Err(OstiumError::Network(msg)) => {
            eprintln!("Network error: {}", msg);
            // Implement retry logic
        }
        Err(OstiumError::GraphQL(msg)) => {
            eprintln!("API error: {}", msg);
            // Check API status or report issue
        }
        Err(e) => {
            eprintln!("Unexpected error: {}", e);
        }
    }
    
    Ok(())
}
```

## See Also

- [Client API Reference]client.md - Main client interface
- [Trading API Reference]trading.md - Trading operations
- [Types Reference]types.md - Data structures
- [Risk Management Guide]../guides/risk-management.md - Risk management strategies