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
//! Common types used throughout the Ostium SDK
//!
//! This module defines the core data structures for trading, market data,
//! and account management.

use alloy_primitives::{Address, U256};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Trading pair information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TradingPair {
    /// Unique identifier for the pair
    pub id: String,
    /// Base asset symbol (e.g., "BTC")
    pub base_asset: String,
    /// Quote asset symbol (e.g., "USD")
    pub quote_asset: String,
    /// Full pair symbol (e.g., "BTC/USD")
    pub symbol: String,
    /// Whether the pair is currently active for trading
    pub is_active: bool,
    /// Minimum position size
    pub min_position_size: Decimal,
    /// Maximum position size
    pub max_position_size: Decimal,
    /// Price precision (decimal places)
    pub price_precision: u8,
    /// Quantity precision (decimal places)
    pub quantity_precision: u8,
}

/// Price information for a trading pair
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Price {
    /// Trading pair symbol
    pub symbol: String,
    /// Current mark price
    pub mark_price: Decimal,
    /// Current index price
    pub index_price: Decimal,
    /// 24h high price
    pub high_24h: Decimal,
    /// 24h low price
    pub low_24h: Decimal,
    /// 24h volume in quote currency
    pub volume_24h: Decimal,
    /// Timestamp of the price update
    pub timestamp: DateTime<Utc>,
}

/// Trading hours information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TradingHours {
    /// Trading pair symbol
    pub symbol: String,
    /// Whether trading is currently open
    pub is_open: bool,
    /// Next opening time (if currently closed)
    pub next_open: Option<DateTime<Utc>>,
    /// Next closing time (if currently open)
    pub next_close: Option<DateTime<Utc>>,
}

/// Position side (Long or Short)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PositionSide {
    /// Long position (buy)
    Long,
    /// Short position (sell)
    Short,
}

/// Order type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderType {
    /// Market order
    Market,
    /// Limit order
    Limit,
    /// Stop market order
    StopMarket,
    /// Stop limit order
    StopLimit,
}

/// Order status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderStatus {
    /// Order is pending execution
    Pending,
    /// Order is partially filled
    PartiallyFilled,
    /// Order is completely filled
    Filled,
    /// Order was cancelled
    Cancelled,
    /// Order was rejected
    Rejected,
}

/// Trading position
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Position {
    /// Unique position ID
    pub id: String,
    /// Trading pair symbol
    pub symbol: String,
    /// Position side (Long/Short)
    pub side: PositionSide,
    /// Position size
    pub size: Decimal,
    /// Average entry price
    pub entry_price: Decimal,
    /// Current mark price
    pub mark_price: Decimal,
    /// Unrealized PnL
    pub unrealized_pnl: Decimal,
    /// Realized PnL
    pub realized_pnl: Decimal,
    /// Margin used
    pub margin: Decimal,
    /// Leverage
    pub leverage: Decimal,
    /// Liquidation price
    pub liquidation_price: Option<Decimal>,
    /// Take profit price
    pub take_profit: Option<Decimal>,
    /// Stop loss price
    pub stop_loss: Option<Decimal>,
    /// Position creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

/// Trading order
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Order {
    /// Unique order ID
    pub id: String,
    /// Trading pair symbol
    pub symbol: String,
    /// Order type
    pub order_type: OrderType,
    /// Position side
    pub side: PositionSide,
    /// Order size
    pub size: Decimal,
    /// Order price (for limit orders)
    pub price: Option<Decimal>,
    /// Stop price (for stop orders)
    pub stop_price: Option<Decimal>,
    /// Order status
    pub status: OrderStatus,
    /// Filled size
    pub filled_size: Decimal,
    /// Average fill price
    pub avg_fill_price: Option<Decimal>,
    /// Order creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last update timestamp
    pub updated_at: DateTime<Utc>,
}

/// Account balance information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Balance {
    /// Asset symbol (e.g., "USDC")
    pub asset: String,
    /// Available balance
    pub available: Decimal,
    /// Balance locked in orders/positions
    pub locked: Decimal,
    /// Total balance (available + locked)
    pub total: Decimal,
}

/// Open interest information for a trading pair
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenInterestInfo {
    /// Trading pair symbol
    pub symbol: String,
    /// Current total open interest in USD
    pub total_open_interest: Decimal,
    /// Current long open interest in USD
    pub long_open_interest: Decimal,
    /// Current short open interest in USD
    pub short_open_interest: Decimal,
    /// Maximum allowed open interest in USD
    pub max_open_interest: Decimal,
    /// Current utilization percentage (0-100)
    pub utilization_percent: Decimal,
    /// Whether new positions are currently blocked
    pub is_capped: bool,
}

/// Open interest cap validation result
#[derive(Debug, Clone)]
pub struct OpenInterestCapResult {
    /// Whether the position would exceed the cap
    pub would_exceed_cap: bool,
    /// Current utilization before the trade
    pub current_utilization: Decimal,
    /// Projected utilization after the trade
    pub projected_utilization: Decimal,
    /// Available capacity in USD
    pub available_capacity: Decimal,
    /// Recommended maximum position size
    pub max_recommended_size: Decimal,
}

/// Transaction hash type
pub type TxHash = alloy_primitives::TxHash;

/// Parameters for opening a new position
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenPositionParams {
    /// Trading pair symbol
    pub symbol: String,
    /// Position side
    pub side: PositionSide,
    /// Position size
    pub size: Decimal,
    /// Leverage to use
    pub leverage: Decimal,
    /// Optional take profit price
    pub take_profit: Option<Decimal>,
    /// Optional stop loss price
    pub stop_loss: Option<Decimal>,
    /// Slippage tolerance (as decimal, e.g., 0.01 for 1%)
    pub slippage_tolerance: Decimal,
}

