# SwapMath

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

#### \_amountToBinNetOfProtocolFee <a href="#amounttobinnetofprotocolfee" id="amounttobinnetofprotocolfee"></a>

Internal function to calculate the amount after deducting the protocol fee.

```
```

```solidity
function _amountToBinNetOfProtocolFee(uint256 deltaInErc, uint256 feeBasis, uint256 protocolFeeD3)
    private
    pure
    returns (uint256 amount);
```

**Parameters**

| Name            | Type      | Description                                                                                                        |
| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------ |
| `deltaInErc`    | `uint256` | Input delta in ERC token.                                                                                          |
| `feeBasis`      | `uint256` | Fee basis for calculating the fee amount.                                                                          |
| `protocolFeeD3` | `uint256` | Proportion of the fee that goes to the protocol in three-decimal format. e.g. fee of 100 is 10% protocol fee rate. |

**Returns**

| Name     | Type      | Description                                  |
| -------- | --------- | -------------------------------------------- |
| `amount` | `uint256` | The amount after deducting the protocol fee. |

#### \_remainingBinInputSpaceGivenOutput <a href="#remainingbininputspacegivenoutput" id="remainingbininputspacegivenoutput"></a>

Internal function to calculate the remaining input space given the output.

```solidity
function _remainingBinInputSpaceGivenOutput(uint256 binLiquidity, uint256 output, uint256 sqrtPrice, bool tokenAIn)
    private
    pure
    returns (uint256 binAmountIn);
```

**Parameters**

| Name           | Type      | Description                        |
| -------------- | --------- | ---------------------------------- |
| `binLiquidity` | `uint256` | The current liquidity in the bin.  |
| `output`       | `uint256` | The desired output amount.         |
| `sqrtPrice`    | `uint256` | The current square root price.     |
| `tokenAIn`     | `bool`    | True if the swap input is token A. |

**Returns**

| Name          | Type      | Description                                           |
| ------------- | --------- | ----------------------------------------------------- |
| `binAmountIn` | `uint256` | The input amount that can be accommodated in the bin. |

#### computeEndPrice <a href="#computeendprice" id="computeendprice"></a>

Compute end price of a swap as well as the approximate end price in the tick domain. The resulting output fraction tick part is written to the input newDelta object.

Ain: `endSqrtP = in / L + sqrtP`

Bin: `endSqrtP = 1 / (in / L + 1 / sqrtP) = L / (in + L / sqrtP)`

fractional Tick: `(endSqrtP - lowerSqrtP) / (upperSqrtP - lowerSqrtP)`

```solidity
function computeEndPrice(
    Delta.Instance memory delta,
    Delta.Instance memory newDelta,
    IMaverickV2Pool.TickData memory tickData
) internal pure;
```

#### computeSwapExactIn <a href="#computeswapexactin" id="computeswapexactin"></a>

Calculate swap data for an exact input swap.

```solidity
function computeSwapExactIn(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountIn,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);
```

**Parameters**

| Name            | Type                       | Description                                      |
| --------------- | -------------------------- | ------------------------------------------------ |
| `sqrtPrice`     | `uint256`                  | Current price.                                   |
| `tickData`      | `IMaverickV2Pool.TickData` | Reserve and liquidity values of the bin.         |
| `amountIn`      | `uint256`                  | Desired input amount.                            |
| `tokenAIn`      | `bool`                     | True if the swap input is token A.               |
| `fee`           | `uint256`                  | Ratio that the swapper pays in D18 format.       |
| `protocolFeeD3` | `uint256`                  | Proportion of the fee that goes to the protocol. |

**Returns**

| Name    | Type             | Description                               |
| ------- | ---------------- | ----------------------------------------- |
| `delta` | `Delta.Instance` | Swap data delta for the exact input swap. |

#### computeSwapExactOut <a href="#computeswapexactout" id="computeswapexactout"></a>

Calculate swap data for an exact output swap.

```solidity
function computeSwapExactOut(
    uint256 sqrtPrice,
    IMaverickV2Pool.TickData memory tickData,
    uint256 amountOut,
    bool tokenAIn,
    uint256 fee,
    uint256 protocolFeeD3
) internal pure returns (Delta.Instance memory delta);
```

**Parameters**

| Name            | Type                       | Description                                      |
| --------------- | -------------------------- | ------------------------------------------------ |
| `sqrtPrice`     | `uint256`                  | Current price.                                   |
| `tickData`      | `IMaverickV2Pool.TickData` | Reserve and liquidity values of the bin.         |
| `amountOut`     | `uint256`                  | Desired output amount.                           |
| `tokenAIn`      | `bool`                     | True if the swap input is token A.               |
| `fee`           | `uint256`                  | Ratio that the swapper pays in D18 format.       |
| `protocolFeeD3` | `uint256`                  | Proportion of the fee that goes to the protocol. |

**Returns**

| Name    | Type             | Description                                |
| ------- | ---------------- | ------------------------------------------ |
| `delta` | `Delta.Instance` | Swap data delta for the exact output swap. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mav.xyz/technical-reference/maverick-v2/v2-contracts/maverick-v2-amm-contracts/poollib/swapmath.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
