-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathinstruction.rs
More file actions
630 lines (593 loc) · 23.4 KB
/
instruction.rs
File metadata and controls
630 lines (593 loc) · 23.4 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Instruction types.
use {crate::error::TokenError, pinocchio::program_error::ProgramError};
/// Instructions supported by the token program.
#[repr(u8)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(test, derive(strum_macros::FromRepr, strum_macros::EnumIter))]
pub enum TokenInstruction {
/// Initializes a new mint and optionally deposits all the newly minted
/// tokens in an account.
///
/// The `InitializeMint` instruction requires no signers and MUST be
/// included within the same Transaction as the system program's
/// `CreateAccount` instruction that creates the account being initialized.
/// Otherwise another party can acquire ownership of the uninitialized
/// account.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The mint to initialize.
/// 1. `[]` Rent sysvar.
///
/// Data expected by this instruction:
///
/// - `u8` The number of base 10 digits to the right of the decimal place.
/// - `Pubkey` The authority/multisignature to mint tokens.
/// - `Option<Pubkey>` The freeze authority/multisignature of the mint.
InitializeMint,
/// Initializes a new account to hold tokens. If this account is associated
/// with the native mint then the token balance of the initialized account
/// will be equal to the amount of SOL in the account. If this account is
/// associated with another mint, that mint must be initialized before this
/// command can succeed.
///
/// The [`TokenInstruction::InitializeAccount`] instruction requires no
/// signers and MUST be included within the same Transaction as the
/// system program's `CreateAccount` instruction that creates the
/// account being initialized. Otherwise another party can acquire
/// ownership of the uninitialized account.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The account to initialize.
/// 1. `[]` The mint this account will be associated with.
/// 2. `[]` The new account's owner/multisignature.
/// 3. `[]` Rent sysvar.
InitializeAccount,
/// Initializes a multisignature account with N provided signers.
///
/// Multisignature accounts can used in place of any single owner/delegate
/// accounts in any token instruction that require an owner/delegate to be
/// present. The variant field represents the number of signers (M)
/// required to validate this multisignature account.
///
/// The [`TokenInstruction::InitializeMultisig`] instruction requires no
/// signers and MUST be included within the same Transaction as the
/// system program's `CreateAccount` instruction that creates the
/// account being initialized. Otherwise another party can acquire
/// ownership of the uninitialized account.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The multisignature account to initialize.
/// 1. `[]` Rent sysvar.
/// 2. `..+N` `[signer]` The signer accounts, must equal to N where `1 <=
/// N <= 11`.
///
/// Data expected by this instruction:
///
/// - `u8` The number of signers (M) required to validate this
/// multisignature account.
InitializeMultisig,
/// Transfers tokens from one account to another either directly or via a
/// delegate. If this account is associated with the native mint then equal
/// amounts of SOL and Tokens will be transferred to the destination
/// account.
///
/// Accounts expected by this instruction:
///
/// * Single owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[writable]` The destination account.
/// 2. `[signer]` The source account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[writable]` The destination account.
/// 2. `[]` The source account's multisignature owner/delegate.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens to transfer.
Transfer,
/// Approves a delegate. A delegate is given the authority over tokens on
/// behalf of the source account's owner.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The source account.
/// 1. `[]` The delegate.
/// 2. `[signer]` The source account owner.
///
/// * Multisignature owner
/// 0. `[writable]` The source account.
/// 1. `[]` The delegate.
/// 2. `[]` The source account's multisignature owner.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens the delegate is approved for.
Approve,
/// Revokes the delegate's authority.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The source account.
/// 1. `[signer]` The source account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[]` The source account's multisignature owner/delegate.
/// 2. `..+M` `[signer]` M signer accounts.
Revoke,
/// Sets a new authority of a mint or account.
///
/// Accounts expected by this instruction:
///
/// * Single authority
/// 0. `[writable]` The mint or account to change the authority of.
/// 1. `[signer]` The current authority of the mint or account.
///
/// * Multisignature authority
/// 0. `[writable]` The mint or account to change the authority of.
/// 1. `[]` The mint's or account's current multisignature authority.
/// 2. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `AuthorityType` The type of authority to update.
/// - `Option<Pubkey>` The new authority.
SetAuthority,
/// Mints new tokens to an account. The native mint does not support
/// minting.
///
/// Accounts expected by this instruction:
///
/// * Single authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[signer]` The mint's minting authority.
///
/// * Multisignature authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[]` The mint's multisignature mint-tokens authority.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of new tokens to mint.
MintTo,
/// Burns tokens by removing them from an account. `Burn` does not support
/// accounts associated with the native mint, use `CloseAccount` instead.
///
/// Accounts expected by this instruction:
///
/// * Single owner/delegate
/// 0. `[writable]` The account to burn from.
/// 1. `[writable]` The token mint.
/// 2. `[signer]` The account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The account to burn from.
/// 1. `[writable]` The token mint.
/// 2. `[]` The account's multisignature owner/delegate.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens to burn.
Burn,
/// Close an account by transferring all its SOL to the destination account.
/// Non-native accounts may only be closed if its token amount is zero.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The account to close.
/// 1. `[writable]` The destination account.
/// 2. `[signer]` The account's owner.
///
/// * Multisignature owner
/// 0. `[writable]` The account to close.
/// 1. `[writable]` The destination account.
/// 2. `[]` The account's multisignature owner.
/// 3. `..+M` `[signer]` M signer accounts.
CloseAccount,
/// Freeze an Initialized account using the Mint's `freeze_authority` (if
/// set).
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The account to freeze.
/// 1. `[]` The token mint.
/// 2. `[signer]` The mint freeze authority.
///
/// * Multisignature owner
/// 0. `[writable]` The account to freeze.
/// 1. `[]` The token mint.
/// 2. `[]` The mint's multisignature freeze authority.
/// 3. `..+M` `[signer]` M signer accounts.
FreezeAccount,
/// Thaw a Frozen account using the Mint's `freeze_authority` (if set).
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The account to freeze.
/// 1. `[]` The token mint.
/// 2. `[signer]` The mint freeze authority.
///
/// * Multisignature owner
/// 0. `[writable]` The account to freeze.
/// 1. `[]` The token mint.
/// 2. `[]` The mint's multisignature freeze authority.
/// 3. `..+M` `[signer]` M signer accounts.
ThawAccount,
/// Transfers tokens from one account to another either directly or via a
/// delegate. If this account is associated with the native mint then equal
/// amounts of SOL and Tokens will be transferred to the destination
/// account.
///
/// This instruction differs from Transfer in that the token mint and
/// decimals value is checked by the caller. This may be useful when
/// creating transactions offline or within a hardware wallet.
///
/// Accounts expected by this instruction:
///
/// * Single owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[]` The token mint.
/// 2. `[writable]` The destination account.
/// 3. `[signer]` The source account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[]` The token mint.
/// 2. `[writable]` The destination account.
/// 3. `[]` The source account's multisignature owner/delegate.
/// 4. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens to transfer.
/// - `u8` Expected number of base 10 digits to the right of the decimal
/// place.
TransferChecked,
/// Approves a delegate. A delegate is given the authority over tokens on
/// behalf of the source account's owner.
///
/// This instruction differs from Approve in that the token mint and
/// decimals value is checked by the caller. This may be useful when
/// creating transactions offline or within a hardware wallet.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The source account.
/// 1. `[]` The token mint.
/// 2. `[]` The delegate.
/// 3. `[signer]` The source account owner.
///
/// * Multisignature owner
/// 0. `[writable]` The source account.
/// 1. `[]` The token mint.
/// 2. `[]` The delegate.
/// 3. `[]` The source account's multisignature owner.
/// 4. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens the delegate is approved for.
/// - `u8` Expected number of base 10 digits to the right of the decimal
/// place.
ApproveChecked,
/// Mints new tokens to an account. The native mint does not support
/// minting.
///
/// This instruction differs from [`TokenInstruction::MintTo`] in that the
/// decimals value is checked by the caller. This may be useful when
/// creating transactions offline or within a hardware wallet.
///
/// Accounts expected by this instruction:
///
/// * Single authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[signer]` The mint's minting authority.
///
/// * Multisignature authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[]` The mint's multisignature mint-tokens authority.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of new tokens to mint.
/// - `u8` Expected number of base 10 digits to the right of the decimal
/// place.
MintToChecked,
/// Burns tokens by removing them from an account.
/// [`TokenInstruction::BurnChecked`] does not support accounts
/// associated with the native mint, use `CloseAccount` instead.
///
/// This instruction differs from Burn in that the decimals value is checked
/// by the caller. This may be useful when creating transactions offline or
/// within a hardware wallet.
///
/// Accounts expected by this instruction:
///
/// * Single owner/delegate
/// 0. `[writable]` The account to burn from.
/// 1. `[writable]` The token mint.
/// 2. `[signer]` The account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The account to burn from.
/// 1. `[writable]` The token mint.
/// 2. `[]` The account's multisignature owner/delegate.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens to burn.
/// - `u8` Expected number of base 10 digits to the right of the decimal
/// place.
BurnChecked,
/// Like [`TokenInstruction::InitializeAccount`], but the owner pubkey is
/// passed via instruction data rather than the accounts list. This
/// variant may be preferable when using Cross Program Invocation from
/// an instruction that does not need the owner's `AccountInfo`
/// otherwise.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The account to initialize.
/// 1. `[]` The mint this account will be associated with.
/// 2. `[]` Rent sysvar.
///
/// Data expected by this instruction:
///
/// - `Pubkey` The new account's owner/multisignature.
InitializeAccount2,
/// Given a wrapped / native token account (a token account containing SOL)
/// updates its amount field based on the account's underlying `lamports`.
/// This is useful if a non-wrapped SOL account uses
/// `system_instruction::transfer` to move lamports to a wrapped token
/// account, and needs to have its token `amount` field updated.
///
/// Accounts expected by this instruction:
///
/// * Using runtime Rent sysvar
/// 0. `[writable]` The native token account to sync with its underlying
/// lamports.
///
/// * Using Rent sysvar account
/// 0. `[writable]` The native token account to sync with its underlying
/// lamports.
/// 1. `[]` Rent sysvar.
SyncNative,
/// Like [`TokenInstruction::InitializeAccount2`], but does not require the
/// Rent sysvar to be provided
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The account to initialize.
/// 1. `[]` The mint this account will be associated with.
///
/// Data expected by this instruction:
///
/// - `Pubkey` The new account's owner/multisignature.
InitializeAccount3,
/// Like [`TokenInstruction::InitializeMultisig`], but does not require the
/// Rent sysvar to be provided
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The multisignature account to initialize.
/// 1. `..+N` `[signer]` The signer accounts, must equal to N where `1 <=
/// N <= 11`.
///
/// Data expected by this instruction:
///
/// - `u8` The number of signers (M) required to validate this
/// multisignature account.
InitializeMultisig2,
/// Like [`TokenInstruction::InitializeMint`], but does not require the Rent
/// sysvar to be provided
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The mint to initialize.
///
/// Data expected by this instruction:
///
/// - `u8` The number of base 10 digits to the right of the decimal place.
/// - `Pubkey` The authority/multisignature to mint tokens.
/// - `Option<Pubkey>` The freeze authority/multisignature of the mint.
InitializeMint2,
/// Gets the required size of an account for the given mint as a
/// little-endian `u64`.
///
/// Return data can be fetched using `sol_get_return_data` and deserializing
/// the return data as a little-endian `u64`.
///
/// Accounts expected by this instruction:
///
/// 0. `[]` The mint to calculate for.
GetAccountDataSize,
/// Initialize the Immutable Owner extension for the given token account
///
/// Fails if the account has already been initialized, so must be called
/// before [`TokenInstruction::InitializeAccount`].
///
/// No-ops in this version of the program, but is included for compatibility
/// with the Associated Token Account program.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` The account to initialize.
InitializeImmutableOwner,
/// Convert an Amount of tokens to a `UiAmount` `string`, using the given
/// mint. In this version of the program, the mint can only specify the
/// number of decimals.
///
/// Fails on an invalid mint.
///
/// Return data can be fetched using `sol_get_return_data` and deserialized
/// with `String::from_utf8`.
///
/// Accounts expected by this instruction:
///
/// 0. `[]` The mint to calculate for
///
/// Data expected by this instruction:
///
/// - `u64` The amount of tokens to reformat.
AmountToUiAmount,
/// Convert a `UiAmount` of tokens to a little-endian `u64` raw Amount,
/// using the given mint. In this version of the program, the mint can
/// only specify the number of decimals.
///
/// Return data can be fetched using `sol_get_return_data` and deserializing
/// the return data as a little-endian `u64`.
///
/// Accounts expected by this instruction:
///
/// 0. `[]` The mint to calculate for.
///
/// Data expected by this instruction:
///
/// - `&str` The `ui_amount` of tokens to reformat.
UiAmountToAmount,
/// This instruction is to be used to rescue SOL sent to any `TokenProgram`
/// owned account by sending them to any other account, leaving behind only
/// lamports for rent exemption.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` Source Account owned by the token program
/// 1. `[writable]` Destination account
/// 2. `[signer]` Authority
/// 3. `..+M` `[signer]` M signer accounts.
WithdrawExcessLamports = 38,
/// Transfer lamports from a native SOL account to a destination account.
///
/// This is useful to unwrap lamports from a wrapped SOL account.
///
/// Accounts expected by this instruction:
///
/// * Single owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[writable]` The destination account.
/// 2. `[signer]` The source account's owner/delegate.
///
/// * Multisignature owner/delegate
/// 0. `[writable]` The source account.
/// 1. `[writable]` The destination account.
/// 2. `[]` The source account's multisignature owner/delegate.
/// 3. `..+M` `[signer]` M signer accounts.
///
/// Data expected by this instruction:
///
/// - `Option<u64>` The amount of lamports to transfer. When an amount is
/// not specified, the entire balance of the source account will be
/// transferred.
UnwrapLamports = 45,
/// Executes a batch of instructions. The instructions to be executed are
/// specified in sequence on the instruction data. Each instruction
/// provides:
/// - `u8`: number of accounts
/// - `u8`: instruction data length (includes the discriminator)
/// - `u8`: instruction discriminator
/// - `[u8]`: instruction data
///
/// Accounts follow a similar pattern, where accounts for each instruction
/// are specified in sequence. Therefore, the number of accounts
/// expected by this instruction is variable, i.e., it depends on the
/// instructions provided.
///
/// Both the number of accounts and instruction data length are used to
/// identify the slice of accounts and instruction data for each
/// instruction.
///
/// Note that it is not sound to have a `batch` instruction that contains
/// other `batch` instruction; an error will be raised when this is
/// detected.
Batch = 255,
// Any new variants also need to be added to program-2022 `TokenInstruction`, so that the
// latter remains a superset of this instruction set. New variants also need to be added to
// token/js/src/instructions/types.ts to maintain @solana/spl-token compatibility
}
impl TryFrom<u8> for TokenInstruction {
type Error = ProgramError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
// SAFETY: `value` is guaranteed to be in the range of the enum variants.
0..=24 | 38 | 45 | 255 => {
Ok(unsafe { core::mem::transmute::<u8, TokenInstruction>(value) })
}
_ => Err(TokenError::InvalidInstruction.into()),
}
}
}
/// Specifies the authority type for `SetAuthority` instructions
#[repr(u8)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(test, derive(strum_macros::FromRepr, strum_macros::EnumIter))]
pub enum AuthorityType {
/// Authority to mint new tokens
MintTokens,
/// Authority to freeze any account associated with the Mint
FreezeAccount,
/// Owner of a given token account
AccountOwner,
/// Authority to close a token account
CloseAccount,
}
impl TryFrom<u8> for AuthorityType {
type Error = ProgramError;
#[inline(always)]
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
// SAFETY: `value` is guaranteed to be in the range of the enum variants.
0..=3 => Ok(unsafe { core::mem::transmute::<u8, AuthorityType>(value) }),
_ => Err(TokenError::InvalidInstruction.into()),
}
}
}
#[cfg(test)]
mod tests {
use {
super::{AuthorityType, TokenInstruction},
strum::IntoEnumIterator,
};
#[test]
fn test_token_instruction_from_u8_exhaustive() {
for variant in TokenInstruction::iter() {
let variant_u8 = variant.clone() as u8;
assert_eq!(
TokenInstruction::from_repr(variant_u8),
Some(TokenInstruction::try_from(variant_u8).unwrap())
);
assert_eq!(TokenInstruction::try_from(variant_u8).unwrap(), variant);
}
}
#[test]
fn test_authority_type_from_u8_exhaustive() {
for variant in AuthorityType::iter() {
let variant_u8 = variant.clone() as u8;
assert_eq!(
AuthorityType::from_repr(variant_u8),
Some(AuthorityType::try_from(variant_u8).unwrap())
);
assert_eq!(AuthorityType::try_from(variant_u8).unwrap(), variant);
}
}
}