Fix CpiGuard typo, but actually DRY processor methods (#3791)

* Fix typo

* DRY CpiGuard

* DRY RequiredMemoTransfers
This commit is contained in:
Tyera Eulberg 2022-11-02 10:33:37 -06:00 committed by GitHub
parent edac3c50f0
commit 574fe73465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 67 deletions

View File

@ -18,7 +18,12 @@ use {
}, },
}; };
fn process_enable_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 account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?; let token_account_info = next_account_info(account_info_iter)?;
let owner_info = next_account_info(account_info_iter)?; let owner_info = next_account_info(account_info_iter)?;
@ -44,37 +49,7 @@ fn process_enable_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> Pr
} else { } else {
account.init_extension::<CpiGuard>(true)? account.init_extension::<CpiGuard>(true)?
}; };
extension.lock_cpi = true.into(); extension.lock_cpi = enable.into();
Ok(())
}
fn process_diasble_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 = false.into();
Ok(()) Ok(())
} }
@ -88,11 +63,11 @@ pub(crate) fn process_instruction(
match decode_instruction_type(input)? { match decode_instruction_type(input)? {
CpiGuardInstruction::Enable => { CpiGuardInstruction::Enable => {
msg!("CpiGuardInstruction::Enable"); msg!("CpiGuardInstruction::Enable");
process_enable_cpi_guard(program_id, accounts) process_toggle_cpi_guard(program_id, accounts, true /* enable */)
} }
CpiGuardInstruction::Disable => { CpiGuardInstruction::Disable => {
msg!("CpiGuardInstruction::Disable"); msg!("CpiGuardInstruction::Disable");
process_diasble_cpi_guard(program_id, accounts) process_toggle_cpi_guard(program_id, accounts, false /* disable */)
} }
} }
} }

View File

@ -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, program_id: &Pubkey,
accounts: &[AccountInfo], accounts: &[AccountInfo],
enable: bool,
) -> ProgramResult { ) -> ProgramResult {
let account_info_iter = &mut accounts.iter(); let account_info_iter = &mut accounts.iter();
let token_account_info = next_account_info(account_info_iter)?; let token_account_info = next_account_info(account_info_iter)?;
@ -42,36 +44,7 @@ fn process_enable_required_memo_transfers(
} else { } else {
account.init_extension::<MemoTransfer>(true)? account.init_extension::<MemoTransfer>(true)?
}; };
extension.require_incoming_transfer_memos = true.into(); extension.require_incoming_transfer_memos = enable.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();
Ok(()) Ok(())
} }
@ -85,11 +58,11 @@ pub(crate) fn process_instruction(
match decode_instruction_type(input)? { match decode_instruction_type(input)? {
RequiredMemoTransfersInstruction::Enable => { RequiredMemoTransfersInstruction::Enable => {
msg!("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 => { RequiredMemoTransfersInstruction::Disable => {
msg!("RequiredMemoTransfersInstruction::Disable"); msg!("RequiredMemoTransfersInstruction::Disable");
process_diasble_required_memo_transfers(program_id, accounts) process_toggle_required_memo_transfers(program_id, accounts, false /* disable */)
} }
} }
} }