-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathconfig.ts.template
63 lines (56 loc) · 2.56 KB
/
config.ts.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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'
import bs58 from 'bs58'
export const owner: Keypair = Keypair.fromSecretKey(bs58.decode('<YOUR_WALLET_SECRET_KEY>'))
export const connection = new Connection('<YOUR_RPC_URL>') //<YOUR_RPC_URL>
// export const connection = new Connection(clusterApiUrl('devnet')) //<YOUR_RPC_URL>
export const txVersion = TxVersion.V0 // or TxVersion.LEGACY
const cluster = 'mainnet' // 'mainnet' | 'devnet'
let raydium: Raydium | undefined
export const initSdk = async (params?: { loadToken?: boolean }) => {
if (raydium) return raydium
if (connection.rpcEndpoint === clusterApiUrl('mainnet-beta'))
console.warn('using free rpc node might cause unexpected error, strongly suggest uses paid rpc node')
console.log(`connect to rpc ${connection.rpcEndpoint} in ${cluster}`)
raydium = await Raydium.load({
owner,
connection,
cluster,
disableFeatureCheck: true,
disableLoadToken: !params?.loadToken,
blockhashCommitment: 'finalized',
// urlConfigs: {
// BASE_HOST: '<API_HOST>', // api url configs, currently api doesn't support devnet
// },
})
/**
* By default: sdk will automatically fetch token account data when need it or any sol balace changed.
* if you want to handle token account by yourself, set token account data after init sdk
* code below shows how to do it.
* note: after call raydium.account.updateTokenAccount, raydium will not automatically fetch token account
*/
/*
raydium.account.updateTokenAccount(await fetchTokenAccountData())
connection.onAccountChange(owner.publicKey, async () => {
raydium!.account.updateTokenAccount(await fetchTokenAccountData())
})
*/
return raydium
}
export const fetchTokenAccountData = async () => {
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
}
export const grpcUrl = '<YOUR_GRPC_URL>'
export const grpcToken = '<YOUR_GRPC_TOKEN>'