JAMMLibrary

Overview

JAMMLibrary is the core calculation library of JAMM DEX, providing various mathematical calculations and utility functions required by the AMM system. It contains key functions such as token address sorting, trading pair address calculation, reserve queries, and price calculations, serving as the mathematical foundation of the entire protocol.

Library Function Categories

JAMMLibrary mainly contains the following categories of functions:

  • Address handling functions

  • Reserve query functions

  • Price calculation functions

  • Multi-hop path calculation functions

Address Handling Functions

Token Address Sorting

function sortTokens(
    address tokenA,
    address tokenB
) internal pure returns (address token0, address token1) {
    require(tokenA != tokenB, "JAMMLibrary: IDENTICAL_ADDRESSES");
    (token0, token1) = tokenA < tokenB
        ? (tokenA, tokenB)
        : (tokenB, tokenA);
    require(token0 != address(0), "JAMMLibrary: ZERO_ADDRESS");
}

Function Description:

  • Ensure two token addresses are not identical

  • Sort by address size, smaller address becomes token0

  • Verify token0 is not zero address

  • Return sorted address pair

Purpose:

  • Ensure trading pair uniqueness

  • Standardize token order in trading pairs

  • Provide consistent input for CREATE2 address calculation

Trading Pair Address Calculation

CREATE2 Address Calculation:

  • 0xff: CREATE2 opcode prefix

  • factory: Factory contract address

  • salt: Hash value composed of token0, token1, and fee

  • INIT_CODE_PAIR_HASH: Initialization code hash of JAMMPair contract (actual value determined at deployment)

Features:

  • Deterministic address generation

  • Calculate address without calling Factory contract

  • Support offline address pre-calculation

Reserve Query Functions

Get Reserves

Function Flow:

  1. Sort token addresses

  2. Calculate trading pair address

  3. Call trading pair contract to get reserves

  4. Return corresponding reserves based on token order

Return Values:

  • reserveA: Reserve amount of tokenA

  • reserveB: Reserve amount of tokenB

Price Calculation Functions

Proportional Calculation

Calculation Formula:

Purpose:

  • Calculate optimal ratio when adding liquidity

  • Price queries and display

  • Proportional calculation when removing liquidity

Output Amount Calculation

Calculation Formula:

Fee Handling:

  • Deduct trading fee from input amount

  • Fee expressed in basis points (10000 = 100%)

  • Amount after fee deduction participates in AMM calculation

Input Amount Calculation

Calculation Formula:

Precision Handling:

  • Add 1 to result to ensure sufficient input amount

  • Avoid swap failures due to precision loss

Multi-hop Path Calculation

Multi-hop Output Calculation

Calculation Flow:

  1. Verify path length is at least 2

  2. Initialize amounts array, first element is input amount

  3. Iterate through each hop in the path

  4. Get reserves for current hop

  5. Calculate output amount for current hop

  6. Output amount becomes input for next hop

Multi-hop Input Calculation

Calculation Flow:

  1. Verify path length

  2. Initialize amounts array, last element is desired output amount

  3. Iterate backward through the path

  4. Get reserves for current hop

  5. Calculate required input amount for current hop

  6. Continue forward calculation until first hop

Usage Examples

Basic Price Queries

Multi-hop Path Calculation

Address Calculation

Mathematical Principles

AMM Pricing Formula

JAMM DEX uses the constant product formula:

When considering fees:

Where:

  • x, y: Reserves before swap

  • Δx: Input amount

  • Δy: Output amount

  • fee: Trading fee rate

Price Impact

Price impact can be calculated using the following formula:

Where:

  • Actual Price = Δy / Δx

  • Theoretical Price = y / x

Slippage Calculation

Slippage is the difference between expected and actual execution price:

Last updated