Router02

Because routers are stateless and do not hold token balances, they can be replaced safely and trustlessly, if necessary. This may happen if more efficient smart contract patterns are discovered, or if additional functionality is desired. For this reason, routers have release numbers, starting at 01. This is currently recommended release, 02.

Code

UniswapV2Router02.sol

Address

UniswapV2Router02 is deployed at 0x28e0f3ebab59a998C4f1019358388B5E2ca92cfAon on Arbitrum Nova

Read-Only Functions

factory

function factory() external pure returns (address);Copy

Returns factory address.

WETH

function WETH() external pure returns (address);Copy

Returns the canonical WETH address on the Ethereum mainnet, or the Ropsten, Rinkeby, Görli, or Kovan testnets.

State-Changing Functions

addLiquidity

function addLiquidity(  address tokenA,  address tokenB,  uint amountADesired,  uint amountBDesired,  uint amountAMin,  uint amountBMin,  address to,  uint deadline) external returns (uint amountA, uint amountB, uint liquidity);Copy

Adds liquidity to an ERC-20⇄ERC-20 pool.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.

  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.

  • If a pool for the passed tokens does not exists, one is created automatically, and exactly amountADesired/amountBDesired tokens are added.

addLiquidityETH

function addLiquidityETH(  address token,  uint amountTokenDesired,  uint amountTokenMin,  uint amountETHMin,  address to,  uint deadline) external payable returns (uint amountToken, uint amountETH, uint liquidity);Copy

Adds liquidity to an ERC-20⇄WETH pool with ETH.

  • To cover all possible scenarios, msg.sender should have already given the router an allowance of at least amountTokenDesired on token.

  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.

  • msg.value is treated as a amountETHDesired.

  • Leftover ETH, if any, is returned to msg.sender.

  • If a pool for the passed token and WETH does not exists, one is created automatically, and exactly amountTokenDesired/msg.value tokens are added.

removeLiquidity

function removeLiquidity(  address tokenA,  address tokenB,  uint liquidity,  uint amountAMin,  uint amountBMin,  address to,  uint deadline) external returns (uint amountA, uint amountB);Copy

Removes liquidity from an ERC-20⇄ERC-20 pool.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.

removeLiquidityETH

function removeLiquidityETH(  address token,  uint liquidity,  uint amountTokenMin,  uint amountETHMin,  address to,  uint deadline) external returns (uint amountToken, uint amountETH);Copy

Removes liquidity from an ERC-20⇄WETH pool and receive ETH.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.

removeLiquidityWithPermit

function removeLiquidityWithPermit(  address tokenA,  address tokenB,  uint liquidity,  uint amountAMin,  uint amountBMin,  address to,  uint deadline,  bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountA, uint amountB);Copy

Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.

removeLiquidityETHWithPermit

function removeLiquidityETHWithPermit(  address token,  uint liquidity,  uint amountTokenMin,  uint amountETHMin,  address to,  uint deadline,  bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountToken, uint amountETH);Copy

Removes liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval, thanks to permit.

removeLiquidityETHSupportingFeeOnTransferTokens

function removeLiquidityETHSupportingFeeOnTransferTokens(  address token,  uint liquidity,  uint amountTokenMin,  uint amountETHMin,  address to,  uint deadline) external returns (uint amountETH);Copy

Identical to removeLiquidityETH, but succeeds for tokens that take a fee on transfer.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.

removeLiquidityETHWithPermitSupportingFeeOnTransferTokens

function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(  address token,  uint liquidity,  uint amountTokenMin,  uint amountETHMin,  address to,  uint deadline,  bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountETH);Copy

Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.

swapExactTokensForTokens

function swapExactTokensForTokens(  uint amountIn,  uint amountOutMin,  address[] calldata path,  address to,  uint deadline) external returns (uint[] memory amounts);Copy

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • msg.sender should have already given the router an allowance of at least amountIn on the input token.

swapTokensForExactTokens

function swapTokensForExactTokens(  uint amountOut,  uint amountInMax,  address[] calldata path,  address to,  uint deadline) external returns (uint[] memory amounts);Copy

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • msg.sender should have already given the router an allowance of at least amountInMax on the input token.

swapExactETHForTokens

function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)  external  payable  returns (uint[] memory amounts);Copy

Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

swapTokensForExactETH

function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)  external  returns (uint[] memory amounts);Copy

Receive an exact amount of ETH for as few input tokens as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • msg.sender should have already given the router an allowance of at least amountInMax on the input token.

  • If the to address is a smart contract, it must have the ability to receive ETH.

swapExactTokensForETH

function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)  external  returns (uint[] memory amounts);Copy

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path. The first element of path is the input token, the last must be WETH, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • If the to address is a smart contract, it must have the ability to receive ETH.

swapETHForExactTokens

function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)  external  payable  returns (uint[] memory amounts);Copy

