V1 Router
DiamondSwap Router01
The DiamondSwap Router01 contract is a critical component of the DiamondSwap protocol, providing a user-friendly interface for interacting with the decentralized exchange (DEX). The Router01 contract simplifies the process of swapping tokens, adding and removing liquidity, and interacting with the protocol’s liquidity pools. It is designed to work with the core functionality provided by the DiamondSwap Factory and Pair contracts. Below is a detailed explanation of the key functions and features of the DiamondSwap Router01 contract.
Overview
The DiamondSwap Router01 contract acts as a bridge between users and the DiamondSwap protocol. It abstracts away the complexities of interacting directly with the Pair contracts, allowing users to easily execute trades and manage their liquidity positions. The Router01 contract provides functions for swapping tokens, adding liquidity to pools, and removing liquidity, all while ensuring that the protocol's invariant, (x \cdot y = k), is maintained.
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.
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.
Conclusion
The DiamondSwap Router01 contract is an essential tool for users and developers interacting with the DiamondSwap protocol. By providing easy-to-use functions for swapping tokens, adding liquidity, and removing liquidity, the Router01 contract abstracts away the complexities of the protocol and ensures that trades and liquidity operations are executed efficiently and securely. Whether you're a trader looking to swap tokens or a liquidity provider managing your pool positions, understanding the Router01 contract is crucial for navigating the DiamondSwap ecosystem.
Updated 3 months ago