Arbitrage in decentralized finance (DeFi) isn’t just theory—it’s one of the most practical and profitable strategies when executed correctly. With your Aave V3 flashloan contract ready, it’s time to connect the dots: real trading execution on decentralized exchanges (DEXs).
In this chapter, we’ll explore how to leverage Uniswap (V2 and V3) and Paraswap to turn borrowed liquidity into profitable trades. Along the way, you’ll discover how arbitrage bots work, how to avoid pitfalls like slippage and gas wars, and why automation and optimization are critical.
By the end of this guide, you’ll understand the mechanics of DEX arbitrage, how to code swap functions, and how to safely bundle them into a flashloan-powered trading loop.
Table of Contents
Toggle6.1 What is DEX Arbitrage and Why Does it Work?
Arbitrage is the practice of exploiting price discrepancies for the same asset across different markets. In DeFi, those markets are DEX liquidity pools—and because each pool is independent, prices don’t always match perfectly.
Here’s why opportunities exist:
- Liquidity is fragmented across multiple DEXs
- Large trades shift prices differently per pool
- Gas fees, slippage, and fees create temporary inefficiencies
These small gaps—sometimes lasting just seconds—are where arbitrageurs strike.

Common Types of Arbitrage in DeFi
- Cross-DEX Arbitrage
Buying on Uniswap where ETH is cheaper, then instantly selling on SushiSwap at a higher price. - DEX-to-CEX Arbitrage
Exploiting price gaps between DEXs and centralized exchanges like Binance or Coinbase. - Triangular Arbitrage
Profiting by swapping between three pairs inside the same DEX—for example: USDC → WETH → DAI → USDC. - Cross-Chain Arbitrage
Buying a token on Avalanche and selling it on Ethereum via bridges, taking advantage of chain-specific liquidity gaps.
6.2 Uniswap V2 vs Uniswap V3: Which Should You Use?
| Feature | Uniswap V2 | Uniswap V3 |
|---|---|---|
| Liquidity | Spread evenly | Concentrated liquidity at chosen price ranges |
| Fees | Fixed (0.3%) | Flexible (0.05%, 0.3%, 1%) |
| Interface | Router02 | SwapRouter |
| Paths | Requires manual token pairs | Supports advanced multi-hop trades |
| Gas Costs | Lower | Higher (more complex) |
6.3 Executing Swaps with Uniswap
Uniswap V2 Swap Function
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
Steps to execute:
- Approve router to spend tokens
- Define a swap path (e.g.,
[USDC, WETH]) - Set
amountOutMinwith slippage tolerance
This enables quick swaps inside your executeOperation() arbitrage loop.
Uniswap V3 Swap Example
ISwapRouter.ExactInputSingleParams memory params =
ISwapRouter.ExactInputSingleParams({
tokenIn: USDC,
tokenOut: WETH,
fee: 3000,
recipient: address(this),
deadline: block.timestamp,
amountIn: 100000e6,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
});
IERC20(USDC).approve(address(router), params.amountIn);
uint amountOut = ISwapRouter(router).exactInputSingle(params);
This allows for precision trades with adjustable fees and multi-hop flexibility.
6.4 Paraswap: The Power of Aggregation
Instead of relying on a single exchange, Paraswap scans multiple DEXs in real-time (Uniswap, Sushiswap, Curve, Balancer, etc.) to deliver the best swap price.
Advantages:
- Maximized output by splitting across pools
- Reduced slippage for large trades
- Easy integration with smart contracts via on-chain aggregator
While we’ll dive deeper into Paraswap API encoding in later chapters, know this: it’s an arbitrageur’s secret weapon for squeezing maximum profit.
6.5 Flashloan Arbitrage Flow with Swaps
Here’s what a USDC → WETH → USDC arbitrage loop might look like:
- Borrow 100,000 USDC from Aave via flashloan
- Swap USDC → WETH on Uniswap V2
- Swap WETH → USDC on Paraswap or SushiSwap
- Repay Aave:
100,000 + fee - Keep the difference as profit
Because this runs inside executeOperation(), it’s atomic—either everything succeeds, or it reverts.
6.6 Execution Mechanisms for Arbitrage Bots
To compete in DeFi, arbitrageurs rely on automation and smart contract optimization.
- Automated Trading Bots scan DEXs, calculate spreads, and execute instantly.
- Flash Loans remove capital requirements, enabling large trades with minimal upfront risk.
- Custom Smart Contracts bundle loan + swaps + repayment into one transaction, ensuring atomicity and gas efficiency.
This setup allows arbitrageurs to move faster than manual traders and exploit opportunities before prices normalize.
6.7 Risks and Challenges in DEX Arbitrage
While the strategy is powerful, it comes with challenges:
- Gas Fee Wars: Competing bots push gas fees higher, eating into profits.
- Front-running Attacks: Malicious actors copy trades in the mempool and bid higher gas.
- Slippage: Large trades can move pool prices unfavorably.
- Network Congestion: During volatile periods, transactions may fail before execution.
- Smart Contract Bugs: Poorly written code can cause losses.
Mitigation strategies include Flashbots (private mempool transactions), slippage checks, and rigorous contract testing.
6.8 Real-World Arbitrage Example
- Flashloan 100,000 USDC
- Swap on Uniswap: USDC → 55.55 WETH
- Swap on Sushi: WETH → 101,000 USDC
- Repay Aave: 100,090 USDC
- Profit: ~910 USDC in seconds
This showcases the potential of cross-DEX arbitrage when executed with precision.
6.9 Logging & Tracking Profits
Always log your results for debugging and analytics:
event ArbitrageExecuted(address executor, uint profit);
emit ArbitrageExecuted(msg.sender, profit);
Adding structured event logs helps track profit margins, execution times, and trade history.
6.10 In this section will guide you through
This chapter will guide you through:
- Overview of Uniswap V2 vs. V3 mechanics
- Interacting with Uniswap’s Router contracts
- Using Paraswap as a meta-aggregator for better pricing
- Swapping tokens inside
executeOperation - Managing slippage and gas
- Writing swap helper functions
- Real-world arbitrage code example
| Feature | Uniswap V2 | Uniswap V3 |
|---|---|---|
| Liquidity | Pooled uniformly | Concentrated liquidity |
| Fees | Fixed (0.3%) | Adjustable (0.05%, 0.3%, 1%) |
| Interface | Router02 | SwapRouter |
| Path | Requires manual pair | Supports multi-hop routes |
| Gas Cost | Lower | Higher due to complexity |
Uniswap V2 Router Interface
Uniswap V2 exposes the swapExactTokensForTokens method:
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
To call this:
- Approve the router to spend tokens
- Pass path:
[USDC, WETH] - Set slippage with
amountOutMin
Implementing a Swap on Uniswap V2
Here’s an example swap from USDC → WETH:
function swapOnUniswapV2(
address router,
address tokenIn,
address tokenOut,
uint amountIn,
uint amountOutMin
) internal returns (uint[] memory) {
IERC20(tokenIn).approve(router, amountIn);
address ;
path[0] = tokenIn;
path[1] = tokenOut;
return IUniswapV2Router(router).swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
address(this),
block.timestamp
);
}
Swapping on Uniswap V3
Uniswap V3 uses ISwapRouter and has a more flexible, gas-optimized architecture.
function exactInputSingle(
ExactInputSingleParams calldata params
) external payable returns (uint amountOut);
Struct:
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint deadline;
uint amountIn;
uint amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
Usage Example:
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
tokenIn: USDC,
tokenOut: WETH,
fee: 3000, // 0.3%
recipient: address(this),
deadline: block.timestamp,
amountIn: 100000e6,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
});
IERC20(USDC).approve(address(router), params.amountIn);
uint amountOut = ISwapRouter(router).exactInputSingle(params);
Paraswap Integration
Paraswap aggregates prices from 20+ DEXs, including Uniswap, Sushiswap, and Curve. It’s ideal for getting the best output amount in arbitrage.
You can call Paraswap using:
- Smart contract SDK
- Pre-built on-chain aggregator contract
- Custom encoded calldata (via Paraswap API)
For simplicity, we’ll show how to integrate it in Chapter 12 using static paths. For now, we’ll focus on Uniswap for deterministic logic.
Combining with Flashloan Logic
Let’s put everything together:
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
) external override returns (bool) {
require(msg.sender == address(POOL), "Invalid caller");
// Example: USDC → WETH on Uniswap → USDC on SushiSwap
// Step 1: Swap USDC → WETH
uint[] memory wethOut = swapOnUniswapV2(
uniswapRouter,
USDC,
WETH,
amount,
1 // amountOutMin (set with slippage tolerance)
);
// Step 2: Swap WETH → USDC (reverse trade)
uint[] memory usdcBack = swapOnUniswapV2(
sushiswapRouter,
WETH,
USDC,
wethOut[1],
amount + premium
);
// Step 3: Approve repayment
IERC20(asset).approve(address(POOL), amount + premium);
// Optional: Emit log
emit ArbitrageExecuted(msg.sender, usdcBack[1] - (amount + premium));
return true;
}
Managing Slippage and Safety
Hardcoding amountOutMin = 0 is dangerous. Always set a slippage tolerance (e.g., 0.5%) using off-chain price feeds or oracles.
Best practice:
uint amountOutMin = expectedOutput * 995 / 1000; // 0.5% slippage
If actual swap returns less, the transaction will fail safely.
Conclusion
DEX arbitrage is the beating heart of DeFi strategy. By combining flashloans from Aave, liquidity from Uniswap V2/V3, and Paraswap’s aggregation power, you can execute atomic trades that capitalize on price inefficiencies.
As you refine your bot, focus on slippage management, gas efficiency, and execution speed—because in arbitrage, milliseconds and decimals make the difference between profit and loss.
Call to Action
Want this code ready to run?
👉 Download the Aave V3 Flashloan Bot Source Code
If you’re interested, you can read other chapters here.
Chapter 1: Flash Loan Arbitrage with Solidity: The Ultimate Beginner’s Guide
Chapter 2: The Anatomy of a Flash Loan in Solidity
Chapter 3: Solidity Fundamentals for Flash Loan Developers
Chapter 4: Interfaces and Functions — Building Blocks of Flashloans
Chapter 5: Aave V3 Flashloan Developer Guide — How to Harness DeFi Liquidity Like a Pro
Chapter 6: 7 Proven Strategies for Arbitrage Execution Across DEXs with Uniswap & Paraswap