Ostium Rust SDK
A modern, type-safe Rust SDK for interacting with the Ostium trading platform on Arbitrum. Built with Alloy for robust Ethereum interactions and featuring async/await support.
Features
- 🦀 Pure Rust Implementation - Built with Rust for performance and safety
- 🔐 Type-Safe Contract Interactions - Leveraging Alloy for Ethereum interactions
- ⚡ Async/Await Support - Built on Tokio for efficient async operations
- 🌐 GraphQL Integration - Direct access to Ostium's GraphQL API
- 🔧 Flexible Configuration - Support for mainnet, testnet, and custom networks
- 📊 Comprehensive Trading Features - Full support for perpetual trading operations
- 🛡️ Comprehensive error handling with detailed error types
- 🌐 Multi-network support (Mainnet & Testnet)
- 💰 Precise decimal arithmetic for financial calculations
- 📚 Extensive documentation and examples
Installation
Add the SDK to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
= "1.36"
Or using cargo:
Quick Start
Basic Usage (Read-only)
use ;
async
Trading Operations
use ;
use dec;
async
Architecture
The SDK is organized into several key modules:
client- Main client interface and builder patterncontracts- Smart contract wrappers (Trading, Storage, USDC)config- Network configuration and endpointstypes- Core data structures and enumserror- Comprehensive error handling
Build System & ABI Management
The SDK uses an automated build script (build.rs) that ensures contract ABIs stay synchronized with the Ostium Python SDK:
- Automatic ABI Fetching: Downloads the latest contract ABIs from the Ostium Python SDK
- Python to JSON Conversion: Converts Python ABI format to JSON for use with Rust web3 libraries
- Type-Safe Bindings: Generates Rust modules with ABI constants for compile-time safety
- Smart Change Detection: Only updates files when ABIs have actually changed
- Efficient Builds: Skips network requests during normal builds unless explicitly requested
Updating Contract ABIs
# Force update ABIs from Python SDK
# Or delete generated files to trigger update
&&
The build script generates files in src/abi/generated/:
- Individual contract modules (e.g.,
trading.rs,storage.rs) - Module organization file (
mod.rs) - ABI constants accessible as
{CONTRACT_NAME}_ABI
Contract Addresses
Mainnet (Arbitrum One)
- USDC:
0xaf88d065e77c8cC2239327C5EDb3A432268e5831 - Trading:
0x6D0bA1f9996DBD8885827e1b2e8f6593e7702411 - Storage:
0xcCd5891083A8acD2074690F65d3024E7D13d66E7
Testnet (Arbitrum Sepolia)
- USDC:
0xe73B11Fb1e3eeEe8AF2a23079A4410Fe1B370548 - Trading:
0x2A9B9c988393f46a2537B0ff11E98c2C15a95afe - Storage:
0x0b9F5243B29938668c9Cfbd7557A389EC7Ef88b8
API Reference
Client Creation
// Simple client (read-only)
let client = new.await?;
// Client with private key (for trading)
let client = builder
.with_private_key?
.build
.await?;
// Custom configuration
let client = builder
.with_rpc_url?
.with_private_key?
.build
.await?;
Trading Operations
// Open position
let params = OpenPositionParams ;
let tx_hash = client.open_position.await?;
// Close position
let params = ClosePositionParams ;
let tx_hash = client.close_position.await?;
// Update take profit and stop loss
let params = UpdateTPSLParams ;
let tx_hash = client.update_tp_sl.await?;
Market Data
// Get trading pairs
let pairs = client.get_pairs.await?;
// Get price data
let price = client.get_price.await?;
// Check trading hours
let hours = client.get_trading_hours.await?;
Account Management
// Get balance
let balance = client.get_balance.await?; // Uses signer address
let balance = client.get_balance.await?; // Specific address
// Get positions
let positions = client.get_positions.await?;
// Get orders
let orders = client.get_orders.await?;
Examples
Run the included examples:
# Basic usage (read-only operations)
# Price fetching and API testing
# Account management (balance, positions, orders)
# Trading operations (requires OSTIUM_PRIVATE_KEY env var)
# Contract interaction testing
Error Handling
The SDK provides comprehensive error handling with detailed error types:
use ;
match client.open_position.await
Development Status
✅ Completed Features
- Network configuration and client setup
- Contract interfaces (Trading, Storage, USDC)
- Type-safe contract interactions with Alloy
- Error handling and validation
- Basic trading operations (open, close, update TP/SL)
- Account balance queries
- GraphQL client foundation
- Comprehensive examples and documentation
🚧 In Progress
- Complete GraphQL schema implementation
- Position and order history queries
- Real-time price feeds and WebSocket support
- Advanced order types (limit, stop orders)
- Batch operations and transaction optimization
📋 Planned Features
- Portfolio analytics and PnL calculations
- Risk management utilities
- Integration tests with testnet
- Performance optimizations
- Additional utility functions
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Development Setup
# Clone the repository
# Install dependencies
# Run tests
# Run examples
Security
- Never commit private keys to version control
- Use environment variables for sensitive data
- Always test on testnet before mainnet
- Keep dependencies updated
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Disclaimer: This SDK is for educational and development purposes. Always conduct thorough testing before using in production environments. Trading involves risk, and you should never trade with funds you cannot afford to lose.