Receive an exact amount of tokens for as little ETH as possible, along the route determined by the path. The first element of path must be WETH, the last is the output token and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).

  • Leftover ETH, if any, is returned to msg.sender.

swapExactTokensForTokensSupportingFeeOnTransferTokens

function swapExactTokensForTokensSupportingFeeOnTransferTokens(  uint amountIn,  uint amountOutMin,  address[] calldata path,  address to,  uint deadline) external;Copy

Identical to swapExactTokensForTokens, but succeeds for tokens that take a fee on transfer.

  • msg.sender should have already given the router an allowance of at least amountIn on the input token.

swapExactETHForTokensSupportingFeeOnTransferTokens

function swapExactETHForTokensSupportingFeeOnTransferTokens(  uint amountOutMin,  address[] calldata path,  address to,  uint deadline) external payable;Copy

Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.

swapExactTokensForETHSupportingFeeOnTransferTokens

function swapExactTokensForETHSupportingFeeOnTransferTokens(  uint amountIn,  uint amountOutMin,  address[] calldata path,  address to,  uint deadline) external;Copy

Identical to swapExactTokensForETH, but succeeds for tokens that take a fee on transfer.

  • If the to address is a smart contract, it must have the ability to receive ETH.

quote

See quote.

getAmountOut

See getAmountOut.

getAmountIn

See getAmountIn.

getAmountsOut

function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts);Copy

See getAmountsOut.

getAmountsIn

function getAmountsIn(uint amountOut, address[] memory path) public view returns (uint[] memory amounts);Copy

See getAmountsIn.

Interface

import '@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol';Copy
pragma solidity >=0.6.2;interface IUniswapV2Router01 {    function factory() external pure returns (address);    function WETH() external pure returns (address);    function addLiquidity(        address tokenA,        address tokenB,        uint amountADesired,        uint amountBDesired,        uint amountAMin,        uint amountBMin,        address to,        uint deadline    ) external returns (uint amountA, uint amountB, uint liquidity);    function addLiquidityETH(        address token,        uint amountTokenDesired,        uint amountTokenMin,        uint amountETHMin,        address to,        uint deadline    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);    function removeLiquidity(        address tokenA,        address tokenB,        uint liquidity,        uint amountAMin,        uint amountBMin,        address to,        uint deadline    ) external returns (uint amountA, uint amountB);    function removeLiquidityETH(        address token,        uint liquidity,        uint amountTokenMin,        uint amountETHMin,        address to,        uint deadline    ) external returns (uint amountToken, uint amountETH);    function removeLiquidityWithPermit(        address tokenA,        address tokenB,        uint liquidity,        uint amountAMin,        uint amountBMin,        address to,        uint deadline,        bool approveMax, uint8 v, bytes32 r, bytes32 s    ) external returns (uint amountA, uint amountB);    function removeLiquidityETHWithPermit(        address token,        uint liquidity,        uint amountTokenMin,        uint amountETHMin,        address to,        uint deadline,        bool approveMax, uint8 v, bytes32 r, bytes32 s    ) external returns (uint amountToken, uint amountETH);    function swapExactTokensForTokens(        uint amountIn,        uint amountOutMin,        address[] calldata path,        address to,        uint deadline    ) external returns (uint[] memory amounts);    function swapTokensForExactTokens(        uint amountOut,        uint amountInMax,        address[] calldata path,        address to,        uint deadline    ) external returns (uint[] memory amounts);    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)        external        payable        returns (uint[] memory amounts);    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)        external        returns (uint[] memory amounts);    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)        external        returns (uint[] memory amounts);    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)        external        payable        returns (uint[] memory amounts);    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);}interface IUniswapV2Router02 is IUniswapV2Router01 {    function removeLiquidityETHSupportingFeeOnTransferTokens(        address token,        uint liquidity,        uint amountTokenMin,        uint amountETHMin,        address to,        uint deadline    ) external returns (uint amountETH);    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(        address token,        uint liquidity,        uint amountTokenMin,        uint amountETHMin,        address to,        uint deadline,        bool approveMax, uint8 v, bytes32 r, bytes32 s    ) external returns (uint amountETH);    function swapExactTokensForTokensSupportingFeeOnTransferTokens(        uint amountIn,        uint amountOutMin,        address[] calldata path,        address to,        uint deadline    ) external;    function swapExactETHForTokensSupportingFeeOnTransferTokens(        uint amountOutMin,        address[] calldata path,        address to,        uint deadline    ) external payable;    function swapExactTokensForETHSupportingFeeOnTransferTokens(        uint amountIn,        uint amountOutMin,        address[] calldata path,        address to,        uint deadline    ) external;}Copy

ABI

import IUniswapV2Router02 from '@uniswap/v2-periphery/build/IUniswapV2Router02.json'Copy

https://unpkg.com/@uniswap/v2-periphery@1.1.0-beta.0/build/IUniswapV2Router02.json

Last updated