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:

Swap Base Out Calculation

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

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:

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:

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):

  2. For buying tokens (PC2Coin):

LP Token Calculation

Adding Liquidity

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

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:

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

Last updated