Skip to main content

Overview

An onchain trade (otherwise known as a swap) is a transaction that exchanges one token for another. In this guide, you will learn how to:
  • Estimate a swap price
  • Execute a swap with regular accounts (EOAs) or Smart Accounts
The Beta launch of our Trade API supports Ethereum, Base, Arbitrum, Optimism and Polygon mainnet networks only - stay tuned for additional network support!

Price estimation

The SDK provides two methods for estimating swap prices:

CDP vs. external libraries

You can execute swaps using either:
  1. CDP Wallet Integration (Recommended for most use cases)
    • Use our managed wallet infrastructure
    • Automatic transaction signing and submission
    • Built-in security and compliance features
    • Smart Account support with gas sponsorship via paymasters
    • See the CDP Wallets guide for details on setup and the Non-custodial Wallet Swaps guide for more information on executing swaps
  2. External Library Integration (For custom infrastructure)
    • Use libraries like viem or web3.py
    • Full control over transaction signing
    • Support for custom wallet types (including Smart Accounts with additional setup)
    • Continue reading below for examples
Both options include built-in slippage protection to ensure your swap executes at a fair price, even in volatile market conditions. Gas fees are automatically calculated and optimized for the most cost-effective route.

Slippage

Slippage is the difference between the expected price of a trade and the actual price at which it executes.
In these examples, we set a 1% slippage tolerance (using slippageBps: 100 in TypeScript or slippage_bps: 100 in Python), meaning the trade will only execute if the final price is within 1% of the expected price.This protects you from unfavorable trades if the price moves significantly between when you submit the transaction and when it’s executed.

Prerequisites

It is assumed you have:

Regular Accounts (EOAs)

1. Estimate a swap price

To begin, let’s walk through an example of how to estimate a swap price with a regular account (EOA).
getSwapPrice for Typescript and get_swap_price for Python provide estimates only and do not reserve funds. They are suitable for frequent price updates but may be less precise than creating a swap quote.
πŸ“– Full examples: TypeScript | Python

2. Create a swap quote

Once you’re ready to commit to a swap, you can create a swap quote using the CDP API. This gives you the transaction data needed for execution as opposed to the quick price estimate that we demonstrated above.
Creating a swap quote may reserve funds onchain. This action is strictly rate-limited.
πŸ“– Full examples: TypeScript | Python

3. Execute a swap

Now that we have a swap quote, we can execute it onchain. The easiest way to do this is by using CDP Wallets. We also offer a smoother developer experience using our Non-custodial Wallets. Read the Non-custodial Wallet Swaps guide for more information.
All-in-one swapYou can also create and execute a swap in a single call using account.swap():
πŸ“– Full examples:

Smart Accounts

Smart Accounts provide additional features like gas sponsorship and batch operations. The key differences are:
  • Use the Smart Account address (not the owner’s EOA) as the taker
  • Transactions return userOpHash instead of transactionHash
  • Must wait for user operations to complete

1. Estimate a swap price

When estimating prices for Smart Accounts, use the Smart Account address as the taker:
πŸ“– Full examples: TypeScript | Python

2. Create a swap quote

Once you’re ready to commit to a swap, you can create a swap quote using the CDP API. This gives you the transaction data needed for execution as opposed to the quick price estimate that we demonstrated above.
Creating a swap quote may reserve funds onchain. This action is strictly rate-limited.
πŸ“– Full examples: TypeScript | Python

3. Execute a swap

Now that we have a swap quote, we can execute it onchain. The easiest way to do this is by using CDP Wallets. Executing swaps with Smart Accounts returns a user operation hash instead of a transaction hash. You must wait for the user operation to complete:
All-in-one swapYou can also create and execute a swap in a single call using smartAccount.swap():
πŸ“– Full examples:
Smart Account Benefits
  • Gas Sponsorship: Use paymasters to pay gas fees on behalf of users
  • Batch Operations: Combine multiple swaps or operations in one transaction
  • Account Abstraction: Enhanced UX with features like session keys and social recovery
  • Security: Multi-signature capabilities and spending limits

Using External Wallets

If you prefer to use your own wallet, signing infrastructure, and node for broadcasting transactions, you can use the core Trade APIs (like getSwapPrice and quoteSwap) without a CDP account. πŸ“– Full examples: TypeScript | Python
Custom Wallet IntegrationIf you’re using your own wallet infrastructure (e.g., viem, web3.py, etc.), you can execute swaps using the transaction data from the quote. For Smart Accounts with external wallets, additional ERC-4337 infrastructure setup is required.
  • Welcome: Read why Trade API is easier to use than traditional aggregator solutions.
  • API Reference: Explore the full CDP API v2 documentation.
  • USDC Rewards: Learn how to earn 3.35% rewards on USDC balances in your CDP wallets.