Skip to content

Swap API (Pump.fun, Moonshot, Raydium, …), This repository provides examples for swapping ( buying, selling ) tokens on the Solana blockchain (whether they are on Moonshot, Pump.fun, Raydium, or other platforms).

Notifications You must be signed in to change notification settings

desprdo/solana-swap-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Solana Token Swap Examples

Code samples for performing token swaps on Solana (Basically buying and selling).

Swap API Docs: https://docs.solxtence.com/swap Swap API Status: https://status.solxtence.com

Available Examples

  • JavaScript (Node.js)
  • Python

Each example has its own directory with setup and usage instructions.

Quick Start

  1. Clone the repo
  2. Choose an example
  3. Follow the README in the example directory

JavaScript Example

import {
  Connection,
  Keypair,
  Transaction,
  VersionedTransaction,
  sendAndConfirmTransaction,
} from "@solana/web3.js";
import axios from "axios";
import bs58 from "bs58";

async function performSwap() {
  // Initialize connection and keypair
  const connection = new Connection("https://api.mainnet-beta.solana.com");
  const keypair = Keypair.fromSecretKey(bs58.decode("YOUR_PRIVATE_KEY"));

  // Swap parameters
  const params = new URLSearchParams({
    from: "So11111111111111111111111111111111111111112", // SOL
    to: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
    amount: 0.5, // From amount
    slip: 10, // Slippage
    payer: keypair.publicKey.toBase58(),
    fee: 0.00009, // Priority fee
    txType: "v0", // Change to "legacy" for legacy transactions
  });

  try {
    // Get swap transaction
    const response = await axios.get(
      `https://swap.solxtence.com/swap?${params}`
    );
    const { serializedTx, txType } = response.data.transaction;

    // Fetch the latest blockhash
    const { blockhash } = await connection.getLatestBlockhash();

    // Deserialize and sign the transaction
    let transaction;
    if (txType === "v0") {
      transaction = VersionedTransaction.deserialize(
        Buffer.from(serializedTx, "base64")
      );
      transaction.message.recentBlockhash = blockhash;
      transaction.sign([keypair]);
    } else {
      transaction = Transaction.from(Buffer.from(serializedTx, "base64"));
      transaction.recentBlockhash = blockhash;
      transaction.sign(keypair);
    }

    // Send and confirm the transaction
    const signature = await sendAndConfirmTransaction(connection, transaction);
    console.log("Swap successful! Transaction signature:", signature);
  } catch (error) {
    console.error("Error performing swap:", error);
  }
}

performSwap();

What the Example Does?

  • Prepares swap parameters
  • Fetches serialized swap transaction from SolXtence API
  • Deserializes and signs the transaction
  • Sends the transaction to Solana network

Notes

  • Docs for the API used in examples: https://docs.solxtence.com/swap
  • SolXtence API handles routing through various Solana AMMs (Moonshot, Pump.fun, Raydium, Jupiter...).
  • Examples are for educational purposes. Secure private keys in production.
  • Requires a Solana wallet with enough SOL for transaction fees

Contribute

Feel free to add examples in other languages or improve existing ones. Pull requests welcome.

About

Swap API (Pump.fun, Moonshot, Raydium, …), This repository provides examples for swapping ( buying, selling ) tokens on the Solana blockchain (whether they are on Moonshot, Pump.fun, Raydium, or other platforms).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published