npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

test-raydium-sdk-v2

v0.0.314-alpha

Published

An SDK for building applications on top of Raydium.

Downloads

2,362

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

  • https://github.com/coral-xyz/anchor/tree/master/ts
  • https://github.com/ethers-io/ethers.js/tree/master/packages/bignumber
  • https://github.com/pancakeswap/pancake-swap-sdk
  • https://github.com/project-serum/serum-ts
  • https://yarnpkg.com/advanced/lifecycle-scripts