Code Monkey home page Code Monkey logo

raydium-sdk-swap-example-typescript's Introduction

Labs

Chainstack is the leading suite of services connecting developers with Web3 infrastructure

         

Supported protocolsChainstack blogChainstack docsBlockchain API referenceStart for free

Raydium SDK Swap Example

This project demonstrates how to perform a token swap on the Solana blockchain using Raydium and Chainstack. The example specifically illustrates swapping SOL (native Solana token) for USDC (a stablecoin).

Find the full guide on the Chainstack Developer Portal.

Shoutout to precious-void for the the base code used for this project!

Features

  • Utilizes the Raydium SDK for interacting with the Solana blockchain.
  • Supports both versioned and legacy transactions.
  • Allows simulation of swap transactions before execution.
  • Easy configuration for swap parameters through a dedicated config file.

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js installed (v18 or above recommended)
  • Yarn
  • A Solana wallet with some SOL for testing the swap
  • An environment file (.env) with your RPC URL and WALLET_PRIVATE_KEY

Chainstack Solana node

Deploy a Solana node on Chainstack; the following steps will guide you:

  1. Sign up with Chainstack.
  2. Deploy a node.
  3. View node access and credentials.

Environment variables

Add your RPC endoint and private key to a .env file:

RPC_URL=YOUR_RPC_URL
WALLET_PRIVATE_KEY=YOUR_PRIVATE_KEY

Installation

Clone the repository locally and install the dependencies:

git clone https://github.com/soos3d/raydium-sdk-swap-example.git
cd raydium-sdk-swap-example
yarn

Usage

Edit the configuration in src/swapConfig.ts editing:

  • Select if you want to send the transaction or only simulate
  • The amount to swap
  • The tokens to swap
  • The liquidity file to pull the pool info from
export const swapConfig = {
  executeSwap: false, // Send tx when true, simulate tx when false
  useVersionedTransaction: true,
  tokenAAmount: 0.01, // Swap 0.01 SOL for USDT in this example
  tokenAAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case
  tokenBAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC address
  maxLamports: 1000000, // Max lamports allowed for fees
  direction: "in" as "in" | "out", // Swap direction: 'in' or 'out'
  liquidityFile: "https://api.raydium.io/v2/sdk/liquidity/mainnet.json",
  maxRetries: 10
};

Then run:

yarn swap

raydium-sdk-swap-example-typescript's People

Contributors

soos3d avatar 96ayad avatar hellodword avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.