Skip to content

Commit a33f8df

Browse files
committed
solana: Refactor out claim_from_(multisig_)token_authority fn's
1 parent 5e7b92c commit a33f8df

File tree

1 file changed

+92
-122
lines changed
  • solana/programs/example-native-token-transfers/src/instructions

1 file changed

+92
-122
lines changed

solana/programs/example-native-token-transfers/src/instructions/admin.rs

+92-122
Original file line numberDiff line numberDiff line change
@@ -297,47 +297,22 @@ pub fn set_token_authority_one_step_unchecked(
297297
ctx: Context<SetTokenAuthorityUnchecked>,
298298
) -> Result<()> {
299299
match &ctx.accounts.common.multisig_token_authority {
300-
Some(multisig_token_authority) => {
301-
solana_program::program::invoke_signed(
302-
&spl_token_2022::instruction::set_authority(
303-
&ctx.accounts.token_program.key(),
304-
&ctx.accounts.common.mint.key(),
305-
Some(&ctx.accounts.common.new_authority.key()),
306-
spl_token_2022::instruction::AuthorityType::MintTokens,
307-
&multisig_token_authority.key(),
308-
&[&ctx.accounts.common.token_authority.key()],
309-
)?,
310-
&[
311-
ctx.accounts.common.mint.to_account_info(),
312-
ctx.accounts.common.new_authority.to_account_info(),
313-
multisig_token_authority.to_account_info(),
314-
ctx.accounts.common.token_authority.to_account_info(),
315-
],
316-
&[&[
317-
crate::TOKEN_AUTHORITY_SEED,
318-
&[ctx.bumps.common.token_authority],
319-
]],
320-
)?;
321-
}
322-
None => {
323-
token_interface::set_authority(
324-
CpiContext::new_with_signer(
325-
ctx.accounts.token_program.to_account_info(),
326-
token_interface::SetAuthority {
327-
account_or_mint: ctx.accounts.common.mint.to_account_info(),
328-
current_authority: ctx.accounts.common.token_authority.to_account_info(),
329-
},
330-
&[&[
331-
crate::TOKEN_AUTHORITY_SEED,
332-
&[ctx.bumps.common.token_authority],
333-
]],
334-
),
335-
AuthorityType::MintTokens,
336-
Some(ctx.accounts.common.new_authority.key()),
337-
)?;
338-
}
339-
};
340-
Ok(())
300+
Some(multisig_token_authority) => claim_from_multisig_token_authority(
301+
ctx.accounts.token_program.to_account_info(),
302+
ctx.accounts.common.mint.to_account_info(),
303+
multisig_token_authority.to_account_info(),
304+
ctx.accounts.common.token_authority.to_account_info(),
305+
ctx.bumps.common.token_authority,
306+
ctx.accounts.common.new_authority.key(),
307+
),
308+
None => claim_from_token_authority(
309+
ctx.accounts.token_program.to_account_info(),
310+
ctx.accounts.common.mint.to_account_info(),
311+
ctx.accounts.common.token_authority.to_account_info(),
312+
ctx.bumps.common.token_authority,
313+
ctx.accounts.common.new_authority.key(),
314+
),
315+
}
341316
}
342317

343318
#[derive(Accounts)]
@@ -450,47 +425,22 @@ pub struct ClaimTokenAuthority<'info> {
450425

451426
pub fn claim_token_authority(ctx: Context<ClaimTokenAuthority>) -> Result<()> {
452427
match &ctx.accounts.common.multisig_token_authority {
453-
Some(multisig_token_authority) => {
454-
solana_program::program::invoke_signed(
455-
&spl_token_2022::instruction::set_authority(
456-
&ctx.accounts.common.token_program.key(),
457-
&ctx.accounts.common.mint.key(),
458-
Some(&ctx.accounts.new_authority.key()),
459-
spl_token_2022::instruction::AuthorityType::MintTokens,
460-
&multisig_token_authority.key(),
461-
&[&ctx.accounts.common.token_authority.key()],
462-
)?,
463-
&[
464-
ctx.accounts.common.mint.to_account_info(),
465-
ctx.accounts.new_authority.to_account_info(),
466-
multisig_token_authority.to_account_info(),
467-
ctx.accounts.common.token_authority.to_account_info(),
468-
],
469-
&[&[
470-
crate::TOKEN_AUTHORITY_SEED,
471-
&[ctx.bumps.common.token_authority],
472-
]],
473-
)?;
474-
}
475-
None => {
476-
token_interface::set_authority(
477-
CpiContext::new_with_signer(
478-
ctx.accounts.common.token_program.to_account_info(),
479-
token_interface::SetAuthority {
480-
account_or_mint: ctx.accounts.common.mint.to_account_info(),
481-
current_authority: ctx.accounts.common.token_authority.to_account_info(),
482-
},
483-
&[&[
484-
crate::TOKEN_AUTHORITY_SEED,
485-
&[ctx.bumps.common.token_authority],
486-
]],
487-
),
488-
AuthorityType::MintTokens,
489-
Some(ctx.accounts.new_authority.key()),
490-
)?;
491-
}
492-
};
493-
Ok(())
428+
Some(multisig_token_authority) => claim_from_multisig_token_authority(
429+
ctx.accounts.common.token_program.to_account_info(),
430+
ctx.accounts.common.mint.to_account_info(),
431+
multisig_token_authority.to_account_info(),
432+
ctx.accounts.common.token_authority.to_account_info(),
433+
ctx.bumps.common.token_authority,
434+
ctx.accounts.new_authority.key(),
435+
),
436+
None => claim_from_token_authority(
437+
ctx.accounts.common.token_program.to_account_info(),
438+
ctx.accounts.common.mint.to_account_info(),
439+
ctx.accounts.common.token_authority.to_account_info(),
440+
ctx.bumps.common.token_authority,
441+
ctx.accounts.new_authority.key(),
442+
),
443+
}
494444
}
495445

