odos_sdk/lib.rs
1//! # Odos SDK
2//!
3//! A production-ready Rust SDK for the Odos protocol - a decentralized exchange aggregator
4//! that provides optimal routing for token swaps across multiple EVM chains.
5//!
6//! ## Features
7//!
8//! - **Multi-chain Support**: 16+ EVM chains including Ethereum, Arbitrum, Optimism, Polygon, Base, etc.
9//! - **Type-safe**: Leverages Rust's type system with Alloy primitives for addresses, chain IDs, and amounts
10//! - **Production-ready**: Built-in retry logic, circuit breakers, timeouts, and error handling
11//! - **Builder Pattern**: Ergonomic API using the `bon` crate for request building
12//! - **Comprehensive Error Handling**: Detailed error types for different failure scenarios
13//!
14//! ## Quick Start
15//!
16//! ```rust,no_run
17//! use odos_sdk::*;
18//! use alloy_primitives::{Address, U256};
19//! use std::str::FromStr;
20//!
21//! # async fn example() -> Result<()> {
22//! // Create a client
23//! let client = OdosSorV2::new()?;
24//!
25//! // Build a quote request
26//! let quote_request = QuoteRequest::builder()
27//! .chain_id(1) // Ethereum mainnet
28//! .input_tokens(vec![(
29//! Address::from_str("0xA0b86a33E6441d35a6b083d5b02a8e3F6CE21a2E")?, // WETH
30//! U256::from(1000000000000000000u64) // 1 ETH
31//! ).into()])
32//! .output_tokens(vec![(
33//! Address::from_str("0xA0b86a33E6441d35a6b083d5b02a8e3F6CE21a2E")?, // USDC
34//! 1
35//! ).into()])
36//! .slippage_limit_percent(1.0)
37//! .user_addr("0x742d35Cc6634C0532925a3b8D35f3e7a5edD29c0".to_string())
38//! .compact(false)
39//! .simple(false)
40//! .referral_code(0)
41//! .disable_rfqs(false)
42//! .build();
43//!
44//! // Get a quote
45//! let quote = client.get_swap_quote("e_request).await?;
46//!
47//! // Build transaction data
48//! let swap_context = SwapContext::builder()
49//! .chain(alloy_chains::NamedChain::Mainnet)
50//! .router_address(alloy_chains::NamedChain::Mainnet.v2_router_address()?)
51//! .signer_address(Address::from_str("0x742d35Cc6634C0532925a3b8D35f3e7a5edD29c0")?)
52//! .output_recipient(Address::from_str("0x742d35Cc6634C0532925a3b8D35f3e7a5edD29c0")?)
53//! .token_address(Address::from_str("0xA0b86a33E6441d35a6b083d5b02a8e3F6CE21a2E")?)
54//! .token_amount(U256::from(1000000000000000000u64))
55//! .path_id(quote.path_id().to_string())
56//! .build();
57//!
58//! let transaction = client.build_base_transaction(&swap_context).await?;
59//! # Ok(())
60//! # }
61//! ```
62//!
63//! ## Configuration
64//!
65//! The SDK supports extensive configuration for production use:
66//!
67//! ```rust,no_run
68//! use odos_sdk::*;
69//! use std::time::Duration;
70//!
71//! # fn example() -> Result<()> {
72//! let config = ClientConfig {
73//! timeout: Duration::from_secs(30),
74//! connect_timeout: Duration::from_secs(10),
75//! max_retries: 3,
76//! initial_retry_delay: Duration::from_millis(100),
77//! max_retry_delay: Duration::from_secs(5),
78//! circuit_breaker_threshold: 5,
79//! circuit_breaker_reset_timeout: Duration::from_secs(60),
80//! max_connections: 20,
81//! pool_idle_timeout: Duration::from_secs(90),
82//! };
83//!
84//! let client = OdosSorV2::with_config(config)?;
85//! # Ok(())
86//! # }
87//! ```
88//!
89//! ## Error Handling
90//!
91//! The SDK provides comprehensive error types for different failure scenarios:
92//!
93//! ```rust,no_run
94//! use odos_sdk::*;
95//!
96//! # async fn example() {
97//! # let client = OdosSorV2::new().unwrap();
98//! # let quote_request = QuoteRequest::builder().chain_id(1).input_tokens(vec![]).output_tokens(vec![]).slippage_limit_percent(1.0).user_addr("test".to_string()).compact(false).simple(false).referral_code(0).disable_rfqs(false).build();
99//! match client.get_swap_quote("e_request).await {
100//! Ok(quote) => {
101//! // Handle successful quote
102//! println!("Got quote with path ID: {}", quote.path_id());
103//! }
104//! Err(OdosError::Api { status, message }) => {
105//! // Handle API errors
106//! eprintln!("API error {}: {}", status, message);
107//! }
108//! Err(OdosError::Timeout(msg)) => {
109//! // Handle timeout errors (retryable)
110//! eprintln!("Request timed out: {}", msg);
111//! }
112//! Err(OdosError::RateLimit(msg)) => {
113//! // Handle rate limiting (retryable)
114//! eprintln!("Rate limited: {}", msg);
115//! }
116//! Err(OdosError::CircuitBreakerOpen(msg)) => {
117//! // Handle circuit breaker
118//! eprintln!("Circuit breaker open: {}", msg);
119//! }
120//! Err(err) => {
121//! // Handle other errors
122//! eprintln!("Error: {}", err);
123//! }
124//! }
125//! # }
126//! ```
127
128mod api;
129mod assemble;
130mod chain;
131mod client;
132mod contract;
133mod error;
134#[cfg(test)]
135mod integration_tests;
136mod limit_order_v2;
137mod sor;
138mod swap;
139mod transfer;
140mod v2_router;
141
142pub use api::*;
143pub use assemble::*;
144pub use chain::*;
145pub use client::*;
146pub use contract::*;
147pub use error::*;
148pub use limit_order_v2::*;
149pub use sor::*;
150pub use swap::*;
151pub use transfer::*;
152pub use v2_router::*;