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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Token operations for SPL tokens on Solana.
use crate;
use crateSolanaKiteError;
use cratesend_transaction_from_instructions;
use LiteSVM;
use Keypair;
use Pubkey;
use Signer;
use create_associated_token_account as create_ata_instruction;
use mint_to;
/// Creates a new SPL token mint with the specified mint authority and decimals.
///
/// This function creates a new token mint account with proper rent exemption and
/// initializes it as an SPL token mint. You can optionally specify a custom mint
/// address, or let the function generate a unique one.
///
/// # Arguments
///
/// * `litesvm` - Mutable reference to the LiteSVM instance
/// * `mint_authority` - Keypair that will have authority to mint tokens
/// * `decimals` - Number of decimal places for the token (SPL Token enforces a maximum of 9)
/// * `mint` - Optional custom public key for the mint. If None, a unique address will be generated
///
/// # Returns
///
/// Returns the public key of the newly created mint.
///
/// # Errors
///
/// This function will return an error if the mint creation or initialization fails.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_token_mint, create_wallet};
/// use litesvm::LiteSVM;
/// use solana_pubkey::Pubkey;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let mint_authority = create_wallet(&mut litesvm, 1_000_000_000)?;
///
/// // Create a mint with auto-generated address
/// let mint_pubkey = create_token_mint(&mut litesvm, &mint_authority, 6, None)?;
///
/// // Or create a mint with a specific address
/// let custom_mint = Pubkey::new_unique();
/// let mint_pubkey2 = create_token_mint(&mut litesvm, &mint_authority, 6, Some(custom_mint))?;
/// assert_eq!(mint_pubkey2, custom_mint);
/// # Ok(())
/// # }
/// ```
/// Creates an associated token account for the given owner and mint.
///
/// This function creates an associated token account (ATA) which is a deterministic
/// address derived from the owner and mint addresses. The payer funds the account
/// creation and signs the transaction.
///
/// # Arguments
///
/// * `litesvm` - Mutable reference to the LiteSVM instance
/// * `owner` - Public key of the account that will own the token account
/// * `mint` - Public key of the token mint
/// * `payer` - Keypair that will pay for the account creation and sign the transaction
///
/// # Returns
///
/// Returns the public key of the created associated token account.
///
/// # Errors
///
/// This function will return an error if the account creation fails.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_token_mint, create_associated_token_account, create_wallet};
/// use litesvm::LiteSVM;
/// use solana_keypair::Keypair;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let owner_wallet = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let payer_wallet = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint_authority = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint_pubkey = create_token_mint(&mut litesvm, &mint_authority, 6, None)?;
///
/// let token_account = create_associated_token_account(
/// &mut litesvm,
/// &owner_wallet.pubkey(),
/// &mint_pubkey,
/// &payer_wallet,
/// )?;
/// # Ok(())
/// # }
/// ```
/// Mints tokens to a specified token account.
///
/// # Arguments
///
/// * `litesvm` - Mutable reference to the LiteSVM instance
/// * `mint` - Public key of the token mint
/// * `token_account` - Public key of the destination token account
/// * `amount` - Number of tokens to mint (in base units)
/// * `mint_authority` - Keypair with mint authority
///
/// # Errors
///
/// This function will return an error if the minting transaction fails.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_token_mint, create_associated_token_account, mint_tokens_to_token_account, create_wallet};
/// use litesvm::LiteSVM;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let mint_authority = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let owner = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint = create_token_mint(&mut litesvm, &mint_authority, 6, None)?;
/// let token_account = create_associated_token_account(&mut litesvm, &owner.pubkey(), &mint, &owner)?;
///
/// mint_tokens_to_token_account(
/// &mut litesvm,
/// &mint,
/// &token_account,
/// 1_000_000, // 1 token with 6 decimals
/// &mint_authority,
/// )?;
/// # Ok(())
/// # }
/// ```
/// Gets the token balance of a token account.
///
/// Works for both Classic Token Program and Token Extensions accounts — both
/// share the same base account layout, with the amount at the same byte offset.
///
/// # Arguments
///
/// * `litesvm` - Reference to the LiteSVM instance
/// * `token_account` - Public key of the token account to query
///
/// # Returns
///
/// Returns the token balance as a u64 in base units.
///
/// # Errors
///
/// This function will return an error if the token account doesn't exist or
/// the balance cannot be parsed.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_token_mint, create_associated_token_account,
/// get_token_account_balance, create_wallet};
/// use litesvm::LiteSVM;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let authority = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint = create_token_mint(&mut litesvm, &authority, 6, None)?;
/// let ata = create_associated_token_account(&mut litesvm, &authority.pubkey(), &mint, &authority)?;
/// let balance = get_token_account_balance(&litesvm, &ata)?;
/// assert_eq!(balance, 0);
/// # Ok(())
/// # }
/// ```
/// Derives the associated token account address for a Classic Token Program mint.
///
/// Useful for pre-computing the address before calling
/// [`create_associated_token_account`], e.g. to pass it to a program instruction
/// before the account exists onchain.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_token_mint, create_associated_token_account,
/// get_token_account_address, create_wallet};
/// use litesvm::LiteSVM;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let authority = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint = create_token_mint(&mut litesvm, &authority, 6, None)?;
///
/// let predicted = get_token_account_address(&authority.pubkey(), &mint);
/// let actual = create_associated_token_account(&mut litesvm, &authority.pubkey(), &mint, &authority)?;
/// assert_eq!(predicted, actual);
/// # Ok(())
/// # }
/// ```
/// Asserts that a token account has the expected balance.
///
/// Works for both Classic Token Program and Token Extensions accounts.
/// Convenience wrapper around [`get_token_account_balance`] for test assertions.
///
/// # Panics
///
/// Panics if the actual balance doesn't match the expected balance, with the provided message.
///
/// # Example
///
/// ```rust
/// use solana_kite::{create_wallet, create_token_mint, create_associated_token_account,
/// mint_tokens_to_token_account, assert_token_account_balance};
/// use litesvm::LiteSVM;
/// use solana_signer::Signer;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut litesvm = LiteSVM::new();
/// let authority = create_wallet(&mut litesvm, 1_000_000_000)?;
/// let mint = create_token_mint(&mut litesvm, &authority, 6, None)?;
/// let ata = create_associated_token_account(&mut litesvm, &authority.pubkey(), &mint, &authority)?;
/// mint_tokens_to_token_account(&mut litesvm, &mint, &ata, 1_000_000, &authority)?;
/// assert_token_account_balance(&litesvm, &ata, 1_000_000, "Should have 1 token");
/// # Ok(())
/// # }
/// ```