| Crates.io | lighter-rs |
| lib.rs | lighter-rs |
| version | 0.1.1 |
| created_at | 2025-11-19 17:15:53.19217+00 |
| updated_at | 2025-11-19 17:22:41.534843+00 |
| description | Rust SDK for Lighter Protocol - A comprehensive trading library for blockchain |
| homepage | |
| repository | https://github.com/0xvasanth/lighter-rs |
| max_upload_size | |
| id | 1940437 |
| size | 368,152 |
A production-ready Rust SDK for trading on Lighter Protocol - the lightning-fast order book DEX.
Build high-performance trading applications with the power of Rust and the speed of Lighter Protocol. Fully tested on mainnet with real transactions and comprehensive documentation to get you started in minutes.
Add this to your Cargo.toml:
[dependencies]
lighter-rs = { git = "https://github.com/0xvasanth/lighter-rs" }
tokio = { version = "1.0", features = ["full"] }
dotenv = "0.15"
.env FileLIGHTER_API_KEY=your-40-byte-hex-key-without-0x
LIGHTER_ACCOUNT_INDEX=your-account-index
LIGHTER_API_KEY_INDEX=4
LIGHTER_CHAIN_ID=304
LIGHTER_API_URL=https://mainnet.zklighter.elliot.ai
use dotenv::dotenv;
use lighter_rs::client::TxClient;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize tracing
tracing_subscriber::fmt::init();
dotenv().ok();
// Create client
let client = TxClient::new(
&env::var("LIGHTER_API_URL")?,
&env::var("LIGHTER_API_KEY")?,
env::var("LIGHTER_ACCOUNT_INDEX")?.parse()?,
env::var("LIGHTER_API_KEY_INDEX")?.parse()?,
304, // Mainnet
)?;
// Open a tiny position (0.0001 ETH โ $0.30)
let order = client.create_market_order(
0, // ETH market
chrono::Utc::now().timestamp_millis(),
100, // 0.0001 ETH
3_000_000_000, // $3000 mid price
0, // BUY
false,
None,
).await?;
// Submit to Lighter
match client.send_transaction(&order).await {
Ok(response) if response.code == 200 => {
tracing::info!("โ
Order placed!");
tracing::info!("Tx Hash: {:?}", response.tx_hash);
}
Ok(response) => {
tracing::warn!("Error {}: {:?}", response.code, response.message);
}
Err(e) => {
tracing::error!("Failed: {}", e);
}
}
Ok(())
}
cargo run --example create_order
You should see:
โ
Order placed!
Tx Hash: Some("abc123...")
Congratulations! ๐ You just traded on Lighter Protocol using Rust!
New to Lighter RS? Start here:
Need Help?
Want Deep Dive?
// High-frequency trading bot
// Market making strategies
// Arbitrage systems
// Grid trading
// Automated liquidity provision
// Portfolio rebalancing
// Risk management systems
// Order flow analysis
// Market data collection
// Position tracking
The possibilities are endless! This SDK gives you the building blocks to create any trading application you can imagine.
// Open long position
let open = client.create_market_order(
0, timestamp, 100, 3_000_000_000, 0, false, None
).await?;
// Close position (important: use reduce_only=true!)
let close = client.create_market_order(
0, timestamp, 100, 3_000_000_000, 1, true, None
).await?;
use lighter_rs::types::CreateOrderTxReq;
use lighter_rs::constants::*;
let stop_loss = CreateOrderTxReq {
market_index: 0,
client_order_index: chrono::Utc::now().timestamp_millis(),
base_amount: 100,
price: 2_900_000_000, // Execution price
is_ask: 1, // SELL
order_type: ORDER_TYPE_STOP_LOSS,
time_in_force: TIME_IN_FORCE_IMMEDIATE_OR_CANCEL, // Required for stop loss
reduce_only: 1,
trigger_price: 2_950_000_000, // Trigger at $2950
order_expiry: future_timestamp,
};
let tx = client.create_order(&stop_loss, None).await?;
client.send_transaction(&tx).await?;
// Place limit order
let limit = client.create_limit_order(
market_index, timestamp, amount, price, is_ask, false, None
).await?;
client.send_transaction(&limit).await?;
// Cancel it later
let cancel = CancelOrderTxReq {
market_index: 0,
index: order_client_order_index,
};
let cancel_tx = client.cancel_order(&cancel, None).await?;
client.send_transaction(&cancel_tx).await?;
# Run all unit tests (41 tests)
cargo test
# Run specific example
cargo run --example create_order
# Run with logging
RUST_LOG=debug cargo run --example create_order
# Development build
cargo build
# Optimized release build
cargo build --release
# Generate documentation
cargo doc --open
# Format code
cargo fmt
# Run linter
cargo clippy
# Run all checks
cargo test && cargo clippy && cargo fmt --check
We welcome contributions from everyone! Whether you're fixing a bug, adding a feature, or improving documentation, your help makes this project better.
git checkout -b feature/amazing-feature)cargo test)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)Good First Issues:
Advanced Contributions:
Every contribution matters! Even small improvements help make this SDK better for everyone.
Join our growing community of Rust developers building on Lighter Protocol!
Status: Production-ready for market order trading! ๐
Private Key Safety:
.env file).env file is in .gitignore by defaultAudit Status:
This SDK uses goldilocks-crypto and poseidon-hash crates for cryptography. While these are ports from the official lighter-go implementation, they have not been independently audited. Use appropriate caution in production.
About Lighter Protocol:
Other SDKs:
Built with โค๏ธ by the Rust community for the Lighter Protocol ecosystem.
Special thanks to:
goldilocks-crypto and poseidon-hash cratesMIT License - See LICENSE for details.
Ready to build the future of decentralized trading? ๐
Start with our Quick Start Guide above, explore the examples, and join our community!
Happy Trading! ๐