Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

test-raydium-sdk-v2

raydium-io2.8kGPL-3.00.0.314-alphaTypeScript support: included

An SDK for building applications on top of Raydium.

raydium, solana

readme

Raydium SDK

npm

An SDK for building applications on top of Raydium.

Usage Guide

Installation

$ yarn add @raydium-io/raydium-sdk

Enable special logger

import { setLoggerLevel } from "@raydium-io/raydium-sdk";

setLoggerLevel("Common.Api", "debug");

Features

Initialization

import { Raydium } from '@raydium-io/raydium-sdk'
const raydium = await Raydium.load({
  connection,
  owner, // key pair or publicKey
  signAllTransactions, // optional - provide sign functions provided by @solana/wallet-adapter-react
  tokenAccounts, // optional, if dapp handle it by self can provide to sdk
  tokenAccountRowInfos // optional, if dapp handle it by self can provide to sdk
})

how to transform token account data

import { parseTokenAccountResp } from '@raydium-io/raydium-sdk'

const solAccountResp = await connection.getAccountInfo(ownerPubKey);
const tokenAccountResp = await connection.getTokenAccountsByOwner(
  ownerPubKey,
  { programId: TOKEN_PROGRAM_ID },
);

const { tokenAccounts, tokenAccountRawInfos } = parseTokenAccountResp({
  solAccountResp,
  tokenAccountResp,
})

data after initialization

# token
raydium.token.allTokens
raydium.token.allTokenMap
raydium.token.tokenMints
raydium.token.tokenPrices

# liquidity pool
raydium.liquidity.allPools
raydium.liquidity.allPoolIdSet
raydium.liquidity.allPoolMap
raydium.liquidity.allPairs
raydium.liquidity.allPairsMap
raydium.liquidity.lpTokenMap
raydium.liquidity.lpPriceMap

# clmm pool
raydium.ammv3.pools.data
raydium.ammv3.pools.dataMap
raydium.ammv3.pools.sdkParsedData
raydium.ammv3.pools.sdkParsedDataMap
raydium.ammv3.pools.hydratedData
raydium.ammv3.pools.hydratedDataData

# farm pool
raydium.farm.allFarms
raydium.farm.allParsedFarms
raydium.farm.allHydratedFarms
raydium.farm.allHydratedFarmMap

# token account
raydium.account.tokenAccounts
raydium.account.tokenAccountRawInfos

Liquidity

import { Raydium, Token, Percent, TokenAmount } from '@raydium-io/raydium-sdk'
import BN from 'bn.js'

const raydium = await Raydium.load({
  connection,
  owner // please provide key pair, if want to handle tx by yourself, just provide publicKey
  signAllTransactions // optional - provide sign functions provided by @solana/wallet-adapter-react
})

// Raydium.load call raydium.liquidity.load() automatically, also can call raydium.liquidity.load() manually

// if need trading pair info, call await raydium.liquidity.loadPairs()

const { transaction, signers, execute } = raydium.liquidity.createPool({
  version: 4,
  baseMint: new PublicKey(),
  quoteMint: new PublicKey(),
  marketId: new PublicKey() // https://docs.projectserum.com/serum-ecosystem/build-on-serum/add-a-market-on-serum-serum-academy
})

const { transaction, signers, execute } = raydium.liquidity.initPool({
  version: 4,
  baseMint: new PublicKey(),
  quoteMint: new PublicKey(),
  marketId: new PublicKey(),
  baseAmount: raydium.mintToTokenAmount({ mint, amount: "10" }),
  quoteAmount: raydium.mintToTokenAmount({ mint, amount: "20" }),
})
const { transaction, signers, execute } = raydium.liquidity.addLiquidity({
  poolId: new PublicKey(pool),
  payer: new PublicKey(payer), // optional
  amountInA: raydium.mintToTokenAmount({ mint, amount: "20" }),
  amountInB: raydium.mintToTokenAmount({ mint, amount: "30" }),
  fixedSide: "a", // "a" or "b"
})
const { transaction, signers, execute } = raydium.liquidity.removeLiquidity({
  poolId: new PublicKey(pool),
  payer: new PublicKey(payer), // optional
  amountIn: raydium.mintToTokenAmount({ mint, amount: "20" }),
})

