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

Commit 574fe73

Browse files
author
Tyera Eulberg
authored
Fix CpiGuard typo, but actually DRY processor methods (#3791)
* Fix typo * DRY CpiGuard * DRY RequiredMemoTransfers
1 parent edac3c5 commit 574fe73

File tree

2 files changed

+15
-67
lines changed

2 files changed

+15
-67
lines changed

token/program-2022/src/extension/cpi_guard/processor.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,12 @@ use {
1818
},
1919
};
2020

21-
fn process_enable_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
22-
let account_info_iter = &mut accounts.iter();
23-
let token_account_info = next_account_info(account_info_iter)?;
24-
let owner_info = next_account_info(account_info_iter)?;
25-
let owner_info_data_len = owner_info.data_len();
26-
27-
let mut account_data = token_account_info.data.borrow_mut();
28-
let mut account = StateWithExtensionsMut::<Account>::unpack(&mut account_data)?;
29-
30-
Processor::validate_owner(
31-
program_id,
32-
&account.base.owner,
33-
owner_info,
34-
owner_info_data_len,
35-
account_info_iter.as_slice(),
36-
)?;
37-
38-
if in_cpi() {
39-
return Err(TokenError::CpiGuardSettingsLocked.into());
40-
}
41-
42-
let extension = if let Ok(extension) = account.get_extension_mut::<CpiGuard>() {
43-
extension
44-
} else {
45-
account.init_extension::<CpiGuard>(true)?
46-
};
47-
extension.lock_cpi = true.into();
48-
Ok(())
49-
}
50-
51-
fn process_diasble_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
21+
/// Toggle the CpiGuard extension, initializing the extension if not already present.
22+
fn process_toggle_cpi_guard(
23+
program_id: &Pubkey,
24+
accounts: &[AccountInfo],
25+
enable: bool,
26+
) -> ProgramResult {
5227
let account_info_iter = &mut accounts.iter();
5328
let token_account_info = next_account_info(account_info_iter)?;
5429
let owner_info = next_account_info(account_info_iter)?;
@@ -74,7 +49,7 @@ fn process_diasble_cpi_guard(program_id: &Pubkey, accounts: &[AccountInfo]) -> P
7449
} else {
7550
account.init_extension::<CpiGuard>(true)?
7651
};
77-
extension.lock_cpi = false.into();
52+
extension.lock_cpi = enable.into();
7853
Ok(())
7954
}
8055

@@ -88,11 +63,11 @@ pub(crate) fn process_instruction(
8863
match decode_instruction_type(input)? {
8964
CpiGuardInstruction::Enable => {
9065
msg!("CpiGuardInstruction::Enable");
91-
process_enable_cpi_guard(program_id, accounts)
66+
process_toggle_cpi_guard(program_id, accounts, true /* enable */)
9267
}
9368
CpiGuardInstruction::Disable => {
9469
msg!("CpiGuardInstruction::Disable");
95-
process_diasble_cpi_guard(program_id, accounts)
70+
process_toggle_cpi_guard(program_id, accounts, false /* disable */)
9671
}
9772
}
9873
}

token/program-2022/src/extension/memo_transfer/processor.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use {
1717
},
1818
};
1919

20-
fn process_enable_required_memo_transfers(
20+
/// Toggle the RequiredMemoTransfers extension, initializing the extension if not already present.
21+
fn process_toggle_required_memo_transfers(
2122
program_id: &Pubkey,
2223
accounts: &[AccountInfo],
24+
enable: bool,
2325
) -> ProgramResult {
2426
let account_info_iter = &mut accounts.iter();
2527
let token_account_info = next_account_info(account_info_iter)?;
@@ -42,36 +44,7 @@ fn process_enable_required_memo_transfers(
4244
} else {
4345
account.init_extension::<MemoTransfer>(true)?
4446
};
45-
extension.require_incoming_transfer_memos = true.into();
46-
Ok(())
47-
}
48-
49-
fn process_diasble_required_memo_transfers(
50-
program_id: &Pubkey,
51-
accounts: &[AccountInfo],
52-
) -> ProgramResult {
53-
let account_info_iter = &mut accounts.iter();
54-
let token_account_info = next_account_info(account_info_iter)?;
55-
let owner_info = next_account_info(account_info_iter)?;
56-
let owner_info_data_len = owner_info.data_len();
57-
58-
let mut account_data = token_account_info.data.borrow_mut();
59-
let mut account = StateWithExtensionsMut::<Account>::unpack(&mut account_data)?;
60-
61-
Processor::validate_owner(
62-
program_id,
63-
&account.base.owner,
64-
owner_info,
65-
owner_info_data_len,
66-
account_info_iter.as_slice(),
67-
)?;
68-
69-
let extension = if let Ok(extension) = account.get_extension_mut::<MemoTransfer>() {
70-
extension
71-
} else {
72-
account.init_extension::<MemoTransfer>(true)?
73-
};
74-
extension.require_incoming_transfer_memos = false.into();
47+
extension.require_incoming_transfer_memos = enable.into();
7548
Ok(())
7649
}
7750

@@ -85,11 +58,11 @@ pub(crate) fn process_instruction(
8558
match decode_instruction_type(input)? {
8659
RequiredMemoTransfersInstruction::Enable => {
8760
msg!("RequiredMemoTransfersInstruction::Enable");
88-
process_enable_required_memo_transfers(program_id, accounts)
61+
process_toggle_required_memo_transfers(program_id, accounts, true /* enable */)
8962
}
9063
RequiredMemoTransfersInstruction::Disable => {
9164
msg!("RequiredMemoTransfersInstruction::Disable");
92-
process_diasble_required_memo_transfers(program_id, accounts)
65+
process_toggle_required_memo_transfers(program_id, accounts, false /* disable */)
9366
}
9467
}
9568
}

0 commit comments

Comments
 (0)