V2Router02
V2Router02 is used as a intermediate contract to interact with liquidity pools, or the lower level V2Pair contract. The contract can be used to swap, add liquidity, withdraw liquidity. The contract also contains many variations of these three tasks that can be used for each unique situation like using native gas tokens (i.e. ETH), and Fee-On-Transfer tokens with a tax when transfers happen.
The full contract can be found here.
Read-Only Functions
factory
Returns factory address.
WETH
Returns the canonical WETH address on the Ethereum mainnet, or the canonical native wrapped address for the network the contracts are deployed on.
quote
See quote.
getAmountOut
See getAmountOut.
getAmountIn
See getAmountIn.
getAmountsOut
See getAmountsOut.
getAmountsIn
See getAmountsIn.
State-Changing Functions
addLiquidity
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.
amountAMin
andamountBMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
addLiquidityETH
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.amountTokenMin
andamountETHMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
removeLiquidity
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.amountAMin
andamountBMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
removeLiquidityETH
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.amountTokenMin
andamountETHMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
removeLiquidityWithPermit
Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval.
amountAMin
andamountBMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
removeLiquidityETHWithPermit
Removes liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval.
amountAMin
andamountBMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
removeLiquidityETHSupportingFeeOnTransferTokens
Identical to removeLiquidityETH, but succeeds for tokens that take a fee on transfer.
Parameters
Returns
removeLiquidityETHWithPermitSupportingFeeOnTransferTokens
Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.
Parameters
Returns
swapExactTokensForTokens
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).
path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.msg.sender
should have already given the router an allowance of at least amountIn on the input token.amountOutMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapTokensForExactTokens
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 tokens to trade through (if, for example, a direct pair does not exist).
path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.msg.sender
should have already given the router an allowance of at least amountIn on the input token.amountInMax
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapExactETHForTokens
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).
path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.amountOutMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapTokensForExactETH
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.
path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.amountInMax
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapExactTokensForETH
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.
path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.amountOutMin
can be used for slippage protection.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapETHForExactTokens
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
.path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.deadline
is used to set a time restriction on how long it can take for the tx to be executed.
Parameters
Returns
swapExactTokensForTokensSupportingFeeOnTransferTokens
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.
Parameters
swapExactETHForTokensSupportingFeeOnTransferTokens
Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.
Parameters
swapExactTokensForETHSupportingFeeOnTransferTokens
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.
Parameters
Interface
Last updated