# V2SwapRouter

Router for stateless execution of swaps against Dragonswap V2

### State Variables <a href="#state-variables" id="state-variables"></a>

#### DEFAULT\_AMOUNT\_IN\_CACHED <a href="#default_amount_in_cached" id="default_amount_in_cached"></a>

*Used as the placeholder value for amountInCached, because the computed amount in for an exact output swap can never actually be this value*

```solidity
uint256 private constant DEFAULT_AMOUNT_IN_CACHED = type(uint256).max;
```

#### amountInCached <a href="#amountincached" id="amountincached"></a>

*Transient storage variable used for returning the computed amount in for an exact output swap.*

```solidity
uint256 private amountInCached = DEFAULT_AMOUNT_IN_CACHED;
```

### Functions <a href="#functions" id="functions"></a>

#### getPool <a href="#getpool" id="getpool"></a>

*Returns the pool for the given token pair and fee. The pool contract may or may not exist.*

```solidity
function getPool(address tokenA, address tokenB, uint24 fee) private view returns (IDragonswapV2Pool);
```

#### dragonswapV2SwapCallback <a href="#dragonswapv2swapcallback" id="dragonswapv2swapcallback"></a>

Called to `msg.sender` after executing a swap via IDragonswapV2Pool#swap.

*In the implementation you must pay the pool tokens owed for the swap. The caller of this method must be checked to be a DragonswapV2Pool deployed by the canonical DragonswapV2Factory. amount0Delta and amount1Delta can both be 0 if no tokens were swapped.*

```solidity
function dragonswapV2SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata _data) external override;
```

**Parameters**

| Name           | Type     | Description                                                                                                                                                                             |
| -------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `amount0Delta` | `int256` | The amount of token0 that was sent (negative) or must be received (positive) by the pool by the end of the swap. If positive, the callback must send that amount of token0 to the pool. |
| `amount1Delta` | `int256` | The amount of token1 that was sent (negative) or must be received (positive) by the pool by the end of the swap. If positive, the callback must send that amount of token1 to the pool. |
| `_data`        | `bytes`  |                                                                                                                                                                                         |

#### exactInputInternal <a href="#exactinputinternal" id="exactinputinternal"></a>

*Performs a single exact input swap*

```solidity
function exactInputInternal(
    uint256 amountIn,
    address recipient,
    uint160 sqrtPriceLimitX96,
    SwapCallbackData memory data
) private returns (uint256 amountOut);
```

#### exactInputSingle <a href="#exactinputsingle" id="exactinputsingle"></a>

Swaps `amountIn` of one token for as much as possible of another token

*Setting `amountIn` to 0 will cause the contract to look up its own balance, and swap the entire amount, enabling contracts to send tokens before calling this function.*

```solidity
function exactInputSingle(ExactInputSingleParams memory params) external payable override returns (uint256 amountOut);
```

**Parameters**

| Name     | Type                     | Description                                                                            |
| -------- | ------------------------ | -------------------------------------------------------------------------------------- |
| `params` | `ExactInputSingleParams` | The parameters necessary for the swap, encoded as `ExactInputSingleParams` in calldata |

**Returns**

| Name        | Type      | Description                      |
| ----------- | --------- | -------------------------------- |
| `amountOut` | `uint256` | The amount of the received token |

#### exactInput <a href="#exactinput" id="exactinput"></a>

Swaps `amountIn` of one token for as much as possible of another along the specified path

*Setting `amountIn` to 0 will cause the contract to look up its own balance, and swap the entire amount, enabling contracts to send tokens before calling this function.*

```solidity
function exactInput(ExactInputParams memory params) external payable override returns (uint256 amountOut);
```

**Parameters**

| Name     | Type               | Description                                                                                |
| -------- | ------------------ | ------------------------------------------------------------------------------------------ |
| `params` | `ExactInputParams` | The parameters necessary for the multi-hop swap, encoded as `ExactInputParams` in calldata |

**Returns**

| Name        | Type      | Description                      |
| ----------- | --------- | -------------------------------- |
| `amountOut` | `uint256` | The amount of the received token |

#### exactOutputInternal <a href="#exactoutputinternal" id="exactoutputinternal"></a>

*Performs a single exact output swap*

```solidity
function exactOutputInternal(
    uint256 amountOut,
    address recipient,
    uint160 sqrtPriceLimitX96,
    SwapCallbackData memory data
) private returns (uint256 amountIn);
```

#### exactOutputSingle <a href="#exactoutputsingle" id="exactoutputsingle"></a>

Swaps as little as possible of one token for `amountOut` of another token that may remain in the router after the swap.

```solidity
function exactOutputSingle(ExactOutputSingleParams calldata params)
    external
    payable
    override
    returns (uint256 amountIn);
```

**Parameters**

| Name     | Type                      | Description                                                                             |
| -------- | ------------------------- | --------------------------------------------------------------------------------------- |
| `params` | `ExactOutputSingleParams` | The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata |

**Returns**

| Name       | Type      | Description                   |
| ---------- | --------- | ----------------------------- |
| `amountIn` | `uint256` | The amount of the input token |

#### exactOutput <a href="#exactoutput" id="exactoutput"></a>

Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed) that may remain in the router after the swap.

```solidity
function exactOutput(ExactOutputParams calldata params) external payable override returns (uint256 amountIn);
```

**Parameters**

| Name     | Type                | Description                                                                                 |
| -------- | ------------------- | ------------------------------------------------------------------------------------------- |
| `params` | `ExactOutputParams` | The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata |

**Returns**

| Name       | Type      | Description                   |
| ---------- | --------- | ----------------------------- |
| `amountIn` | `uint256` | The amount of the input token |

### Structs <a href="#structs" id="structs"></a>

#### SwapCallbackData <a href="#swapcallbackdata" id="swapcallbackdata"></a>

```solidity
struct SwapCallbackData {
    bytes path;
    address payer;
}
```
