Skip to main content

UniswapV3Swapper

Git Source

Title: UniswapV3Swapper

Author: Golem Foundation

Uniswap V3 swap utilities for exact input and exact output swaps

Address variables default to ETH mainnet. Inheriting contract must set uniFees for token pairs.

Notes:

State Variables

minAmountToSell

uint256 public minAmountToSell

base

address public base = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2

router

address public router = 0xE592427A0AEce92De3Edee1F18E0157C05861564

uniFees

mapping(address => mapping(address => uint24)) public uniFees

Functions

_setUniFees

Set fees bidirectionally for token pair

function _setUniFees(address _token0, address _token1, uint24 _fee) internal virtual;

Parameters

NameTypeDescription
_token0addressFirst token address
_token1addressSecond token address
_feeuint24Pool fee in basis points

_swapFrom

Exact input swap with automatic routing (direct or via base token)

function _swapFrom(address _from, address _to, uint256 _amountIn, uint256 _minAmountOut)
internal
virtual
returns (uint256 _amountOut);

Parameters

NameTypeDescription
_fromaddressToken address to swap from
_toaddressToken address to swap to
_amountInuint256Amount of _from to swap
_minAmountOutuint256Minimum amount of _to to receive

Returns

NameTypeDescription
_amountOutuint256Actual amount of _to received

_swapTo

Exact output swap with automatic routing (direct or via base token)

function _swapTo(address _from, address _to, uint256 _amountTo, uint256 _maxAmountFrom)
internal
virtual
returns (uint256 _amountIn);

Parameters

NameTypeDescription
_fromaddressToken address to swap from
_toaddressToken address to swap to
_amountTouint256Amount of _to needed out
_maxAmountFromuint256Maximum amount of _from to swap

Returns

NameTypeDescription
_amountInuint256Actual amount of _from swapped

_checkAllowance

Ensure sufficient allowance for contract to spend token

function _checkAllowance(address _contract, address _token, uint256 _amount) internal virtual;

Parameters

NameTypeDescription
_contractaddressAddress of contract that will move the token
_tokenaddressToken address to approve
_amountuint256Amount to approve