Expand description
Rust client for the 1inch DEX Aggregator Swap API v6.0
1inch is one of the most popular DEX aggregators, offering optimal swap routes across hundreds of liquidity sources on multiple chains. This crate provides a type-safe Rust interface to the 1inch Swap API.
§Features
- Multi-chain support (Ethereum, BSC, Polygon, Arbitrum, Optimism, Base, etc.)
- Advanced Pathfinder algorithm for optimal routing
- Type-safe request/response handling
- Automatic rate limiting awareness
- Token approval helpers
§Authentication
The 1inch API requires an API key for authentication. Get your API key at https://portal.1inch.dev.
§Rate Limits
- Free tier: 1 request per second, 100,000 calls per month
- Higher tiers available for production use
§Quick Start
use oinch::{Client, Chain, QuoteRequest};
#[tokio::main]
async fn main() -> Result<(), oinch::Error> {
// Create client with your API key
let client = Client::new("your-api-key")?;
// Get a quote for swapping 1 ETH to USDC on Ethereum
let request = QuoteRequest::new(
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // Native ETH
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
"1000000000000000000", // 1 ETH in wei
)
.with_tokens_info()
.with_protocols_info();
let quote = client.get_quote(Chain::Ethereum, &request).await?;
println!("You will receive: {} USDC (in minimal units)", quote.to_amount);
Ok(())
}§Getting Transaction Data
To execute a swap, use get_swap which returns complete transaction data:
use oinch::{Client, Chain, SwapRequest};
#[tokio::main]
async fn main() -> Result<(), oinch::Error> {
let client = Client::new("your-api-key")?;
let request = SwapRequest::new(
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // ETH
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
"1000000000000000000", // 1 ETH
"0xYourWalletAddress",
1.0, // 1% slippage tolerance
);
let swap = client.get_swap(Chain::Ethereum, &request).await?;
// Use with ethers/alloy to send the transaction
println!("To: {}", swap.tx.to);
println!("Data: {}", swap.tx.data);
println!("Value: {}", swap.tx.value);
Ok(())
}§Token Approval
Before swapping ERC20 tokens (not native ETH), you need to approve the 1inch router to spend your tokens:
use oinch::{Client, Chain};
#[tokio::main]
async fn main() -> Result<(), oinch::Error> {
let client = Client::new("your-api-key")?;
// Check current allowance
let allowance = client.get_approve_allowance(
Chain::Ethereum,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
"0xYourWalletAddress",
).await?;
if allowance == "0" {
// Get approval transaction data
let approval_tx = client.get_approve_transaction(
Chain::Ethereum,
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
None, // unlimited approval
).await?;
println!("Send approval to: {}", approval_tx.to);
println!("Data: {}", approval_tx.data);
}
Ok(())
}§Supported Chains
The client supports the following chains:
| Chain | Chain ID |
|---|---|
| Ethereum | 1 |
| BNB Smart Chain | 56 |
| Polygon | 137 |
| Optimism | 10 |
| Arbitrum One | 42161 |
| Gnosis Chain | 100 |
| Avalanche | 43114 |
| Fantom | 250 |
| Klaytn | 8217 |
| Aurora | 1313161554 |
| zkSync Era | 324 |
| Base | 8453 |
| Linea | 59144 |
See Chain for the full list.
§Error Handling
The crate provides detailed error types for different failure modes:
Error::Http- Network/HTTP errorsError::Json- JSON parsing errorsError::Api- API-level errors (invalid params, etc.)Error::RateLimited- Rate limit exceededError::ServerError- 1inch server errors
All errors implement the RetryableError trait for use with retry utilities.
Re-exports§
pub use client::Client;pub use client::Config;pub use client::DEFAULT_BASE_URL;pub use error::Error;pub use error::Result;pub use types::AllowanceResponse;pub use types::ApiErrorResponse;pub use types::ApprovalTransaction;pub use types::Chain;pub use types::LiquiditySource;pub use types::LiquiditySourcesResponse;pub use types::ParseChainError;pub use types::ProtocolInfo;pub use types::QuoteRequest;pub use types::QuoteResponse;pub use types::SpenderResponse;pub use types::SwapRequest;pub use types::SwapResponse;pub use types::TokenInfo;pub use types::TokenListResponse;pub use types::TransactionData;
Modules§
- client
- HTTP client for the 1inch Swap API v6.0
- error
- Error types for the 1inch API client
- tokens
- Common token addresses across chains
- types
- Types for the 1inch Swap API v6.0
Structs§
- Http
Client Config - HTTP client configuration
- Retry
Config - Configuration for retry behavior
- Retry
Error - Error wrapper that includes retry information
Traits§
- Retryable
Error - Determines if an error should be retried
Functions§
- config_
with_ api_ key - Create a config with an API key
- with_
retry - Execute an async operation with retries
- with_
simple_ retry - Simple retry wrapper for operations that return Result with any error type