0
# SPL Token Operations
1
2
Comprehensive SPL Token program interface for token creation, account management, transfers, and advanced token operations. The library supports both the standard SPL Token program and the newer Token-2022 program with enhanced features.
3
4
## Capabilities
5
6
### Token Client Initialization
7
8
Create token instances for interacting with existing tokens or initialize new tokens with mint authorities and program configurations.
9
10
```python { .api }
11
class Token:
12
def __init__(self, conn: Client, pubkey: Pubkey, program_id: Pubkey, payer: Keypair):
13
"""
14
Initialize token client for existing token.
15
16
Parameters:
17
- conn: RPC client connection
18
- pubkey: Token mint public key
19
- program_id: Token program ID (TOKEN_PROGRAM_ID or TOKEN_2022_PROGRAM_ID)
20
- payer: Keypair for transaction fees
21
"""
22
23
class AsyncToken:
24
def __init__(self, conn: AsyncClient, pubkey: Pubkey, program_id: Pubkey, payer: Keypair):
25
"""
26
Initialize async token client for existing token.
27
28
Parameters:
29
- conn: Async RPC client connection
30
- pubkey: Token mint public key
31
- program_id: Token program ID
32
- payer: Keypair for transaction fees
33
"""
34
```
35
36
### Token Creation
37
38
Create new token mints with configurable decimal precision, mint authority, and optional freeze authority.
39
40
```python { .api }
41
@staticmethod
42
def create_mint(conn: Client, payer: Keypair, mint_authority: Pubkey, decimals: int, program_id: Pubkey, freeze_authority: Optional[Pubkey] = None, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> "Token":
43
"""
44
Create a new token mint.
45
46
Parameters:
47
- conn: RPC client connection
48
- payer: Keypair to pay for mint creation and transaction fees
49
- mint_authority: Public key with authority to mint tokens
50
- decimals: Number of decimal places for the token (0-9)
51
- program_id: Token program ID
52
- freeze_authority: Optional authority to freeze token accounts
53
- skip_confirmation: Skip waiting for transaction confirmation
54
- recent_blockhash: Recent blockhash for transaction
55
56
Returns:
57
Token instance for the newly created mint
58
"""
59
60
@staticmethod
61
async def create_mint(conn: AsyncClient, payer: Keypair, mint_authority: Pubkey, decimals: int, program_id: Pubkey, freeze_authority: Optional[Pubkey] = None, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> "AsyncToken":
62
"""Async version of create_mint."""
63
```
64
65
### Account Management
66
67
Create and manage token accounts including standard accounts, associated token accounts, wrapped SOL accounts, and multisig accounts.
68
69
```python { .api }
70
def create_account(self, owner: Pubkey, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
71
"""
72
Create a new token account.
73
74
Parameters:
75
- owner: Public key that will own the token account
76
- skip_confirmation: Skip waiting for transaction confirmation
77
- recent_blockhash: Recent blockhash for transaction
78
79
Returns:
80
Public key of the created token account
81
"""
82
83
def create_associated_token_account(self, owner: Pubkey, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
84
"""
85
Create an associated token account (ATA).
86
87
Parameters:
88
- owner: Public key that will own the ATA
89
- skip_confirmation: Skip waiting for transaction confirmation
90
- recent_blockhash: Recent blockhash for transaction
91
92
Returns:
93
Public key of the created associated token account
94
"""
95
96
@staticmethod
97
def create_wrapped_native_account(conn: Client, program_id: Pubkey, owner: Pubkey, payer: Keypair, amount: int, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
98
"""
99
Create a wrapped SOL account.
100
101
Parameters:
102
- conn: RPC client connection
103
- program_id: Token program ID
104
- owner: Owner of the wrapped SOL account
105
- payer: Keypair to pay for account creation and fund the account
106
- amount: Amount of SOL to wrap (in lamports)
107
- skip_confirmation: Skip waiting for transaction confirmation
108
- recent_blockhash: Recent blockhash for transaction
109
110
Returns:
111
Public key of the created wrapped SOL account
112
"""
113
114
def create_multisig(self, m: int, multi_signers: List[Pubkey], opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
115
"""
116
Create a multisig account.
117
118
Parameters:
119
- m: Number of signatures required (1 <= m <= signers)
120
- multi_signers: List of signer public keys (max 11)
121
- opts: Transaction options
122
- recent_blockhash: Recent blockhash for transaction
123
124
Returns:
125
Public key of the created multisig account
126
"""
127
```
128
129
### Account Queries
130
131
Retrieve token account information, balances, mint details, and query accounts by ownership or delegation.
132
133
```python { .api }
134
def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetTokenAccountBalanceResp:
135
"""
136
Get token account balance.
137
138
Parameters:
139
- pubkey: Token account public key
140
- commitment: Commitment level for the query
141
142
Returns:
143
GetTokenAccountBalanceResp with balance information
144
"""
145
146
def get_account_info(self, account: Pubkey, commitment: Optional[Commitment] = None) -> AccountInfo:
147
"""
148
Get detailed token account information.
149
150
Parameters:
151
- account: Token account public key
152
- commitment: Commitment level for the query
153
154
Returns:
155
AccountInfo with account details (mint, owner, amount, delegate, etc.)
156
"""
157
158
def get_mint_info(self) -> MintInfo:
159
"""
160
Get mint information for this token.
161
162
Returns:
163
MintInfo with mint details (authority, supply, decimals, freeze_authority)
164
"""
165
166
def get_accounts_by_owner(self, owner: Pubkey, commitment: Optional[Commitment] = None, encoding: str = "base64") -> GetTokenAccountsByOwnerResp:
167
"""
168
Get all token accounts owned by a specific address.
169
170
Parameters:
171
- owner: Owner public key
172
- commitment: Commitment level for the query
173
- encoding: Account data encoding
174
175
Returns:
176
GetTokenAccountsByOwnerResp with array of token accounts
177
"""
178
179
def get_accounts_by_delegate(self, owner: Pubkey, commitment: Optional[Commitment] = None, encoding: str = "base64") -> GetTokenAccountsByDelegateResp:
180
"""
181
Get all token accounts delegated to a specific address.
182
183
Parameters:
184
- owner: Delegate public key
185
- commitment: Commitment level for the query
186
- encoding: Account data encoding
187
188
Returns:
189
GetTokenAccountsByDelegateResp with array of delegated accounts
190
"""
191
```
192
193
### Token Operations
194
195
Transfer tokens, mint new tokens, burn tokens, and manage token approvals with comprehensive token transaction capabilities.
196
197
```python { .api }
198
def transfer(self, source: Pubkey, dest: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
199
"""
200
Transfer tokens between accounts.
201
202
Parameters:
203
- source: Source token account public key
204
- dest: Destination token account public key
205
- owner: Owner keypair or multisig account
206
- amount: Amount to transfer (in token's smallest unit)
207
- multi_signers: Additional signers for multisig accounts
208
- opts: Transaction options
209
- recent_blockhash: Recent blockhash for transaction
210
211
Returns:
212
SendTransactionResp with transaction signature
213
"""
214
215
def mint_to(self, dest: Pubkey, mint_authority: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
216
"""
217
Mint tokens to an account.
218
219
Parameters:
220
- dest: Destination token account public key
221
- mint_authority: Mint authority keypair
222
- amount: Amount to mint (in token's smallest unit)
223
- multi_signers: Additional signers for multisig mint authority
224
- opts: Transaction options
225
- recent_blockhash: Recent blockhash for transaction
226
227
Returns:
228
SendTransactionResp with transaction signature
229
"""
230
231
def burn(self, account: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
232
"""
233
Burn tokens from an account.
234
235
Parameters:
236
- account: Token account to burn from
237
- owner: Account owner keypair
238
- amount: Amount to burn (in token's smallest unit)
239
- multi_signers: Additional signers for multisig accounts
240
- opts: Transaction options
241
- recent_blockhash: Recent blockhash for transaction
242
243
Returns:
244
SendTransactionResp with transaction signature
245
"""
246
247
def approve(self, source: Pubkey, delegate: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
248
"""
249
Approve a delegate to transfer tokens.
250
251
Parameters:
252
- source: Source token account public key
253
- delegate: Delegate public key
254
- owner: Account owner keypair
255
- amount: Amount to approve for delegation
256
- multi_signers: Additional signers for multisig accounts
257
- opts: Transaction options
258
- recent_blockhash: Recent blockhash for transaction
259
260
Returns:
261
SendTransactionResp with transaction signature
262
"""
263
264
def revoke(self, account: Pubkey, owner: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
265
"""
266
Revoke a delegate's approval.
267
268
Parameters:
269
- account: Token account public key
270
- owner: Account owner keypair
271
- multi_signers: Additional signers for multisig accounts
272
- opts: Transaction options
273
- recent_blockhash: Recent blockhash for transaction
274
275
Returns:
276
SendTransactionResp with transaction signature
277
"""
278
```
279
280
### Enhanced Token Operations
281
282
Token operations with additional validation including decimals checking for improved safety and error prevention.
283
284
```python { .api }
285
def transfer_checked(self, source: Pubkey, dest: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
286
"""
287
Transfer tokens with decimals validation.
288
289
Parameters:
290
- source: Source token account public key
291
- dest: Destination token account public key
292
- owner: Owner keypair
293
- amount: Amount to transfer
294
- decimals: Expected decimals for validation
295
- multi_signers: Additional signers for multisig accounts
296
- opts: Transaction options
297
- recent_blockhash: Recent blockhash for transaction
298
299
Returns:
300
SendTransactionResp with transaction signature
301
"""
302
303
def mint_to_checked(self, dest: Pubkey, mint_authority: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
304
"""
305
Mint tokens with decimals validation.
306
307
Parameters:
308
- dest: Destination token account public key
309
- mint_authority: Mint authority keypair
310
- amount: Amount to mint
311
- decimals: Expected decimals for validation
312
- multi_signers: Additional signers for multisig accounts
313
- opts: Transaction options
314
- recent_blockhash: Recent blockhash for transaction
315
316
Returns:
317
SendTransactionResp with transaction signature
318
"""
319
320
def burn_checked(self, account: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
321
"""
322
Burn tokens with decimals validation.
323
324
Parameters:
325
- account: Token account to burn from
326
- owner: Account owner keypair
327
- amount: Amount to burn
328
- decimals: Expected decimals for validation
329
- multi_signers: Additional signers for multisig accounts
330
- opts: Transaction options
331
- recent_blockhash: Recent blockhash for transaction
332
333
Returns:
334
SendTransactionResp with transaction signature
335
"""
336
337
def approve_checked(self, source: Pubkey, delegate: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
338
"""
339
Approve delegate with decimals validation.
340
341
Parameters:
342
- source: Source token account public key
343
- delegate: Delegate public key
344
- owner: Account owner keypair
345
- amount: Amount to approve
346
- decimals: Expected decimals for validation
347
- multi_signers: Additional signers for multisig accounts
348
- opts: Transaction options
349
- recent_blockhash: Recent blockhash for transaction
350
351
Returns:
352
SendTransactionResp with transaction signature
353
"""
354
```
355
356
### Authority Management
357
358
Manage token account authorities, freeze/unfreeze accounts, close accounts, and transfer ownership with comprehensive authority control.
359
360
```python { .api }
361
def set_authority(self, account: Pubkey, current_authority: Keypair, authority_type: AuthorityType, new_authority: Optional[Pubkey] = None, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
362
"""
363
Change or remove authority for an account or mint.
364
365
Parameters:
366
- account: Account or mint public key
367
- current_authority: Current authority keypair
368
- authority_type: Type of authority to change (MINT_TOKENS, FREEZE_ACCOUNT, ACCOUNT_OWNER, CLOSE_ACCOUNT)
369
- new_authority: New authority public key (None to remove authority)
370
- multi_signers: Additional signers for multisig authorities
371
- opts: Transaction options
372
- recent_blockhash: Recent blockhash for transaction
373
374
Returns:
375
SendTransactionResp with transaction signature
376
"""
377
378
def freeze_account(self, account: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
379
"""
380
Freeze a token account.
381
382
Parameters:
383
- account: Token account to freeze
384
- authority: Freeze authority keypair
385
- multi_signers: Additional signers for multisig authorities
386
- opts: Transaction options
387
- recent_blockhash: Recent blockhash for transaction
388
389
Returns:
390
SendTransactionResp with transaction signature
391
"""
392
393
def thaw_account(self, account: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
394
"""
395
Unfreeze a token account.
396
397
Parameters:
398
- account: Token account to unfreeze
399
- authority: Freeze authority keypair
400
- multi_signers: Additional signers for multisig authorities
401
- opts: Transaction options
402
- recent_blockhash: Recent blockhash for transaction
403
404
Returns:
405
SendTransactionResp with transaction signature
406
"""
407
408
def close_account(self, account: Pubkey, dest: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
409
"""
410
Close a token account and transfer remaining SOL.
411
412
Parameters:
413
- account: Token account to close
414
- dest: Destination for remaining SOL
415
- authority: Close authority keypair
416
- multi_signers: Additional signers for multisig authorities
417
- opts: Transaction options
418
- recent_blockhash: Recent blockhash for transaction
419
420
Returns:
421
SendTransactionResp with transaction signature
422
"""
423
```
424
425
### Utility Functions
426
427
Helper functions for calculating rent exemption amounts and other token-related utilities.
428
429
```python { .api }
430
@staticmethod
431
def get_min_balance_rent_for_exempt_for_account(conn: Client) -> int:
432
"""
433
Get minimum balance for rent exemption for token accounts.
434
435
Parameters:
436
- conn: RPC client connection
437
438
Returns:
439
Minimum balance in lamports for token account rent exemption
440
"""
441
442
@staticmethod
443
def get_min_balance_rent_for_exempt_for_mint(conn: Client) -> int:
444
"""
445
Get minimum balance for rent exemption for mint accounts.
446
447
Parameters:
448
- conn: RPC client connection
449
450
Returns:
451
Minimum balance in lamports for mint account rent exemption
452
"""
453
454
@staticmethod
455
def get_min_balance_rent_for_exempt_for_multisig(conn: Client) -> int:
456
"""
457
Get minimum balance for rent exemption for multisig accounts.
458
459
Parameters:
460
- conn: RPC client connection
461
462
Returns:
463
Minimum balance in lamports for multisig account rent exemption
464
"""
465
```
466
467
## SPL Memo Operations
468
469
Simple memo functionality for attaching messages to transactions.
470
471
```python { .api }
472
def create_memo(params: MemoParams) -> Instruction:
473
"""
474
Create a memo instruction.
475
476
Parameters:
477
- params: MemoParams with program_id, signer, and message
478
479
Returns:
480
Instruction that can be added to transactions
481
"""
482
483
def decode_create_memo(instruction: Instruction) -> MemoParams:
484
"""
485
Decode a memo instruction.
486
487
Parameters:
488
- instruction: Memo instruction to decode
489
490
Returns:
491
MemoParams with decoded instruction parameters
492
"""
493
```
494
495
## Types
496
497
```python { .api }
498
class AccountInfo(NamedTuple):
499
mint: Pubkey
500
owner: Pubkey
501
amount: int
502
delegate: Optional[Pubkey]
503
delegated_amount: int
504
is_initialized: bool
505
is_frozen: bool
506
is_native: Optional[int]
507
rent_exempt_reserve: Optional[int]
508
close_authority: Optional[Pubkey]
509
510
class MintInfo(NamedTuple):
511
mint_authority: Optional[Pubkey]
512
supply: int
513
decimals: int
514
is_initialized: bool
515
freeze_authority: Optional[Pubkey]
516
517
class AuthorityType(IntEnum):
518
MINT_TOKENS = 0
519
FREEZE_ACCOUNT = 1
520
ACCOUNT_OWNER = 2
521
CLOSE_ACCOUNT = 3
522
523
class MemoParams(NamedTuple):
524
program_id: Pubkey # Memo program account
525
signer: Pubkey # Signing account
526
message: bytes # Memo message in bytes
527
528
class GetTokenAccountBalanceResp:
529
value: TokenAccountBalance
530
531
class TokenAccountBalance:
532
amount: str
533
decimals: int
534
ui_amount: Optional[float]
535
ui_amount_string: str
536
537
# Constants
538
TOKEN_PROGRAM_ID: Pubkey
539
TOKEN_2022_PROGRAM_ID: Pubkey
540
ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey
541
WRAPPED_SOL_MINT: Pubkey
542
NATIVE_DECIMALS: int = 9
543
MINT_LEN: int = 82
544
ACCOUNT_LEN: int = 165
545
MULTISIG_LEN: int = 355
546
MEMO_PROGRAM_ID: Pubkey
547
```