pub struct OdosClient { /* private fields */ }Expand description
The Odos API client
This is the primary interface for interacting with the Odos API. It provides methods for obtaining swap quotes and assembling transactions.
§Architecture
The client is built on top of OdosHttpClient, which handles:
- HTTP connection management and pooling
- Automatic retries with exponential backoff
- Rate limit handling
- Timeout management
§Examples
§Basic usage with defaults
use odos_sdk::OdosClient;
let client = OdosClient::new()?;§Custom configuration
use odos_sdk::{OdosClient, ClientConfig, Endpoint};
let config = ClientConfig {
endpoint: Endpoint::public_v3(),
..Default::default()
};
let client = OdosClient::with_config(config)?;§Using retry configuration
use odos_sdk::{OdosClient, RetryConfig};
// Conservative retries - only network errors
let client = OdosClient::with_retry_config(RetryConfig::conservative())?;Implementations§
Source§impl OdosClient
impl OdosClient
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new Odos client with default configuration
Uses default settings:
- Public API endpoint
- API version V2
- 30 second timeout
- 3 retry attempts with exponential backoff
§Errors
Returns an error if the underlying HTTP client cannot be initialized. This is rare and typically only occurs due to system resource issues.
§Examples
use odos_sdk::OdosClient;
let client = OdosClient::new()?;Sourcepub fn with_config(config: ClientConfig) -> Result<Self>
pub fn with_config(config: ClientConfig) -> Result<Self>
Create a new Odos SOR client with custom configuration
Allows full control over client behavior including timeouts, retries, endpoint selection, and API version.
§Arguments
config- The client configuration
§Errors
Returns an error if the underlying HTTP client cannot be initialized.
§Examples
use odos_sdk::{OdosClient, ClientConfig, Endpoint};
let config = ClientConfig {
endpoint: Endpoint::enterprise_v3(),
..Default::default()
};
let client = OdosClient::with_config(config)?;Sourcepub fn with_retry_config(retry_config: RetryConfig) -> Result<Self>
pub fn with_retry_config(retry_config: RetryConfig) -> Result<Self>
Create a client with custom retry configuration
This is a convenience constructor that creates a client with the specified retry behavior while using default values for other configuration options.
§Examples
use odos_sdk::{OdosClient, RetryConfig};
// No retries - handle all errors at application level
let client = OdosClient::with_retry_config(RetryConfig::no_retries()).unwrap();
// Conservative retries - only network errors
let client = OdosClient::with_retry_config(RetryConfig::conservative()).unwrap();
// Custom retry behavior
let retry_config = RetryConfig {
max_retries: 5,
retry_server_errors: true,
..Default::default()
};
let client = OdosClient::with_retry_config(retry_config).unwrap();Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
Get the client configuration
Sourcepub fn swap(&self) -> SwapBuilder<'_>
pub fn swap(&self) -> SwapBuilder<'_>
Create a high-level swap builder
This is the recommended way to build swaps for most use cases. It provides a simple, ergonomic API that handles the quote → assemble → build flow.
§Examples
use odos_sdk::{OdosClient, Chain, Slippage};
use alloy_primitives::{address, U256};
let client = OdosClient::new()?;
let tx = client
.swap()
.chain(Chain::ethereum())
.from_token(address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), U256::from(1_000_000))
.to_token(address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"))
.slippage(Slippage::percent(0.5)?)
.signer(address!("742d35Cc6634C0532925a3b8D35f3e7a5edD29c0"))
.build_transaction()
.await?;Sourcepub async fn quote(
&self,
quote_request: &QuoteRequest,
) -> Result<SingleQuoteResponse>
pub async fn quote( &self, quote_request: &QuoteRequest, ) -> Result<SingleQuoteResponse>
Get a swap quote from the Odos API
Requests a quote for swapping tokens on the configured chain. The quote includes routing information, price impact, gas estimates, and a path ID that can be used to assemble the transaction.
§Arguments
quote_request- The quote request containing swap parameters
§Returns
Returns a SingleQuoteResponse containing:
- Path ID for transaction assembly
- Expected output amounts
- Gas estimates
- Price impact
- Routing information
§Errors
This method can fail with various errors:
OdosError::Api- API returned an error (invalid input, unsupported chain, etc.)OdosError::RateLimit- Rate limit exceededOdosError::Http- Network errorOdosError::Timeout- Request timeout
Server errors (5xx) are automatically retried based on the retry configuration.
§Examples
use odos_sdk::{OdosClient, QuoteRequest, InputToken, OutputToken};
use alloy_primitives::{address, U256};
let client = OdosClient::new()?;
let quote_request = QuoteRequest::builder()
.chain_id(1) // Ethereum mainnet
.input_tokens(vec![InputToken::new(
address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), // USDC
U256::from(1000000) // 1 USDC (6 decimals)
)])
.output_tokens(vec![OutputToken::new(
address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"), // WETH
1 // 100% to WETH
)])
.slippage_limit_percent(0.5)
.user_addr(address!("0000000000000000000000000000000000000000"))
.compact(false)
.simple(false)
.referral_code(0)
.disable_rfqs(false)
.build();
let quote = client.quote("e_request).await?;
println!("Path ID: {}", quote.path_id());Sourcepub async fn get_swap_quote(
&self,
quote_request: &QuoteRequest,
) -> Result<SingleQuoteResponse>
👎Deprecated since 0.25.0: Use quote instead
pub async fn get_swap_quote( &self, quote_request: &QuoteRequest, ) -> Result<SingleQuoteResponse>
quote insteadDeprecated: Use quote instead
pub async fn get_assemble_response( &self, assemble_request: AssembleRequest, ) -> Result<Response>
Sourcepub async fn assemble_tx_data(
&self,
signer_address: Address,
output_recipient: Address,
path_id: &str,
) -> Result<TransactionData>
pub async fn assemble_tx_data( &self, signer_address: Address, output_recipient: Address, path_id: &str, ) -> Result<TransactionData>
Assemble transaction data from a quote
Takes a path ID from a quote response and assembles the complete transaction data needed to execute the swap on-chain.
§Arguments
signer_address- Address that will sign and send the transactionoutput_recipient- Address that will receive the output tokenspath_id- Path ID from a previous quote response
§Returns
Returns TransactionData containing:
- Transaction calldata (
data) - ETH value to send (
value) - Target contract address (
to) - Gas estimates
§Errors
OdosError::Api- Invalid path ID, expired quote, or other API errorOdosError::RateLimit- Rate limit exceededOdosError::Http- Network errorOdosError::Timeout- Request timeout
§Examples
use odos_sdk::OdosClient;
use alloy_primitives::address;
let client = OdosClient::new()?;
let path_id = "path_id_from_quote_response";
let tx_data = client.assemble_tx_data(
address!("0000000000000000000000000000000000000001"),
address!("0000000000000000000000000000000000000001"),
path_id
).await?;Sourcepub async fn assemble(
&self,
request: &AssemblyRequest,
) -> Result<TransactionRequest>
pub async fn assemble( &self, request: &AssemblyRequest, ) -> Result<TransactionRequest>
Assemble a transaction from an assembly request
Assembles transaction data and constructs a TransactionRequest ready
for gas parameter configuration and signing. This is a convenience method
that combines assemble_tx_data with transaction
request construction.
§Arguments
request- The assembly request containing addresses and path ID
§Returns
Returns a TransactionRequest with:
to: Router contract addressfrom: Signer addressdata: Encoded swap calldatavalue: ETH amount to send
Gas parameters (gas limit, gas price) are NOT set and must be configured by the caller before signing.
§Errors
OdosError::Api- Invalid path ID or API errorOdosError::RateLimit- Rate limit exceededOdosError::Http- Network errorOdosError::Timeout- Request timeoutOdosError::Hex- Failed to decode transaction data
§Examples
use odos_sdk::{OdosClient, AssemblyRequest};
use alloy_primitives::{address, U256};
use alloy_chains::NamedChain;
let client = OdosClient::new()?;
let request = AssemblyRequest::builder()
.chain(NamedChain::Mainnet)
.signer_address(address!("0000000000000000000000000000000000000001"))
.output_recipient(address!("0000000000000000000000000000000000000001"))
.router_address(address!("0000000000000000000000000000000000000002"))
.token_address(address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"))
.token_amount(U256::from(1000000))
.path_id("path_id_from_quote".to_string())
.build();
let mut tx_request = client.assemble(&request).await?;
// Configure gas parameters before signing
// tx_request = tx_request.with_gas_limit(300000);
// tx_request = tx_request.with_max_fee_per_gas(...);Sourcepub async fn build_base_transaction(
&self,
swap: &AssemblyRequest,
) -> Result<TransactionRequest>
👎Deprecated since 0.25.0: Use assemble instead
pub async fn build_base_transaction( &self, swap: &AssemblyRequest, ) -> Result<TransactionRequest>
assemble insteadDeprecated: Use assemble instead
Trait Implementations§
Source§impl Clone for OdosClient
impl Clone for OdosClient
Source§fn clone(&self) -> OdosClient
fn clone(&self) -> OdosClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OdosClient
impl Debug for OdosClient
Source§impl Default for OdosClient
impl Default for OdosClient
Source§fn default() -> Self
fn default() -> Self
Creates a default Odos client with standard configuration.
§Panics
Panics if the underlying HTTP client cannot be initialized. This should only fail in extremely rare cases such as:
- TLS initialization failure
- System resource exhaustion
- Invalid system configuration
In practice, this almost never fails and is safe for most use cases.
See OdosHttpClient::default for more details.
Auto Trait Implementations§
impl Freeze for OdosClient
impl !RefUnwindSafe for OdosClient
impl Send for OdosClient
impl Sync for OdosClient
impl Unpin for OdosClient
impl !UnwindSafe for OdosClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more