OdosClient

Struct OdosClient 

Source
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

Source

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()?;
Source

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)?;
Source

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();
Source

pub fn config(&self) -> &ClientConfig

Get the client configuration

Source

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?;
Source

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:

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(&quote_request).await?;
println!("Path ID: {}", quote.path_id());
Source

pub async fn get_swap_quote( &self, quote_request: &QuoteRequest, ) -> Result<SingleQuoteResponse>

👎Deprecated since 0.25.0: Use quote instead

Deprecated: Use quote instead

Source

pub async fn get_assemble_response( &self, assemble_request: AssembleRequest, ) -> Result<Response>

Source

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 transaction
  • output_recipient - Address that will receive the output tokens
  • path_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
§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?;
Source

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 address
  • from: Signer address
  • data: Encoded swap calldata
  • value: ETH amount to send

Gas parameters (gas limit, gas price) are NOT set and must be configured by the caller before signing.

§Errors
§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(...);
Source

pub async fn build_base_transaction( &self, swap: &AssemblyRequest, ) -> Result<TransactionRequest>

👎Deprecated since 0.25.0: Use assemble instead

Deprecated: Use assemble instead

Trait Implementations§

Source§

impl Clone for OdosClient

Source§

fn clone(&self) -> OdosClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OdosClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OdosClient

Source§

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more