lighter_rust/
lib.rs

1//! # Lighter Rust SDK
2//!
3//! A Rust SDK for the Lighter trading platform, providing async API access to trading operations,
4//! account management, and real-time market data.
5//!
6//! ## Features
7//!
8//! - **Async/await support** - Built with Tokio for high-performance async operations
9//! - **Type-safe API** - Comprehensive Rust types for all API responses
10//! - **WebSocket & REST** - Support for both REST API and real-time WebSocket feeds
11//! - **Account management** - Handle Standard and Premium account tiers
12//! - **Trading operations** - Place orders, manage positions, query market data
13//! - **Ethereum signing** - Built-in support for Ethereum-compatible wallet signing
14//!
15//! ## Quick Start
16//!
17//! ```no_run
18//! use lighter_rust::{LighterClient, Config};
19//!
20//! #[tokio::main]
21//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
22//!     let config = Config::new().with_api_key("your-api-key");
23//!     let client = LighterClient::new(config, "your-private-key")?;
24//!     
25//!     // Get account info
26//!     let account = client.account().get_account().await?;
27//!     println!("Account: {:?}", account);
28//!     
29//!     Ok(())
30//! }
31//! ```
32
33pub mod api;
34pub mod client;
35pub mod config;
36pub mod error;
37pub mod logging;
38pub mod models;
39pub mod nonce;
40pub mod signers;
41
42pub use api::*;
43pub use client::*;
44pub use config::Config;
45pub use error::{LighterError, Result};
46pub use logging::{init_logging, init_logging_with_filter};
47pub use models::*;
48pub use signers::*;
49
50use crate::client::{ApiClient, SignerClient};
51
52/// Main client for interacting with the Lighter API
53#[derive(Debug)]
54pub struct LighterClient {
55    account_api: AccountApi,
56    order_api: OrderApi,
57    transaction_api: TransactionApi,
58    candlestick_api: CandlestickApi,
59    ws_client: WebSocketClient,
60}
61
62impl LighterClient {
63    /// Create a new Lighter client with the given configuration and private key
64    pub fn new(config: Config, private_key: &str) -> Result<Self> {
65        let api_client = ApiClient::new(config.clone())?;
66        let signer_client = SignerClient::with_ethereum_signer(api_client, private_key)?;
67        let ws_client = WebSocketClient::new(config);
68
69        Ok(Self {
70            account_api: AccountApi::new(signer_client.clone()),
71            order_api: OrderApi::new(signer_client.clone()),
72            transaction_api: TransactionApi::new(signer_client.clone()),
73            candlestick_api: CandlestickApi::new(signer_client),
74            ws_client,
75        })
76    }
77
78    /// Create a new Lighter client with a mnemonic phrase
79    pub fn from_mnemonic(config: Config, mnemonic: &str, account_index: u32) -> Result<Self> {
80        let api_client = ApiClient::new(config.clone())?;
81        let ethereum_signer = signers::EthereumSigner::from_mnemonic(mnemonic, account_index)?;
82        let signer_client = SignerClient::new(api_client, std::sync::Arc::new(ethereum_signer));
83        let ws_client = WebSocketClient::new(config);
84
85        Ok(Self {
86            account_api: AccountApi::new(signer_client.clone()),
87            order_api: OrderApi::new(signer_client.clone()),
88            transaction_api: TransactionApi::new(signer_client.clone()),
89            candlestick_api: CandlestickApi::new(signer_client),
90            ws_client,
91        })
92    }
93
94    /// Create a new client with just an API key (no signing capabilities)
95    pub fn new_read_only(config: Config) -> Result<Self> {
96        let api_client = ApiClient::new(config.clone())?;
97        let ws_client = WebSocketClient::new(config);
98
99        // For read-only client, we'll use a dummy signer that will error on signing operations
100        let dummy_signer = signers::EthereumSigner::from_private_key(
101            "0000000000000000000000000000000000000000000000000000000000000001",
102        )?;
103        let signer_client = SignerClient::new(api_client, std::sync::Arc::new(dummy_signer));
104
105        Ok(Self {
106            account_api: AccountApi::new(signer_client.clone()),
107            order_api: OrderApi::new(signer_client.clone()),
108            transaction_api: TransactionApi::new(signer_client.clone()),
109            candlestick_api: CandlestickApi::new(signer_client),
110            ws_client,
111        })
112    }
113
114    /// Access account-related API endpoints
115    pub fn account(&self) -> &AccountApi {
116        &self.account_api
117    }
118
119    /// Access order-related API endpoints
120    pub fn orders(&self) -> &OrderApi {
121        &self.order_api
122    }
123
124    /// Access transaction-related API endpoints  
125    pub fn transactions(&self) -> &TransactionApi {
126        &self.transaction_api
127    }
128
129    /// Access market data API endpoints
130    pub fn market_data(&self) -> &CandlestickApi {
131        &self.candlestick_api
132    }
133
134    /// Access WebSocket client
135    pub fn websocket(&mut self) -> &mut WebSocketClient {
136        &mut self.ws_client
137    }
138}