SwapBuilder

Struct SwapBuilder 

Source
pub struct SwapBuilder<'a> { /* private fields */ }
Expand description

High-level swap builder for common use cases

Provides an ergonomic API for building swaps without needing to understand the low-level quote → assemble → build flow.

§Examples

§Simple swap

use odos_sdk::{OdosClient, Slippage, Chain};
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?;

§With custom recipient

use odos_sdk::{OdosClient, Slippage, Chain};
use alloy_primitives::{address, U256};

let client = OdosClient::new()?;

let tx = client
    .swap()
    .chain(Chain::arbitrum())
    .from_token(address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), U256::from(1_000_000))
    .to_token(address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"))
    .slippage(Slippage::bps(50)?)
    .signer(address!("742d35Cc6634C0532925a3b8D35f3e7a5edD29c0"))
    .recipient(address!("0000000000000000000000000000000000000001"))
    .build_transaction()
    .await?;

Implementations§

Source§

impl<'a> SwapBuilder<'a>

Source

pub fn chain(self, chain: Chain) -> Self

Set the blockchain to execute the swap on

§Examples
use odos_sdk::{OdosClient, Chain};

let client = OdosClient::new()?;
let builder = client.swap().chain(Chain::ethereum());
Source

pub fn input(self, token: Address, amount: U256) -> Self

Set the input token and amount

§Arguments
  • token - Address of the token to swap from
  • amount - Amount of input token (in token’s base units)
§Examples
use odos_sdk::OdosClient;
use alloy_primitives::{address, U256};

let client = OdosClient::new()?;
let builder = client.swap()
    .input(address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), U256::from(1_000_000));
Source

pub fn from_token(self, token: Address, amount: U256) -> Self

Alias for input() - set the token to swap from

§Examples
use odos_sdk::OdosClient;
use alloy_primitives::{address, U256};

let client = OdosClient::new()?;
let builder = client.swap()
    .from_token(address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), U256::from(1_000_000));
Source

pub fn output(self, token: Address) -> Self

Set the output token (100% of output goes to this token)

§Arguments
  • token - Address of the token to swap to
§Examples
use odos_sdk::OdosClient;
use alloy_primitives::address;

let client = OdosClient::new()?;
let builder = client.swap()
    .output(address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"));
Source

pub fn to_token(self, token: Address) -> Self

Alias for output() - set the token to swap to

§Examples
use odos_sdk::OdosClient;
use alloy_primitives::address;

let client = OdosClient::new()?;
let builder = client.swap()
    .to_token(address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"));
Source

pub fn slippage(self, slippage: Slippage) -> Self

Set the slippage tolerance

§Arguments
  • slippage - Maximum acceptable slippage
§Examples
use odos_sdk::{OdosClient, Slippage};

let client = OdosClient::new()?;
let builder = client.swap()
    .slippage(Slippage::percent(0.5)?);  // 0.5% slippage
Source

pub fn signer(self, address: Address) -> Self

Set the address that will sign and send the transaction

§Arguments
  • address - The signer’s address
§Examples
use odos_sdk::OdosClient;
use alloy_primitives::address;

let client = OdosClient::new()?;
let builder = client.swap()
    .signer(address!("742d35Cc6634C0532925a3b8D35f3e7a5edD29c0"));
Source

pub fn recipient(self, address: Address) -> Self

Set the recipient address for output tokens

If not set, defaults to the signer address.

§Arguments
  • address - The recipient’s address
§Examples
use odos_sdk::OdosClient;
use alloy_primitives::address;

let client = OdosClient::new()?;
let builder = client.swap()
    .signer(address!("742d35Cc6634C0532925a3b8D35f3e7a5edD29c0"))
    .recipient(address!("0000000000000000000000000000000000000001"));
Source

pub fn referral(self, code: ReferralCode) -> Self

Set the referral code

§Arguments
  • code - Referral code for tracking
§Examples
use odos_sdk::{OdosClient, ReferralCode};

let client = OdosClient::new()?;
let builder = client.swap()
    .referral(ReferralCode::new(42));
Source

pub fn compact(self, compact: bool) -> Self

Enable compact mode

§Examples
use odos_sdk::OdosClient;

let client = OdosClient::new()?;
let builder = client.swap().compact(true);
Source

pub fn simple(self, simple: bool) -> Self

Enable simple mode

§Examples
use odos_sdk::OdosClient;

let client = OdosClient::new()?;
let builder = client.swap().simple(true);
Source

pub fn disable_rfqs(self, disable: bool) -> Self

Disable RFQs (Request for Quotes)

§Examples
use odos_sdk::OdosClient;

let client = OdosClient::new()?;
let builder = client.swap().disable_rfqs(true);
Source

pub async fn quote(&self) -> Result<SingleQuoteResponse>

Get a quote for this swap without building the transaction

This is useful if you want to inspect the quote before proceeding.

§Returns

Returns a SingleQuoteResponse with routing information, price impact, and gas estimates.

§Errors

Returns an error if:

  • Required fields are missing
  • The Odos API returns an error
  • Network issues occur
§Examples
use odos_sdk::{OdosClient, Chain, Slippage};
use alloy_primitives::{address, U256};

let client = OdosClient::new()?;

let quote = 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"))
    .quote()
    .await?;

println!("Expected output: {} tokens", quote.out_amount().unwrap());
println!("Price impact: {}%", quote.price_impact());
Source

pub async fn build_transaction(&self) -> Result<TransactionRequest>

Build the complete transaction for this swap

This method:

  1. Gets a quote from the Odos API
  2. Assembles the transaction data
  3. Returns a TransactionRequest ready for signing

The returned transaction still needs gas parameters set before signing.

§Returns

Returns a TransactionRequest with:

  • to: Router contract address
  • from: Signer address
  • data: Encoded swap calldata
  • value: ETH amount to send (if swapping from native token)
§Errors

Returns an error if:

  • Required fields are missing
  • The Odos API returns an error
  • Transaction assembly fails
  • Network issues occur
§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?;

// Set gas parameters and sign
// let tx = tx.with_gas_limit(300_000);
// provider.send_transaction(tx).await?;

Trait Implementations§

Source§

impl<'a> Debug for SwapBuilder<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for SwapBuilder<'a>

§

impl<'a> !RefUnwindSafe for SwapBuilder<'a>

§

impl<'a> Send for SwapBuilder<'a>

§

impl<'a> Sync for SwapBuilder<'a>

§

impl<'a> Unpin for SwapBuilder<'a>

§

impl<'a> !UnwindSafe for SwapBuilder<'a>

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> 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, 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