# Price Calculation

The CoolDEX uses a constant product market maker (CPMM) formula for price calculations.

## Constant Product Formula

The core formula for the CoolDEX is:

```
k = x * y
```

Where:

* `x` is the amount of token X in the pool
* `y` is the amount of token Y in the pool
* `k` is a constant that must be maintained during trades

This formula ensures that the product of the two token reserves remains constant, which creates a hyperbolic price curve.

## Swap Base In Calculation

When a user specifies the input amount for a swap, the output amount is calculated as:

```
amount_out = (pool_out * amount_in) / (pool_in + amount_in)
```

Where:

* `pool_out` is the amount of output token in the pool
* `pool_in` is the amount of input token in the pool
* `amount_in` is the specified input amount

### Example Calculation

Let's say:

* Pool has 1,000,000 TOKEN and 1,000 SOL
* User wants to swap 10 SOL for TOKEN

The calculation would be:

```
amount_out = (1,000,000 * 10) / (1,000 + 10)
amount_out = 10,000,000 / 1,010
amount_out ≈ 9,900.99 TOKEN
```

## Swap Base Out Calculation

When a user specifies the output amount for a swap, the required input amount is calculated as:

```
amount_in = (pool_in * amount_out) / (pool_out - amount_out)
```

Where:

* `pool_in` is the amount of input token in the pool
* `pool_out` is the amount of output token in the pool
* `amount_out` is the specified output amount

### Example Calculation

Let's say:

* Pool has 1,000,000 TOKEN and 1,000 SOL
* User wants to receive exactly 10,000 TOKEN

The calculation would be:

```
amount_in = (1,000 * 10,000) / (1,000,000 - 10,000)
amount_in = 10,000,000 / 990,000
amount_in ≈ 10.10 SOL
```

## Fee Calculation

Fees are applied to the swap amounts. The exact fee calculation depends on the token type:

### Standard Tokens

For standard (non-SC Bonding Curve) tokens, a simple fee is applied:

```
fee_amount = amount_in * fee_numerator / fee_denominator
amount_in_after_fee = amount_in - fee_amount
```

### SC Bonding Curve Tokens

For SC Bonding Curve tokens, the fee calculation is more complex and depends on the token's specific parameters:

1. **For selling tokens (Coin2PC)**:

   ```
   taxable_amount = amount_in * swap_fee_numerator * (cooldex_team_fee_wsol_fee_denominator - cooldex_team_fee_wsol_fee_numerator) / (swap_fee_denominator * cooldex_team_fee_wsol_fee_denominator)
   amount_to_burn = taxable_amount * burn_rate / CURVE_BURN_RATE_DENOMINATOR
   cult_token = (taxable_amount - amount_to_burn) / 2
   ```
2. **For buying tokens (PC2Coin)**:

   ```
   taxable_amount = amount_in * swap_fee_numerator / swap_fee_denominator
   platform_tax = taxable_amount * cooldex_team_fee_wsol_fee_numerator / cooldex_team_fee_wsol_fee_denominator
   cult_wsol = (taxable_amount - platform_tax) * (CURVE_BURN_RATE_DENOMINATOR - burn_rate) / (2 * CURVE_BURN_RATE_DENOMINATOR)
   ```

## LP Token Calculation

### Adding Liquidity

When adding liquidity, the amount of LP tokens minted is proportional to the share of the pool being added:

```
mint_lp_amount = (deposit_amount / total_amount) * total_lp_supply
```

Where:

* `deposit_amount` is the amount of the base token being deposited
* `total_amount` is the total amount of that token in the pool
* `total_lp_supply` is the current total supply of LP tokens

### Removing Liquidity

When removing liquidity, the amounts of tokens received are proportional to the share of LP tokens being burned:

```
token_amount = (lp_amount / total_lp_supply) * total_token_amount
```

Where:

* `lp_amount` is the amount of LP tokens being burned
* `total_lp_supply` is the current total supply of LP tokens
* `total_token_amount` is the total amount of the token in the pool


---

# 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.something.cool/cooldex/price-calculation.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.
