@@ -42,7 +42,10 @@ pub enum TokenInstruction {
42
42
/// 2. `[]` (optional) The owner/multisignature of the mint if supply is non-zero, if
43
43
/// present then further minting is supported.
44
44
///
45
- InitializeMint ( TokenInfo ) ,
45
+ InitializeMint {
46
+ /// The financial specifics of the token.
47
+ info : TokenInfo ,
48
+ } ,
46
49
/// Initializes a new account to hold tokens.
47
50
///
48
51
/// The `InitializeAccount` instruction requires no signers and MUST be included within
@@ -69,7 +72,10 @@ pub enum TokenInstruction {
69
72
///
70
73
/// 0. `[writable]` The multisignature account to initialize.
71
74
/// 1. ..1+N. `[]` The signer accounts, must equal to N where 1 <= N <= 11.
72
- InitializeMultisig ( u8 ) ,
75
+ InitializeMultisig {
76
+ /// The number of signers (M) required to validate this multisignature account.
77
+ m : u8 ,
78
+ } ,
73
79
/// Transfers tokens from one account to another either directly or via a delegate.
74
80
///
75
81
/// Accounts expected by this instruction:
@@ -84,7 +90,10 @@ pub enum TokenInstruction {
84
90
/// 1. `[writable]` The destination account.
85
91
/// 2. '[]' The source account's multisignature owner/delegate.
86
92
/// 3. ..3+M '[signer]' M signer accounts.
87
- Transfer ( u64 ) ,
93
+ Transfer {
94
+ /// The amount of tokens to transfer.
95
+ amount : u64 ,
96
+ } ,
88
97
/// Approves a delegate. A delegate is given the authority over
89
98
/// tokens on behalf of the source account's owner.
90
99
@@ -100,7 +109,10 @@ pub enum TokenInstruction {
100
109
/// 1. `[]` The delegate.
101
110
/// 2. '[]' The source account's multisignature owner.
102
111
/// 3. ..3+M '[signer]' M signer accounts
103
- Approve ( u64 ) ,
112
+ Approve {
113
+ /// The amount of tokens the delegate is approved for.
114
+ amount : u64 ,
115
+ } ,
104
116
/// Revokes the delegate's authority.
105
117
///
106
118
/// Accounts expected by this instruction:
@@ -143,22 +155,26 @@ pub enum TokenInstruction {
143
155
/// 1. `[writable]` The account to mint tokens to.
144
156
/// 2. `[]` The mint's multisignature owner.
145
157
/// 3. ..3+M '[signer]' M signer accounts.
146
- MintTo ( u64 ) ,
147
- /// Burns tokens by removing them from an account and the mint's total supply.
158
+ MintTo {
159
+ /// The amount of new tokens to mint.
160
+ amount : u64 ,
161
+ } ,
162
+ /// Burns tokens by removing them from an account and thus circulation.
148
163
///
149
164
/// Accounts expected by this instruction:
150
165
///
151
166
/// * Single owner/delegate
152
167
/// 0. `[writable]` The account to burn from.
153
- /// 1. `[writable]` The mint being burned.
154
168
/// 2. `[signer]` The account's owner/delegate.
155
169
///
156
170
/// * Multisignature owner/delegate
157
171
/// 0. `[writable]` The account to burn from.
158
- /// 1. `[writable]` The mint being burned.
159
172
/// 2. `[]` The account's multisignature owner/delegate
160
173
/// 3. ..3+M '[signer]' M signer accounts.
161
- Burn ( u64 ) ,
174
+ Burn {
175
+ /// The amount of tokens to burn.
176
+ amount : u64 ,
177
+ } ,
162
178
}
163
179
impl TokenInstruction {
164
180
/// Deserializes a byte buffer into an [TokenInstruction](enum.TokenInstruction.html).
@@ -168,37 +184,37 @@ impl TokenInstruction {
168
184
}
169
185
Ok ( match input[ 0 ] {
170
186
0 => {
171
- if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < TokenInfo > ( ) {
187
+ if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < u64 > ( ) + size_of :: < u8 > ( ) {
172
188
return Err ( ProgramError :: InvalidAccountData ) ;
173
189
}
174
190
#[ allow( clippy:: cast_ptr_alignment) ]
175
- let info: & TokenInfo = unsafe { & * ( & input[ 1 ] as * const u8 as * const TokenInfo ) } ;
176
- Self :: InitializeMint ( * info)
191
+ let info = unsafe { * ( & input[ 1 ] as * const u8 as * const TokenInfo ) } ;
192
+ Self :: InitializeMint { info }
177
193
}
178
194
1 => Self :: InitializeAccount ,
179
195
2 => {
180
196
if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < u8 > ( ) {
181
197
return Err ( ProgramError :: InvalidAccountData ) ;
182
198
}
183
199
#[ allow( clippy:: cast_ptr_alignment) ]
184
- let m: & u8 = unsafe { & * ( & input[ 1 ] as * const u8 as * const u8 ) } ;
185
- Self :: InitializeMultisig ( * m )
200
+ let m = unsafe { * ( & input[ 1 ] as * const u8 ) } ;
201
+ Self :: InitializeMultisig { m }
186
202
}
187
203
3 => {
188
204
if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < u64 > ( ) {
189
205
return Err ( ProgramError :: InvalidAccountData ) ;
190
206
}
191
207
#[ allow( clippy:: cast_ptr_alignment) ]
192
- let amount: & u64 = unsafe { & * ( & input[ 1 ] as * const u8 as * const u64 ) } ;
193
- Self :: Transfer ( * amount)
208
+ let amount = unsafe { * ( & input[ size_of :: < u8 > ( ) ] as * const u8 as * const u64 ) } ;
209
+ Self :: Transfer { amount }
194
210
}
195
211
4 => {
196
212
if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < u64 > ( ) {
197
213
return Err ( ProgramError :: InvalidAccountData ) ;
198
214
}
199
215
#[ allow( clippy:: cast_ptr_alignment) ]
200
- let amount: & u64 = unsafe { & * ( & input[ 1 ] as * const u8 as * const u64 ) } ;
201
- Self :: Approve ( * amount)
216
+ let amount = unsafe { * ( & input[ size_of :: < u8 > ( ) ] as * const u8 as * const u64 ) } ;
217
+ Self :: Approve { amount }
202
218
}
203
219
5 => Self :: Revoke ,
204
220
6 => Self :: SetOwner ,
@@ -207,16 +223,16 @@ impl TokenInstruction {
207
223
return Err ( ProgramError :: InvalidAccountData ) ;
208
224
}
209
225
#[ allow( clippy:: cast_ptr_alignment) ]
210
- let amount: & u64 = unsafe { & * ( & input[ 1 ] as * const u8 as * const u64 ) } ;
211
- Self :: MintTo ( * amount)
226
+ let amount = unsafe { * ( & input[ size_of :: < u8 > ( ) ] as * const u8 as * const u64 ) } ;
227
+ Self :: MintTo { amount }
212
228
}
213
229
8 => {
214
230
if input. len ( ) < size_of :: < u8 > ( ) + size_of :: < u64 > ( ) {
215
231
return Err ( ProgramError :: InvalidAccountData ) ;
216
232
}
217
233
#[ allow( clippy:: cast_ptr_alignment) ]
218
- let amount: & u64 = unsafe { & * ( & input[ 1 ] as * const u8 as * const u64 ) } ;
219
- Self :: Burn ( * amount)
234
+ let amount = unsafe { * ( & input[ size_of :: < u8 > ( ) ] as * const u8 as * const u64 ) } ;
235
+ Self :: Burn { amount }
220
236
}
221
237
_ => return Err ( ProgramError :: InvalidAccountData ) ,
222
238
} )
@@ -226,43 +242,43 @@ impl TokenInstruction {
226
242
pub fn serialize ( self : & Self ) -> Result < Vec < u8 > , ProgramError > {
227
243
let mut output = vec ! [ 0u8 ; size_of:: <TokenInstruction >( ) ] ;
228
244
match self {
229
- Self :: InitializeMint ( info) => {
245
+ Self :: InitializeMint { info } => {
230
246
output[ 0 ] = 0 ;
231
247
#[ allow( clippy:: cast_ptr_alignment) ]
232
248
let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut TokenInfo ) } ;
233
249
* value = * info;
234
250
}
235
251
Self :: InitializeAccount => output[ 0 ] = 1 ,
236
- Self :: InitializeMultisig ( m ) => {
252
+ Self :: InitializeMultisig { m } => {
237
253
output[ 0 ] = 2 ;
238
254
#[ allow( clippy:: cast_ptr_alignment) ]
239
- let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut u8 ) } ;
255
+ let value = unsafe { & mut * ( & mut output[ size_of :: < u8 > ( ) ] as * mut u8 as * mut u8 ) } ;
240
256
* value = * m;
241
257
}
242
- Self :: Transfer ( amount) => {
258
+ Self :: Transfer { amount } => {
243
259
output[ 0 ] = 3 ;
244
260
#[ allow( clippy:: cast_ptr_alignment) ]
245
- let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut u64 ) } ;
261
+ let value = unsafe { & mut * ( & mut output[ size_of :: < u8 > ( ) ] as * mut u8 as * mut u64 ) } ;
246
262
* value = * amount;
247
263
}
248
- Self :: Approve ( amount) => {
264
+ Self :: Approve { amount } => {
249
265
output[ 0 ] = 4 ;
250
266
#[ allow( clippy:: cast_ptr_alignment) ]
251
- let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut u64 ) } ;
267
+ let value = unsafe { & mut * ( & mut output[ size_of :: < u8 > ( ) ] as * mut u8 as * mut u64 ) } ;
252
268
* value = * amount;
253
269
}
254
270
Self :: Revoke => output[ 0 ] = 5 ,
255
271
Self :: SetOwner => output[ 0 ] = 6 ,
256
- Self :: MintTo ( amount) => {
272
+ Self :: MintTo { amount } => {
257
273
output[ 0 ] = 7 ;
258
274
#[ allow( clippy:: cast_ptr_alignment) ]
259
- let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut u64 ) } ;
275
+ let value = unsafe { & mut * ( & mut output[ size_of :: < u8 > ( ) ] as * mut u8 as * mut u64 ) } ;
260
276
* value = * amount;
261
277
}
262
- Self :: Burn ( amount) => {
278
+ Self :: Burn { amount } => {
263
279
output[ 0 ] = 8 ;
264
280
#[ allow( clippy:: cast_ptr_alignment) ]
265
- let value = unsafe { & mut * ( & mut output[ 1 ] as * mut u8 as * mut u64 ) } ;
281
+ let value = unsafe { & mut * ( & mut output[ size_of :: < u8 > ( ) ] as * mut u8 as * mut u64 ) } ;
266
282
* value = * amount;
267
283
}
268
284
}
@@ -276,12 +292,12 @@ pub fn initialize_mint(
276
292
mint_pubkey : & Pubkey ,
277
293
account_pubkey : Option < & Pubkey > ,
278
294
owner_pubkey : Option < & Pubkey > ,
279
- token_info : TokenInfo ,
295
+ info : TokenInfo ,
280
296
) -> Result < Instruction , ProgramError > {
281
- let data = TokenInstruction :: InitializeMint ( token_info ) . serialize ( ) ?;
297
+ let data = TokenInstruction :: InitializeMint { info } . serialize ( ) ?;
282
298
283
299
let mut accounts = vec ! [ AccountMeta :: new( * mint_pubkey, false ) ] ;
284
- if token_info . supply != 0 {
300
+ if info . supply != 0 {
285
301
match account_pubkey {
286
302
Some ( pubkey) => accounts. push ( AccountMeta :: new ( * pubkey, false ) ) ,
287
303
None => {
@@ -292,7 +308,7 @@ pub fn initialize_mint(
292
308
match owner_pubkey {
293
309
Some ( pubkey) => accounts. push ( AccountMeta :: new_readonly ( * pubkey, false ) ) ,
294
310
None => {
295
- if token_info . supply == 0 {
311
+ if info . supply == 0 {
296
312
return Err ( TokenError :: OwnerRequiredIfNoInitialSupply . into ( ) ) ;
297
313
}
298
314
}
@@ -340,7 +356,7 @@ pub fn initialize_multisig(
340
356
{
341
357
return Err ( ProgramError :: MissingRequiredSignature ) ;
342
358
}
343
- let data = TokenInstruction :: InitializeMultisig ( m ) . serialize ( ) ?;
359
+ let data = TokenInstruction :: InitializeMultisig { m } . serialize ( ) ?;
344
360
345
361
let mut accounts = Vec :: with_capacity ( 1 + signer_pubkeys. len ( ) ) ;
346
362
accounts. push ( AccountMeta :: new ( * multisig_pubkey, false ) ) ;
@@ -364,7 +380,7 @@ pub fn transfer(
364
380
signer_pubkeys : & [ & Pubkey ] ,
365
381
amount : u64 ,
366
382
) -> Result < Instruction , ProgramError > {
367
- let data = TokenInstruction :: Transfer ( amount) . serialize ( ) ?;
383
+ let data = TokenInstruction :: Transfer { amount } . serialize ( ) ?;
368
384
369
385
let mut accounts = Vec :: with_capacity ( 3 + signer_pubkeys. len ( ) ) ;
370
386
accounts. push ( AccountMeta :: new ( * source_pubkey, false ) ) ;
@@ -393,7 +409,7 @@ pub fn approve(
393
409
signer_pubkeys : & [ & Pubkey ] ,
394
410
amount : u64 ,
395
411
) -> Result < Instruction , ProgramError > {
396
- let data = TokenInstruction :: Approve ( amount) . serialize ( ) ?;
412
+ let data = TokenInstruction :: Approve { amount } . serialize ( ) ?;
397
413
398
414
let mut accounts = Vec :: with_capacity ( 3 + signer_pubkeys. len ( ) ) ;
399
415
accounts. push ( AccountMeta :: new_readonly ( * source_pubkey, false ) ) ;
@@ -476,7 +492,7 @@ pub fn mint_to(
476
492
signer_pubkeys : & [ & Pubkey ] ,
477
493
amount : u64 ,
478
494
) -> Result < Instruction , ProgramError > {
479
- let data = TokenInstruction :: MintTo ( amount) . serialize ( ) ?;
495
+ let data = TokenInstruction :: MintTo { amount } . serialize ( ) ?;
480
496
481
497
let mut accounts = Vec :: with_capacity ( 3 + signer_pubkeys. len ( ) ) ;
482
498
accounts. push ( AccountMeta :: new ( * mint_pubkey, false ) ) ;
@@ -505,7 +521,7 @@ pub fn burn(
505
521
signer_pubkeys : & [ & Pubkey ] ,
506
522
amount : u64 ,
507
523
) -> Result < Instruction , ProgramError > {
508
- let data = TokenInstruction :: Burn ( amount) . serialize ( ) ?;
524
+ let data = TokenInstruction :: Burn { amount } . serialize ( ) ?;
509
525
510
526
let mut accounts = Vec :: with_capacity ( 3 + signer_pubkeys. len ( ) ) ;
511
527
accounts. push ( AccountMeta :: new ( * account_pubkey, false ) ) ;
0 commit comments