JAMMRouter Contract

Overview

JAMMRouter is the user interface layer contract of JAMM DEX, providing user-friendly trading and liquidity management functions. It encapsulates complex underlying logic, supports multi-hop swaps, automatic path calculation, deadline protection, and is deeply integrated with WJU, enabling users to trade directly using JU tokens.

Contract Basic Information

contract JAMMRouter is IJAMMRouter02 {
    address public immutable override factory;
    address public immutable override WJU;
    
    constructor(address _factory, address _WJU) {
        factory = _factory;
        WJU = _WJU;
    }
}

Immutable Variables:

  • factory: JAMMFactory contract address

  • WJU: WJU wrapped token contract address

Modifiers

Deadline Protection

All trading functions use this modifier to ensure transactions execute within the specified time.

ETH Reception

Router only accepts ETH from WJU contract, used for ETH transfers during WJU unwrapping.

Liquidity Management

Internal Liquidity Calculation

Calculation Logic:

  1. Check if trading pair exists, create if not

  2. Get current reserves

  3. If first addition, use desired amounts

  4. Otherwise calculate optimal ratio, ensuring minimum amount requirements

Add Liquidity

Add JU Liquidity

JU Processing Flow:

  1. Calculate optimal amounts

  2. Transfer tokens to trading pair

  3. Wrap JU to WJU and transfer to trading pair

  4. Mint LP tokens

  5. Refund excess JU

Remove Liquidity

Remove JU Liquidity

Permit Support

Remove Liquidity with Permit

Permit Advantages:

  • No need to pre-call approve

  • Complete authorization and operation in one transaction

  • Reduce gas consumption and transaction steps

Token Swapping

Internal Swap Logic

Multi-hop Swap Logic:

  1. Validate relationship between path and fee array lengths

  2. Iterate through swap path

  3. Calculate output amount for each hop

  4. Determine recipient address for next hop

  5. Call trading pair's swap function

Exact Input Swap

Exact Output Swap

JU Token Swapping

Buy Tokens with JU

Sell Tokens for JU

Fee-on-Transfer Token Support

Internal Swap Logic (Fee-on-Transfer Support)

Fee-on-Transfer Token Handling:

  • Don't pre-calculate amounts, base on actual received amounts

  • Recalculate input amount for each hop

  • Suitable for tokens that charge fees on transfer

Fee-on-Transfer Swap Functions

Query Functions

Price Query

Output Amount Calculation

Input Amount Calculation

Multi-hop Amount Calculation

Usage Examples

Token Swapping

JU Token Swapping

Add Liquidity

Price Query

Last updated