Overview
This guide walks you through the complete process of executing a token swap using Mobula’s Swap API. You’ll learn how to:- Get an optimized swap quote
- Sign the transaction with your wallet
- Broadcast the transaction to the blockchain
- Monitor the transaction status
Supported EVM Chains
All EVM swaps are executed through MobulaRouter, a smart contract deployed on:
MobulaRouter automatically routes to the best available aggregator for optimal pricing across all DEX liquidity on each chain.
Prerequisites
- Node.js 18+ installed
- A Mobula API key (get one at admin.mobula.io)
- Basic knowledge of TypeScript/JavaScript
- A wallet with funds on the blockchain you want to swap on
Installation
First, install the required dependencies:Complete Swap Flow
Step 1: Get a Swap Quote
The first step is to request a swap quote. The API will return the estimated output amount and a serialized transaction ready to be signed.Step 2: Sign the Transaction
Once you have the quote, you need to sign the transaction with your wallet. The process differs between Solana and EVM chains.Solana Transaction Signing
EVM Transaction Signing
Step 3: Send the Signed Transaction
After signing, broadcast the transaction to the blockchain.Step 4: Monitor Transaction Status
After broadcasting, monitor the transaction on the blockchain.Complete Examples
Example 1: Swap SOL to USDC on Solana
Example 2: Swap ETH to USDC on Ethereum
Example 3: Advanced Swap with Protocol Restrictions
Error Handling Best Practices
Important Notes
Token Addresses
- EVM native tokens: Use
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeEfor native tokens on any EVM chain (ETH, BNB, MATIC, AVAX) - Solana native: Use
So11111111111111111111111111111111111111112for SOL - Token amounts: Always use the smallest unit (wei for EVM, lamports for Solana)
EVM Chains
All EVM swap transactions go through MobulaRouter. The response provides a ready-to-sign transaction in theevm.transaction field:
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE as tokenIn. The API will set the correct value field in the transaction.
For ERC-20 token swaps, you must first approve the MobulaRouter contract to spend your tokens. The router address is returned in evm.transaction.to.
Amount Conversion
Protocol Filtering
-
onlyProtocols: Restricts routing to specific tradable protocols. Only valid tradable pool types will be considered. -
excludedProtocols: Excludes specific factory addresses from routing.
Slippage Recommendations
- Stablecoins: 0.1% - 0.5%
- Major tokens: 0.5% - 1%
- Low liquidity tokens: 2% - 5%
- Memecoins: 5% - 10%
Troubleshooting
Common Issues
-
“Slippage too high”
- Increase slippage tolerance
- Reduce swap amount
- Wait for better market conditions
-
“No route found”
- Check token addresses are correct
- Verify tokens have liquidity on the chain
- Try without protocol restrictions
-
“Transaction simulation failed”
- Insufficient balance (including gas fees)
- Token approval may be needed (for non-native tokens)
- Slippage tolerance too low
-
Transaction not confirming
- Network congestion (especially on Ethereum)
- Gas price too low (EVM chains)
- Check blockchain explorer with transaction hash