# RouterUtil

## Overview

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

### 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).*

{% hint style="info" %}
For the IBT-PT curve pools deployed with the factory, the coin at index 0 is the [Interest Bearing Token](https://dev.spectra.finance/glossary#ibt) and the coin at index 1 is the [Principal Token](https://dev.spectra.finance/technical-reference/contract-functions/principal-token).
{% endhint %}

{% hint style="info" %}
&#x20;Exchange rate is in 18 decimals
{% endhint %}

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_i</code></td><td>uint256</td><td>The input token index </td></tr><tr><td><code>_j</code></td><td>uin256</td><td>The output token index </td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td>uint256</td><td>The quote for the exchange rate of <code>_</code><em><code>i</code> for <code>_</code></em><code>j</code></td></tr></tbody></table>

### convertIBTToYTSpot

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

{% hint style="warning" %}
This function is used for setting initial guess for the root finding algorithms used when performing [flash swaps](https://dev.spectra.finance/guides/routing). This method should not be used to evaluate a precise YT price.
{% endhint %}

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_inputIBTAmount</code></td><td>address</td><td>The amount of IBT to convert</td></tr><tr><td><code>_curvePool</code></td><td>uint256</td><td>The address of the curve pool contract</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>ytAmount</code></td><td>uint256</td><td>An upper bound on the YT amount expected</td></tr></tbody></table>

### 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)
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_outputYTAmount</code></td><td>uint256</td><td>The desired YT amount</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>inputIBTAmount</code></td><td>uint256</td><td>The estimated IBT amount required for the swap</td></tr><tr><td><code>borrowedIBTAmount</code></td><td>uint256</td><td>The calculated IBT amount to be borrowed in the flash swap.</td></tr></tbody></table>

### 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) 
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_inputIBTAmount</code></td><td>uint256</td><td>IBT amount for the swap.</td></tr></tbody></table>

<table><thead><tr><th width="275.3333333333333">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minGuessOutputYTAmount</code></td><td>uint256</td><td>The minimum estimated YT obtained from the swap.</td></tr><tr><td><code>maxGuessOutputYTAmount</code></td><td>uint256</td><td>The maximum estimated YT obtained from the swap.</td></tr><tr><td><code>borrowedIBTAmount</code></td><td>uint256</td><td>The calculated IBT amount to be borrowed in the flash swap.</td></tr></tbody></table>

### 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) 
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool to trade in</td></tr><tr><td><code>_inputYTAmount</code></td><td>uint256</td><td>The YT amount provided for the swap.</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>outputIBTAmount</code></td><td>uint256</td><td>Estimated IBT amount obtainable from the swap.</td></tr><tr><td><code>borrowedIBTAmount</code></td><td>uint256</td><td>The calculated IBT amount to be borrowed in the flash swap.</td></tr></tbody></table>

### 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)
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>i</code></td><td>uint256</td><td>The index of the input token in the Curve pool.</td></tr><tr><td><code>j</code></td><td>uint256</td><td>The index of the output token in the Curve pool.</td></tr><tr><td><code>targetDy</code></td><td>uint256</td><td>The desired output amount in the swap.</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>guess</code></td><td>uint256</td><td>The estimated input amount (<code>dx</code>) that should be provided to the Curve pool to obtain the <code>targetDy</code>.</td></tr></tbody></table>

### previewAddLiquidityWithAsset

Estimate the amount of LP tokens the user will get by routing assets for LP tokens ([see Routing](https://dev.spectra.finance/guides/routing)).

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_assets</code></td><td>uint256</td><td>The amount of assets to deposit as total liquidity.</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minMintAmount</code></td><td>uint256</td><td>The estimated amount of Curve LP tokens</td></tr></tbody></table>

### previewAddLiquidityWithIBT

Estimate the amount of LP tokens the user will get by routing IBTs for LP tokens ([see Routing](https://dev.spectra.finance/guides/routing)).

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_ibts</code></td><td>uint256</td><td>The amount of ibts to deposit as total liquidity.</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minMintAmount</code></td><td>uint256</td><td>The estimated amount of Curve LP tokens</td></tr></tbody></table>

### 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](https://dev.spectra.finance/glossary#ibt) and for token 1, the [Principal Token](https://dev.spectra.finance/technical-reference/contract-functions/principal-token).

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td>_curvePool</td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td>_amounts</td><td>uint256[2]</td><td>The amounts of token 0 (IBT) and token 1 (PT) to provide as liquidity</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minMintAmount</code></td><td>uint256</td><td>The estimated amount of Curve LP tokens</td></tr></tbody></table>

### 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](https://dev.spectra.finance/guides/routing)).

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

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_lpAmount</code></td><td>uint256</td><td>The amount of lp tokens to burn</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>assets</code></td><td>uint256</td><td>The estimated assets withdrawn from the pool.</td></tr></tbody></table>

### previewRemoveLiquidityForIBT

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

<pre class="language-solidity"><code class="lang-solidity"><strong>function previewRemoveLiquidityForIBT(
</strong>        address _curvePool,
        uint256 _lpAmount
    ) public view returns (uint256 ibts)
</code></pre>

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_lpAmount</code></td><td>uint256</td><td>The amount of lp tokens to burn</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>ibts</code></td><td>uint256</td><td>The estimated IBTs withdrawn from the pool.</td></tr></tbody></table>

### 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)
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_lpAmount</code></td><td>uint256</td><td>The amount of lp tokens to burn</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minAmounts</code></td><td>uint256[2]</td><td>The estimated amounts of IBTs and PTs withdrawn from the pool.</td></tr></tbody></table>

### 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)
```

<table><thead><tr><th width="231.33333333333331">Input Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>_curvePool</code></td><td>address</td><td>The address of the curve pool contract</td></tr><tr><td><code>_lpAmount</code></td><td>uint256</td><td>The amount of LP tokens to burn</td></tr><tr><td><code>_i</code></td><td>uin256</td><td>The index of the token to withdraw</td></tr></tbody></table>

<table><thead><tr><th width="231.33333333333331">Return Parameter</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>minAmounts</code></td><td>uint256</td><td>The estimated amounts of token withdrawn</td></tr></tbody></table>
