Token Swaps Guide

Overview

Token swaps are the core functionality of JAMM DEX, allowing users to trade between different ERC-20 tokens without permission. This guide details how to use JAMM DEX for various types of token swaps, including exact input, exact output, and swaps involving JU tokens.

Swap Types

Exact Input Swaps

Users specify the exact input amount, and the system calculates the output amount.

// swapExactTokensForTokens
const amountIn = ethers.utils.parseEther("1.0"); // Exact input 1 TokenA
const amountOutMin = ethers.utils.parseEther("0.95"); // Minimum 0.95 TokenB
const path = [tokenA_address, tokenB_address];
const fees = [100]; // 1% fee rate (100 basis points)
const to = userAddress;
const referrer = ethers.constants.AddressZero;
const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // Expires in 20 minutes

const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    path,
    fees,
    to,
    referrer,
    deadline
);

Exact Output Swaps

Users specify the exact output amount, and the system calculates the required input amount.

JU Token Swaps

Buy Tokens with JU

Buy Exact Amount of Tokens with JU

Sell Tokens for JU

Sell Tokens for Exact Amount of JU

Pre-Swap Preparation

1. Token Approval

Before swapping ERC-20 tokens, you need to approve the Router contract to spend your tokens:

2. Price Query

Query expected output amount before swapping:

3. Slippage Settings

Set reasonable slippage protection based on market volatility:

Complete Swap Examples

Basic Token Swap

JU Token Swap Example

Advanced Features

Referrer System

Using a referrer can provide fee discounts:

Batch Swaps

Error Handling

Common Errors and Solutions

Best Practices

1. Pre-Swap Checks

2. Price Impact Calculation

3. Dynamic Slippage Settings

Monitoring and Analytics

Swap Event Monitoring

Swap History Query

Summary

Token swaps are the core functionality of JAMM DEX. This guide covers:

  1. Swap Types: Exact input, exact output, JU token swaps

  2. Preparation: Approval, price queries, slippage settings

  3. Complete Examples: Practical swap code templates

  4. Advanced Features: Referrer system, batch swaps

  5. Error Handling: Common error identification and resolution

  6. Best Practices: Safe and efficient swap strategies

By following this guide, developers can safely and efficiently integrate JAMM DEX token swap functionality.