0.0.208-alpha • Published 6 months ago

test-raydium-sdk-v2 v0.0.208-alpha

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
6 months ago

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

0.0.193-alpha

6 months ago

0.0.192-alpha

6 months ago

0.0.194-alpha

6 months ago

0.0.195-alpha

6 months ago

0.0.196-alpha

6 months ago

0.0.201-alpha

6 months ago

0.0.200-alpha

6 months ago

0.0.206-alpha

6 months ago

0.0.205-alpha

6 months ago

0.0.199-alpha

6 months ago

0.0.207-alpha

6 months ago

0.0.204-alpha

6 months ago

0.0.197-alpha

6 months ago

0.0.202-alpha

6 months ago

0.0.208-alpha

6 months ago

0.0.198-alpha

6 months ago

0.0.203-alpha

6 months ago

0.0.191-alpha

6 months ago

0.0.187-alpha

6 months ago

0.0.188-alpha

6 months ago

0.0.190-alpha

6 months ago

0.0.159-alpha

7 months ago

0.0.163-alpha

7 months ago

0.0.186-alpha

6 months ago

0.0.157-alpha

7 months ago

0.0.171-alpha

7 months ago

0.0.161-alpha

7 months ago

0.0.164-alpha

7 months ago

0.0.184-alpha

6 months ago

0.0.167-alpha

7 months ago

0.0.155-alpha

7 months ago

0.0.183-alpha

6 months ago

0.0.156-alpha

7 months ago

0.0.182-alpha

6 months ago

0.0.162-alpha

7 months ago

0.0.170-alpha

7 months ago

0.0.168-alpha

7 months ago

0.0.152-alpha

7 months ago

0.0.181-alpha

6 months ago

0.0.175-alpha

7 months ago

0.0.153-alpha

7 months ago

0.0.176-alpha

7 months ago

0.0.169-alpha

7 months ago

0.0.177-alpha

7 months ago

0.0.154-alpha

7 months ago

0.0.174-alpha

7 months ago

0.0.172-alpha

7 months ago

0.0.160-alpha

7 months ago

0.0.173-alpha

7 months ago

0.0.166-alpha

7 months ago

0.0.179-alpha

6 months ago

0.0.185-alpha

6 months ago

0.0.165-alpha

7 months ago

0.0.178-alpha

7 months ago

0.0.180-alpha

6 months ago

0.0.158-alpha

7 months ago

0.0.147-alpha

8 months ago

0.0.124-alpha

9 months ago

0.0.119-alpha

9 months ago

0.0.135-alpha

8 months ago

0.0.112-alpha

10 months ago

0.0.140-alpha

8 months ago

0.0.107-alpha

10 months ago

0.0.113-alpha

10 months ago

0.0.123-alpha

9 months ago

0.0.106-alpha

10 months ago

0.0.146-alpha

8 months ago

0.0.136-alpha

8 months ago

0.0.137-alpha

8 months ago

0.0.134-alpha

8 months ago

0.0.129-alpha

9 months ago

0.0.109-alpha

10 months ago

0.0.149-alpha

8 months ago

0.0.117-alpha

10 months ago

0.0.151-alpha

7 months ago

0.0.141-alpha

8 months ago

0.0.131-alpha

9 months ago

0.0.111-alpha

10 months ago

0.0.121-alpha

9 months ago

0.0.144-alpha

8 months ago

0.0.132-alpha

9 months ago

0.0.139-alpha

8 months ago

0.0.115-alpha

10 months ago

0.0.120-alpha

9 months ago

0.0.127-alpha

9 months ago

0.0.143-alpha

8 months ago

0.0.133-alpha

8 months ago

0.0.116-alpha

10 months ago

0.0.126-alpha

9 months ago

0.0.142-alpha

8 months ago

0.0.122-alpha

9 months ago

0.0.125-alpha

9 months ago

0.0.130-alpha

9 months ago

0.0.110-alpha

10 months ago

0.0.150-alpha

7 months ago

0.0.145-alpha

8 months ago

0.0.148-alpha

8 months ago

0.0.138-alpha

8 months ago

0.0.108-alpha

10 months ago

0.0.128-alpha

9 months ago

0.0.118-alpha

9 months ago

0.0.80-alpha

1 year ago

0.0.75-alpha

1 year ago

0.0.58-alpha

1 year ago

