Skip to content

Commit 09cf665

Browse files
committed
add create_ixs and close account
1 parent 00d9918 commit 09cf665

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

src/instruction_builders.rs

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use solana_program::{
1111
use spl_associated_token_account::get_associated_token_address;
1212

1313
use crate::{
14-
get_seat_deposit_collector_address, get_seat_manager_address,
14+
get_authorized_delegate_pda, get_seat_deposit_collector_address, get_seat_manager_address,
1515
instruction::SeatManagerInstruction,
1616
};
1717

@@ -319,3 +319,99 @@ pub fn create_confirm_renounce_seat_manager_authority_instruction(
319319
data: SeatManagerInstruction::ConfirmRenounceSeatManagerAuthority.to_vec(),
320320
}
321321
}
322+
323+
pub fn create_add_approved_evictor_instruction(
324+
authority: &Pubkey,
325+
authorized_delegate: &Pubkey,
326+
) -> Instruction {
327+
let (authorized_delegate_pda, _) = get_authorized_delegate_pda(authority, authorized_delegate);
328+
Instruction {
329+
program_id: crate::id(),
330+
accounts: vec![
331+
AccountMeta::new(*authority, true),
332+
AccountMeta::new_readonly(*authorized_delegate, false),
333+
AccountMeta::new(authorized_delegate_pda, false),
334+
],
335+
data: SeatManagerInstruction::AddApprovedEvictor.to_vec(),
336+
}
337+
}
338+
339+
pub fn create_remove_approved_evictor_instruction(
340+
authority: &Pubkey,
341+
authorized_delegate: &Pubkey,
342+
) -> Instruction {
343+
let (authorized_delegate_pda, _) = get_authorized_delegate_pda(authority, authorized_delegate);
344+
Instruction {
345+
program_id: crate::id(),
346+
accounts: vec![
347+
AccountMeta::new(*authority, true),
348+
AccountMeta::new_readonly(*authorized_delegate, false),
349+
AccountMeta::new(authorized_delegate_pda, false),
350+
],
351+
data: SeatManagerInstruction::RemoveApprovedEvictor.to_vec(),
352+
}
353+
}
354+
355+
pub fn create_evict_seat_with_authorized_delegate_instruction(
356+
market: &Pubkey,
357+
base_mint: &Pubkey,
358+
quote_mint: &Pubkey,
359+
authorized_delegate: &Pubkey,
360+
traders: Vec<EvictTraderAccountBackup>,
361+
seat_manager_authority: &Pubkey,
362+
) -> Instruction {
363+
let (base_vault, _) = get_vault_address(market, base_mint);
364+
let (quote_vault, _) = get_vault_address(market, quote_mint);
365+
let (seat_manager, _) = get_seat_manager_address(market);
366+
let (seat_deposit_collector, _) = get_seat_deposit_collector_address(market);
367+
368+
let (authorized_delegate_pda, _) =
369+
get_authorized_delegate_pda(seat_manager_authority, authorized_delegate);
370+
371+
let mut accounts = vec![
372+
AccountMeta::new_readonly(phoenix::id(), false),
373+
AccountMeta::new_readonly(phoenix_log_authority::id(), false),
374+
AccountMeta::new(*market, false),
375+
AccountMeta::new_readonly(seat_manager, false),
376+
AccountMeta::new(seat_deposit_collector, false),
377+
AccountMeta::new_readonly(*base_mint, false),
378+
AccountMeta::new_readonly(*quote_mint, false),
379+
AccountMeta::new(base_vault, false),
380+
AccountMeta::new(quote_vault, false),
381+
AccountMeta::new_readonly(spl_associated_token_account::id(), false),
382+
AccountMeta::new_readonly(spl_token::id(), false),
383+
AccountMeta::new_readonly(system_program::id(), false),
384+
AccountMeta::new_readonly(*authorized_delegate, true),
385+
AccountMeta::new_readonly(authorized_delegate_pda, false),
386+
];
387+
388+
for trader_accounts in traders.iter() {
389+
let base_account = get_associated_token_address(&trader_accounts.trader_pubkey, base_mint);
390+
let quote_account =
391+
get_associated_token_address(&trader_accounts.trader_pubkey, quote_mint);
392+
let (seat, _) = get_seat_address(market, &trader_accounts.trader_pubkey);
393+
accounts.push(AccountMeta::new(trader_accounts.trader_pubkey, false));
394+
accounts.push(AccountMeta::new(seat, false));
395+
accounts.push(AccountMeta::new(base_account, false));
396+
accounts.push(AccountMeta::new(quote_account, false));
397+
398+
for backup_account in [
399+
trader_accounts.base_token_account_backup,
400+
trader_accounts.quote_token_account_backup,
401+
]
402+
.iter()
403+
{
404+
if backup_account.is_some() {
405+
accounts.push(AccountMeta::new(backup_account.unwrap(), false));
406+
} else {
407+
accounts.push(AccountMeta::new_readonly(Pubkey::default(), false));
408+
}
409+
}
410+
}
411+
412+
Instruction {
413+
program_id: crate::id(),
414+
accounts,
415+
data: SeatManagerInstruction::EvictSeatWithAuthorizedDelegate.to_vec(),
416+
}
417+
}

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ pub fn get_seat_manager_address(market: &Pubkey) -> (Pubkey, u8) {
7878
Pubkey::find_program_address(&[&market.to_bytes()], &crate::id())
7979
}
8080