const txId = execute()

Liquidity

import { Raydium, Token, Percent, TokenAmount } from '@raydium-io/raydium-sdk'
import BN from 'bn.js'

const raydium = await Raydium.load({
  connection,
  owner // please provide key pair, if want to handle tx by yourself, just provide publicKey
  signAllTransactions // optional - provide sign functions provided by @solana/wallet-adapter-react
})

await raydium.ammV3.load() // load all clmm pool data

Farm

import { Raydium, Token, Percent, TokenAmount } from '@raydium-io/raydium-sdk'
import BN from 'bn.js'

const raydium = await Raydium.load({
  connection,
  owner // please provide key pair, if want to handle tx by yourself, just provide publicKey
  signAllTransactions // optional - provide sign functions provided by @solana/wallet-adapter-react
})

await raydium.farm.load() // default load farms data
await raydium.farm.loadHydratedFarmInfo // load farms data width apr and detail info

Farm methods

raydium.farm.create({
  poolId, // oneOf liquidity pool id in https://api.raydium.io/v2/sdk/liquidity/mainnet.json
  rewardInfos // reward info array
})
const { transaction, signers, execute } = raydium.farm.restartReward({ farmId, rewardInfos })
const { transaction, signers, execute } = raydium.farm.addNewRewardToken({ poolId, newRewardInfo })
const { transaction, signers, execute } = raydium.farm.deposit({ farmId, amount })
const { transaction, signers, execute } = raydium.farm.withdraw({ farmId, amount })
const { transaction, signers, execute } = raydium.farm.withdraw({ farmId, withdrawMint: new PublicKey(xxx) })
const txId = execute()

Reward info example

const startTime = new BN(new Date("2022-08-20 15:00").getTime() / 1000)
const endTime = new BN(new Date("2022-08-30 15:00").getTime() / 1000)
const rewardPerSecond = new BN(totalAmount / (endTime - startTime))

const rewardInfo = {
  poolId: "13uCPybNakXHGVd2DDVB7o2uwXuf9GqPFkvJMVgKy6UJ",
  rewardInfos:[{
    rewardOpenTime: startTime,
    rewardEndTime: endTime,
    rewardMint: new PublicKey("So11111111111111111111111111111111111111112"),
    rewardPerSecond: rewardPerSecond.
  }]
}

Swap

direct swap with automatically routes

import { Raydium, Token, Percent, TokenAmount } from '@raydium-io/raydium-sdk'
import BN from 'bn.js'

const raydium = Raydium.load({
  connection,
  owner // please provide key pair, if want to handle tx by yourself, just provide publicKey
  signAllTransactions // optional - provide sign functions provided by @solana/wallet-adapter-react
})

const { transaction, signers, execute } = await raydium.trade.directSwap({
  inputMint: ${rayMint},
  outputMint: PublicKey.default, // PublicKey as sol mint
  amountIn: "1.2345",
  slippage: new Percent(5, 100),
  fixedSide: "in"
})

const txId = execute()

custom controlled route swap

const { availablePools, best, routedPools } = await raydium.trade.getAvailablePools({
  inputMint: ${rayMint},
  outputMint: "sol",
})

const inputToken = raydium.mintToToken("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6Rdecimals")
// or use new Token({ mint: "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6Rdecimals: 6, name: "Raydium", symbol: "RAY" })
const outToken = raydium.mintToToken(PublicKey.default)
// or use new Token({ mint: PublicKey.default }) <= sdk will generate wsol token automatically

const { amountOut, minAmountOut, routes, routeType } =
  await raydium.swap.getBestAmountOut({
  pools: routedPools, // optional, if not passed, will auto choose best route
  inputToken: inputToken,
  outputToken: outToken,
  amountIn: '1.2345', // or new BN("1,2345");
  slippage: new Percent(10, 100) // 10%
})

const { transaction, signers, execute } = await raydium.trade.swap({
  routes,
  routeType,
  amountIn: raydium.mintToTokenAmount({ mint: ${rayMint}), amount: "1.2345" }),
  amountOut: minAmountOut,
  fixedSide: "in"
})

