JAMM System Architecture

Architecture Overview

JAMM DEX uses a layered, modular architecture to ensure scalability, security, and maintainability. The system consists of a core contract layer, a library layer, and an interface layer.

Core Contracts

JAMMFactory

Responsibilities: Creates and manages trading pairs.

  • Maintains a registry of all trading pairs.

  • Deploys pair contracts deterministically using CREATE2.

  • Manages protocol fee settings.

  • Handles the registration for the referral system.

Key Functions:

JAMMPair

Responsibilities: Implements the specific liquidity pool.

  • Maintains reserves of two tokens.

  • Executes token swap logic.

  • Manages adding and removing liquidity.

  • Calculates and allocates trading fees.

Key Functions:

JAMMRouter

Responsibilities: Provides a user-friendly interface for trading.

  • Offers high-level functions for trades.

  • Handles multi-hop swap paths.

  • Manages deadline checks.

  • Supports automatic conversion of ETH/WJU.

Key Functions:

JAMMERC20

Responsibilities: Standard implementation of LP tokens.

  • Implements the ERC-20 standard.

  • Supports the EIP-2612 permit function.

  • Manages the minting and burning of LP tokens.

Key Functions:

WJU

Responsibilities: Implementation of the wrapped JU token.

  • Provides wrapping and unwrapping of the JU token.

  • Fully compatible with the ERC-20 standard.

  • Supports receiving ETH directly.

Key Functions:

Libraries

JAMMLibrary

Provides core mathematical calculations and utility functions:

  • sortTokens(): Sorts token addresses.

  • pairFor(): Calculates the pair address.

  • getReserves(): Fetches reserves.

  • getAmountOut(), getAmountIn(): Calculates swap amounts.

  • getAmountsOut(), getAmountsIn(): Calculates amounts for multi-hop swaps.

Math

Provides safe mathematical operations:

  • sqrt(): Square root calculation (using Babylonian method).

  • min(): Finds the minimum of two numbers.

  • Uses Solidity >=0.8.0 for built-in overflow/underflow protection.

TransferHelper

Provides safe token transfer functions:

  • safeTransfer(): Safe token transfer.

  • safeTransferFrom(): Safe token transfer from another address.

  • safeTransferETH(): Safe ETH transfer.

  • safeApprove(): Safe token approval.

UQ112x112

Provides support for fixed-point number arithmetic:

  • Used for price accumulation.

  • Enables high-precision math.

Data Flow Architecture

Trading Flow

Liquidity Management Flow

Security Architecture

Re-entrancy Guard

All critical functions are protected by a re-entrancy lock:

Mathematical Safety

  • Uses Solidity ^0.8.21 for built-in overflow and underflow protection.

  • The Math library provides a safe sqrt function.

  • The JAMMLibrary includes checks for zero reserves and amounts.

Access Control

  • Key functions in JAMMFactory can only be called by feeToSetter.

  • The initialize function in JAMMPair can only be called by the factory.

  • A referrer can only be set once per user.

Fee Architecture

Fee Tiers

Fee Distribution

  • Trading Fees: A portion of the swap fee (fee/50000 or fee/100000) is sent to a designated feeTo address. If a referrer is active, the fee is split between the protocol and the referrer.

  • Protocol Fees: A portion of the liquidity provider fees can be minted as new LP tokens and sent to a mintTo address, calculated based on the growth of the k value.

Address Generation

CREATE2 Deterministic Deployment

Pair addresses are generated using CREATE2, ensuring predictability:

Init Code Hash

Current Value: 0x2e599419feff8382bdfc47abf847537b06e96b0525db3802b2a9fb0bfe068ed8

Event System

Core Events

  • PairCreated(address indexed token0, address indexed token1, uint24 fee, address pair, uint)

  • Swap(address indexed sender, uint amount0In, uint amount1In, uint amount0Out, uint amount1Out, address indexed to)

  • Mint(address indexed sender, uint amount0, uint amount1)

  • Burn(address indexed sender, uint amount0, uint amount1, address indexed to)

  • Sync(uint112 reserve0, uint112 reserve1)

  • Fee(address indexed sender, address indexed referrer, address token, uint amount)

  • Referrer(address indexed sender, address indexed referrer)

These events provide a complete on-chain data trail for front-end applications and analytics tools.

Last updated