# Router

Router contract is mainly used for **interacting** with a **Pair**. Routers are stateless, meaning they don't hold token balances. They also have the capability to interact with Factory (in order to create Pair) if it does not exist yet.&#x20;

In order to understand what are the capabilities of the Router, we can take a look at some of its main functionalities that will allow XdYe users to easily operate with the XdYe DEX contract structure.

**XdYe Router** Smart Contract can be found at:&#x20;

{% embed url="<https://github.com/rogerCapone/XdYe_Smart_Contracts/blob/main/XdYeRouter.sol>" %}
XdYe Router Contract Source Code
{% endembed %}

### Contract main functions

* *`addLiquidity()`*

This method is used for adding liquidity to a certain pair, this pair is token-token.  Router contract takes care of checking if this pair exists, if not then Router asks Factory to create the Pair for those tokens inside this method.

* *`addLiquidityETH()`*

This method is used for adding liquidity to a certain pair, containing ETH (in XdYe case VT), this is important because the Router contract will take care of converting ETH to wETH (in XdYe case VT to wVT). This method also checks if Pair exists, if it does not then takes care of asking Factory contract to created so liquidity can be added.&#x20;

* *`removeLiquidity()`*

This method is used for removing liquidity that has already been provided by the user on token-token pairs.

* *`removeLiquidityETH()`*

This method is used for removing liquidity that has already been provided by the user on token-ETH pairs. In fact, Pair will be internally handled through wETH but when user withdraws/removes liquidity the Router contract will convert that wETH into ETH.

* *`swapExactTokensForTokens()`*

This method considers a specific *input amount* and allows XdYe users to swap their tokens. In this case we need to also specify a minimum amount out in order to be flexible to price variations, this is normally defined through *Slippage Tolerance*. We calculate that value, through a percentage.&#x20;

*F.e. You are trading/swapping TokenA for TokenB, current ratio (price) is 1 TokenA = 2 TokenB, if you want to swap and while you send your transaction there was an already processing swap that changes that ratio to 1 TokenA = 1.195 TokenB, and you have a slippage tolerance of 10% your swap will execute because the 'output Amount' will be less than that percentage. If the ratio dropped to 1 TokenA - 1.7 TokenB, then your transaction will be reverted by the Router contract.*

* *`swapTokensForExactTokens()`*

This method considers a specific *output amount* and allows XdYe users to swap their tokens meeting the amount out. In this case we need to also specify a maximum amount in, then XdYe will 'offer' the best possible deal/ratio that satisfies user's needs.

* *`swapExactETHForTokens()`*

This method is similar to *swapExactTokensForTokens*, in this method it needs to perform the *'wrap'* from ETH *(VT, in XdYe)* in to wETH *(wVT, in XdYE)*. User specifies the amount of ETH he wants to swap and also the minimum amount out of token.

* *`swapTokensForExactETH()`*

This method is similar to *swapTokensForExactTokens* with the difference of that in this case the Router contract needs to *'unwrap'* the wETH --> ETH *(wVT --> VT).*

* *`swapETHForExactTokens`*

This method is similar to *swapTokensForExactETH* but in this case we need to perform the *'wrap'* of ETH into wETH and we want to specify the tokens we want to get in return and we give some flexibility to the amount of ETH that will be provided for the trade.

**Utility functions inside Router**

Those functions perform specific computations, that allow Router contract to obtain data that can ensure users swaps. This is possible due to *UniswapV2Library* that is used as reference in order to collect data from important/useful parts of XdYe (DEX) structure.

* *`getAmountOut()`*
* *`getAmountIn()`*
* *`getAmountsOut()`*
* *`getAmountsIn()`*

Those methods are pretty self explanatory, they retrieve values that will derive from a certain *input data* (function arguments/parameters).

The difference is that first 2 methods are for a specific Pair, while on the other two we can set multiple pairs.&#x20;

{% hint style="warning" %}
**Attention:** If you are trying to replicate the code, be sure to consider the imports or visit the corresponding [Github Repository](https://github.com/rogerCapone/XdYe_Smart_Contracts/tree/main) in order to have the whole source code.
{% endhint %}
