Exact Amount Swaps Guide

Overview

JAMM DEX supports two exact amount swap modes: Exact Input and Exact Output. These modes meet different trading needs, allowing users to precisely control either the input or output amount of swaps.

Exact Input Swaps

Basic Concept

Exact input swaps allow users to specify the exact input amount, and the system calculates and returns the corresponding output amount. This is the most commonly used swap mode.

Features:

  • Users know exactly how many tokens they will pay

  • Output amount is calculated based on current market price

  • Requires setting minimum output amount for slippage protection

Implementation

Token to Token Exact Input

async function swapExactTokensForTokens(
    tokenIn,
    tokenOut,
    amountIn,
    slippagePercent,
    userAddress,
    signer
) {
    const router = new ethers.Contract(ROUTER_ADDRESS, routerABI, signer);
    
    // 1. Query expected output
    const path = [tokenIn, tokenOut];
    const fees = [100]; // 1% fee rate
    const amounts = await router.getAmountsOut(amountIn, path, fees);
    const expectedOutput = amounts[1];
    
    // 2. Calculate minimum output (slippage protection)
    const slippageBps = Math.floor(slippagePercent * 100); // Convert to basis points
    const amountOutMin = expectedOutput.mul(10000 - slippageBps).div(10000);
    
    // 3. Set deadline
    const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes
    
    // 4. Execute swap
    const tx = await router.swapExactTokensForTokens(
        amountIn,
        amountOutMin,
        path,
        fees,
        userAddress,
        ethers.constants.AddressZero, // No referrer
        deadline
    );
    
    console.log("Exact input swap submitted:", tx.hash);
    const receipt = await tx.wait();
    
    return {
        transactionHash: tx.hash,
        blockNumber: receipt.blockNumber,
        expectedOutput,
        actualOutput: await getActualOutput(receipt) // Parse actual output from events
    };
}

JU to Token Exact Input

Token to JU Exact Input

Exact Output Swaps

Basic Concept

Exact output swaps allow users to specify the exact output amount, and the system calculates and requires the corresponding input amount. This mode is suitable for scenarios where you need to receive a precise amount of tokens.

Features:

  • Users know exactly how many tokens they will receive

  • Input amount is calculated based on current market price

  • Requires setting maximum input amount for slippage protection

Implementation

Token to Token Exact Output

JU to Token Exact Output

Token to JU Exact Output

Advanced Features

Multi-Hop Exact Swaps

Batch Exact Swaps

Price Calculation and Preview

Exact Input Price Preview

Exact Output Price Preview

Price Impact Calculation

Utility Functions

Swap Result Parsing

Slippage Calculator

Best Practices

1. Swap Mode Selection

2. Dynamic Slippage Adjustment

3. Pre-Swap Validation

Summary

Exact amount swaps are a core feature of JAMM DEX. This guide covers:

  1. Exact Input Swaps: Specify input amount, calculate output amount

  2. Exact Output Swaps: Specify output amount, calculate input amount

  3. JU Token Swaps: Special handling for native token swaps

  4. Multi-Hop Swaps: Exact amount swaps through complex paths

  5. Price Preview: Price calculation and impact analysis before swaps

  6. Best Practices: Mode selection, slippage management, validation mechanisms

By understanding and correctly using these two swap modes, developers can provide users with more precise and flexible trading experiences.