0.0.100-alpha

1 year ago

0.0.91-alpha

1 year ago

0.0.63-alpha

1 year ago

0.0.98-alpha

1 year ago

0.0.99-alpha

1 year ago

0.0.59-alpha

1 year ago

0.0.86-alpha

1 year ago

0.0.76-alpha

1 year ago

0.0.52-alpha

1 year ago

0.0.62-alpha

1 year ago

0.0.92-alpha

1 year ago

0.0.90-alpha

1 year ago

0.0.50-alpha

1 year ago

0.0.65-alpha

1 year ago

0.0.85-alpha

1 year ago

0.0.70-alpha

1 year ago

0.0.93-alpha

1 year ago

0.0.88-alpha

1 year ago

0.0.68-alpha

1 year ago

0.0.73-alpha

1 year ago

0.0.53-alpha

1 year ago

0.0.101-alpha

1 year ago

0.0.55-alpha

1 year ago

0.0.60-alpha

1 year ago

0.0.104-alpha

1 year ago

0.0.95-alpha

1 year ago

0.0.78-alpha

1 year ago

0.0.1-beta

1 year ago

0.0.83-alpha

1 year ago

0.0.89-alpha

1 year ago

0.0.49-alpha

1 year ago

0.0.103-alpha

1 year ago

0.0.96-alpha

1 year ago

0.0.56-alpha

1 year ago

0.0.66-alpha

1 year ago

0.0.72-alpha

1 year ago

0.0.82-alpha

1 year ago

0.0.79-alpha

1 year ago

0.0.105-alpha

1 year ago

0.0.102-alpha

1 year ago

0.0.94-alpha

1 year ago

0.0.74-alpha

1 year ago

0.0.81-alpha

1 year ago

0.0.71-alpha

1 year ago

0.0.84-alpha

1 year ago

0.0.61-alpha

1 year ago

0.0.51-alpha

1 year ago

0.0.97-alpha

1 year ago

0.0.77-alpha

1 year ago

0.0.57-alpha

1 year ago

0.0.67-alpha

1 year ago

0.0.87-alpha

1 year ago

0.0.64-alpha

1 year ago

0.0.54-alpha

1 year ago

0.0.4-alpha

1 year ago

0.0.15-alpha

1 year ago

0.0.40-alpha

1 year ago

0.0.35-alpha

1 year ago

0.0.20-alpha

1 year ago

0.0.38-alpha

1 year ago

0.0.18-alpha

1 year ago

0.0.32-alpha

1 year ago

0.0.12-alpha

1 year ago

0.0.47-alpha

1 year ago

0.0.27-alpha

1 year ago

0.0.43-alpha

1 year ago

0.0.23-alpha

1 year ago

0.0.7-alpha

1 year ago

0.0.29-alpha

1 year ago

0.0.19-alpha

1 year ago

0.0.3-alpha

1 year ago

0.0.46-alpha

1 year ago

0.0.42-alpha

1 year ago

0.0.6-alpha

1 year ago

0.0.36-alpha

1 year ago

0.0.26-alpha

1 year ago

0.0.16-alpha

1 year ago

2.0.0-beta.9

2 years ago

0.0.45-alpha

1 year ago

2.0.0-beta.8

2 years ago

0.0.10-alpha

1 year ago

2.0.0-beta.7

2 years ago

0.0.9-alpha

1 year ago

0.0.30-alpha

1 year ago

0.0.48-alpha

1 year ago

0.0.22-alpha

1 year ago

0.0.28-alpha

1 year ago

0.0.37-alpha

1 year ago

0.0.17-alpha

1 year ago

0.0.2-alpha

1 year ago

0.0.13-alpha

1 year ago

0.0.33-alpha

1 year ago

2.0.0-beta.6

2 years ago

2.0.0-beta.5

2 years ago

0.0.25-alpha

1 year ago

2.0.0-beta.4

2 years ago

2.0.0-beta.3

2 years ago

0.0.11-alpha

1 year ago

0.0.14-alpha

1 year ago

0.0.5-alpha

1 year ago

0.0.21-alpha

1 year ago

0.0.31-alpha

1 year ago

0.0.24-alpha

1 year ago

0.0.1-alpha

1 year ago

0.0.34-alpha

1 year ago

0.0.8-alpha

1 year ago

0.0.44-alpha

1 year ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.0

2 years ago