Skip to content

Commit 7feaf94

Browse files
authored
program: Create InstructionVariant for checked-ness (#170)
#### Problem In #160, the three different transfer instruction implementations were made more clearly different through an enum instead of various options, but the other checked / unchecked instructions are still not clear. #### Summary of changes Add `InstructionVariant` enum and use it for mint / burn / approve.
1 parent 4b68db4 commit 7feaf94

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

program/src/processor.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ pub(crate) enum TransferInstruction {
6969
CheckedWithFee { decimals: u8, fee: u64 },
7070
}
7171

72+
pub(crate) enum InstructionVariant {
73+
Unchecked,
74+
Checked { decimals: u8 },
75+
}
76+
7277
/// Program state handler.
7378
pub struct Processor {}
7479
impl Processor {
@@ -567,21 +572,22 @@ impl Processor {
567572
}
568573

569574
/// Processes an [`Approve`](enum.TokenInstruction.html) instruction.
570-
pub fn process_approve(
575+
pub(crate) fn process_approve(
571576
program_id: &Pubkey,
572577
accounts: &[AccountInfo],
573578
amount: u64,
574-
expected_decimals: Option<u8>,
579+
instruction_variant: InstructionVariant,
575580
) -> ProgramResult {
576581
let account_info_iter = &mut accounts.iter();
577582

578583
let source_account_info = next_account_info(account_info_iter)?;
579584

580-
let expected_mint_info = if let Some(expected_decimals) = expected_decimals {
581-
Some((next_account_info(account_info_iter)?, expected_decimals))
582-
} else {
583-
None
584-
};
585+
let expected_mint_info =
586+
if let InstructionVariant::Checked { decimals } = instruction_variant {
587+
Some((next_account_info(account_info_iter)?, decimals))
588+
} else {
589+
None
590+
};
585591
let delegate_info = next_account_info(account_info_iter)?;
586592
let owner_info = next_account_info(account_info_iter)?;
587593
let owner_info_data_len = owner_info.data_len();
@@ -965,11 +971,11 @@ impl Processor {
965971
}
966972

967973
/// Processes a [`MintTo`](enum.TokenInstruction.html) instruction.
968-
pub fn process_mint_to(
974+
pub(crate) fn process_mint_to(
969975
program_id: &Pubkey,
970976
accounts: &[AccountInfo],
971977
amount: u64,
972-
expected_decimals: Option<u8>,
978+
instruction_variant: InstructionVariant,
973979
) -> ProgramResult {
974980
let account_info_iter = &mut accounts.iter();
975981
let mint_info = next_account_info(account_info_iter)?;
@@ -1014,8 +1020,8 @@ impl Processor {
10141020
return Err(TokenError::IllegalMintBurnConversion.into());
10151021
}
10161022

1017-
if let Some(expected_decimals) = expected_decimals {
1018-
if expected_decimals != mint.base.decimals {
1023+
if let InstructionVariant::Checked { decimals } = instruction_variant {
1024+
if decimals != mint.base.decimals {
10191025
return Err(TokenError::MintDecimalsMismatch.into());
10201026
}
10211027
}
@@ -1054,11 +1060,11 @@ impl Processor {
10541060
}
10551061

10561062
/// Processes a [`Burn`](enum.TokenInstruction.html) instruction.
1057-
pub fn process_burn(
1063+
pub(crate) fn process_burn(
10581064
program_id: &Pubkey,
10591065
accounts: &[AccountInfo],
10601066
amount: u64,
1061-
expected_decimals: Option<u8>,
1067+
instruction_variant: InstructionVariant,
10621068
) -> ProgramResult {
10631069
let account_info_iter = &mut accounts.iter();
10641070

@@ -1086,8 +1092,8 @@ impl Processor {
10861092
return Err(TokenError::MintMismatch.into());
10871093
}
10881094

1089-
if let Some(expected_decimals) = expected_decimals {
1090-
if expected_decimals != mint.base.decimals {
1095+
if let InstructionVariant::Checked { decimals } = instruction_variant {
1096+
if decimals != mint.base.decimals {
10911097
return Err(TokenError::MintDecimalsMismatch.into());
10921098
}
10931099
}
@@ -1681,7 +1687,12 @@ impl Processor {
16811687
PodTokenInstruction::Approve => {
16821688
msg!("Instruction: Approve");
16831689
let data = decode_instruction_data::<AmountData>(input)?;
1684-
Self::process_approve(program_id, accounts, data.amount.into(), None)
1690+
Self::process_approve(
1691+
program_id,
1692+
accounts,
1693+
data.amount.into(),
1694+
InstructionVariant::Unchecked,
1695+
)
16851696
}
16861697
PodTokenInstruction::Revoke => {
16871698
msg!("Instruction: Revoke");
@@ -1701,12 +1712,22 @@ impl Processor {
17011712
PodTokenInstruction::MintTo => {
17021713
msg!("Instruction: MintTo");
17031714
let data = decode_instruction_data::<AmountData>(input)?;
1704-
Self::process_mint_to(program_id, accounts, data.amount.into(), None)
1715+
Self::process_mint_to(
1716+
program_id,
1717+
accounts,
1718+
data.amount.into(),
1719+
InstructionVariant::Unchecked,
1720+
)
17051721
}
17061722
PodTokenInstruction::Burn => {
17071723
msg!("Instruction: Burn");
17081724
let data = decode_instruction_data::<AmountData>(input)?;
1709-
Self::process_burn(program_id, accounts, data.amount.into(), None)
1725+
Self::process_burn(
1726+
program_id,
1727+
accounts,
1728+
data.amount.into(),
1729+
InstructionVariant::Unchecked,
1730+
)
17101731
}
17111732
PodTokenInstruction::CloseAccount => {
17121733
msg!("Instruction: CloseAccount");
@@ -1739,7 +1760,9 @@ impl Processor {
17391760
program_id,
17401761
accounts,
17411762
data.amount.into(),
1742-
Some(data.decimals),
1763+
InstructionVariant::Checked {
1764+
decimals: data.decimals,
1765+
},
17431766
)
17441767
}
17451768
PodTokenInstruction::MintToChecked => {
@@ -1749,7 +1772,9 @@ impl Processor {
17491772
program_id,
17501773
accounts,
17511774
data.amount.into(),
1752-
Some(data.decimals),
1775+
InstructionVariant::Checked {
1776+
decimals: data.decimals,
1777+
},
17531778
)
17541779
}
17551780
PodTokenInstruction::BurnChecked => {
@@ -1759,7 +1784,9 @@ impl Processor {
17591784
program_id,
17601785
accounts,
17611786
data.amount.into(),
1762-
Some(data.decimals),
1787+
InstructionVariant::Checked {
1788+
decimals: data.decimals,
1789+
},
17631790
)
17641791
}
17651792
PodTokenInstruction::SyncNative => {

0 commit comments

Comments
 (0)