Skip to content

Commit

Permalink
Merge pull request #13 from mrgnlabs/j/t22
Browse files Browse the repository at this point in the history
fix: t22
  • Loading branch information
LevBeta authored Jul 1, 2024
2 parents 6d76cb7 + 9baabc0 commit be1ff53
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/liquidator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl Liquidator {
let rpc_client = Arc::new(RpcClient::new(self.general_config.rpc_url.clone()));
self.load_marginfi_accounts(rpc_client.clone()).await?;
self.load_oracles_and_banks(rpc_client.clone()).await?;
self.liquidator_account
.load_initial_data(rpc_client.as_ref(), self.get_all_mints())
.await?;
Ok(())
}

Expand Down Expand Up @@ -665,4 +668,11 @@ impl Liquidator {

values
}

fn get_all_mints(&self) -> Vec<Pubkey> {
self.banks
.values()
.map(|bank| bank.bank.mint)
.collect::<Vec<_>>()
}
}
63 changes: 43 additions & 20 deletions src/marginfi_ixs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anchor_lang::{system_program, InstructionData, Key, ToAccountMetas};

use anchor_spl::token_2022;
use log::trace;
use solana_sdk::instruction::AccountMeta;
use solana_sdk::{
Expand Down Expand Up @@ -38,20 +39,25 @@ pub fn make_deposit_ix(
signer_token_account: Pubkey,
bank_liquidity_vault: Pubkey,
token_program: Pubkey,
mint: Pubkey,
amount: u64,
) -> Instruction {
let mut accounts = marginfi::accounts::LendingAccountDeposit {
marginfi_group,
marginfi_account,
signer,
bank,
signer_token_account,
bank_liquidity_vault,
token_program,
}
.to_account_metas(Some(true));

maybe_add_bank_mint(&mut accounts, mint, &token_program);

Instruction {
program_id: marginfi_program_id,
accounts: marginfi::accounts::LendingAccountDeposit {
marginfi_group,
marginfi_account,
signer,
bank,
signer_token_account,
bank_liquidity_vault,
token_program,
}
.to_account_metas(Some(true)),
accounts,
data: marginfi::instruction::LendingAccountDeposit { amount }.data(),
}
}
Expand All @@ -65,21 +71,26 @@ pub fn make_repay_ix(
signer_token_account: Pubkey,
bank_liquidity_vault: Pubkey,
token_program: Pubkey,
mint: Pubkey,
amount: u64,
repay_all: Option<bool>,
) -> Instruction {
let mut accounts = marginfi::accounts::LendingAccountRepay {
marginfi_group,
marginfi_account,
signer,
bank,
signer_token_account,
bank_liquidity_vault,
token_program,
}
.to_account_metas(Some(true));

maybe_add_bank_mint(&mut accounts, mint, &token_program);

Instruction {
program_id: marginfi_program_id,
accounts: marginfi::accounts::LendingAccountRepay {
marginfi_group,
marginfi_account,
signer,
bank,
signer_token_account,
bank_liquidity_vault,
token_program,
}
.to_account_metas(Some(true)),
accounts,
data: marginfi::instruction::LendingAccountRepay { amount, repay_all }.data(),
}
}
Expand All @@ -95,6 +106,7 @@ pub fn make_withdraw_ix(
bank_liquidity_vault: Pubkey,
token_program: Pubkey,
observation_accounts: Vec<Pubkey>,
mint: Pubkey,
amount: u64,
withdraw_all: Option<bool>,
) -> Instruction {
Expand All @@ -110,6 +122,8 @@ pub fn make_withdraw_ix(
}
.to_account_metas(Some(true));

maybe_add_bank_mint(&mut accounts, mint, &token_program);

trace!(
"make_withdraw_ix: observation_accounts: {:?}",
observation_accounts
Expand Down Expand Up @@ -148,6 +162,7 @@ pub fn make_liquidate_ix(
liquidatee_observation_accounts: Vec<Pubkey>,
asset_bank_oracle: Pubkey,
liab_bank_oracle: Pubkey,
liab_mint: Pubkey,
asset_amount: u64,
) -> Instruction {
let mut accounts = marginfi::accounts::LendingAccountLiquidate {
Expand All @@ -164,6 +179,8 @@ pub fn make_liquidate_ix(
}
.to_account_metas(Some(true));

maybe_add_bank_mint(&mut accounts, liab_mint, &token_program);

accounts.extend([
AccountMeta::new_readonly(asset_bank_oracle, false),
AccountMeta::new_readonly(liab_bank_oracle, false),
Expand All @@ -187,3 +204,9 @@ pub fn make_liquidate_ix(
data: marginfi::instruction::LendingAccountLiquidate { asset_amount }.data(),
}
}

fn maybe_add_bank_mint(accounts: &mut Vec<AccountMeta>, mint: Pubkey, token_program: &Pubkey) {
if token_program == &token_2022::ID {
accounts.push(AccountMeta::new_readonly(mint, false));
}
}
48 changes: 39 additions & 9 deletions src/wrappers/liquidator_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct LiquidatorAccount {
pub account_wrapper: MarginfiAccountWrapper,
pub signer_keypair: Arc<Keypair>,
program_id: Pubkey,
token_program: Pubkey,
token_program_per_mint: HashMap<Pubkey, Pubkey>,
group: Pubkey,
transaction_tx: Sender<BatchTransactions>,
}
Expand All @@ -34,24 +34,40 @@ impl LiquidatorAccount {
let signer_keypair = Arc::new(read_keypair_file(&config.keypair_path).unwrap());

let account = rpc_client.get_account(&liquidator_pubkey)?;

let marginfi_account = bytemuck::from_bytes::<MarginfiAccount>(&account.data[8..]);

let account_wrapper = MarginfiAccountWrapper::new(liquidator_pubkey, *marginfi_account);

let program_id = marginfi::id();
let token_program = spl_token::id();
let group = account_wrapper.account.group;

Ok(Self {
account_wrapper,
signer_keypair,
program_id,
token_program,
group,
transaction_tx,
token_program_per_mint: HashMap::new(),
})
}

pub async fn load_initial_data(
&mut self,
rpc_client: &RpcClient,
mints: Vec<Pubkey>,
) -> anyhow::Result<()> {
let token_program_per_mint = rpc_client
.get_multiple_accounts(&mints)
.unwrap()
.iter()
.zip(mints)
.map(|(account, mint)| (mint, account.as_ref().unwrap().owner))
.collect();

self.token_program_per_mint = token_program_per_mint;

Ok(())
}

pub async fn liquidate(
&mut self,
liquidate_account: &MarginfiAccountWrapper,
Expand All @@ -63,6 +79,7 @@ impl LiquidatorAccount {
let liquidator_account_address = self.account_wrapper.address;
let liquidatee_account_address = liquidate_account.address;
let signer_pk = self.signer_keypair.pubkey();
let liab_mint = liab_bank.bank.mint;

let (bank_liquidaity_vault_authority, _) = crate::utils::find_bank_vault_authority_pda(
&liab_bank.address,
Expand Down Expand Up @@ -93,11 +110,12 @@ impl LiquidatorAccount {
bank_liquidaity_vault_authority,
bank_liquidaity_vault,
bank_insurante_vault,
self.token_program,
self.token_program_per_mint.get(&liab_mint).unwrap().clone(),
liquidator_observation_accounts,
liquidatee_observation_accounts,
asset_bank.bank.config.oracle_keys[0],
liab_bank.bank.config.oracle_keys[0],
liab_mint,
asset_amount,
);

Expand Down Expand Up @@ -129,6 +147,9 @@ impl LiquidatorAccount {
self.account_wrapper
.get_observation_accounts(&[], &banks_to_exclude, banks);

let mint = bank.bank.mint;
let token_program = self.token_program_per_mint.get(&mint).unwrap().clone();

let withdraw_ix = make_withdraw_ix(
self.program_id,
self.group,
Expand All @@ -143,8 +164,9 @@ impl LiquidatorAccount {
)
.0,
bank.bank.liquidity_vault,
self.token_program,
token_program,
observation_accounts,
mint,
amount,
withdraw_all,
);
Expand All @@ -166,6 +188,9 @@ impl LiquidatorAccount {

let signer_pk = self.signer_keypair.pubkey();

let mint = bank.bank.mint;
let token_program = self.token_program_per_mint.get(&mint).unwrap().clone();

let repay_ix = make_repay_ix(
self.program_id,
self.group,
Expand All @@ -174,7 +199,8 @@ impl LiquidatorAccount {
bank.address,
*token_account,
bank.bank.liquidity_vault,
self.token_program,
token_program,
mint,
amount,
repay_all,
);
Expand All @@ -195,6 +221,9 @@ impl LiquidatorAccount {

let signer_pk = self.signer_keypair.pubkey();

let mint = bank.bank.mint;
let token_program = self.token_program_per_mint.get(&mint).unwrap().clone();

let deposit_ix = make_deposit_ix(
self.program_id,
self.group,
Expand All @@ -203,7 +232,8 @@ impl LiquidatorAccount {
bank.address,
token_account,
bank.bank.liquidity_vault,
self.token_program,
token_program,
mint,
amount,
);

Expand Down

0 comments on commit be1ff53

Please sign in to comment.