Skip to content

vitwit/solana-amm

Repository files navigation

Token Swap Program on Solana

A decentralized token swap program built on Solana using the Anchor framework. This program implements a simple automated market maker (AMM) with constant product formula for token swapping.

Features

  • Pool Initialization: Create liquidity pools for any SPL token pair
  • Add Liquidity: Deposit tokens into existing pools
  • Token Swapping: Swap between tokens using constant product formula (x × y = k)
  • Slippage Protection: Minimum output amount protection for swaps

Prerequisites

Before you begin, ensure you have the following installed:

Installation

  1. Clone the repository:

    git clone https://github.com/Hemanthghs/token-swap
    cd token-swap
  2. Install dependencies:

    yarn install
    # or
    npm install

Configuration

  1. Set up Solana CLI for local development:

    # Set to localhost for local testing
    solana config set --url localhost
    
    # Generate a new keypair (if you don't have one)
    solana-keygen new
  2. Configure Anchor:

    # Check your wallet address
    solana address
    
    # Update Anchor.toml with your program ID and wallet
    anchor keys list

Building the Program

  1. Build the program:

    anchor build
  2. Get the program ID:

    anchor keys list
  3. Update the program ID in your code: (Important!)

    • Update declare_id!() in programs/swap-2/src/lib.rs
    • Update Anchor.toml with the correct program ID
  4. Rebuild after updating program ID:

    anchor build

Deployment

Deploy to Local Testnet

  1. Start the local Solana test validator:

    # Start local validator in a new terminal window
    solana-test-validator

    Keep this terminal open - the validator needs to run continuously.

  2. In a new terminal, configure for localhost:

    solana config set --url localhost
    
    # Check that you have SOL (local validator provides test SOL automatically)
    solana balance
  3. Deploy the program to local testnet:

    anchor deploy
  4. Verify deployment:

    solana program show <PROGRAM_ID>

Client Usage

Setup Client for Local Testing

  1. Deploy your program (if not already deployed):

    # In another terminal
    anchor deploy
  2. Compile TypeScript:

    npx tsc
  3. Run the client:

    node app/client.js

Client Configuration

Make sure your client connects to the local cluster:

// In your client code
import * as anchor from "@coral-xyz/anchor";

// Connect to local cluster
const connection = new anchor.web3.Connection("http://localhost:8899", "confirmed");
const provider = new anchor.AnchorProvider(connection, wallet, {});
anchor.setProvider(provider);

Program Instructions

1. Initialize Pool

Creates a new liquidity pool for a token pair.

Parameters:

  • mint_a: First token mint
  • mint_b: Second token mint

2. Add Liquidity

Adds tokens to an existing pool.

Parameters:

  • amount_a: Amount of token A to add
  • amount_b: Amount of token B to add

3. Swap

Swaps tokens using the constant product formula.

Parameters:

  • amount_in: Amount of input tokens
  • minimum_amount_out: Minimum acceptable output amount
  • a_to_b: Direction of swap (true for A→B, false for B→A)

Account Structure

Pool Account

  • authority: Pool creator's public key
  • mint_a: First token mint address
  • mint_b: Second token mint address
  • bump: PDA bump seed

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors