Router
Our router contract is based on the Uniswap V2 router, but the functions differs in spots.
The primary change to watch out for is the extra parameters for signifying stable pools.
Parameter | Type |
---|---|
from | address |
to | address |
stable | bool |
function factory() external pure returns (address);
Returns factory address.
function WETH() external pure returns (address);
Returns the address of the wrapped native token (e.g. WBNB on BNB Chain).
function isPair(address pair) external view returns (bool);
Checks if the address is an existing pair.
function sortTokens(address tokenA, address tokenB) public pure returns (address token0, address token1);
Sorts token addresses.
function pairFor(address tokenA, address tokenB, bool stable) internal pure returns (address pair);
Calculates the address for a pair and pool type without making any external calls.
function getReserves(address tokenA, address tokenB, bool stable) public view returns (uint reserveA, uint reserveB);
Calls getReserves on the pair for the passed tokens, and returns the results.
function quote(uint amountA, uint reserveA, uint reserveB) public pure returns (uint amountB);
Given some asset amount and reserves, returns an amount of the other asset representing equivalent value.
function getAmountOut(uint amountIn, address tokenIn, address tokenOut) public view returns (uint amountOut, bool stable);
Given an input asset and output asset, returns the maximum output amount of the output asset (accounting for fees) and if the best version of that pool is stable or not.
- Used in getAmountsOut.
function getAmountOut(uint amountIn, uint tokenIn, uint tokenOut, bool isStable) public view returns (uint amountOut);
Given an input asset, an output asset, and a flag representing if the pool is a stable pool, returns the maximum output amount of the output asset (accounting for fees).
- Used in getAmountsOut.
function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts);
Given an input asset amount and an array of token addresses, calculates all subsequent maximum output token amounts by calling getReserves for each pair of token addresses in the path in turn, and using these to call getAmountOut.
- Useful for calculating optimal token amounts before calling swap.
- Auto-routes through optimal pool types.
function getAmountsOut(uint amountIn, route[] memory routes) public view returns (uint[] memory amounts);
Given an input asset amount and an array of routes, calculates all subsequent maximum output token amounts by calling getReserves for each pair of token addresses in the path in turn, and using these to call getAmountOut.
- Useful for calculating optimal token amounts before calling swap.
- Routes through specified pool types.
function addLiquidity(address tokenA, address tokenB, bool stable, uint amountADesired, uint amountBDesired, uint amountAMin, uint amountBMin, address to, uint deadline) external returns (uint amountA, uint amountB, uint liquidity);
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.
Name | Type | Text |
---|---|---|
tokenA | address | A pool token. |
tokenB | address | A pool token. |
stable | boolean | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
amountADesired | uint | The amount of tokenA to add as liquidity if the B/A price is <= amountBDesired/amountADesired (A depreciates). |
amountBDesired | uint | The amount of tokenB to add as liquidity if the A/B price is <= amountADesired/amountBDesired (B depreciates). |
amountAMin | uint | Bounds the extent to which the B/A price can go up before the transaction reverts. Must be <= amountADesired. |
amountBMin | uint | Bounds the extent to which the A/B price can go up before the transaction reverts. Must be <= amountBDesired. |
to | address | Recipient of the liquidity tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amountA | uint | The amount of tokenA sent to the pool. |
amountB | uint | The amount of tokenB sent to the pool. |
liquidity | uint | The amount of liquidity tokens minted. |
function addLiquidityETH(address token, bool stable, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external payable returns (uint amountToken, uint amountETH, uint liquidity);
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.
Name | Type | Text |
---|---|---|
token | address | A pool token. |
stable | boolean | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
amountTokenDesired | uint | The amount of token to add as liquidity if the WETH/token price is <= msg.value /amountTokenDesired (token depreciates). |
msg.value (amountETHDesired) | uint | The amount of ETH to add as liquidity if the token/WETH price is <= amountTokenDesired/ msg.value (WETH depreciates). |
amountTokenMin | uint | Bounds the extent to which the WETH/token price can go up before the transaction reverts. Must be <= amountTokenDesired. |
amountETHMin | uint | Bounds the extent to which the token/WETH price can go up before the transaction reverts. Must be <= msg.value . |
to | address | Recipient of the liquidity tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amountToken | uint | The amount of token sent to the pool. |
amountETH | uint | The amount of ETH converted to WETH and sent to the pool. |
liquidity | uint | The amount of liquidity tokens minted. |
function removeLiquidity(address tokenA, address tokenB, bool stable, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline) external returns (uint amountA, uint amountB);
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.
Name | Type | Text |
---|---|---|
tokenA | address | A pool token. |
tokenB | address | A pool token. |
stable | boolean | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountAMin | uint | The minimum amount of tokenA that must be received for the transaction not to revert. |
amountBMin | uint | The minimum amount of tokenB that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amountA | uint | The amount of tokenA received. |
amountB | uint | The amount of tokenB received. |
function removeLiquidityETH(address token, uint liquidity, bool stable, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external returns (uint amountToken, uint amountETH);
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.
Name | Type | Text |
---|---|---|
token | address | A pool token. |
stable | boolean | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountTokenMin | uint | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amountToken | uint | The amount of token received. |
amountETH | uint | The amount of ETH received. |
function removeLiquidityWithPermit(address tokenA, address tokenB, bool stable, uint liquidity, uint amountAMin, uint amountBMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountA, uint amountB);
Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.
Name | Type | Text |
---|---|---|
tokenA | address | A pool token. |
tokenB | address | A pool token. |
stable | bool | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountAMin | uint | The minimum amount of tokenA that must be received for the transaction not to revert. |
amountBMin | uint | The minimum amount of tokenB that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint(-1) . |
v | uint8 | The v component of the permit signature. |
r | bytes32 | The r component of the permit signature. |
s | bytes32 | The s component of the permit signature. |
| | |
amountA | uint | The amount of tokenA received. |
amountB | uint | The amount of tokenB received. |
function removeLiquidityETHWithPermit(address token, bool stable, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountToken, uint amountETH);
Name | Type | Text |
---|---|---|
token | address | A pool token. |
stable | bool | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountTokenMin | uint | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint(-1) . |
v | uint8 | The v component of the permit signature. |
r | bytes32 | The r component of the permit signature. |
s | bytes32 | The s component of the permit signature. |
| | |
amountToken | uint | The amount of token received. |
amountETH | uint | The amount of ETH received. |
function removeLiquidityETHSupportingFeeOnTransferTokens(address token, bool stable, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external returns (uint amountETH);
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.
Name | Type | Text |
---|---|---|
token | address | A pool token. |
stable | bool | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountTokenMin | uint | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amountETH | uint | The amount of ETH received. |
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(address token, bool stable, uint liquidity, uint amountTokenMin, uint amountETHMin, address to, uint deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) external returns (uint amountETH);
Identical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.
Name | Type | Text |
---|---|---|
token | address | A pool token. |
stable | bool | Flag to decide if the pool is for a stable pair, true if both assets are stable. |
liquidity | uint | The amount of liquidity tokens to remove. |
amountTokenMin | uint | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint(-1) . |
v | uint8 | The v component of the permit signature. |
r | bytes32 | The r component of the permit signature. |
s | bytes32 | The s component of the permit signature. |
| | |
amountETH | uint | The amount of ETH received. |
function swapExactTokensForTokens(uint amountIn, uint amountOutMin, route[] calldata routes, address to, uint deadline) external returns (uint[] memory amounts);
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.
Name | Type | Text |
---|---|---|
amountIn | uint | The amount of input tokens to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. path.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amounts | uint[] memory | The input token amount and all subsequent output token amounts. |
function swapExactETHForTokens(uint amountOutMin, route[] calldata routes, address to, uint deadline) external payable returns (uint[] memory amounts);
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).
Name | Type | Text |
---|---|---|
msg.value (amountIn) | uint | The amount of ETH to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. routes.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amounts | uint[] memory | The input token amount and all subsequent output token amounts. |
function swapExactTokensForETH(uint amountIn, uint amountOutMin, route[] calldata routes, address to, uint deadline) external returns (uint[] memory amounts);
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.
Name | Type | Text |
---|---|---|
amountIn | uint | The amount of input tokens to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. routes.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the ETH. |
deadline | uint | Unix timestamp after which the transaction will revert. |
| | |
amounts | uint[] memory | The input token amount and all subsequent output token amounts. |
function swapExactTokensForTokensSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, route[] calldata routes, address to, uint deadline) external;
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.
Name | Type | Text |
---|---|---|
amountIn | uint | The amount of input tokens to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. routes.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, route[] calldata routes, address to, uint deadline) external payable;
Identical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.
Name | Type | Text |
---|---|---|
msg.value (amountIn) | uint | The amount of ETH to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. routes.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint | Unix timestamp after which the transaction will revert. |
function swapExactTokensForETHSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, route[] calldata routes, address to, uint deadline) external;
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.
Name | Type | Text |
---|---|---|
amountIn | uint | The amount of input tokens to send. |
amountOutMin | uint | The minimum amount of output tokens that must be received for the transaction not to revert. |
routes | route[] calldata | An array of swap routes. routes.length must be >= 1. Each consecutive pair must exist and have liquidity. |
to | address | Recipient of the ETH. |
deadline | uint | Unix timestamp after which the transaction will revert. |
Last modified 8mo ago