496446
#[derive(Accounts)]
@@ -521,46 +471,66 @@ pub fn claim_token_authority_to_multisig(
521471
}
522472

523473
match &ctx.accounts.common.multisig_token_authority {
524-
Some(multisig_token_authority) => {
525-
solana_program::program::invoke_signed(
526-
&spl_token_2022::instruction::set_authority(
527-
&ctx.accounts.common.token_program.key(),
528-
&ctx.accounts.common.mint.key(),
529-
Some(&ctx.accounts.new_multisig_authority.key()),
530-
spl_token_2022::instruction::AuthorityType::MintTokens,
531-
&multisig_token_authority.key(),
532-
&[&ctx.accounts.common.token_authority.key()],
533-
)?,
534-
&[
535-
ctx.accounts.common.mint.to_account_info(),
536-
ctx.accounts.new_multisig_authority.to_account_info(),
537-
multisig_token_authority.to_account_info(),
538-
ctx.accounts.common.token_authority.to_account_info(),
539-
],
540-
&[&[
541-
crate::TOKEN_AUTHORITY_SEED,
542-
&[ctx.bumps.common.token_authority],
543-
]],
544-
)?;
545-
}
546-
None => {
547-
token_interface::set_authority(
548-
CpiContext::new_with_signer(
549-
ctx.accounts.common.token_program.to_account_info(),
550-
token_interface::SetAuthority {
551-
account_or_mint: ctx.accounts.common.mint.to_account_info(),
552-
current_authority: ctx.accounts.common.token_authority.to_account_info(),
553-
},
554-
&[&[
555-
crate::TOKEN_AUTHORITY_SEED,
556-
&[ctx.bumps.common.token_authority],
557-
]],
558-
),
559-
AuthorityType::MintTokens,
560-
Some(ctx.accounts.new_multisig_authority.key()),
561-
)?;
562-
}
563-
};
474+
Some(multisig_token_authority) => claim_from_multisig_token_authority(
475+
ctx.accounts.common.token_program.to_account_info(),
476+
ctx.accounts.common.mint.to_account_info(),
477+
multisig_token_authority.to_account_info(),
478+
ctx.accounts.common.token_authority.to_account_info(),
479+
ctx.bumps.common.token_authority,
480+
ctx.accounts.new_multisig_authority.key(),
481+
),
482+
None => claim_from_token_authority(
483+
ctx.accounts.common.token_program.to_account_info(),
484+
ctx.accounts.common.mint.to_account_info(),
485+
ctx.accounts.common.token_authority.to_account_info(),
486+
ctx.bumps.common.token_authority,
487+
ctx.accounts.new_multisig_authority.key(),
488+
),
489+
}
490+
}
491+
492+
fn claim_from_token_authority<'info>(
493+
token_program: AccountInfo<'info>,
494+
mint: AccountInfo<'info>,
495+
token_authority: AccountInfo<'info>,
496+
token_authority_bump: u8,
497+
new_authority: Pubkey,
498+
) -> Result<()> {
499+
token_interface::set_authority(
500+
CpiContext::new_with_signer(
501+
token_program.to_account_info(),
502+
token_interface::SetAuthority {
503+
account_or_mint: mint.to_account_info(),
504+
current_authority: token_authority.to_account_info(),
505+
},
506+
&[&[crate::TOKEN_AUTHORITY_SEED, &[token_authority_bump]]],
507+
),
508+
AuthorityType::MintTokens,
509+
Some(new_authority),
510+
)?;
511+
Ok(())
512+
}
513+
514+
fn claim_from_multisig_token_authority<'info>(
515+
token_program: AccountInfo<'info>,
516+
mint: AccountInfo<'info>,
517+
multisig_token_authority: AccountInfo<'info>,
518+
token_authority: AccountInfo<'info>,
519+
token_authority_bump: u8,
520+
new_authority: Pubkey,
521+
) -> Result<()> {
522+
solana_program::program::invoke_signed(
523+
&spl_token_2022::instruction::set_authority(
524+
&token_program.key(),
525+
&mint.key(),
526+
Some(&new_authority),
527+
spl_token_2022::instruction::AuthorityType::MintTokens,
528+
&multisig_token_authority.key(),
529+
&[&token_authority.key()],
530+
)?,
531+
&[mint, multisig_token_authority, token_authority],
532+
&[&[crate::TOKEN_AUTHORITY_SEED, &[token_authority_bump]]],
533+
)?;
564534
Ok(())
565535
}
566536

0 commit comments

Comments
 (0)