Odos Rust SDK
One-to-one Swap Example
Note: This example demonstrates the workflow for executing a token swap using the Odos SDK. It assumes you have set up an alloy provider and signer. For a complete working example, see the documentation.
use alloy_chains::NamedChain;
use alloy_primitives::{Address, U256};
use odos_sdk::{OdosSor, QuoteRequest, SwapContext};
# async fn example() -> Result<(), Box<dyn std::error::Error>> {
# // Setup (not shown): Initialize your alloy provider and signer
# // let provider = ...; // Your alloy provider
# // let signer = ...; // Your alloy signer with private key
# let signer_address = Address::ZERO; // Replace with actual signer address
# let balance = U256::from(1000000000000000000u64); // Example: 1 token
#
// Token addresses
const WETH: &str = "0x4200000000000000000000000000000000000006";
const USDC: &str = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
const RECIPIENT: &str = "0x83384D138420436f4b0DaE97b02002dd5011a7D9";
let chain = NamedChain::Base;
let weth_address = WETH.parse::<Address>()?;
// Step 1: Get a quote from Odos
let quote_request = QuoteRequest::builder()
.chain_id(chain.into())
.input_tokens(vec![(weth_address, balance).into()])
.output_tokens(vec![(USDC.parse::<Address>()?, 1).into()])
.slippage_limit_percent(1.0)
.user_addr(signer_address.to_string())
.compact(false)
.simple(false)
.referral_code(0)
.disable_rfqs(true)
.build();
let sor_client = OdosSor::new()?;
let quote = sor_client.get_swap_quote("e_request).await?;
// Step 2: Approve the Odos router to spend your tokens
// (Using your ERC20 contract implementation)
// let router_address = chain.v2_router_address()?;
// token_contract.approve(router_address, balance).await?;
// Step 3: Build the swap transaction
let swap_params = SwapContext::builder()
.chain(chain)
.router_address(chain.v2_router_address()?)
.signer_address(signer_address)
.output_recipient(RECIPIENT.parse::<Address>()?)
.token_address(weth_address)
.token_amount(balance)
.path_id(quote.path_id().to_string())
.build();
let tx_request = sor_client
.build_base_transaction(&swap_params)
.await?;
// Step 4: Send the transaction
// (Using your alloy signer)
// let pending_tx = signer.send_transaction(tx_request).await?;
// let receipt = pending_tx.get_receipt().await?;
// assert!(receipt.status());
println!("Swap transaction built successfully!");
# Ok(())
# }