Created
November 26, 2024 15:04
-
-
Save 99darwin/7f65eb7364d637d91f59f0b2a4141ba4 to your computer and use it in GitHub Desktop.
Initialize Raydium SDK with configs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Raydium, TxVersion, parseTokenAccountResp } from '@raydium-io/raydium-sdk-v2' | |
| import { Connection, Keypair, clusterApiUrl } from '@solana/web3.js' | |
| import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token' | |
| const RPC_ENDPOINT = process.env.RPC_ENDPOINT | |
| /** | |
| * Get an RPC endpoint from Helius or similar | |
| * Alternatively use a free endpoint (less reliable) | |
| */ | |
| export const connection = new Connection(RPC_ENDPOINT); | |
| export const txVersion = TxVersion.V0; | |
| let raydium: Raydium | undefined; | |
| export const initSdk = async (owner: Keypair, params?: { loadToken?: boolean },) => { | |
| logger.info(`Connection acquired`); | |
| if (raydium) return raydium; | |
| raydium = await Raydium.load({ | |
| owner, | |
| connection, | |
| cluster: 'mainnet', | |
| disableFeatureCheck: true, | |
| disableLoadToken: !params?.loadToken, | |
| }); | |
| raydium.account.updateTokenAccount(await fetchTokenAccountData(owner)); | |
| connection.onAccountChange(owner.publicKey, async () => { | |
| raydium!.account.updateTokenAccount(await fetchTokenAccountData(owner)); | |
| }); | |
| return raydium; | |
| }; | |
| export const fetchTokenAccountData = async (owner: Keypair) => { | |
| const solAccountResp = await connection.getAccountInfo(owner.publicKey); | |
| const tokenAccountResp = await connection.getTokenAccountsByOwner(owner.publicKey, { programId: TOKEN_PROGRAM_ID }); | |
| const token2022Req = await connection.getTokenAccountsByOwner(owner.publicKey, { programId: TOKEN_2022_PROGRAM_ID }); | |
| const tokenAccountData = parseTokenAccountResp({ | |
| owner: owner.publicKey, | |
| solAccountResp, | |
| tokenAccountResp: { | |
| context: tokenAccountResp.context, | |
| value: [...tokenAccountResp.value, ...token2022Req.value], | |
| }, | |
| }); | |
| return tokenAccountData; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment