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 prefixfactory: Factory contract addresssalt: Hash value composed of token0, token1, and feeINIT_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:
Sort token addresses
Calculate trading pair address
Call trading pair contract to get reserves
Return corresponding reserves based on token order
Return Values:
reserveA: Reserve amount of tokenAreserveB: 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:
Verify path length is at least 2
Initialize amounts array, first element is input amount
Iterate through each hop in the path
Get reserves for current hop
Calculate output amount for current hop
Output amount becomes input for next hop
Multi-hop Input Calculation
Calculation Flow:
Verify path length
Initialize amounts array, last element is desired output amount
Iterate backward through the path
Get reserves for current hop
Calculate required input amount for current hop
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 amountfee: 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