Skip to content

Commit

Permalink
solana: simplify remove logic
Browse files Browse the repository at this point in the history
  • Loading branch information
a5-pickle committed Aug 3, 2023
1 parent f2ff015 commit 4d91dc7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@ pub struct RemoveGuardian<'info> {
pub fn remove_guardian(ctx: Context<RemoveGuardian>) -> Result<()> {
let guardians: &mut Vec<_> = &mut ctx.accounts.guardians;
let removed = ctx.accounts.guardian.key();
match guardians.iter().position(|&guardian| guardian == removed) {
Some(index) => {
// Remove pubkey to guardians account.
guardians.swap_remove(index);

// Update config.
ctx.accounts.config.num_guardians -= 1;
// It is safe to unwrap because the key we are removing is guaranteed to exist since there is
// a guardian info account for it.
let index = guardians
.iter()
.position(|&guardian| guardian == removed)
.unwrap();

emit!(crate::event::GuardianRemoved { guardian: removed });
// Remove pubkey to guardians account.
guardians.swap_remove(index);

Ok(())
}
None => err!(TbtcError::GuardianNonexistent),
}
// Update config.
ctx.accounts.config.num_guardians -= 1;

emit!(crate::event::GuardianRemoved { guardian: removed });

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ pub struct RemoveMinter<'info> {
pub fn remove_minter(ctx: Context<RemoveMinter>) -> Result<()> {
let minters: &mut Vec<_> = &mut ctx.accounts.minters;
let removed = ctx.accounts.minter.key();
match minters.iter().position(|&minter| minter == removed) {
Some(index) => {
// Remove pubkey to minters account.
minters.swap_remove(index);

// Update config.
ctx.accounts.config.num_minters -= 1;
// It is safe to unwrap because the key we are removing is guaranteed to exist since there is
// a minter info account for it.
let index = minters
.iter()
.position(|&minter| minter == removed)
.unwrap();

emit!(crate::event::MinterRemoved { minter: removed });
// Remove pubkey to minters account.
minters.swap_remove(index);

Ok(())
}
None => err!(TbtcError::GuardianNonexistent),
}
// Update config.
ctx.accounts.config.num_minters -= 1;

emit!(crate::event::MinterRemoved { minter: removed });

Ok(())
}

0 comments on commit 4d91dc7

Please sign in to comment.