DMMRouter02
#
Code#
AddressDMMRouter02
is deployed at 0x1c87257f5e8609940bc751a07bb085bb7f8cdbe6
on the Ethereum mainnet. It was built from commit 04b47545c58fa0ab99d4bb9eae729b3be31efb0c.
#
Read-Only Functions#
factoryReturns the factory address.
#
wethReturns the canonical WETH address on the Ethereum mainnet, or the Ropsten, Rinkeby, Görli, or Kovan testnets.
#
quoteGiven some asset amount and reserves, returns an amount of the other asset representing equivalent value.
#
getAmountsOutFirst calls verifyPoolsPathSwap to verify the validity of the poolsPath
and path
variables.
Then, given an input asset amount and an array of token and corresponding pool addresses, calculates all subsequent maximum output token amounts by calling getTradeInfo for each pair of token addresses in the path in turn, and using these to call getAmountOut.
#
getAmountsInFirst calls verifyPoolsPathSwap to verify the validity of the poolsPath
and path
variables.
Then, given an output asset amount and an array of token addresses, calculates all preceding minimum input token amounts by calling getTradeInfo for each pair of token addresses in the path in turn, and using these to call getAmountIn.
#
State-Changing Functions#
addLiquidityNewPoolCreates a new ERC-20⇄ERC-20 pool.
msg.sender
should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.- There can only be 1 unamplified pool (
ampBps = 1
). If one already exists, the function will instead add liquidity to the existing pool. - Exactly
amountADesired
andamountBDesired
tokens are added.
Name | Type | |
---|---|---|
tokenA | IERC20 | The contract address of the desired token. |
tokenB | IERC20 | The contract address of the desired token. |
ampBps | uint32 | The amplification factor in basis points. Learn more in this section |
amountADesired | uint256 | The amount of tokenA to add as liquidity if the B/A price is <= amountBDesired/amountADesired (A depreciates). |
amountBDesired | uint256 | The amount of tokenB to add as liquidity if the A/B price is <= amountADesired/amountBDesired (B depreciates). |
amountAMin | uint256 | Bounds the extent to which the B/A price can go up before the transaction reverts. Must be <= amountADesired. |
amountBMin | uint256 | 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 | uint256 | Unix timestamp after which the transaction will revert. |
amountA | uint256 | The amount of tokenA sent to the pool. |
amountB | uint256 | The amount of tokenB sent to the pool. |
liquidity | uint256 | The amount of liquidity tokens minted. |
#
addLiquidityAdds liquidity to an existing ERC-20⇄ERC-20 pool.
pool
should already be an existing pool of the token pair oftokenA
andtokenB
. Otherwise, the transaction will revert.msg.sender
should have already given the router an allowance of at leastamountADesired
/amountBDesired
ontokenA
/tokenB
.- Always adds assets at the ideal ratio, according to the price when the transaction is executed.
Name | Type | |
---|---|---|
tokenA | IERC20 | The contract address of the desired token. |
tokenB | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to add liquidity to. |
amountADesired | uint256 | The amount of tokenA to add as liquidity if the B/A price is <= amountBDesired/amountADesired (A depreciates). |
amountBDesired | uint256 | The amount of tokenB to add as liquidity if the A/B price is <= amountADesired/amountBDesired (B depreciates). |
amountAMin | uint256 | Bounds the extent to which the B/A price can go up before the transaction reverts. Must be <= amountADesired. |
amountBMin | uint256 | Bounds the extent to which the A/B price can go up before the transaction reverts. Must be <= amountBDesired. |
vReserveRatioBounds | uint256[2] | Bounds the extent to which the virtual B/A price can move before the transaction reverts. |
to | address | Recipient of the liquidity tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amountA | uint256 | The amount of tokenA sent to the pool. |
amountB | uint256 | The amount of tokenB sent to the pool. |
liquidity | uint256 | The amount of liquidity tokens minted. |
#
addLiquidityNewPoolETHCreates a new ERC-20⇄WETH pool with ETH.
- To cover all possible scenarios,
msg.sender
should have already given the router an allowance of at leastamountTokenDesired
on token. - There can only be 1 unamplified pool (
ampBps = 1
). If one already exists, the function will instead add liquidity to the existing pool. msg.value
is treated as a amountETHDesired.- Exactly
msg.value
andamountTokenDesired
tokens are added.
Name | Type | |
---|---|---|
token | IERC20 | The contract address of the desired token. |
ampBps | uint32 | The amplification factor in basis points. Learn more in this section |
amountTokenDesired | uint256 | The amount of token to add as liquidity if the WETH/token price is <= msg.value /amountTokenDesired (token depreciates). |
msg.value (amountETHDesired) | uint256 | The amount of ETH to add as liquidity if the token/WETH price is <= amountTokenDesired/msg.value (WETH depreciates). |
amountTokenMin | uint256 | Bounds the extent to which the WETH/token price can go up before the transaction reverts. Must be <= amountTokenDesired. |
amountETHMin | uint256 | 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 | uint256 | Unix timestamp after which the transaction will revert. |
amountToken | uint256 | The amount of token sent to the pool. |
amountETH | uint256 | The amount of ETH converted to WETH and sent to the pool. |
liquidity | uint256 | The amount of liquidity tokens minted. |
#
addLiquidityETHAdds liquidity to an existing ERC-20⇄WETH pool with ETH.
pool
should already be an existing pool of the token pair oftokenA
andtokenB
. Otherwise, the transaction will revert.- To cover all possible scenarios,
msg.sender
should have already given the router an allowance of at leastamountTokenDesired
on token. - Always adds assets at the ideal ratio, according to the price when the transaction is executed.
msg.value
is treated asamountETHDesired
.- Leftover ETH, if any, is returned to
msg.sender
.
Name | Type | |
---|---|---|
token | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to add liquidity to. |
amountTokenDesired | uint256 | The amount of token to add as liquidity if the WETH/token price is <= msg.value /amountTokenDesired (token depreciates). |
msg.value (amountETHDesired) | uint256 | The amount of ETH to add as liquidity if the token/WETH price is <= amountTokenDesired/msg.value (WETH depreciates). |
amountTokenMin | uint256 | Bounds the extent to which the WETH/token price can go up before the transaction reverts. Must be <= amountTokenDesired. |
amountETHMin | uint256 | Bounds the extent to which the token/WETH price can go up before the transaction reverts. Must be <= msg.value . |
vReserveRatioBounds | uint256[2] | Bounds the extent to which the virtual B/A price can move before the transaction reverts. |
to | address | Recipient of the liquidity tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amountToken | uint256 | The amount of token sent to the pool. |
amountETH | uint256 | The amount of ETH converted to WETH and sent to the pool. |
liquidity | uint256 | The amount of liquidity tokens minted. |
#
removeLiquidityRemoves liquidity from a specified ERC-20⇄ERC-20 pool.
msg.sender
should have already given the router an allowance of at least liquidity on the pool.
Name | Type | |
---|---|---|
tokenA | IERC20 | The contract address of the desired token. |
tokenB | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountAMin | uint256 | The minimum amount of tokenA that must be received for the transaction not to revert. |
amountBMin | uint256 | The minimum amount of tokenB that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amountA | uint256 | The amount of tokenA received. |
amountB | uint256 | The amount of tokenB received. |
#
removeLiquidityETHRemoves 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 | |
---|---|---|
token | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountTokenMin | uint256 | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint256 | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amountToken | uint256 | The amount of token received. |
amountETH | uint256 | The amount of ETH received. |
#
removeLiquidityWithPermitRemoves liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.
Name | Type | |
---|---|---|
tokenA | IERC20 | The contract address of the desired token. |
tokenB | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountAMin | uint256 | The minimum amount of tokenA that must be received for the transaction not to revert. |
amountBMin | uint256 | The minimum amount of tokenB that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint256(-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 | uint256 | The amount of tokenA received. |
amountB | uint256 | The amount of tokenB received. |
#
removeLiquidityETHWithPermitRemoves liquidity from an ERC-20⇄WETTH pool and receive ETH without pre-approval, thanks to permit.
Name | Type | |
---|---|---|
token | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountTokenMin | uint256 | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint256 | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint256(-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 | uint256 | The amount of token received. |
amountETH | uint256 | The amount of ETH received. |
#
removeLiquidityETHSupportingFeeOnTransferTokensIdentical 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 | |
---|---|---|
token | IERC20 | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountTokenMin | uint256 | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint256 | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amountETH | uint256 | The amount of ETH received. |
#
removeLiquidityETHWithPermitSupportingFeeOnTransferTokensIdentical to removeLiquidityETHWithPermit, but succeeds for tokens that take a fee on transfer.
Name | Type | |
---|---|---|
token | address | The contract address of the desired token. |
pool | address | The contract address of the desired pool to remove liquidity from. |
liquidity | uint256 | The amount of liquidity tokens to remove. |
amountTokenMin | uint256 | The minimum amount of token that must be received for the transaction not to revert. |
amountETHMin | uint256 | The minimum amount of ETH that must be received for the transaction not to revert. |
to | address | Recipient of the underlying assets. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
approveMax | bool | Whether or not the approval amount in the signature is for liquidity or uint256(-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 | uint256 | The amount of ETH received. |
#
swapExactTokensForTokensSwaps an exact amount of input tokens for as many output tokens as possible. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
msg.sender
should have already given the router an allowance of at leastamountIn
on the input token.
Name | Type | |
---|---|---|
amountIn | uint256 | The amount of input tokens to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapTokensForExactTokensReceive an exact amount of output tokens for as few input tokens as possible. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
msg.sender
should have already given the router an allowance of at leastamountInMax
on the input token.
Name | Type | |
---|---|---|
amountOut | uint256 | The amount of output tokens to receive. |
amountInMax | uint256 | The maximum amount of input tokens that can be required before the transaction reverts. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapExactETHForTokensSwaps an exact amount of ETH for as many output tokens as possible. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
Name | Type | |
---|---|---|
msg.value (amountIn) | uint256 | The amount of ETH to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapTokensForExactETHReceive an exact amount of ETH for as few input tokens as possible. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
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.
Name | Type | |
---|---|---|
amountOut | uint256 | The amount of ETH to receive. |
amountInMax | uint256 | The maximum amount of input tokens that can be required before the transaction reverts. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of ETH. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapExactTokensForETHSwaps an exact amount of tokens for as much ETH as possible. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
- If the to address is a smart contract, it must have the ability to receive ETH.
Name | Type | |
---|---|---|
amountIn | uint256 | The amount of input tokens to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the ETH. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapETHForExactTokensReceive an exact amount of tokens for as little ETH as possible.
. The token and pool routes are specified by the poolsPath
and path
variables respectively.
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). The nth element of poolsPath
is the first pool to be used for the nth and (n+1)th elements of path
. It therefore is a requirement for poolsPath
to be a size smaller than path
, ie. poolsPath.length = path.length - 1
.
For more information on deciding which pools to use, see this section.
- Leftover ETH, if any, is returned to
msg.sender
.
Name | Type | |
---|---|---|
amountOut | uint256 | The amount of tokens to receive. |
msg.value (amountInMax) | uint256 | The maximum amount of ETH that can be required before the transaction reverts. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
amounts | uint256[] memory | The input token amount and all subsequent output token amounts. |
#
swapExactTokensForTokensSupportingFeeOnTransferTokensIdentical to swapExactTokensForTokens, but succeeds for tokens that take a fee on transfer.
msg.sender
should have already given the router an allowance of at leastamountIn
on the input token.
Name | Type | |
---|---|---|
amountIn | uint256 | The amount of input tokens to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
#
swapExactETHForTokensSupportingFeeOnTransferTokensIdentical to swapExactETHForTokens, but succeeds for tokens that take a fee on transfer.
Name | Type | |
---|---|---|
msg.value (amountIn) | uint256 | The amount of ETH to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the output tokens. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
#
swapExactTokensForETHSupportingFeeOnTransferTokensIdentical 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 | |
---|---|---|
amountIn | uint256 | The amount of input tokens to send. |
amountOutMin | uint256 | The minimum amount of output tokens that must be received for the transaction not to revert. |
poolsPath | address[] calldata | An array of pool addresses. poolsPath.length must be smaller than path.length by 1. Each pool specified is a pool for each consecutive pair of addresses and must have liquidity. |
path | IERC20[] calldata | An array of token addresses. path.length must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity. |
to | address | Recipient of the ETH. |
deadline | uint256 | Unix timestamp after which the transaction will revert. |
#
Internal Functions#
verifyPoolsPathSwapVerifies the length of the poolsPath
and path
variables, and will call verifyPoolAddress
for each pool specified per token pair.
#
verifyPoolAddressChecks requirement that pool
is indeed a valid pool address for the token pair tokenA
and tokenB
.