const txId = execute()

Reference

changelog

Changelog

1.1.0-beta.0 (2022-03-14)

  • Add
    • Build mjs & cjs
  • Change
    • Fix swap fee currency

1.0.1-beta.46 (2022-03-14)

  • Change
    • Fix stable price

1.0.1-beta.45 (2022-03-14)

  • Change
    • Fix stable price

1.0.1-beta.44 (2022-03-14)

  • Change
    • Fix price impact

1.0.1-beta.43 (2022-03-14)

  • Change
    • Fix adjust price impact
    • Return swap fee

1.0.1-beta.42 (2022-03-11)

  • Change
    • Fix init stable model layout

1.0.1-beta.41 (2022-03-11)

  • Change
    • Fix startTime

1.0.1-beta.40 (2022-03-11)

  • Add
    • Return startTime for Liquidity.fetchMultipleInfo

1.0.1-beta.39 (2022-03-11)

  • Add
    • Stable layout

1.0.1-beta.38 (2022-03-10)

  • Add
    • Stable and route for stable

1.0.1-beta.37 (2022-02-25)

  • Change
    • Fix route pools bug

1.0.1-beta.36 (2022-02-24)

  • Add
    • executionPrice for Route

1.0.1-beta.35 (2022-02-24)

  • Add
    • priceImpact for Route

1.0.1-beta.34 (2022-02-22)

  • Change
    • Fix route bug

1.0.1-beta.33 (2022-02-21)

  • Change
    • Trade return data structure

1.0.1-beta.32 (2022-02-21)

  • Change
    • Fixes route

1.0.1-beta.31 (2022-02-20)

  • Add
    • getEnabledFeatures for Liquidity
    • computeAmountOut for Route
  • Change
    • Rename computeQuotePrice to getRate of Liquidity
    • Amount calculation is consistent with on-chain
    • Route program ID and instructions

1.0.1-beta.30 (2022-02-16)

  • Add
    • makeCreatePoolTransaction for Liquidity
    • makeInitPoolTransaction for Liquidity
  • Change
    • Update dev dependencies

1.0.1-beta.29 (2022-02-10)

  • Change
    • Fixes bugs
    • Update dev dependencies

1.0.1-beta.28 (2022-02-03)

  • Change
    • Optional param startTime of Liquidity.makeInitPoolInstruction

1.0.1-beta.27 (2022-02-03)

  • Add
    • Add param startTime of Liquidity.makeInitPoolInstruction
  • Change
    • Fixes bugs
    • Update dev dependencies

1.0.1-beta.26 (2022-01-18)

  • Change
    • Fixes bugs

1.0.1-beta.25 (2022-01-17)

  • Change
    • Accept token accounts to replace the original specified account when make transaction

1.0.1-beta.24 (2022-01-15)

  • Change
    • Fixes
    • Update dev dependencies

1.0.1-beta.23 (2022-01-15)

  • Add
    • Return priceImpact for Liquidity.computeCurrencyAmountOut and Liquidity.computeCurrencyAmountIn
  • Change
    • Fixes

1.0.1-beta.22 (2022-01-15)

  • Change
    • Fixes

1.0.1-beta.21 (2022-01-14)

  • Add
    • makeSwapInstruction for Liquidity
    • gt and lt for CurrencyAmount

1.0.1-beta.19 (2022-01-12)

  • Add
    • makeRemoveLiquidityTransaction for Liquidity
  • Change
    • Rename params of Liquidity.makeAddLiquidityTransaction
    • Rename params of Liquidity.makeAddLiquidityInstructionV4
    • Rename computeAnotherTokenAmount to computeAnotherCurrencyAmount of Liquidity
    • Rename params of Liquidity.computeAnotherCurrencyAmount
    • Rename computeTokenAmountOut to computeCurrencyAmountOut of Liquidity
    • Rename params of Liquidity.computeCurrencyAmountOut
    • Rename computeTokenAmountIn to computeCurrencyAmountIn of Liquidity
    • Rename params of Liquidity.computeCurrencyAmountIn