/// Advanced parameters for opening positions with specific order types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AdvancedOrderParams {
    /// Trading pair symbol
    pub symbol: String,
    /// Position side
    pub side: PositionSide,
    /// Position size
    pub size: Decimal,
    /// Leverage to use
    pub leverage: Decimal,
    /// Order type to use
    pub order_type: OrderExecutionType,
    /// Price for limit/stop orders
    pub price: Option<Decimal>,
    /// Optional take profit price
    pub take_profit: Option<Decimal>,
    /// Optional stop loss price
    pub stop_loss: Option<Decimal>,
    /// Slippage tolerance (as decimal, e.g., 0.01 for 1%)
    pub slippage_tolerance: Decimal,
}

/// Order execution type for advanced orders
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderExecutionType {
    /// Market order - execute immediately at current market price
    Market,
    /// Limit order - execute when price reaches or is better than specified price
    Limit,
    /// Stop order - execute when price crosses the stop level
    Stop,
}

/// Parameters for placing a limit order
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LimitOrderParams {
    /// Trading pair symbol
    pub symbol: String,
    /// Position side
    pub side: PositionSide,
    /// Position size
    pub size: Decimal,
    /// Leverage to use
    pub leverage: Decimal,
    /// Limit price for execution
    pub limit_price: Decimal,
    /// Optional take profit price
    pub take_profit: Option<Decimal>,
    /// Optional stop loss price
    pub stop_loss: Option<Decimal>,
}

/// Parameters for placing a stop order
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StopOrderParams {
    /// Trading pair symbol
    pub symbol: String,
    /// Position side
    pub side: PositionSide,
    /// Position size
    pub size: Decimal,
    /// Leverage to use
    pub leverage: Decimal,
    /// Stop price that triggers the order
    pub stop_price: Decimal,
    /// Optional take profit price
    pub take_profit: Option<Decimal>,
    /// Optional stop loss price
    pub stop_loss: Option<Decimal>,
}

/// Parameters for canceling an order
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CancelOrderParams {
    /// Order ID to cancel (format: "trader:pair_index:index")
    pub order_id: String,
}

/// Parameters for updating an existing limit order
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpdateLimitOrderParams {
    /// Order ID to update (format: "trader:pair_index:index")
    pub order_id: String,
    /// New limit price (None = keep current)
    pub limit_price: Option<Decimal>,
    /// New take profit price (None = keep current)
    pub take_profit: Option<Decimal>,
    /// New stop loss price (None = keep current)
    pub stop_loss: Option<Decimal>,
}

/// Parameters for closing a position
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ClosePositionParams {
    /// Position ID to close
    pub position_id: String,
    /// Size to close (None = close entire position)
    pub size: Option<Decimal>,
    /// Slippage tolerance (as decimal, e.g., 0.01 for 1%)
    pub slippage_tolerance: Decimal,
}

/// Parameters for updating take profit and stop loss
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UpdateTPSLParams {
    /// Position ID to update
    pub position_id: String,
    /// New take profit price (None = remove TP)
    pub take_profit: Option<Decimal>,
    /// New stop loss price (None = remove SL)
    pub stop_loss: Option<Decimal>,
}

/// Type of order being placed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OpenOrderType {
    /// Market order
    Market,
    /// Limit order
    Limit,
    /// Stop order
    Stop,
}

/// Trade information for opening a position
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Trade {
    /// Collateral amount in USDC (with 6 decimals)
    pub collateral: U256,
    /// Open price (with 18 decimals)
    pub open_price: u128,
    /// Take profit price (with 18 decimals)
    pub tp: u128,
    /// Stop loss price (with 18 decimals)
    pub sl: u128,
    /// Trader address
    pub trader: Address,
    /// Leverage (with 2 decimals, e.g., 500 = 5x)
    pub leverage: u32,
    /// Pair index
    pub pair_index: u16,
    /// Position index
    pub index: u8,
    /// True for long, false for short
    pub buy: bool,
}

/// Open limit order information
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenLimitOrder {
    /// Collateral amount in USDC (with 6 decimals)
    pub collateral: U256,
    /// Target price to execute at (with 18 decimals)
    pub target_price: u128,
    /// Take profit price (with 18 decimals)
    pub tp: u128,
    /// Stop loss price (with 18 decimals)
    pub sl: u128,
    /// Trader address
    pub trader: Address,
    /// Leverage (with 2 decimals, e.g., 500 = 5x)
    pub leverage: u32,
    /// Creation timestamp
    pub created_at: u32,
    /// Last update timestamp
    pub last_updated: u32,
    /// Pair index
    pub pair_index: u16,
    /// Order type (0 = market, 1 = limit, 2 = stop)
    pub order_type: u8,
    /// Position index
    pub index: u8,
    /// True for long, false for short
    pub buy: bool,
}

/// Unsigned transaction data that can be sent to frontend for signing
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnsignedTransaction {
    /// Contract address to call
    pub to: Address,
    /// Transaction data (encoded function call)
    pub data: Vec<u8>,
    /// Value to send (usually 0 for contract calls)
    pub value: U256,
    /// Estimated gas limit
    pub gas_limit: Option<U256>,
    /// Gas price estimate
    pub gas_price: Option<U256>,
    /// Chain ID
    pub chain_id: u64,
    /// Nonce (if known)
    pub nonce: Option<u64>,
}

/// Parameters for building unsigned transactions
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnsignedTransactionParams {
    /// Address that will sign and send the transaction
    pub from: Address,
    /// Whether to include gas estimates
    pub include_gas_estimates: bool,
    /// Whether to include nonce
    pub include_nonce: bool,
}