81+
pub fn get_authorized_delegate_pda(
82+
seat_manager_authority: &Pubkey,
83+
authorized_delegate: &Pubkey,
84+
) -> (Pubkey, u8) {
85+
Pubkey::find_program_address(
86+
&[
87+
&seat_manager_authority.to_bytes(),
88+
&authorized_delegate.to_bytes(),
89+
],
90+
&crate::id(),
91+
)
92+
}
93+
8194
pub fn get_seat_deposit_collector_seeds(
8295
market: &Pubkey,
8396
seat_deposit_collector: &Pubkey,

src/processor/authorized_evictor.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use phoenix::program::{
2-
checkers::{Program, Signer, PDA},
2+
checkers::{Program, Signer},
33
system_utils::create_account,
44
};
55
use solana_program::{
@@ -37,7 +37,7 @@ pub fn process_authorized_evictor(
3737
authorized_delegate_pda_seeds.clone(),
3838
)?;
3939
} else {
40-
// TODO
40+
close_account(authorized_delegate_pda, seat_manager_authority.as_ref())?;
4141
}
4242

4343
Ok(())
@@ -76,3 +76,22 @@ pub fn get_authorized_delegate_seeds_and_validate(
7676
return Err(ProgramError::InvalidInstructionData.into());
7777
}
7878
}
79+
80+
pub fn close_account<'info>(
81+
info: &AccountInfo<'info>,
82+
sol_destination: &AccountInfo<'info>,
83+
) -> Result<(), ProgramError> {
84+
let dest_starting_lamports = sol_destination.lamports();
85+
86+
**sol_destination.lamports.borrow_mut() =
87+
dest_starting_lamports.checked_add(info.lamports()).unwrap();
88+
89+
**info.lamports.borrow_mut() = 0;
90+
91+
info.assign(&system_program::ID);
92+
info.realloc(0, false).map_err(Into::into)
93+
}
94+
95+
pub fn does_pda_exist(program_id: &Pubkey, pda_ai: &AccountInfo) -> bool {
96+
pda_ai.owner == program_id && pda_ai.lamports() != 0
97+
}

src/processor/evict_seat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use solana_program::{
2323
};
2424
use spl_associated_token_account::instruction::create_associated_token_account;
2525

26-
use super::get_authorized_delegate_seeds_and_validate;
26+
use super::{does_pda_exist, get_authorized_delegate_seeds_and_validate};
2727

2828
struct TraderAccountsContext<'a, 'info> {
2929
trader: &'a AccountInfo<'info>,
@@ -118,7 +118,7 @@ pub fn process_evict_seat(
118118
)?;
119119

120120
// If the PDA exists, then the signer is an authorized delegate
121-
is_authorized_delegate = authorized_delegate_pda.lamports() > 0;
121+
is_authorized_delegate = does_pda_exist(program_id, authorized_delegate_pda);
122122
}
123123

124124
// Get market parameters to perform checks

0 commit comments

Comments
 (0)