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

# ALTBCEquations

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

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

## State Variables

### FLOAT\_2

```solidity theme={null}
packedFloat constant FLOAT_2 = packedFloat.wrap(0x7f6c00000000000000000000000000000f0bdc21abb48db201e86d4000000000);
```

### FLOAT\_NEG\_1

```solidity theme={null}
packedFloat constant FLOAT_NEG_1 = packedFloat.wrap(0x7f6d00000000000000000000000000000785ee10d5da46d900f436a000000000);
```

### FLOAT\_0

```solidity theme={null}
packedFloat constant FLOAT_0 = packedFloat.wrap(0);
```

### FLOAT\_1

```solidity theme={null}
packedFloat constant FLOAT_1 = packedFloat.wrap(0x7f6c00000000000000000000000000000785ee10d5da46d900f436a000000000);
```

### FLOAT\_WAD

```solidity theme={null}
packedFloat constant FLOAT_WAD =
    packedFloat.wrap(57507338264406853159277167054180511853162945875507645848942038639672188469248);
```

## Functions

### calculateBn

The result will be a packedFloat

Bn is equal to V / (Xn + C) in the spec

*This function calculates B(n) and stores it in the tbc definition as b.*

```solidity theme={null}
function calculateBn(ALTBCDef storage altbc, packedFloat Xn) internal;
```

**Parameters**

| Name    | Type          | Description        |
| ------- | ------------- | ------------------ |
| `altbc` | `ALTBCDef`    | the tbc definition |
| `Xn`    | `packedFloat` | the X value at n   |

### calculatefx

The result for f(x) will be a packedFloat.

This equation is used to calculate the spot price of the x token and is equal to (bn \* x) + cn

*This function calculates f(x) at n.*

```solidity theme={null}
function calculatefx(ALTBCDef storage altbc, packedFloat x) internal view returns (packedFloat result);
```

**Parameters**

| Name    | Type          | Description        |
| ------- | ------------- | ------------------ |
| `altbc` | `ALTBCDef`    | the tbc definition |
| `x`     | `packedFloat` | value for x at n   |

**Returns**

| Name     | Type          | Description                                           |
| -------- | ------------- | ----------------------------------------------------- |
| `result` | `packedFloat` | the calculated f(x), this value will be a packedFloat |

### calculateDn

The result for Dn will be a packedFloat

This equation is used to calculate the area under the curve at n and is equal to (1/2)(bn*x^2) + cn*x

*This function calculates D at n.*

```solidity theme={null}
function calculateDn(ALTBCDef storage altbc, packedFloat x) internal view returns (packedFloat result);
```

**Parameters**

| Name    | Type          | Description        |
| ------- | ------------- | ------------------ |
| `altbc` | `ALTBCDef`    | the tbc definition |
| `x`     | `packedFloat` | value for x at n   |

**Returns**

| Name     | Type          | Description |
| -------- | ------------- | ----------- |
| `result` | `packedFloat` | result      |

### calculateH

This method is implemented using packedFloats and the float128 library

This equation in the spec is equal to (Ln + Zn) / (Wn - wInactive) + phi

*This function calculates h at n which is the total revenue per unit of liquidity at time n.*

```solidity theme={null}
function calculateH(ALTBCDef storage altbc, packedFloat L, packedFloat W, packedFloat wInactive, packedFloat phi)
    internal
    view
    returns (packedFloat result);
```

**Parameters**

| Name        | Type          | Description                                           |
| ----------- | ------------- | ----------------------------------------------------- |
| `altbc`     | `ALTBCDef`    |                                                       |
| `L`         | `packedFloat` | the x coordinate                                      |
| `W`         | `packedFloat` | the total amount of units of liquidity in circulation |
| `wInactive` | `packedFloat` |                                                       |
| `phi`       | `packedFloat` | the total amount of units of liquidity in circulation |

**Returns**

| Name     | Type          | Description      |
| -------- | ------------- | ---------------- |
| `result` | `packedFloat` | the calculated h |

### calculateXofNPlus1

This method is implemented using packedFloats and the float128 library

This equation in the spec is equal to 2Dn / (c + sqrt(c^2 + 2bDn))

*This function calculates the value of Xn+1.*

```solidity theme={null}
function calculateXofNPlus1(ALTBCDef storage altbc, packedFloat Dn) internal view returns (packedFloat newX);
```

**Parameters**

| Name    | Type          | Description               |
| ------- | ------------- | ------------------------- |
| `altbc` | `ALTBCDef`    | the tbc definition        |
| `Dn`    | `packedFloat` | the area under the curve. |

**Returns**

| Name   | Type          | Description         |
| ------ | ------------- | ------------------- |
| `newX` | `packedFloat` | the calculated Xn+1 |

### calculateC

*This function calculates the parameter c and stores it in the tbc definition.*

```solidity theme={null}
function calculateC(ALTBCDef storage altbc, packedFloat Xn, packedFloat oldBn) internal;
```

**Parameters**

| Name    | Type          | Description             |
| ------- | ------------- | ----------------------- |
| `altbc` | `ALTBCDef`    | the tbc definition.     |
| `Xn`    | `packedFloat` | the x coordinate        |
| `oldBn` | `packedFloat` | the previous state of b |

