forked from raydium-io/raydium-sdk-V1-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswapOnlyCLMM.ts
108 lines (99 loc) · 3.26 KB
/
swapOnlyCLMM.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
ApiClmmPoolsItem,
Clmm,
fetchMultipleMintInfos,
Percent,
Token,
TokenAmount,
} from '@raydium-io/raydium-sdk';
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
import {
Keypair,
PublicKey,
} from '@solana/web3.js';
import {
connection,
DEFAULT_TOKEN,
makeTxVersion,
wallet
} from '../config';
import { formatClmmKeysById } from './formatClmmKeysById';
import {
buildAndSendTx,
getWalletTokenAccount,
} from './util';
type WalletTokenAccounts = Awaited<ReturnType<typeof getWalletTokenAccount>>
type TestTxInputInfo = {
outputToken: Token
targetPool: string
inputTokenAmount: TokenAmount
slippage: Percent
walletTokenAccounts: WalletTokenAccounts
wallet: Keypair
}
async function swapOnlyCLMM(input: TestTxInputInfo) {
// -------- pre-action: fetch Clmm pools info --------
const clmmPools: ApiClmmPoolsItem[] = [await formatClmmKeysById(input.targetPool)]
const { [input.targetPool]: clmmPoolInfo } = await Clmm.fetchMultiplePoolInfos({
connection,
poolKeys: clmmPools,
chainTime: new Date().getTime() / 1000,
})
// -------- step 1: fetch tick array --------
const tickCache = await Clmm.fetchMultiplePoolTickArrays({
connection,
poolKeys: [clmmPoolInfo.state],
batchRequest: true,
})
// -------- step 2: calc amount out by SDK function --------
// Configure input/output parameters, in this example, this token amount will swap 0.0001 USDC to RAY
const { minAmountOut, remainingAccounts } = Clmm.computeAmountOutFormat({
poolInfo: clmmPoolInfo.state,
tickArrayCache: tickCache[input.targetPool],
amountIn: input.inputTokenAmount,
currencyOut: input.outputToken,
slippage: input.slippage,
epochInfo: await connection.getEpochInfo(),
token2022Infos: await fetchMultipleMintInfos({
connection, mints: [
...clmmPools.map(i => [{ mint: i.mintA, program: i.mintProgramIdA }, { mint: i.mintB, program: i.mintProgramIdB }]).flat().filter(i => i.program === TOKEN_2022_PROGRAM_ID.toString()).map(i => new PublicKey(i.mint)),
]
}),
catchLiquidityInsufficient: false,
})
// -------- step 3: create instructions by SDK function --------
const { innerTransactions } = await Clmm.makeSwapBaseInInstructionSimple({
connection,
poolInfo: clmmPoolInfo.state,
ownerInfo: {
feePayer: input.wallet.publicKey,
wallet: input.wallet.publicKey,
tokenAccounts: input.walletTokenAccounts,
},
inputMint: input.inputTokenAmount.token.mint,
amountIn: input.inputTokenAmount.raw,
amountOutMin: minAmountOut.amount.raw,
remainingAccounts,
makeTxVersion,
})
return { txids: await buildAndSendTx(innerTransactions) }
}
async function howToUse() {
const inputToken = DEFAULT_TOKEN.USDC // USDC
const outputToken = DEFAULT_TOKEN.RAY // RAY
const targetPool = '61R1ndXxvsWXXkWSyNkCxnzwd3zUNB8Q2ibmkiLPC8ht' // USDC-RAY pool
const inputTokenAmount = new TokenAmount(inputToken, 100)
const slippage = new Percent(1, 100)
const walletTokenAccounts = await getWalletTokenAccount(connection, wallet.publicKey)
swapOnlyCLMM({
outputToken,
targetPool,
inputTokenAmount,
slippage,
walletTokenAccounts,
wallet: wallet,
}).then(({ txids }) => {
/** continue with txids */
console.log('txids', txids)
})
}