JAMMPair Contract

Overview

JAMMPair is the core trading pair contract of JAMM DEX, implementing specific AMM liquidity pool functionality. Each trading pair is an independent JAMMPair contract instance, responsible for managing reserves of two tokens, executing swap logic, handling liquidity operations, and maintaining price oracle data.

Contract Inheritance Structure

contract JAMMPair is JAMMERC20 {
    using UQ112x112 for uint224;
    // ...
}

JAMMPair inherits from JAMMERC20, meaning each trading pair is itself an ERC-20 token (LP token).

Core Constants

uint public constant MINIMUM_LIQUIDITY = 10 ** 3;  // 1000
bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)")));
  • MINIMUM_LIQUIDITY: Permanently locked minimum liquidity amount

  • SELECTOR: ERC-20 transfer function selector for safe transfers

State Variables

Basic Information

Reserve Management

Storage Optimization: Three variables packed in one storage slot:

  • uint112 + uint112 + uint32 = 256 bits

Price Oracle

Reentrancy Protection

Initialization

Constructor

Constructor only sets factory address, actual initialization is done through initialize function.

Initialize Function

Security: Only Factory contract can call the initialize function.

Core Functions

Reserve Query

This is the most commonly used query function, returning current reserves and last update time.

Reserve Update

Update Logic:

  1. Check balances won't overflow uint112

  2. Calculate time elapsed

  3. Update cumulative prices (if time elapsed and reserves are non-zero)

  4. Update reserves and timestamp

  5. Emit Sync event

Liquidity Management

Add Liquidity (Mint)

Minting Process:

  1. Get current reserves and balances

  2. Calculate newly added token amounts

  3. Handle protocol fees

  4. Calculate LP tokens to mint

  5. Mint LP tokens to specified address

  6. Update reserves

  7. Update k value (if protocol fees enabled)

  8. Emit Mint event

Remove Liquidity (Burn)

Burning Process:

  1. Get current state

  2. Handle protocol fees

  3. Calculate proportional token amounts to return

  4. Burn LP tokens

  5. Transfer tokens to specified address

  6. Update reserves

  7. Emit Burn event

Token Swapping

Main Swap Function

Swap Process:

  1. Validate output amounts and liquidity sufficiency

  2. Optimistic transfer (transfer tokens first)

  3. Support flash loan callback

  4. Calculate actual input amounts

  5. Collect trading fees

  6. Verify constant product formula

  7. Update reserves

  8. Emit Swap event

Fee Collection

Fee Distribution Logic:

  • No referrer: Protocol collects fee/50000 of fees

  • Has referrer: Protocol and referrer each collect fee/100000 of fees

Protocol Fee Minting

Protocol Fee Calculation:

  • Based on k value growth to calculate protocol fees

  • Formula: liquidity = totalSupply * (√k - √kLast) * 8 / (√k * 17 + √kLast * 8)

Safe Transfer

Safe Transfer Function

Fee Transfer Function

Features:

  • Handles non-standard ERC-20 tokens

  • Zero value transfers return directly

  • Fee transfers emit Fee events

Utility Functions

Skim Function

Purpose: Remove excess token balances beyond reserves.

Sync Function

Purpose: Force synchronization of reserves with actual balances.

Event System

Core Events

Usage Examples

Query Trading Pair Information

Listen to Swap Events

Listen to Fee Events

Last updated