How to Use Raydium SDK to Build a Swap — Part 2

Image source

Step 4: Get the liquidity pool information

import { jsonInfo2PoolKeys, LiquidityPoolJsonInfo, LiquidityPoolKeys } from "@raydium-io/raydium-sdk";const RAY_SOL_LP_V4_POOL_KEY = '89ZKE4aoyfLBe2RuV6jM3JGNhaV18Nxh8eNtjRcndBip';const RAYDIUM_LIQUIDITY_JSON = 'https://api.raydium.io/v2/sdk/liquidity/mainnet.json';const [raySolPoolKey, setRaySolPoolKey] = useState<LiquidityPoolKeys>();// ...const getPoolInfo = async () => {// fetch the liquidity pool list
const liquidityJsonResp = await fetch(RAYDIUM_LIQUIDITY_JSON);
if (!(await liquidityJsonResp).ok) return []const liquidityJson = await liquidityJsonResp.json();const allPoolKeysJson = [...(liquidityJson?.official ?? []), ...(liquidityJson?.unOfficial ?? [])]// find the liquidity pair
const
poolKeysRaySolJson: LiquidityPoolJsonInfo = allPoolKeysJson.filter((item) => item.lpMint === RAY_SOL_LP_V4_POOL_KEY)?.[0] || null;
// convert the json info to pool key using jsonInfo2PoolKeys
const
raySolPk = jsonInfo2PoolKeys(poolKeysRaySolJson);
setRaySolPoolKey(raySolPk);}

Step 5: Calculate the amount out using the pool key

Step 6: Make the swap transaction and send it! 🚀

import { Liquidity } from "@raydium-io/raydium-sdk";import { useConnection, useWallet } from '@solana/wallet-adapter-react';import { calcAmountOut } from '../utils';const { publicKey, sendTransaction } = useWallet();const { connection } = useConnection();// ...const { amountIn, minAmountOut } = await calcAmountOut(connection, raySolPoolKey, inputNumber, swapInDirection);const { transaction, signers } = await Liquidity.makeSwapTransaction({  connection,  poolKeys: raySolPoolKey,  userKeys: {    tokenAccounts,    owner: publicKey,  },  amountIn,  amountOut: minAmountOut,  fixedSide: "in"});const txid = await sendTransaction(transaction, connection, { signers, skipPreflight: true });console.log(`https://solscan.io/tx/${txid}`);

--

--

--

Software engineer, dreamer, another cat lover. Just share what I’ve learned.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Handling changes on belongsToMany relations with Laravel Scout Extended

dangoDB: A MongoDB ODM for Deno

Asynchronous Javascript Part 1: Single-Threaded Programming and the Call Stack

Javascript

The difference between the process.nextTick, and setImmediate.

Getting Started with ReactJS

Everything Redux — Part 1

Angular 2 interview questions — Angular Js Interview Questions

My first mobile application using Ionic-4, Angular-7 and Apollo Client

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abby Low

Abby Low

Software engineer, dreamer, another cat lover. Just share what I’ve learned.

More from Medium

Learn to Build a Swap Application with Solana Wallet Adapter and Raydium SDK — Part 1

Frequently asked: Blockchain Interview Questions and Answers

Build NFT Applications using Onecdot

How To Create a Solana NFT With IPFS