|
1 | | -import { getCreateAccountInstruction } from '@solana-program/system'; |
2 | | -import { |
3 | | - Address, |
4 | | - TransactionMessage, |
5 | | - Commitment, |
6 | | - Rpc, |
7 | | - RpcSubscriptions, |
8 | | - SolanaRpcApi, |
9 | | - SolanaRpcSubscriptionsApi, |
10 | | - TransactionMessageWithBlockhashLifetime, |
11 | | - TransactionMessageWithFeePayer, |
12 | | - TransactionPlan, |
13 | | - TransactionPlanResult, |
14 | | - TransactionPlanner, |
15 | | - TransactionSigner, |
16 | | - airdropFactory, |
17 | | - appendTransactionMessageInstructions, |
18 | | - assertIsSendableTransaction, |
19 | | - assertIsTransactionWithBlockhashLifetime, |
20 | | - createSolanaRpc, |
21 | | - createSolanaRpcSubscriptions, |
22 | | - createTransactionMessage, |
23 | | - createTransactionPlanExecutor, |
24 | | - createTransactionPlanner, |
25 | | - generateKeyPairSigner, |
26 | | - getSignatureFromTransaction, |
27 | | - lamports, |
28 | | - pipe, |
29 | | - sendAndConfirmTransactionFactory, |
30 | | - setTransactionMessageFeePayerSigner, |
31 | | - setTransactionMessageLifetimeUsingBlockhash, |
32 | | - signTransactionMessageWithSigners, |
33 | | -} from '@solana/kit'; |
| 1 | +import path from 'node:path'; |
| 2 | + |
| 3 | +import { systemProgram } from '@solana-program/system'; |
| 4 | +import { Address, TransactionSigner, createClient, generateKeyPairSigner, lamports } from '@solana/kit'; |
| 5 | +import { litesvm } from '@solana/kit-plugin-litesvm'; |
| 6 | +import { airdropSigner, generatedSigner } from '@solana/kit-plugin-signer'; |
| 7 | + |
34 | 8 | import { |
35 | 9 | TOKEN_PROGRAM_ADDRESS, |
| 10 | + associatedTokenProgram, |
36 | 11 | findAssociatedTokenPda, |
37 | | - getInitializeAccountInstruction, |
38 | | - getInitializeMintInstruction, |
39 | | - getMintSize, |
40 | | - getMintToATAInstructionPlan, |
41 | | - getMintToInstruction, |
42 | 12 | getTokenSize, |
| 13 | + tokenProgram, |
43 | 14 | } from '../src'; |
44 | 15 |
|
45 | | -type Client = { |
46 | | - rpc: Rpc<SolanaRpcApi>; |
47 | | - rpcSubscriptions: RpcSubscriptions<SolanaRpcSubscriptionsApi>; |
48 | | - sendTransactionPlan: (transactionPlan: TransactionPlan) => Promise<TransactionPlanResult>; |
49 | | -}; |
50 | | - |
51 | | -export const createDefaultSolanaClient = (): Client => { |
52 | | - const rpc = createSolanaRpc('http://127.0.0.1:8899'); |
53 | | - const rpcSubscriptions = createSolanaRpcSubscriptions('ws://127.0.0.1:8900'); |
54 | | - |
55 | | - const sendAndConfirm = sendAndConfirmTransactionFactory({ |
56 | | - rpc, |
57 | | - rpcSubscriptions, |
58 | | - }); |
59 | | - const transactionPlanExecutor = createTransactionPlanExecutor({ |
60 | | - executeTransactionMessage: async (context, transactionMessage) => { |
61 | | - const signedTransaction = await signTransactionMessageWithSigners(transactionMessage); |
62 | | - context.transaction = signedTransaction; |
63 | | - assertIsSendableTransaction(signedTransaction); |
64 | | - assertIsTransactionWithBlockhashLifetime(signedTransaction); |
65 | | - await sendAndConfirm(signedTransaction, { commitment: 'confirmed' }); |
66 | | - return signedTransaction; |
67 | | - }, |
68 | | - }); |
69 | | - |
70 | | - const sendTransactionPlan = async (transactionPlan: TransactionPlan) => { |
71 | | - return transactionPlanExecutor(transactionPlan); |
72 | | - }; |
73 | | - |
74 | | - return { rpc, rpcSubscriptions, sendTransactionPlan }; |
| 16 | +const TOKEN_BINARY_PATH = path.resolve(__dirname, '..', '..', '..', 'target', 'deploy', 'pinocchio_token_program.so'); |
| 17 | + |
| 18 | +export const createTestClient = () => { |
| 19 | + return createClient() |
| 20 | + .use(generatedSigner()) |
| 21 | + .use(litesvm()) |
| 22 | + .use(airdropSigner(lamports(1_000_000_000n))) |
| 23 | + .use(client => { |
| 24 | + // Load the token program into the LiteSVM instance from its compiled |
| 25 | + // `.so` file. This must run after the `litesvm()` plugin so that |
| 26 | + // `client.svm` is available. The system and associated-token |
| 27 | + // programs are LiteSVM builtins and need no loading. |
| 28 | + client.svm.addProgramFromFile(TOKEN_PROGRAM_ADDRESS, TOKEN_BINARY_PATH); |
| 29 | + return client; |
| 30 | + }) |
| 31 | + .use(systemProgram()) |
| 32 | + .use(tokenProgram()) |
| 33 | + .use(associatedTokenProgram()); |
75 | 34 | }; |
76 | 35 |
|
77 | | -export const generateKeyPairSignerWithSol = async (client: Client, putativeLamports: bigint = 1_000_000_000n) => { |
78 | | - const signer = await generateKeyPairSigner(); |
79 | | - await airdropFactory(client)({ |
80 | | - recipientAddress: signer.address, |
81 | | - lamports: lamports(putativeLamports), |
82 | | - commitment: 'confirmed', |
83 | | - }); |
84 | | - return signer; |
85 | | -}; |
| 36 | +export type TestClient = Awaited<ReturnType<typeof createTestClient>>; |
86 | 37 |
|
87 | | -export const createDefaultTransaction = async (client: Client, feePayer: TransactionSigner) => { |
88 | | - const { value: latestBlockhash } = await client.rpc.getLatestBlockhash().send(); |
89 | | - return pipe( |
90 | | - createTransactionMessage({ version: 0 }), |
91 | | - tx => setTransactionMessageFeePayerSigner(feePayer, tx), |
92 | | - tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), |
93 | | - ); |
94 | | -}; |
95 | | - |
96 | | -export const signAndSendTransaction = async ( |
97 | | - client: Client, |
98 | | - transactionMessage: TransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithBlockhashLifetime, |
99 | | - commitment: Commitment = 'confirmed', |
100 | | -) => { |
101 | | - const signedTransaction = await signTransactionMessageWithSigners(transactionMessage); |
102 | | - const signature = getSignatureFromTransaction(signedTransaction); |
103 | | - assertIsSendableTransaction(signedTransaction); |
104 | | - assertIsTransactionWithBlockhashLifetime(signedTransaction); |
105 | | - await sendAndConfirmTransactionFactory(client)(signedTransaction, { |
106 | | - commitment, |
107 | | - }); |
108 | | - return signature; |
109 | | -}; |
110 | | - |
111 | | -export const createDefaultTransactionPlanner = (client: Client, feePayer: TransactionSigner): TransactionPlanner => { |
112 | | - return createTransactionPlanner({ |
113 | | - createTransactionMessage: async () => { |
114 | | - const { value: latestBlockhash } = await client.rpc.getLatestBlockhash().send(); |
115 | | - |
116 | | - return pipe( |
117 | | - createTransactionMessage({ version: 0 }), |
118 | | - tx => setTransactionMessageFeePayerSigner(feePayer, tx), |
119 | | - tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), |
120 | | - ); |
121 | | - }, |
122 | | - }); |
123 | | -}; |
124 | | - |
125 | | -export const getBalance = async (client: Client, address: Address) => |
126 | | - (await client.rpc.getBalance(address, { commitment: 'confirmed' }).send()).value; |
127 | | - |
128 | | -export const createMint = async ( |
129 | | - client: Client, |
130 | | - payer: TransactionSigner, |
131 | | - mintAuthority: Address, |
132 | | - decimals: number = 0, |
133 | | -): Promise<Address> => { |
134 | | - const space = BigInt(getMintSize()); |
135 | | - const [transactionMessage, rent, mint] = await Promise.all([ |
136 | | - createDefaultTransaction(client, payer), |
137 | | - client.rpc.getMinimumBalanceForRentExemption(space).send(), |
138 | | - generateKeyPairSigner(), |
139 | | - ]); |
140 | | - const instructions = [ |
141 | | - getCreateAccountInstruction({ |
142 | | - payer, |
143 | | - newAccount: mint, |
144 | | - lamports: rent, |
145 | | - space, |
146 | | - programAddress: TOKEN_PROGRAM_ADDRESS, |
147 | | - }), |
148 | | - getInitializeMintInstruction({ |
149 | | - mint: mint.address, |
150 | | - decimals, |
151 | | - mintAuthority, |
152 | | - }), |
153 | | - ]; |
154 | | - await pipe( |
155 | | - transactionMessage, |
156 | | - tx => appendTransactionMessageInstructions(instructions, tx), |
157 | | - tx => signAndSendTransaction(client, tx), |
158 | | - ); |
159 | | - |
160 | | - return mint.address; |
161 | | -}; |
162 | | - |
163 | | -export const createToken = async ( |
164 | | - client: Client, |
165 | | - payer: TransactionSigner, |
166 | | - mint: Address, |
167 | | - owner: Address, |
168 | | -): Promise<Address> => { |
| 38 | +export const createToken = async (client: TestClient, mint: Address, owner: Address): Promise<Address> => { |
169 | 39 | const space = BigInt(getTokenSize()); |
170 | | - const [transactionMessage, rent, token] = await Promise.all([ |
171 | | - createDefaultTransaction(client, payer), |
| 40 | + const [rent, token] = await Promise.all([ |
172 | 41 | client.rpc.getMinimumBalanceForRentExemption(space).send(), |
173 | 42 | generateKeyPairSigner(), |
174 | 43 | ]); |
175 | | - const instructions = [ |
176 | | - getCreateAccountInstruction({ |
177 | | - payer, |
| 44 | + await client.sendTransaction([ |
| 45 | + client.system.instructions.createAccount({ |
178 | 46 | newAccount: token, |
179 | 47 | lamports: rent, |
180 | 48 | space, |
181 | 49 | programAddress: TOKEN_PROGRAM_ADDRESS, |
182 | 50 | }), |
183 | | - getInitializeAccountInstruction({ account: token.address, mint, owner }), |
184 | | - ]; |
185 | | - await pipe( |
186 | | - transactionMessage, |
187 | | - tx => appendTransactionMessageInstructions(instructions, tx), |
188 | | - tx => signAndSendTransaction(client, tx), |
189 | | - ); |
| 51 | + client.token.instructions.initializeAccount({ account: token.address, mint, owner }), |
| 52 | + ]); |
190 | 53 |
|
191 | 54 | return token.address; |
192 | 55 | }; |
193 | 56 |
|
194 | 57 | export const createTokenWithAmount = async ( |
195 | | - client: Client, |
196 | | - payer: TransactionSigner, |
| 58 | + client: TestClient, |
197 | 59 | mintAuthority: TransactionSigner, |
198 | 60 | mint: Address, |
199 | 61 | owner: Address, |
200 | 62 | amount: bigint, |
201 | 63 | ): Promise<Address> => { |
202 | 64 | const space = BigInt(getTokenSize()); |
203 | | - const [transactionMessage, rent, token] = await Promise.all([ |
204 | | - createDefaultTransaction(client, payer), |
| 65 | + const [rent, token] = await Promise.all([ |
205 | 66 | client.rpc.getMinimumBalanceForRentExemption(space).send(), |
206 | 67 | generateKeyPairSigner(), |
207 | 68 | ]); |
208 | | - const instructions = [ |
209 | | - getCreateAccountInstruction({ |
210 | | - payer, |
| 69 | + await client.sendTransaction([ |
| 70 | + client.system.instructions.createAccount({ |
211 | 71 | newAccount: token, |
212 | 72 | lamports: rent, |
213 | 73 | space, |
214 | 74 | programAddress: TOKEN_PROGRAM_ADDRESS, |
215 | 75 | }), |
216 | | - getInitializeAccountInstruction({ account: token.address, mint, owner }), |
217 | | - getMintToInstruction({ mint, token: token.address, mintAuthority, amount }), |
218 | | - ]; |
219 | | - await pipe( |
220 | | - transactionMessage, |
221 | | - tx => appendTransactionMessageInstructions(instructions, tx), |
222 | | - tx => signAndSendTransaction(client, tx), |
223 | | - ); |
| 76 | + client.token.instructions.initializeAccount({ account: token.address, mint, owner }), |
| 77 | + client.token.instructions.mintTo({ mint, token: token.address, mintAuthority, amount }), |
| 78 | + ]); |
224 | 79 |
|
225 | 80 | return token.address; |
226 | 81 | }; |
227 | 82 |
|
228 | 83 | export const createTokenPdaWithAmount = async ( |
229 | | - client: Client, |
230 | | - payer: TransactionSigner, |
| 84 | + client: TestClient, |
231 | 85 | mintAuthority: TransactionSigner, |
232 | 86 | mint: Address, |
233 | 87 | owner: Address, |
234 | 88 | amount: bigint, |
235 | 89 | decimals: number, |
236 | 90 | ): Promise<Address> => { |
237 | | - const [token] = await findAssociatedTokenPda({ |
238 | | - owner, |
239 | | - mint, |
240 | | - tokenProgram: TOKEN_PROGRAM_ADDRESS, |
241 | | - }); |
242 | | - |
243 | | - const transactionPlan = await createDefaultTransactionPlanner( |
244 | | - client, |
245 | | - payer, |
246 | | - )( |
247 | | - getMintToATAInstructionPlan({ |
248 | | - payer, |
249 | | - ata: token, |
250 | | - owner, |
251 | | - mint, |
252 | | - mintAuthority, |
253 | | - amount, |
254 | | - decimals, |
255 | | - }), |
256 | | - ); |
257 | | - |
258 | | - await client.sendTransactionPlan(transactionPlan); |
| 91 | + await client.token.instructions.mintToATA({ owner, mint, mintAuthority, amount, decimals }).sendTransaction(); |
| 92 | + const [token] = await findAssociatedTokenPda({ owner, mint, tokenProgram: TOKEN_PROGRAM_ADDRESS }); |
259 | 93 | return token; |
260 | 94 | }; |
0 commit comments