> ## Documentation Index
> Fetch the complete documentation index at: https://docs.forteamm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# ALTBCPool

[Git Source](https://github.com/thrackle-io/liquidity-altbc/blob/a089af3d235b9241cf0ed6c700a259b616853515/src/amm/ALTBCPool.sol)

**Inherits:**
PoolBase, Initializable

**Author:**
@oscarsernarosero @mpetersoCode55 @cirsteve

*This contract serves the purpose of facilitating swaps between a pair
of tokens, where one is an xToken and the other one is a yToken.*

## State Variables

### tbc

```solidity theme={null}
ALTBCDef public tbc;
```

## Functions

### constructor

*constructor*

```solidity theme={null}
constructor(
    address _xToken,
    address _yToken,
    address _lpToken,
    uint256 _inactiveLpId,
    FeeInfo memory fees,
    ALTBCInput memory _tbcInput,
    string memory _VERSION
) PoolBase(_xToken, _yToken, _lpToken, _inactiveLpId, fees);
```

**Parameters**

| Name            | Type         | Description                     |
| --------------- | ------------ | ------------------------------- |
| `_xToken`       | `address`    | address of the X token (x axis) |
| `_yToken`       | `address`    | address of the Y token (y axis) |
| `_lpToken`      | `address`    |                                 |
| `_inactiveLpId` | `uint256`    |                                 |
| `fees`          | `FeeInfo`    | fee infomation                  |
| `_tbcInput`     | `ALTBCInput` | input parameters for the TBC    |
| `_VERSION`      | `string`     |                                 |

### initializePool

*This is the function to initialize the pool.*

```solidity theme={null}
function initializePool(address deployer, uint256 initialLiq, uint256 ___wInactive) external onlyOwner initializer;
```

**Parameters**

| Name           | Type      | Description                             |
| -------------- | --------- | --------------------------------------- |
| `deployer`     | `address` | The address of the deployer             |
| `initialLiq`   | `uint256` |                                         |
| `___wInactive` | `uint256` | initial inactive liquidity for the pool |

### simulateLiquidityDeposit

*This is the function to simulate a liquidity deposit into the pool.*

```solidity theme={null}
function simulateLiquidityDeposit(uint256 _A, uint256 _B)
    public
    view
    returns (uint256 A, uint256 B, uint256 Q, int256 ratio, packedFloat qFloat, packedFloat L);
```

**Parameters**

| Name | Type      | Description                                                          |
| ---- | --------- | -------------------------------------------------------------------- |
| `_A` | `uint256` | The amount of xToken being deposited as liquidity in the simulation. |
| `_B` | `uint256` | The amount of yToken being deposited as liquidity in the simulation. |

**Returns**

| Name     | Type          | Description                                                                                                                         |
| -------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `A`      | `uint256`     | calculated A value which is the amount of xToken that will be deposited                                                             |
| `B`      | `uint256`     | calculated B value which is the amount of yToken that will be deposited                                                             |
| `Q`      | `uint256`     | calculated Q value which is the ratio of this provided liquidity unit to the total liquidity of the pool                            |
| `ratio`  | `int256`      | calculated ratio of xToken to yToken required for the deposit                                                                       |
| `qFloat` | `packedFloat` | calculated qFloat value which is the ratio of this provided liquidity unit to the total liquidity of the pool in packedFloat format |
| `L`      | `packedFloat` |                                                                                                                                     |

### tokenDepositUpdate

```solidity theme={null}
function tokenDepositUpdate(uint256 tokenId, packedFloat wj) internal returns (uint256);
```

### depositLiquidity

If the tokenId provided is owned by the lp, this tokenId will be updated based on liquidity deposit

*This is the function to deposit liquidity into the pool.*

```solidity theme={null}
function depositLiquidity(uint256 tokenId, uint256 _A, uint256 _B, uint256 _minA, uint256 _minB, uint256 expires)
    external
    whenNotPaused
    checkExpiration(expires)
    returns (uint256 A, uint256 B);
```

**Parameters**

| Name      | Type      | Description                                                              |
| --------- | --------- | ------------------------------------------------------------------------ |
| `tokenId` | `uint256` | The tokenId owned by the liquidity provider.                             |
| `_A`      | `uint256` | The amount of xToken being deposited as liquidity.                       |
| `_B`      | `uint256` | The amount of yToken being deposited as liquidity.                       |
| `_minA`   | `uint256` | The minimum acceptable amount of xToken actually deposited as liquidity. |
| `_minB`   | `uint256` | The minimum acceptable amount of yToken actually deposited as liquidity. |
| `expires` | `uint256` | Timestamp at which the deposit transaction will expire.                  |

**Returns**

| Name | Type      | Description        |
| ---- | --------- | ------------------ |
| `A`  | `uint256` | calculated A value |
| `B`  | `uint256` | calculated B value |

### simulateWithdrawLiquidity

*This is the function to simulate a liquidity withdrawal from the pool.*

*To get rj and uj, call the getLPToken function and pass in the rj and uj values*

```solidity theme={null}
function simulateWithdrawLiquidity(uint256 tokenId, uint256 uj, packedFloat _uj)
    public
    view
    returns (
        uint256 Ax,
        uint256 Ay,
        uint256 revenueAccrued,
        packedFloat q,
        packedFloat L,
        packedFloat wj,
        packedFloat rj
    );
```

**Parameters**

| Name      | Type          | Description                                                   |
| --------- | ------------- | ------------------------------------------------------------- |
| `tokenId` | `uint256`     | The tokenId owned by the liquidity provider.                  |
| `uj`      | `uint256`     | The amount of liquidity being withdrawn                       |
| `_uj`     | `packedFloat` | The amount of liquidity being withdrawn in packedFloat format |

**Returns**

| Name             | Type          | Description                                                                  |
| ---------------- | ------------- | ---------------------------------------------------------------------------- |
| `Ax`             | `uint256`     | The amount of xToken to be received                                          |
| `Ay`             | `uint256`     | The amount of yToken to be received                                          |
| `revenueAccrued` | `uint256`     | The amount of revenue accrued to the liquidity position                      |
| `q`              | `packedFloat` | The ratio of this provided liquidity unit to the total liquidity of the pool |
| `L`              | `packedFloat` |                                                                              |
| `wj`             | `packedFloat` |                                                                              |
| `rj`             | `packedFloat` |                                                                              |

### withdrawPartialLiquidity

*This is the function to withdraw partial liquidity from the pool.*

```solidity theme={null}
function withdrawPartialLiquidity(
    uint256 tokenId,
    uint256 uj,
    address recipient,
    uint256 _minAx,
    uint256 _minAy,
    uint256 expires
) external checkExpiration(expires);
```

**Parameters**

| Name        | Type      | Description                                                                |
| ----------- | --------- | -------------------------------------------------------------------------- |
| `tokenId`   | `uint256` | The tokenId owned by the liquidity provider.                               |
| `uj`        | `uint256` | The amount of liquidity being withdrawn                                    |
| `recipient` | `address` | address that receives withdrawn liquidity                                  |
| `_minAx`    | `uint256` | The minimum acceptable amount of xToken actually withdrawn from liquidity. |
| `_minAy`    | `uint256` | The minimum acceptable amount of yToken actually withdrawn from liquidity. |
| `expires`   | `uint256` | Timestamp at which the withdraw transaction will expire.                   |

### withdrawAllLiquidity

*This is the function to withdraw all token liquidity from the pool.*

```solidity theme={null}
function withdrawAllLiquidity(uint256 tokenId, address recipient, uint256 _minAx, uint256 _minAy, uint256 expires)
    external
    checkExpiration(expires);
```

**Parameters**

| Name        | Type      | Description                                                                |
| ----------- | --------- | -------------------------------------------------------------------------- |
| `tokenId`   | `uint256` | The tokenId owned by the liquidity provider.                               |
| `recipient` | `address` | address that receives withdrawn liquidity                                  |
| `_minAx`    | `uint256` | The minimum acceptable amount of xToken actually withdrawn from liquidity. |
| `_minAy`    | `uint256` | The minimum acceptable amount of yToken actually withdrawn from liquidity. |
| `expires`   | `uint256` | Timestamp at which the withdraw transaction will expire.                   |

### \_withdrawLiquidity

*This is the function to withdraw liquidity from the pool.*

```solidity theme={null}
function _withdrawLiquidity(uint256 tokenId, packedFloat _uj, address recipient, uint256 _minAx, uint256 _minAy)
    internal;
```

**Parameters**

| Name        | Type          | Description                                                                |
| ----------- | ------------- | -------------------------------------------------------------------------- |
| `tokenId`   | `uint256`     | The tokenId owned by the liquidity provider.                               |
| `_uj`       | `packedFloat` | The amount of liquidity being withdrawn                                    |
| `recipient` | `address`     | address that receives withdrawn liquidity                                  |
| `_minAx`    | `uint256`     | The minimum acceptable amount of xToken actually withdrawn from liquidity. |
| `_minAy`    | `uint256`     | The minimum acceptable amount of yToken actually withdrawn from liquidity. |

### \_emitLiquidityWithdrawn

*This is the function to emit the LiquidityWithdrawn event.*

```solidity theme={null}
function _emitLiquidityWithdrawn(uint256 tokenId, uint256 Ax, uint256 Ay, uint256 revenueAccrued, address recipient)
    private;
```

**Parameters**

| Name             | Type      | Description                                             |
| ---------------- | --------- | ------------------------------------------------------- |
| `tokenId`        | `uint256` | The tokenId owned by the liquidity provider.            |
| `Ax`             | `uint256` | The amount of xToken to be received                     |
| `Ay`             | `uint256` | The amount of yToken to be received                     |
| `revenueAccrued` | `uint256` | The amount of revenue accrued to the liquidity position |
| `recipient`      | `address` | The address that receives the withdrawn liquidity       |

### withdrawRevenue

*This is the function to withdraw revenue from the pool.*

```solidity theme={null}
function withdrawRevenue(uint256 tokenId, uint256 Q, address recipient) external returns (uint256 revenue);
```

**Parameters**

| Name        | Type      | Description                                  |
| ----------- | --------- | -------------------------------------------- |
| `tokenId`   | `uint256` | The tokenId owned by the liquidity provider. |
| `Q`         | `uint256` | The amount of revenue being withdrawn        |
| `recipient` | `address` |                                              |

**Returns**

| Name      | Type      | Description                           |
| --------- | --------- | ------------------------------------- |
| `revenue` | `uint256` | The amount of revenue being withdrawn |

### revenueAvailable

*This is the function to get the revenue available for a liquidity position.*

```solidity theme={null}
function revenueAvailable(uint256 tokenId) public view returns (uint256 _revenueAvailable);
```

**Parameters**

| Name      | Type      | Description                                     |
| --------- | --------- | ----------------------------------------------- |
| `tokenId` | `uint256` | The tokenId representing the liquidity position |

**Returns**

| Name                | Type      | Description                                                |
| ------------------- | --------- | ---------------------------------------------------------- |
| `_revenueAvailable` | `uint256` | The amount of revenue available for the liquidity position |

### \_getRevenueAvailable

*This is the function to get the revenue available for a liquidity provider.*

```solidity theme={null}
function _getRevenueAvailable(uint256 tokenId)
    internal
    view
    returns (
        packedFloat hn,
        packedFloat _wj,
        packedFloat _rj,
        packedFloat _revenueAvailable,
        uint256 revenueAvailableUint
    );
```

**Parameters**

| Name      | Type      | Description                                 |
| --------- | --------- | ------------------------------------------- |
| `tokenId` | `uint256` | The tokenId owned by the liquidity provider |

**Returns**

| Name                   | Type          | Description                                                |
| ---------------------- | ------------- | ---------------------------------------------------------- |
| `hn`                   | `packedFloat` | The total revenue per liquidity unit for the pool          |
| `_wj`                  | `packedFloat` | The amount of liquidity units of the specified token       |
| `_rj`                  | `packedFloat` | The revenue accrued to the liquidity position              |
| `_revenueAvailable`    | `packedFloat` | The amount of revenue available for the liquidity provider |
| `revenueAvailableUint` | `uint256`     |                                                            |

### \_spotPrice

x + 1 is used for returning the price of the next token sold, not the price of the last token sold

*This is the function to retrieve the current spot price of the x token.*

```solidity theme={null}
function _spotPrice() internal view override returns (packedFloat sPrice);
```

**Returns**

| Name     | Type          | Description                  |
| -------- | ------------- | ---------------------------- |
| `sPrice` | `packedFloat` | the price in YToken Decimals |

### \_updateParameters

*This function updates the state of the math values of the pool.*

```solidity theme={null}
function _updateParameters() internal override;
```

### \_calculateAmountOfXRequiredBuyingY

*This function calculates the amount of token X required for the user to purchase a specific amount of Token Y (buy y with x : out perspective).*

```solidity theme={null}
function _calculateAmountOfXRequiredBuyingY(packedFloat _amountOfY)
    internal
    view
    override
    returns (packedFloat amountOfX);
```

**Parameters**

| Name         | Type          | Description               |
| ------------ | ------------- | ------------------------- |
| `_amountOfY` | `packedFloat` | desired amount of token Y |

**Returns**

| Name        | Type          | Description                |
| ----------- | ------------- | -------------------------- |
| `amountOfX` | `packedFloat` | required amount of token X |

### \_calculateAmountOfYRequiredBuyingX

*This function calculates the amount of token Y required for the user to purchase a specific amount of Token X (buy x with y : out perspective).*

```solidity theme={null}
function _calculateAmountOfYRequiredBuyingX(packedFloat _amountOfX)
    internal
    view
    override
    returns (packedFloat amountOfY);
```

**Parameters**

| Name         | Type          | Description                                              |
| ------------ | ------------- | -------------------------------------------------------- |
| `_amountOfX` | `packedFloat` | desired amount of token X (also known as An in the spec) |

**Returns**

| Name        | Type          | Description                |
| ----------- | ------------- | -------------------------- |
| `amountOfY` | `packedFloat` | required amount of token Y |

### \_calculateAmountOfYReceivedSellingX

*This function calculates the amount of token Y the user will receive when selling token X (sell x for y : in perspective).*

```solidity theme={null}
function _calculateAmountOfYReceivedSellingX(packedFloat _amountOfX)
    internal
    view
    override
    returns (packedFloat amountOfY);
```

**Parameters**

| Name         | Type          | Description                  |
| ------------ | ------------- | ---------------------------- |
| `_amountOfX` | `packedFloat` | amount of token X to be sold |

**Returns**

| Name        | Type          | Description                      |
| ----------- | ------------- | -------------------------------- |
| `amountOfY` | `packedFloat` | amount of token Y to be received |

### \_calculateAmountOfXReceivedSellingY

*This function calculates the amount of token X the user will receive when selling token Y (sell y for x : in perspective).*

```solidity theme={null}
function _calculateAmountOfXReceivedSellingY(packedFloat _amountOfY)
    internal
    view
    override
    returns (packedFloat amountOfX);
```

**Parameters**

| Name         | Type          | Description                  |
| ------------ | ------------- | ---------------------------- |
| `_amountOfY` | `packedFloat` | amount of token Y to be sold |

**Returns**

| Name        | Type          | Description                      |
| ----------- | ------------- | -------------------------------- |
| `amountOfX` | `packedFloat` | amount of token X to be received |

### \_validateTBC

*A helper function to validate most of constructor's inputs.*

```solidity theme={null}
function _validateTBC(ALTBCInput memory _tbcInput) internal pure;
```

**Parameters**

| Name        | Type         | Description                  |
| ----------- | ------------ | ---------------------------- |
| `_tbcInput` | `ALTBCInput` | input parameters for the TBC |

### checkInactiveLiquidity

The threshold is set to 1% of the active liquidity units

*Check for ration  of inactive to active (token Id 2) liquidity, reverts if ratio is above threshold*

```solidity theme={null}
function checkInactiveLiquidity(packedFloat _active, packedFloat _inactive) internal pure;
```

**Parameters**

| Name        | Type          | Description              |
| ----------- | ------------- | ------------------------ |
| `_active`   | `packedFloat` | active liquidity units   |
| `_inactive` | `packedFloat` | inactive liquidity units |

### retrieveH

```solidity theme={null}
function retrieveH() public view returns (packedFloat h);
```

### \_emitCurveState

```solidity theme={null}
function _emitCurveState() internal override;
```