1.0.1-beta.18 (2022-01-12)

  • Add
    • makeAddLiquidityTransaction for Liquidity
    • fetchInfo for Liquidity
  • Change
    • Rename getPools to fetchAllPoolKeys of Liquidity
    • Rename getMultipleInfo to fetchMultipleInfo of Liquidity
    • Rename getAmountB to computeAnotherTokenAmount of Liquidity
    • Rename getAmountOut to computeTokenAmountOut of Liquidity
    • Rename getAmountIn to computeTokenAmountIn of Liquidity
    • Update dev dependencies

1.0.1-beta.17 (2022-01-10)

  • Add
    • makeSwapFixedOutInstructionV4 for Liquidity
  • Change
    • Rename makeSwapInstructionV4 to makeSwapFixedInInstructionV4 of Liquidity
    • Rename params side to tradeSide of Liquidity
    • Update dev dependencies

1.0.1-beta.15 (2022-01-09)

  • Add
    • getQuote for Liquidity
    • getAmountB for Liquidity
    • getAmountOut for Liquidity
    • getAmountIn for Liquidity
  • Change
    • Rename getOutputAmount to getAmountOut of Liquidity
    • Update dev dependencies

1.0.1-beta.14 (2021-12-21)

  • Change
    • Fix pendingRewards calculating for Farm stake pool

1.0.1-beta.11 (2021-12-17)

  • Add
    • ledger PDA for Farm v3
  • Change
    • Raising the priority of version parameters
    • Return Object instead of Array whenever possible, but the order of the Object will be consistent with the Array, use a unique value for the key, such as id or mint or something else, etc.
    • Don't calculate ATA / PDA in function
    • Rename type PublicKeyIsh to PublicKeyish
    • Rename type BigNumberIsh to BigNumberish
    • Rename marketVaultSigner to marketAuthority for poolKeys
    • Rename getAssociatedVaultSigner to getAssociatedAuthority for Market
    • Update dev dependencies

1.0.1-beta.10 (2021-12-11)

  • Change
    • Fix Spl functions amount params
    • Update dev dependencies

1.0.1-beta.9 (2021-12-08)

  • Add
    • makeCreatePoolInstruction and makeInitPoolInstruction for Liquidity
  • Change
    • Rename all getAssociated functions
    • Rename tempLpVault to lpVault
    • Rename getAssociatedTokenAddress to getAssociatedTokenAccount of Spl
    • Return nonce for Liquidity.getAssociatedAuthority
    • Fix Farm ledger PDA
    • Update dev dependencies

1.0.1-beta.8 (2021-12-07)

  • Add
    • getMultipleInfo for Liquidity
  • Change
    • Fix Farm v5 instructions
    • Update dev dependencies

1.0.1-beta.7 (2021-12-05)

  • Add
    • Calculate pendingRewards for Farm.getMultipleInfo
  • Change
    • Fix Farm ledger PDA algorithm
    • Fix Farm layout versions
    • Update dev dependencies

1.0.1-beta.6 (2021-12-02)

  • Change
    • Fix typo

1.0.1-beta.5 (2021-12-02)

  • Add
    • poolKeys2JsonInfo (convert poolKeys to JsonInfo)
  • Change
    • Fix CurrencyAmount
    • Update dev dependencies

1.0.1-beta.4 (2021-11-29)

  • Add
    • getOutputAmount static function for Liquidity
  • Change
    • Rename and change the types of MAINNET_LIQUIDITY_POOLS, TESTNET_LIQUIDITY_POOLS, DEVNET_LIQUIDITY_POOLS
    • Return variable names of Liquidity.getInfo: baseBalance to baseReserve, quoteBalance to quoteReserve

1.0.1-beta.3 (2021-11-28)

  • Add
    • getPools static function for Liquidity (fetch pools on-chain)
  • Change
    • Flat params of Liquidity.getInfo
    • Rename all layouts types
    • Change version types to number in all poolKeys
    • Update peer dependencies
    • Update dev dependencies
  • Remove
    • getInfo static function for Liquidity

1.0.1-beta.1 (2021-11-21)

  • Add
    • getInfo static function for Liquidity (simulate way)
  • Change
    • Update dev dependencies