### calculateLastRevenueClaim

The result for last revenue claim will be a Float.

*This function calculates the last revenue claim to be stored in the associated LPToken variable rj. The result will be a WAD value.*

```solidity theme={null}
function calculateLastRevenueClaim(packedFloat hn, packedFloat wj, packedFloat r_hat, packedFloat w_hat)
    internal
    pure
    returns (packedFloat);
```

**Parameters**

| Name    | Type          | Description                                                                                  |
| ------- | ------------- | -------------------------------------------------------------------------------------------- |
| `hn`    | `packedFloat` | The revenue parameter. Expected to be a Float.                                               |
| `wj`    | `packedFloat` | The share of the pool's liquidity the associated LPToken represents. Expected to be a Float. |
| `r_hat` | `packedFloat` | The current last revenue claim value of the associated LPToken. Expected to be a Float.      |
| `w_hat` | `packedFloat` | The current liquidity amount of the associated LPToken. Expected to be a Float.              |

### calculateL

*This function calculates the parameter L.*

```solidity theme={null}
function calculateL(ALTBCDef storage altbc, packedFloat Xn) internal view returns (packedFloat result);
```

**Parameters**

| Name    | Type          | Description         |
| ------- | ------------- | ------------------- |
| `altbc` | `ALTBCDef`    | the tbc definition. |
| `Xn`    | `packedFloat` | the x coordinate    |

**Returns**

| Name     | Type          | Description                |
| -------- | ------------- | -------------------------- |
| `result` | `packedFloat` | the calculate L parameter. |

### calculateZ

*This function calculates the parameter Z, which is a balancing quantity used to ensure fair LP accounting.*

```solidity theme={null}
function calculateZ(
    ALTBCDef storage altbc,
    packedFloat Ln,
    packedFloat Wn,
    packedFloat WIn,
    packedFloat q,
    bool withdrawal
) internal;
```

**Parameters**

| Name         | Type          | Description                                            |
| ------------ | ------------- | ------------------------------------------------------ |
| `altbc`      | `ALTBCDef`    | the tbc definition.                                    |
| `Ln`         | `packedFloat` | the liquidity parameter.                               |
| `Wn`         | `packedFloat` | the total amount of units of liquidity in circulation. |
| `WIn`        | `packedFloat` | the total amount of units of liquidity in circulation. |
| `q`          | `packedFloat` | the liquidity units to receive in exchange for A and B |
| `withdrawal` | `bool`        | the boolean value for withdrawal                       |

### calculateQ

*This function calculates q.*

```solidity theme={null}
function calculateQ(
    ALTBCDef storage altbc,
    packedFloat Xn,
    packedFloat _A,
    packedFloat _B,
    packedFloat L,
    packedFloat Dn
) internal view returns (packedFloat A, packedFloat B, packedFloat q);
```

**Parameters**

| Name    | Type          | Description                        |
| ------- | ------------- | ---------------------------------- |
| `altbc` | `ALTBCDef`    | the tbc definition.                |
| `Xn`    | `packedFloat` | the x coordinate                   |
| `_A`    | `packedFloat` | The amount of incoming X Token.    |
| `_B`    | `packedFloat` | The amount of incoming collateral. |
| `L`     | `packedFloat` | the liquidity parameter.           |
| `Dn`    | `packedFloat` | The current area under the curve.  |

**Returns**

| Name | Type          | Description                                            |
| ---- | ------------- | ------------------------------------------------------ |
| `A`  | `packedFloat` | the actual amount to take for token x                  |
| `B`  | `packedFloat` | the actual amount to take for token y                  |
| `q`  | `packedFloat` | the liquidity units to receive in exchange for A and B |

### calculateRevenueAvailable

*This function calculates the revenue available for a given LPToken.*

```solidity theme={null}
function calculateRevenueAvailable(packedFloat wj, packedFloat hn, packedFloat rj)
    internal
    pure
    returns (packedFloat result);
```

**Parameters**

| Name | Type          | Description                                                          |
| ---- | ------------- | -------------------------------------------------------------------- |
| `wj` | `packedFloat` | The share of the pool's liquidity the associated LPToken represents. |
| `hn` | `packedFloat` | The revenue parameter.                                               |
| `rj` | `packedFloat` | The last revenue claim for the associated LPToken.                   |

**Returns**

| Name     | Type          | Description                                       |
| -------- | ------------- | ------------------------------------------------- |
| `result` | `packedFloat` | The calculated revenue available for the LPToken. |

### \_liquidityUpdateHelper

*This function updates related tbc variables when a liquidity deposit or withdrawal is made*

```solidity theme={null}
function _liquidityUpdateHelper(ALTBCDef storage altbc, packedFloat Xn, packedFloat multiplier)
    internal
    returns (packedFloat x);
```

**Parameters**

| Name         | Type          | Description                             |
| ------------ | ------------- | --------------------------------------- |
| `altbc`      | `ALTBCDef`    | the tbc definition.                     |
| `Xn`         | `packedFloat` | the x coordinate.                       |
| `multiplier` | `packedFloat` | The value for multiplier for pool state |

**Returns**

| Name | Type          | Description          |
| ---- | ------------- | -------------------- |
| `x`  | `packedFloat` | The updated x value. |
