Skip to content

UniswapV3Swapper

Git Source

Inherits: ISwapper

A token swapper implementation using Uniswap V3

This contract implements the ISwapper interface to provide token swapping functionality through Uniswap V3 pools. It supports single-hop swaps with configurable fee tiers and slippage protection.

State Variables

uniswapRouter

The Uniswap V3 router contract used for executing swaps

IUniswapSwapRouterLike public immutable uniswapRouter

quoter

The Uniswap V3 quoter contract used for price quotes

IUniswapQuoterLike public immutable quoter

Functions

constructor

Constructs the UniswapV3Swapper contract

constructor(IUniswapSwapRouterLike uniswapRouter_, IUniswapQuoterLike quoter_) ;
Parameters
NameTypeDescription
uniswapRouter_IUniswapSwapRouterLikeThe address of the Uniswap V3 router contract
quoter_IUniswapQuoterLikeThe address of the Uniswap V3 quoter contract

swap

Swaps tokens using Uniswap V3

Executes a single-hop token swap through Uniswap V3 with the specified parameters. The function validates inputs, approves tokens, and executes the swap through the router.

function swap(
    address assetIn,
    uint256 amountIn,
    address assetOut,
    uint256 minAmountOut,
    address recipient,
    bytes calldata swapperParams
)
    public
    virtual
    returns (uint256 amountOut);
Parameters
NameTypeDescription
assetInaddressThe address of the input token to swap from
amountInuint256The amount of input tokens to swap
assetOutaddressThe address of the output token to swap to
minAmountOutuint256The minimum amount of output tokens expected (slippage protection)
recipientaddressThe address that will receive the output tokens
swapperParamsbytesABI-encoded SwapperParams struct containing the fee tier
Returns
NameTypeDescription
amountOutuint256The actual amount of output tokens received from the swap

getAmountOut

Quotes the amount of output tokens for a given input

Uses the Uniswap V3 quoter to estimate the output amount without executing the swap. This is useful for price discovery and slippage calculations.

function getAmountOut(
    address assetIn,
    uint256 amountIn,
    address assetOut,
    bytes calldata swapperParams
)
    public
    virtual
    returns (uint256 amountOut);
Parameters
NameTypeDescription
assetInaddressThe address of the input token
amountInuint256The amount of input tokens
assetOutaddressThe address of the output token
swapperParamsbytesABI-encoded SwapperParams struct containing the fee tier
Returns
NameTypeDescription
amountOutuint256The estimated amount of output tokens

Errors

ZeroAddress

Thrown when a zero address is provided where a valid address is required

error ZeroAddress();

IdenticalAddresses

Thrown when identical addresses are provided for input and output tokens

error IdenticalAddresses();

InsufficientInputAmount

Thrown when the input amount is zero or invalid

error InsufficientInputAmount();

InsufficientOutputAmount

Thrown when the output amount is below the minimum required

error InsufficientOutputAmount();

Structs

SwapperParams

Parameters required for Uniswap V3 swaps

struct SwapperParams {
    uint24 fee; // The fee tier of the Uniswap V3 pool to be used for the swap
}
Properties
NameTypeDescription
feeuint24The fee tier of the Uniswap V3 pool to be used for the swap