V2 Router
DiamondSwap Router02
The DiamondSwap Router02 contract is an advanced interface for interacting with the DiamondSwap protocol, extending the functionality of Router01 with additional features. Like Router01, Router02 simplifies the process of swapping tokens, adding and removing liquidity, and interacting with the protocol’s liquidity pools. It also introduces new functions such as support for removing liquidity with permit signatures and the ability to swap tokens supporting fee-on-transfer tokens. Below is a detailed explanation of the key functions and features of the DiamondSwap Router02 contract.
For more details on the V1 Router, you can refer to the DiamondSwap V1 Router documentation.
Overview
The DiamondSwap Router02 contract builds upon the capabilities of Router01 by adding more advanced features that improve flexibility and ease of use. It allows users to perform token swaps, manage liquidity, and execute other decentralized finance (DeFi) operations more effectively. The Router02 contract includes all the functionality of Router01 while introducing new functions to handle more complex scenarios, such as interacting with fee-on-transfer tokens.
Functions
addLiquidity(address tokenA, address tokenB, uint256 amountADesired, uint256 amountBDesired, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) → (uint256 amountA, uint256 amountB, uint256 liquidity)
addLiquidity(address tokenA, address tokenB, uint256 amountADesired, uint256 amountBDesired, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) → (uint256 amountA, uint256 amountB, uint256 liquidity)
This function adds liquidity to a DiamondSwap pool, given a pair of tokens. It ensures that the user provides the correct ratio of tokens based on the pool's current reserves.
- tokenA: The address of the first token.
- tokenB: The address of the second token.
- amountADesired: The desired amount of tokenA to add as liquidity.
- amountBDesired: The desired amount of tokenB to add as liquidity.
- amountAMin: The minimum amount of tokenA that must be added (to protect against price slippage).
- amountBMin: The minimum amount of tokenB that must be added (to protect against price slippage).
- to: The address that will receive the liquidity tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: A tuple containing:
amountA
: The actual amount of tokenA added as liquidity.amountB
: The actual amount of tokenB added as liquidity.liquidity
: The amount of liquidity tokens minted.
This function is essential for liquidity providers who want to contribute to the protocol's pools and earn a share of the trading fees.
addLiquidityETH(address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) → (uint256 amountToken, uint256 amountETH, uint256 liquidity)
addLiquidityETH(address token, uint256 amountTokenDesired, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) → (uint256 amountToken, uint256 amountETH, uint256 liquidity)
This function adds liquidity to a DiamondSwap pool where one of the assets is ETH. The function wraps ETH into WETH (Wrapped ETH) before adding liquidity.
- token: The address of the ERC20 token.
- amountTokenDesired: The desired amount of the ERC20 token to add as liquidity.
- amountTokenMin: The minimum amount of the ERC20 token that must be added (to protect against price slippage).
- amountETHMin: The minimum amount of ETH that must be added (to protect against price slippage).
- to: The address that will receive the liquidity tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: A tuple containing:
amountToken
: The actual amount of the ERC20 token added as liquidity.amountETH
: The actual amount of ETH added as liquidity.liquidity
: The amount of liquidity tokens minted.
This function simplifies the process of providing liquidity to ETH pairs, allowing users to add both ETH and an ERC20 token to the pool in a single transaction.
removeLiquidity(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) → (uint256 amountA, uint256 amountB)
removeLiquidity(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline) → (uint256 amountA, uint256 amountB)
This function removes liquidity from a DiamondSwap pool, returning the underlying tokens to the user.
- tokenA: The address of the first token.
- tokenB: The address of the second token.
- liquidity: The amount of liquidity tokens to burn.
- amountAMin: The minimum amount of tokenA that must be received (to protect against price slippage).
- amountBMin: The minimum amount of tokenB that must be received (to protect against price slippage).
- to: The address that will receive the underlying tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: A tuple containing:
amountA
: The actual amount of tokenA received.amountB
: The actual amount of tokenB received.
This function is crucial for liquidity providers who want to withdraw their liquidity from the pool and reclaim their assets, along with any accumulated fees.
removeLiquidityETH(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) → (uint256 amountToken, uint256 amountETH)
removeLiquidityETH(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline) → (uint256 amountToken, uint256 amountETH)
This function removes liquidity from a DiamondSwap pool where one of the assets is ETH. The function converts WETH back into ETH before returning it to the user.
- token: The address of the ERC20 token.
- liquidity: The amount of liquidity tokens to burn.
- amountTokenMin: The minimum amount of the ERC20 token that must be received (to protect against price slippage).
- amountETHMin: The minimum amount of ETH that must be received (to protect against price slippage).
- to: The address that will receive the underlying tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: A tuple containing:
amountToken
: The actual amount of the ERC20 token received.amountETH
: The actual amount of ETH received.
This function simplifies the process of removing liquidity from ETH pairs, allowing users to receive both ETH and the ERC20 token in a single transaction.
removeLiquidityWithPermit(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) → (uint256 amountA, uint256 amountB)
removeLiquidityWithPermit(address tokenA, address tokenB, uint256 liquidity, uint256 amountAMin, uint256 amountBMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) → (uint256 amountA, uint256 amountB)
This function removes liquidity from a DiamondSwap pool using an off-chain signature to approve the transaction. This allows users to save on gas fees by avoiding the need for an on-chain approval transaction.
- tokenA: The address of the first token.
- tokenB: The address of the second token.
- liquidity: The amount of liquidity tokens to burn.
- amountAMin: The minimum amount of tokenA that must be received (to protect against price slippage).
- amountBMin: The minimum amount of tokenB that must be received (to protect against price slippage).
- to: The address that will receive the underlying tokens.
- deadline: The timestamp by which the transaction must be completed.
- approveMax: If
true
, sets the allowance to the maximum possible value. - v, r, s: Components of the off-chain signature.
- Returns: A tuple containing:
amountA
: The actual amount of tokenA received.amountB
: The actual amount of tokenB received.
This function is especially useful for users who want to remove liquidity while minimizing gas costs by using off-chain signatures for approval.
removeLiquidityETHWithPermit(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) → (uint256 amountToken, uint256 amountETH)
removeLiquidityETHWithPermit(address token, uint256 liquidity, uint256 amountTokenMin, uint256 amountETHMin, address to, uint256 deadline, bool approveMax, uint8 v, bytes32 r, bytes32 s) → (uint256 amountToken, uint256 amountETH)
This function removes liquidity from a DiamondSwap pool where one of the assets is ETH, using an off-chain signature to approve the transaction.
- token: The address of the ERC20 token.
- liquidity: The amount of liquidity tokens to burn.
- amountTokenMin: The minimum amount of the ERC20 token that must be received (to protect against price slippage).
- amountETHMin: The minimum amount of ETH that must be received (to protect against price slippage).
- to: The address that will receive the underlying tokens.
- deadline: The timestamp by which the transaction must be completed.
- approveMax: If
true
, sets the allowance to the maximum possible value. - v, r, s: Components of the off-chain signature.
- Returns: A tuple containing:
amountToken
: The actual amount of the ERC20 token received.amountETH
: The actual amount of ETH received.
This function allows users to remove liquidity from ETH pairs with minimized gas costs by utilizing off-chain signature approvals.
swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps an exact amount of input tokens for as many output tokens as possible, given the reserves of the pools along the swap path.
- amountIn: The amount of the input token to swap.
- amountOutMin: The minimum amount of the output token that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function is essential for users who want to swap tokens within the DiamondSwap protocol, ensuring that they receive the best possible output while protecting against slippage.
swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps as few input tokens as possible to receive an exact amount of output tokens, given the reserves of the pools along the swap path.
- amountOut: The exact amount of the output token to receive.
- amountInMax: The maximum amount of the input token that can be swapped (to protect against price slippage).
- path: An array of token addresses representing the swap path.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function is useful for users who need a specific amount of output tokens and want to minimize the input token amount, ensuring that they don’t overpay for the swap.
swapExactETHForTokens(uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapExactETHForTokens(uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps an exact amount of ETH for as many output tokens as possible, given the reserves of the pools along the swap path.
- amountOutMin: The minimum amount of the output token that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path, starting with WETH.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function simplifies the process of swapping ETH for other tokens, making it easy for users to trade ETH directly within the DiamondSwap protocol.
swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps as few input tokens as possible to receive an exact amount of ETH, given the reserves of the pools along the swap path.
- amountOut: The exact amount of ETH to receive.
- amountInMax: The maximum amount of the input token that can be swapped (to protect against price slippage).
- path: An array of token addresses representing the swap path, ending with WETH.
- to: The address that will receive the ETH.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function is useful for users who need a specific amount of ETH and want to minimize the input token amount, ensuring that they don’t overpay for the swap.
swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps an exact amount of input tokens for as much ETH as possible, given the reserves of the pools along the swap path.
- amountIn: The amount of the input token to swap.
- amountOutMin: The minimum amount of ETH that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path, ending with WETH.
- to: The address that will receive the ETH.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function allows users to swap their tokens for ETH, ensuring they receive the best possible output while protecting against slippage.
swapETHForExactTokens(uint256 amountOut, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
swapETHForExactTokens(uint256 amountOut, address[] calldata path, address to, uint256 deadline) → uint256[] memory amounts
This function swaps as little ETH as possible to receive an exact amount of output tokens, given the reserves of the pools along the swap path.
- amountOut: The exact amount of the output token to receive.
- path: An array of token addresses representing the swap path, starting with WETH.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
- Returns: An array containing the amounts of tokens involved in each step of the swap.
This function is useful for users who need a specific amount of tokens and want to minimize the ETH amount, ensuring that they don’t overpay for the swap.
swapExactTokensForTokensSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
swapExactTokensForTokensSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
This function swaps an exact amount of input tokens for as many output tokens as possible, supporting tokens that charge a fee on transfer.
- amountIn: The amount of the input token to swap.
- amountOutMin: The minimum amount of the output token that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
This function is useful when dealing with tokens that deduct a fee during transfers, ensuring that the swap still completes successfully.
swapExactETHForTokensSupportingFeeOnTransferTokens(uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
swapExactETHForTokensSupportingFeeOnTransferTokens(uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
This function swaps an exact amount of ETH for as many output tokens as possible, supporting tokens that charge a fee on transfer.
- amountOutMin: The minimum amount of the output token that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path, starting with WETH.
- to: The address that will receive the output tokens.
- deadline: The timestamp by which the transaction must be completed.
This function is useful when dealing with fee-on-transfer tokens, ensuring that the ETH swap is executed successfully.
swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
swapExactTokensForETHSupportingFeeOnTransferTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline)
This function swaps an exact amount of input tokens for as much ETH as possible, supporting tokens that charge a fee on transfer.
- amountIn: The amount of the input token to swap.
- amountOutMin: The minimum amount of ETH that must be received (to protect against price slippage).
- path: An array of token addresses representing the swap path, ending with WETH.
- to: The address that will receive the ETH.
- deadline: The timestamp by which the transaction must be completed.
This function allows users to swap fee-on-transfer tokens for ETH, ensuring the swap completes even if a fee is deducted during the transfer.
Conclusion
The DiamondSwap Router02 contract is an advanced and versatile tool for users and developers interacting with the DiamondSwap protocol. By providing easy-to-use functions for swapping tokens, adding liquidity, removing liquidity, and handling fee-on-transfer tokens, the Router02 contract enhances the flexibility and usability of the DiamondSwap protocol. Whether you're a trader executing swaps or a liquidity provider managing your positions, understanding the Router02 contract is essential for navigating the DiamondSwap ecosystem effectively.
For more details, visit the DiamondSwap V1 Router documentation.
Updated 3 months ago