Skip to content

Commit

Permalink
Fix CpiGuard typo, but actually DRY processor methods (#3791)
Browse files Browse the repository at this point in the history
* Fix typo

* DRY CpiGuard

* DRY RequiredMemoTransfers
  • Loading branch information
Tyera Eulberg authored Nov 2, 2022
1 parent edac3c5 commit 574fe73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 67 deletions.
43 changes: 9 additions & 34 deletions token/program-2022/src/extension/cpi_guard/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,12 @@ use {
},
};

fn process_enable_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?;
let owner_info = next_account_info(account_info_iter)?;
let owner_info_data_len = owner_info.data_len();

let mut account_data = token_account_info.data.borrow_mut();
let mut account = StateWithExtensionsMut::<Account>::unpack(&mut account_data)?;

Processor::validate_owner(
program_id,
&account.base.owner,
owner_info,
owner_info_data_len,
account_info_iter.as_slice(),
)?;

if in_cpi() {
return Err(TokenError::CpiGuardSettingsLocked.into());
}

let extension = if let Ok(extension) = account.get_extension_mut::<CpiGuard>() {
extension
} else {
account.init_extension::<CpiGuard>(true)?
};
extension.lock_cpi = true.into();
Ok(())
}

fn process_diasble_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
/// Toggle the CpiGuard extension, initializing the extension if not already present.
fn process_toggle_cpi_guard(
program_id: &Pubkey,
accounts: &[AccountInfo],
enable: bool,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?;
let owner_info = next_account_info(account_info_iter)?;
Expand All @@ -74,7 +49,7 @@ fn process_diasble_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> P
} else {
account.init_extension::<CpiGuard>(true)?
};
extension.lock_cpi = false.into();
extension.lock_cpi = enable.into();
Ok(())
}

Expand All @@ -88,11 +63,11 @@ pub(crate) fn process_instruction(
match decode_instruction_type(input)? {
CpiGuardInstruction::Enable => {
msg!("CpiGuardInstruction::Enable");
process_enable_cpi_guard(program_id, accounts)
process_toggle_cpi_guard(program_id, accounts, true /* enable */)
}
CpiGuardInstruction::Disable => {
msg!("CpiGuardInstruction::Disable");
process_diasble_cpi_guard(program_id, accounts)
process_toggle_cpi_guard(program_id, accounts, false /* disable */)
}
}
}
39 changes: 6 additions & 33 deletions token/program-2022/src/extension/memo_transfer/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use {
},
};

fn process_enable_required_memo_transfers(
/// Toggle the RequiredMemoTransfers extension, initializing the extension if not already present.
fn process_toggle_required_memo_transfers(
program_id: &Pubkey,
accounts: &[AccountInfo],
enable: bool,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?;
Expand All @@ -42,36 +44,7 @@ fn process_enable_required_memo_transfers(
} else {
account.init_extension::<MemoTransfer>(true)?
};
extension.require_incoming_transfer_memos = true.into();
Ok(())
}

fn process_diasble_required_memo_transfers(
program_id: &Pubkey,
accounts: &[AccountInfo],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?;
let owner_info = next_account_info(account_info_iter)?;
let owner_info_data_len = owner_info.data_len();

let mut account_data = token_account_info.data.borrow_mut();
let mut account = StateWithExtensionsMut::<Account>::unpack(&mut account_data)?;

Processor::validate_owner(
program_id,
&account.base.owner,
owner_info,
owner_info_data_len,
account_info_iter.as_slice(),
)?;

let extension = if let Ok(extension) = account.get_extension_mut::<MemoTransfer>() {
extension
} else {
account.init_extension::<MemoTransfer>(true)?
};
extension.require_incoming_transfer_memos = false.into();
extension.require_incoming_transfer_memos = enable.into();
Ok(())
}

Expand All @@ -85,11 +58,11 @@ pub(crate) fn process_instruction(
match decode_instruction_type(input)? {
RequiredMemoTransfersInstruction::Enable => {
msg!("RequiredMemoTransfersInstruction::Enable");
process_enable_required_memo_transfers(program_id, accounts)
process_toggle_required_memo_transfers(program_id, accounts, true /* enable */)
}
RequiredMemoTransfersInstruction::Disable => {
msg!("RequiredMemoTransfersInstruction::Disable");
process_diasble_required_memo_transfers(program_id, accounts)
process_toggle_required_memo_transfers(program_id, accounts, false /* disable */)
}
}
}

0 comments on commit 574fe73

Please sign in to comment.