Skip to content

vitwit/sui-amm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple AMM (Automated Market Maker) on Sui

A basic implementation of an Automated Market Maker built on the Sui blockchain using Move programming language. This project demonstrates token creation, liquidity pool management, and token swapping functionality.

🌟 Features

  • Custom Token Creation: Create and mint USDC and custom tokens
  • Liquidity Pool Management: Create pools and add liquidity
  • Token Swapping: Swap between USDC and custom tokens using constant product formula
  • TypeScript Client: Complete client library for interacting with the contracts
  • Local Testing: Full testing suite for local development

📁 Project Structure

sui-amm/
├── move/
│   ├── sources/
│   │   ├── token_a.move        # USDC token implementation
│   │   ├── token_b.move        # Custom token (MYTOKEN) implementation
│   │   └── token_swap.move     # AMM pool and swap logic
│   └── Move.toml
├── ts-tests/
│   ├── src/
│   │   └── index.ts           # TypeScript client implementation
│   ├── package.json
│   └── tsconfig.json
└── README.md

🔧 Prerequisites

  • Sui CLI: Install from Sui documentation
  • Node.js: Version 16 or higher
  • TypeScript: For running the client
  • Local Sui Network: For testing

🚀 Quick Start

1. Setup Local Sui Network

# Start local Sui network
sui start --with-faucet --force-regenesis

# Create a new address (if needed)
sui client new-address ed25519

# Get test SUI tokens
sui client faucet

2. Deploy Smart Contracts

# Navigate to move directory
cd move/

# Build and publish contracts
sui client publish --gas-budget 20000000

Save the Package ID and Treasury Cap IDs from the publish output!

3. Setup TypeScript Client

# Navigate to typescript client directory
cd ts-tests/

# Install dependencies
npm install

# Update the contract addresses in src/index.ts:
# - PACKAGE_ID: Your published package ID
# - USDC_TREASURY_CAP: USDC treasury cap object ID
# - MYTOKEN_TREASURY_CAP: MYTOKEN treasury cap object ID

4. Run the Demo

# Execute the demo script
npm run test
# or
npx && ts-node src/index.ts

📖 Smart Contract Details

Token Contracts

USDC Token (token_a.move)

  • Symbol: USDC
  • Name: USD Coin
  • Decimals: 6
  • Features: Mintable with treasury cap

Custom Token (token_b.move)

  • Symbol: MTK
  • Name: My Token
  • Decimals: 6
  • Features: Mintable with treasury cap

AMM Contract (token_swap.move)

Core Functions

Pool Management:

  • create_pool(): Initialize a new liquidity pool
  • add_liquidity(): Add tokens to existing pool

Trading:

  • swap_usdc_to_mytoken(): Swap USDC for custom tokens
  • swap_mytoken_to_usdc(): Swap custom tokens for USDC

Queries:

  • get_reserves(): Get current pool reserves
  • get_amount_out(): Calculate swap output amount

AMM Formula

The AMM uses the constant product formula: x * y = k

Where:

  • x = USDC reserve
  • y = MYTOKEN reserve
  • k = constant product

Swap calculation:

amount_out = (reserve_out * amount_in) / (reserve_in + amount_in)

💻 TypeScript Client Usage

Basic Operations

import { SuiAMMClient } from './src/index';

// Initialize client
const ammClient = new SuiAMMClient();

// Mint tokens
await ammClient.mintUSDC(BigInt(1000 * 1e6)); // 1000 USDC
await ammClient.mintMYTOKEN(BigInt(2000 * 1e6)); // 2000 MYTOKEN

// Create pool
const poolResult = await ammClient.createPool(
    BigInt(500 * 1e6),  // 500 USDC
    BigInt(1000 * 1e6)  // 1000 MYTOKEN
);

// Perform swap
await ammClient.swapUSDCToMYTOKEN(poolId, BigInt(100 * 1e6));

🧪 Testing

The project includes a complete testing workflow that:

  1. Mints tokens for testing
  2. Creates a liquidity pool with initial reserves
  3. Performs token swaps and verifies results
  4. Checks pool state before and after operations

📚 Additional Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published