-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* evict helper start * more impl * evict * evict * test fix * second tree implementation * tests pass * double sized global * comments * comment * fmt * scoping * change sizes * constant * deflake tests * log evictee balance * test change * test fix * reduce max seats for test * Do not crash when not found * move check * fmt
- Loading branch information
Showing
18 changed files
with
913 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
programs/manifest/src/program/instruction_builders/global_evict_instruction.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::{ | ||
program::{global_deposit::GlobalDepositParams, ManifestInstruction}, | ||
validation::{get_global_address, get_global_vault_address}, | ||
}; | ||
use borsh::BorshSerialize; | ||
use solana_program::{ | ||
instruction::{AccountMeta, Instruction}, | ||
pubkey::Pubkey, | ||
}; | ||
|
||
pub fn global_evict_instruction( | ||
mint: &Pubkey, | ||
payer: &Pubkey, | ||
trader_token_account: &Pubkey, | ||
evictee_token_account: &Pubkey, | ||
token_program: &Pubkey, | ||
num_atoms: u64, | ||
) -> Instruction { | ||
let (global, _global_bump) = get_global_address(mint); | ||
let (global_vault, _global_vault_bump) = get_global_vault_address(mint); | ||
Instruction { | ||
program_id: crate::id(), | ||
accounts: vec![ | ||
AccountMeta::new(*payer, true), | ||
AccountMeta::new(global, false), | ||
AccountMeta::new_readonly(*mint, false), | ||
AccountMeta::new(global_vault, false), | ||
AccountMeta::new(*trader_token_account, false), | ||
AccountMeta::new(*evictee_token_account, false), | ||
AccountMeta::new_readonly(*token_program, false), | ||
], | ||
data: [ | ||
ManifestInstruction::GlobalEvict.to_vec(), | ||
GlobalDepositParams::new(num_atoms).try_to_vec().unwrap(), | ||
] | ||
.concat(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
programs/manifest/src/program/processor/global_evict.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
use std::cell::RefMut; | ||
|
||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke, pubkey::Pubkey, | ||
}; | ||
|
||
use crate::{ | ||
global_vault_seeds_with_bump, | ||
logs::{emit_stack, GlobalDepositLog, GlobalEvictLog, GlobalWithdrawLog}, | ||
program::{get_mut_dynamic_account, ManifestError}, | ||
quantities::{GlobalAtoms, WrapperU64}, | ||
require, | ||
state::GlobalRefMut, | ||
validation::{get_global_vault_address, loaders::GlobalEvictContext}, | ||
}; | ||
use solana_program::program::invoke_signed; | ||
|
||
#[derive(BorshDeserialize, BorshSerialize)] | ||
pub struct GlobalEvictParams { | ||
// Deposit amount that must be greater than the evictee deposit amount | ||
amount_atoms: u64, | ||
} | ||
|
||
impl GlobalEvictParams { | ||
pub fn new(amount_atoms: u64) -> Self { | ||
GlobalEvictParams { amount_atoms } | ||
} | ||
} | ||
|
||
pub(crate) fn process_global_evict( | ||
_program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
data: &[u8], | ||
) -> ProgramResult { | ||
let global_evict_context: GlobalEvictContext = GlobalEvictContext::load(accounts)?; | ||
let GlobalEvictParams { amount_atoms } = GlobalEvictParams::try_from_slice(data)?; | ||
|
||
let GlobalEvictContext { | ||
payer, | ||
global, | ||
mint, | ||
global_vault, | ||
trader_token, | ||
evictee_token, | ||
token_program, | ||
} = global_evict_context; | ||
|
||
// 1. Withdraw for the evictee | ||
// 2. Evict the seat on the global account and claim | ||
// 3. Deposit for the evictor | ||
let global_data: &mut RefMut<&mut [u8]> = &mut global.try_borrow_mut_data()?; | ||
let mut global_dynamic_account: GlobalRefMut = get_mut_dynamic_account(global_data); | ||
let evictee_balance: GlobalAtoms = | ||
global_dynamic_account.get_balance_atoms(&evictee_token.get_owner()); | ||
|
||
{ | ||
// Do verifications that this is a valid eviction. | ||
require!( | ||
evictee_balance < GlobalAtoms::new(amount_atoms), | ||
ManifestError::InvalidEvict, | ||
"Evictee balance {} is more than evictor wants to deposit", | ||
evictee_balance.as_u64(), | ||
)?; | ||
global_dynamic_account.verify_min_balance(&evictee_token.get_owner())?; | ||
} | ||
|
||
// Withdraw | ||
{ | ||
let evictee_balance: GlobalAtoms = | ||
global_dynamic_account.get_balance_atoms(&evictee_token.get_owner()); | ||
global_dynamic_account.withdraw_global(&evictee_token.get_owner(), evictee_balance)?; | ||
|
||
let (_, bump) = get_global_vault_address(mint.info.key); | ||
|
||
// Do the token transfer | ||
if *global_vault.owner == spl_token_2022::id() { | ||
invoke_signed( | ||
&spl_token_2022::instruction::transfer_checked( | ||
token_program.key, | ||
global_vault.key, | ||
mint.info.key, | ||
evictee_token.key, | ||
global_vault.key, | ||
&[], | ||
evictee_balance.into(), | ||
mint.mint.decimals, | ||
)?, | ||
&[ | ||
token_program.as_ref().clone(), | ||
evictee_token.as_ref().clone(), | ||
mint.as_ref().clone(), | ||
global_vault.as_ref().clone(), | ||
], | ||
global_vault_seeds_with_bump!(mint.info.key, bump), | ||
)?; | ||
} else { | ||
invoke_signed( | ||
&spl_token::instruction::transfer( | ||
token_program.key, | ||
global_vault.key, | ||
evictee_token.key, | ||
global_vault.key, | ||
&[], | ||
evictee_balance.into(), | ||
)?, | ||
&[ | ||
token_program.as_ref().clone(), | ||
global_vault.as_ref().clone(), | ||
evictee_token.as_ref().clone(), | ||
], | ||
global_vault_seeds_with_bump!(mint.info.key, bump), | ||
)?; | ||
} | ||
|
||
emit_stack(GlobalWithdrawLog { | ||
global: *global.key, | ||
trader: *payer.key, | ||
global_atoms: GlobalAtoms::new(amount_atoms), | ||
})?; | ||
} | ||
|
||
// Evict | ||
{ | ||
global_dynamic_account | ||
.evict_and_take_seat(&evictee_token.get_owner(), &trader_token.get_owner())?; | ||
|
||
emit_stack(GlobalEvictLog { | ||
evictee: evictee_token.get_owner(), | ||
evictor: trader_token.get_owner(), | ||
evictor_atoms: GlobalAtoms::new(amount_atoms), | ||
evictee_atoms: evictee_balance, | ||
})?; | ||
} | ||
|
||
// Deposit | ||
{ | ||
global_dynamic_account.deposit_global(payer.key, GlobalAtoms::new(amount_atoms))?; | ||
|
||
// Do the token transfer | ||
if *global_vault.owner == spl_token_2022::id() { | ||
invoke( | ||
&spl_token_2022::instruction::transfer_checked( | ||
token_program.key, | ||
trader_token.key, | ||
mint.info.key, | ||
global_vault.key, | ||
payer.key, | ||
&[], | ||
amount_atoms, | ||
mint.mint.decimals, | ||
)?, | ||
&[ | ||
token_program.as_ref().clone(), | ||
trader_token.as_ref().clone(), | ||
mint.as_ref().clone(), | ||
global_vault.as_ref().clone(), | ||
payer.as_ref().clone(), | ||
], | ||
)?; | ||
} else { | ||
invoke( | ||
&spl_token::instruction::transfer( | ||
token_program.key, | ||
trader_token.key, | ||
global_vault.key, | ||
payer.key, | ||
&[], | ||
amount_atoms, | ||
)?, | ||
&[ | ||
token_program.as_ref().clone(), | ||
trader_token.as_ref().clone(), | ||
global_vault.as_ref().clone(), | ||
payer.as_ref().clone(), | ||
], | ||
)?; | ||
} | ||
|
||
emit_stack(GlobalDepositLog { | ||
global: *global.key, | ||
trader: *payer.key, | ||
global_atoms: GlobalAtoms::new(amount_atoms), | ||
})?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.