Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit ea52428

Browse files
token: Simplify initialize_mint (#327)
* Simplify initialize_mint * C headers
1 parent 9b59d4e commit ea52428

File tree

3 files changed

+154
-319
lines changed

3 files changed

+154
-319
lines changed

token/program2/inc/token2.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ typedef enum Token_TokenInstruction_Tag {
118118
* Accounts expected by this instruction:
119119
*
120120
* 0. `[writable]` The mint to initialize.
121-
* 1. If supply is non-zero: `[writable]` The account to hold all the newly minted tokens.
122121
*
123122
*/
124123
Token_TokenInstruction_InitializeMint,
@@ -308,17 +307,12 @@ typedef enum Token_TokenInstruction_Tag {
308307
} Token_TokenInstruction_Tag;
309308

310309
typedef struct Token_TokenInstruction_Token_InitializeMint_Body {
311-
/**
312-
* Initial amount of tokens to mint.
313-
*/
314-
uint64_t amount;
315310
/**
316311
* Number of base 10 digits to the right of the decimal place.
317312
*/
318313
uint8_t decimals;
319314
/**
320-
* The authority/multisignature to mint tokens, if supply is non-zero. If present,
321-
* further minting is supported.
315+
* The authority/multisignature to mint tokens. If present, further minting is supported.
322316
*/
323317
Token_COption_Pubkey mint_authority;
324318
/**

token/program2/src/instruction.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,11 @@ pub enum TokenInstruction {
2626
/// Accounts expected by this instruction:
2727
///
2828
/// 0. `[writable]` The mint to initialize.
29-
/// 1. If supply is non-zero: `[writable]` The account to hold all the newly minted tokens.
3029
///
3130
InitializeMint {
32-
/// Initial amount of tokens to mint.
33-
amount: u64,
3431
/// Number of base 10 digits to the right of the decimal place.
3532
decimals: u8,
36-
/// The authority/multisignature to mint tokens, if supply is non-zero. If present,
37-
/// further minting is supported.
33+
/// The authority/multisignature to mint tokens. If present, further minting is supported.
3834
mint_authority: COption<Pubkey>,
3935
/// The freeze authority/multisignature of the mint.
4036
freeze_authority: COption<Pubkey>,
@@ -230,17 +226,12 @@ impl TokenInstruction {
230226
}
231227
Ok(match input[0] {
232228
0 => {
233-
if input.len()
234-
< size_of::<u8>() + size_of::<u64>() + size_of::<u8>() + size_of::<bool>()
235-
{
229+
if input.len() < size_of::<u8>() + size_of::<u8>() + size_of::<bool>() {
236230
return Err(TokenError::InvalidInstruction.into());
237231
}
238232
let mut input_len = 0;
239233
input_len += size_of::<u8>();
240234

241-
#[allow(clippy::cast_ptr_alignment)]
242-
let amount = unsafe { *(&input[input_len] as *const u8 as *const u64) };
243-
input_len += size_of::<u64>();
244235
let decimals = unsafe { *(&input[input_len] as *const u8) };
245236
input_len += size_of::<u8>();
246237

@@ -258,7 +249,6 @@ impl TokenInstruction {
258249
Self::InitializeMint {
259250
mint_authority,
260251
freeze_authority,
261-
amount,
262252
decimals,
263253
}
264254
}
@@ -339,17 +329,11 @@ impl TokenInstruction {
339329
Self::InitializeMint {
340330
mint_authority,
341331
freeze_authority,
342-
amount,
343332
decimals,
344333
} => {
345334
output[output_len] = 0;
346335
output_len += size_of::<u8>();
347336

348-
#[allow(clippy::cast_ptr_alignment)]
349-
let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut u64) };
350-
*value = *amount;
351-
output_len += size_of::<u64>();
352-
353337
let value = unsafe { &mut *(&mut output[output_len] as *mut u8) };
354338
*value = *decimals;
355339
output_len += size_of::<u8>();
@@ -480,31 +464,20 @@ impl AuthorityType {
480464
pub fn initialize_mint(
481465
token_program_id: &Pubkey,
482466
mint_pubkey: &Pubkey,
483-
account_pubkey: Option<&Pubkey>,
484-
mint_authority_pubkey: Option<&Pubkey>,
467+
mint_authority_pubkey: &Pubkey,
485468
freeze_authority_pubkey: Option<&Pubkey>,
486-
amount: u64,
487469
decimals: u8,
488470
) -> Result<Instruction, ProgramError> {
489-
let mint_authority = mint_authority_pubkey.cloned().into();
471+
let mint_authority = COption::Some(*mint_authority_pubkey);
490472
let freeze_authority = freeze_authority_pubkey.cloned().into();
491473
let data = TokenInstruction::InitializeMint {
492474
mint_authority,
493475
freeze_authority,
494-
amount,
495476
decimals,
496477
}
497478
.pack()?;
498479

499-
let mut accounts = vec![AccountMeta::new(*mint_pubkey, false)];
500-
if amount != 0 {
501-
match account_pubkey {
502-
Some(pubkey) => accounts.push(AccountMeta::new(*pubkey, false)),
503-
None => {
504-
return Err(ProgramError::NotEnoughAccountKeys);
505-
}
506-
}
507-
}
480+
let accounts = vec![AccountMeta::new(*mint_pubkey, false)];
508481

509482
Ok(Instruction {
510483
program_id: *token_program_id,
@@ -832,25 +805,23 @@ mod test {
832805
#[test]
833806
fn test_instruction_packing() {
834807
let check = TokenInstruction::InitializeMint {
835-
amount: 1,
836808
decimals: 2,
837809
mint_authority: COption::None,
838810
freeze_authority: COption::None,
839811
};
840812
let packed = check.pack().unwrap();
841-
let expect = Vec::from([0u8, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]);
813+
let expect = Vec::from([0u8, 2, 0, 0]);
842814
assert_eq!(packed, expect);
843815
let unpacked = TokenInstruction::unpack(&expect).unwrap();
844816
assert_eq!(unpacked, check);
845817

846818
let check = TokenInstruction::InitializeMint {
847-
amount: 1,
848819
decimals: 2,
849820
mint_authority: COption::Some(Pubkey::new(&[2u8; 32])),
850821
freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
851822
};
852823
let packed = check.pack().unwrap();
853-
let mut expect = vec![0u8, 1, 0, 0, 0, 0, 0, 0, 0, 2];
824+
let mut expect = vec![0u8, 2];
854825
expect.extend_from_slice(&[1]);
855826
expect.extend_from_slice(&[2u8; 32]);
856827
expect.extend_from_slice(&[1]);

0 commit comments

Comments
 (0)