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>
impl<'a> SwapBuilder<'a>
Sourcepub fn chain(self, chain: Chain) -> Self
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());Sourcepub fn input(self, token: Address, amount: U256) -> Self
pub fn input(self, token: Address, amount: U256) -> Self
Set the input token and amount
§Arguments
token- Address of the token to swap fromamount- 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));Sourcepub fn from_token(self, token: Address, amount: U256) -> Self
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));Sourcepub fn to_token(self, token: Address) -> Self
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"));Sourcepub fn recipient(self, address: Address) -> Self
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"));Sourcepub fn referral(self, code: ReferralCode) -> Self
pub fn referral(self, code: ReferralCode) -> Self
Sourcepub fn compact(self, compact: bool) -> Self
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);Sourcepub fn simple(self, simple: bool) -> Self
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);Sourcepub fn disable_rfqs(self, disable: bool) -> Self
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);Sourcepub async fn quote(&self) -> Result<SingleQuoteResponse>
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());Sourcepub async fn build_transaction(&self) -> Result<TransactionRequest>
pub async fn build_transaction(&self) -> Result<TransactionRequest>
Build the complete transaction for this swap
This method:
- Gets a quote from the Odos API
- Assembles the transaction data
- Returns a
TransactionRequestready for signing
The returned transaction still needs gas parameters set before signing.
§Returns
Returns a TransactionRequest with:
to: Router contract addressfrom: Signer addressdata: Encoded swap calldatavalue: 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§
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> 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> 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