# RouterUtil



## Overview [#overview]

`RouterUtil.sol`  provides miscellaneous utils and preview functions related to Router executions.

### spotExchangeRate [#spotexchangerate]

Gives the spot exchange rate of token i in terms of token j.

*For example `spotExchangeRate(any, 1, 0)` gives you the PT/IBT quote (how much IBT is worth one PT).*

*in IBT while `spotExchangeRate(any, 0, 1)` gives you the IBT/PT  quote (how much PT is worth one IBT).*

<Callout type="info">
  For the IBT-PT curve pools deployed with the factory, the coin at index 0 is the [Interest Bearing Token](/docs/glossary#ibt) and the coin at index 1 is the [Principal Token](/docs/technical-reference/contract-functions/principal-token).
</Callout>

<Callout type="info">
  Exchange rate is in 18 decimals
</Callout>

```solidity
function spotExchangeRate(
        address _curvePool,
        uint256 _i,
        uint256 _j
    ) public view returns (uint256)
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract |
| `_i`            | uint256 | The input token index                  |
| `_j`            | uin256  | The output token index                 |

| Return Parameter | Type    | Description                                                      |
| ---------------- | ------- | ---------------------------------------------------------------- |
|                  | uint256 | The quote for the exchange rate of&#x20;`_`&#x2A;`i` for `_`*`j` |

### convertIBTToYTSpot [#convertibttoytspot]

Returns the maximal amount of YT one can obtain with a given amount of IBT (i.e without fees or slippage).

<Callout type="warn">
  This function is used for setting initial guess for the root finding algorithms used when performing [flash swaps](/docs/guides/routing). This method should not be used to evaluate a precise YT price.
</Callout>

```solidity
function convertIBTToYTSpot(
        uint256 _inputIBTAmount,
        address _curvePool
    ) public view returns (uint256)
```

| Input Parameter   | Type    | Description                            |
| ----------------- | ------- | -------------------------------------- |
| `_inputIBTAmount` | address | The amount of IBT to convert           |
| `_curvePool`      | uint256 | The address of the curve pool contract |

| Return Parameter | Type    | Description                              |
| ---------------- | ------- | ---------------------------------------- |
| `ytAmount`       | uint256 | An upper bound on the YT amount expected |

### previewFlashSwapIBTToExactYT [#previewflashswapibttoexactyt]

Estimates the amount of IBT required to perform a flash swap to obtain a specific amount of YT.

```solidity
function previewFlashSwapIBTToExactYT(
        address _curvePool,
        uint256 _outputYTAmount
    ) public view returns (uint256, uint256)
```

| Input Parameter   | Type    | Description                            |
| ----------------- | ------- | -------------------------------------- |
| `_curvePool`      | address | The address of the curve pool contract |
| `_outputYTAmount` | uint256 | The desired YT amount                  |

| Return Parameter    | Type    | Description                                                 |
| ------------------- | ------- | ----------------------------------------------------------- |
| `inputIBTAmount`    | uint256 | The estimated IBT amount required for the swap              |
| `borrowedIBTAmount` | uint256 | The calculated IBT amount to be borrowed in the flash swap. |

### previewFlashSwapExactIBTToYT [#previewflashswapexactibttoyt]

Estimates the amount of YT that can be obtained by providing a specific amount of IBT in a flash swap.

```solidity
function previewFlashSwapExactIBTToYT(
        address _curvePool,
        uint256 _inputIBTAmount
    ) public view returns (uint256, uint256) 
```

| Input Parameter   | Type    | Description                            |
| ----------------- | ------- | -------------------------------------- |
| `_curvePool`      | address | The address of the curve pool contract |
| `_inputIBTAmount` | uint256 | IBT amount for the swap.               |

| Return Parameter         | Type    | Description                                                 |
| ------------------------ | ------- | ----------------------------------------------------------- |
| `minGuessOutputYTAmount` | uint256 | The minimum estimated YT obtained from the swap.            |
| `maxGuessOutputYTAmount` | uint256 | The maximum estimated YT obtained from the swap.            |
| `borrowedIBTAmount`      | uint256 | The calculated IBT amount to be borrowed in the flash swap. |

### previewFlashSwapExactYTToIBT [#previewflashswapexactyttoibt]

Estimates the amount of IBT that can be obtained by swapping a specific amount of YT.

```solidity
function previewFlashSwapExactYTToIBT(
        address _curvePool,
        uint256 _inputYTAmount
    ) public view returns (uint256, uint256) 
```

| Input Parameter  | Type    | Description                               |
| ---------------- | ------- | ----------------------------------------- |
| `_curvePool`     | address | The address of the curve pool to trade in |
| `_inputYTAmount` | uint256 | The YT amount provided for the swap.      |

| Return Parameter    | Type    | Description                                                 |
| ------------------- | ------- | ----------------------------------------------------------- |
| `outputIBTAmount`   | uint256 | Estimated IBT amount obtainable from the swap.              |
| `borrowedIBTAmount` | uint256 | The calculated IBT amount to be borrowed in the flash swap. |

### getDx [#getdx]

Estimate the required input amount of a token (`dx`) for a Curve pool swap, given a target output amount (`targetDy`).

```solidity
function getDx(
        ICurvePool curvePool,
        uint256 i,
        uint256 j,
        uint256 targetDy
    ) public view returns (uint256)
```

| Input Parameter | Type    | Description                                      |
| --------------- | ------- | ------------------------------------------------ |
| `curvePool`     | address | The address of the curve pool contract           |
| `i`             | uint256 | The index of the input token in the Curve pool.  |
| `j`             | uint256 | The index of the output token in the Curve pool. |
| `targetDy`      | uint256 | The desired output amount in the swap.           |

| Return Parameter | Type    | Description                                                                                           |
| ---------------- | ------- | ----------------------------------------------------------------------------------------------------- |
| `guess`          | uint256 | The estimated input amount (`dx`) that should be provided to the Curve pool to obtain the `targetDy`. |

### previewAddLiquidityWithAsset [#previewaddliquiditywithasset]

Estimate the amount of LP tokens the user will get by routing assets for LP tokens ([see Routing](/docs/guides/routing)).

```solidity
function previewAddLiquidityWithAsset(
        address _curvePool,
        uint256 _assets
    ) public view returns (uint256 minMintAmount)
```

| Input Parameter | Type    | Description                                         |
| --------------- | ------- | --------------------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract              |
| `_assets`       | uint256 | The amount of assets to deposit as total liquidity. |

| Return Parameter | Type    | Description                             |
| ---------------- | ------- | --------------------------------------- |
| `minMintAmount`  | uint256 | The estimated amount of Curve LP tokens |

### previewAddLiquidityWithIBT [#previewaddliquiditywithibt]

Estimate the amount of LP tokens the user will get by routing IBTs for LP tokens ([see Routing](/docs/guides/routing)).

```solidity
function previewAddLiquidityWithIBT(
        address _curvePool,
        uint256 _ibts
    ) public view returns (uint256 minMintAmount)
```

| Input Parameter | Type    | Description                                       |
| --------------- | ------- | ------------------------------------------------- |
| `curvePool`     | address | The address of the curve pool contract            |
| `_ibts`         | uint256 | The amount of ibts to deposit as total liquidity. |

| Return Parameter | Type    | Description                             |
| ---------------- | ------- | --------------------------------------- |
| `minMintAmount`  | uint256 | The estimated amount of Curve LP tokens |

### previewAddLiquidity [#previewaddliquidity]

Estimate the amount of LP tokens the user will get by adding `amounts` liquidity. `amounts` is an array of the amount for token 0, the [IBT](/docs/glossary#ibt) and for token 1, the [Principal Token](/docs/technical-reference/contract-functions/principal-token).

```solidity
function previewAddLiquidity(
        address _curvePool,
        uint256[2] memory _amounts
    ) public view returns (uint256 minMintAmount)
```

| Input Parameter | Type        | Description                                                           |
| --------------- | ----------- | --------------------------------------------------------------------- |
| \_curvePool     | address     | The address of the curve pool contract                                |
| \_amounts       | uint256\[2] | The amounts of token 0 (IBT) and token 1 (PT) to provide as liquidity |

| Return Parameter | Type    | Description                             |
| ---------------- | ------- | --------------------------------------- |
| `minMintAmount`  | uint256 | The estimated amount of Curve LP tokens |

### previewRemoveLiquidityForAsset [#previewremoveliquidityforasset]

Estimate the amount of underlying token (assets) a user will get for burning `_lpAmount` LP tokens and redeeming PT and IBT for underlying. ([see Routing](/docs/guides/routing)).

```solidity
function previewRemoveLiquidityForAsset(
        address _curvePool,
        uint256 _lpAmount
    ) public view returns (uint256 assets)
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract |
| `_lpAmount`     | uint256 | The amount of lp tokens to burn        |

| Return Parameter | Type    | Description                                   |
| ---------------- | ------- | --------------------------------------------- |
| `assets`         | uint256 | The estimated assets withdrawn from the pool. |

### previewRemoveLiquidityForIBT [#previewremoveliquidityforibt]

Estimate the amount of IBTs a user will get for burning `_lpAmount` LP tokens and redeeming PT for IBT. ([see Routing](/docs/guides/routing)).

```solidity
function previewRemoveLiquidityForIBT(
        address _curvePool,
        uint256 _lpAmount
    ) public view returns (uint256 ibts)
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract |
| `_lpAmount`     | uint256 | The amount of lp tokens to burn        |

| Return Parameter | Type    | Description                                 |
| ---------------- | ------- | ------------------------------------------- |
| `ibts`           | uint256 | The estimated IBTs withdrawn from the pool. |

### previewRemoveLiquidity [#previewremoveliquidity]

Estimate the amounts of IBT and PT received for burning `_lpAmount` LP tokens.

```solidity
function previewRemoveLiquidity(
        address _curvePool,
        uint256 _lpAmount
    ) public view returns (uint256[2] memory minAmounts)
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract |
| `_lpAmount`     | uint256 | The amount of lp tokens to burn        |

| Return Parameter | Type        | Description                                                    |
| ---------------- | ----------- | -------------------------------------------------------------- |
| `minAmounts`     | uint256\[2] | The estimated amounts of IBTs and PTs withdrawn from the pool. |

### previewRemoveLiquidityOneCoin [#previewremoveliquidityonecoin]

Estimate the amounts of either IBT or PT received for burning `_lpAmount` LP tokens.  `_i=0` for IBT and `_i=1` for PT.

```solidity
function previewRemoveLiquidityOneCoin(
        address _curvePool,
        uint256 _lpAmount,
        uint256 _i
    ) public view returns (uint256 minAmount)
```

| Input Parameter | Type    | Description                            |
| --------------- | ------- | -------------------------------------- |
| `_curvePool`    | address | The address of the curve pool contract |
| `_lpAmount`     | uint256 | The amount of LP tokens to burn        |
| `_i`            | uin256  | The index of the token to withdraw     |

| Return Parameter | Type    | Description                              |
| ---------------- | ------- | ---------------------------------------- |
| `minAmounts`     | uint256 | The estimated amounts of token withdrawn |
