Skip to content

Commit

Permalink
Fix: Liquidator crank
Browse files Browse the repository at this point in the history
  • Loading branch information
LevBeta committed Sep 5, 2024
1 parent ba587aa commit 9ce77d6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
18 changes: 3 additions & 15 deletions src/rebalancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ impl Rebalancer {
transaction_tx.clone(),
general_config.clone(),
)
.await
.unwrap();
.await?;

let preferred_mints = config.preferred_mints.iter().cloned().collect();

Expand Down Expand Up @@ -307,23 +306,12 @@ impl Rebalancer {
.collect();

if !active_swb_oracles.is_empty() {
let client = NonBlockingRpcClient::new(self.general_config.rpc_url.clone());
// Gateway creation is giving some issues without loading a queue first
// This method doesn't make sense, but it's a workaround for now
// Tho, it's not a good idea to load a queue for every rebalance like this
let queue = QueueAccountData::load(
&client,
&Pubkey::from_str("A43DyUGA7s8eXPxqEjJY6EBu1KKbNgfxF8h17VAHn13w")?,
)
.await?;
let gw = &queue.fetch_gateways(&client).await?[0];

if let Ok(crank_data) = PullFeed::fetch_update_many_ix(
&client,
&self.liquidator_account.non_blocking_rpc_client,
FetchUpdateManyParams {
feeds: active_swb_oracles,
payer: self.general_config.signer_pubkey,
gateway: gw.clone(),
gateway: self.liquidator_account.swb_gateway.clone(),
num_signatures: Some(1),
..Default::default()
},
Expand Down
68 changes: 65 additions & 3 deletions src/wrappers/liquidator_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use crate::{
};
use crossbeam::channel::Sender;
use marginfi::state::{marginfi_account::MarginfiAccount, marginfi_group::BankVaultType};
use solana_client::rpc_client::RpcClient;
use solana_client::{
nonblocking::rpc_client::RpcClient as NonBlockingRpcClient, rpc_client::RpcClient,
};
use solana_program::pubkey::Pubkey;
use solana_sdk::{
signature::{read_keypair_file, Keypair},
signer::Signer,
};
use std::{collections::HashMap, sync::Arc};
use std::{collections::HashMap, str::FromStr, sync::Arc};
use switchboard_on_demand_client::{FetchUpdateManyParams, Gateway, PullFeed, QueueAccountData};

/// Wraps the liquidator account into a dedicated strecture
pub struct LiquidatorAccount {
Expand All @@ -22,6 +25,8 @@ pub struct LiquidatorAccount {
token_program_per_mint: HashMap<Pubkey, Pubkey>,
group: Pubkey,
pub transaction_tx: Sender<BatchTransactions>,
pub swb_gateway: Gateway,
pub non_blocking_rpc_client: NonBlockingRpcClient,
}

impl LiquidatorAccount {
Expand All @@ -38,13 +43,29 @@ impl LiquidatorAccount {
let account_wrapper = MarginfiAccountWrapper::new(liquidator_pubkey, *marginfi_account);
let group = account_wrapper.account.group;

let non_blocking_rpc_client = NonBlockingRpcClient::new(config.rpc_url.clone());

let queue = QueueAccountData::load(
&non_blocking_rpc_client,
&Pubkey::from_str("A43DyUGA7s8eXPxqEjJY6EBu1KKbNgfxF8h17VAHn13w").unwrap(),
)
.await
.unwrap();
let swb_gateway = queue
.fetch_gateways(&non_blocking_rpc_client)
.await
.unwrap()[0]
.clone();

Ok(Self {
account_wrapper,
signer_keypair,
program_id: config.marginfi_program_id,
group,
transaction_tx,
token_program_per_mint: HashMap::new(),
swb_gateway,
non_blocking_rpc_client,
})
}

Expand Down Expand Up @@ -97,6 +118,46 @@ impl LiquidatorAccount {
let liquidatee_observation_accounts =
liquidate_account.get_observation_accounts(&[], &[], banks);

let joined_observation_accounts = liquidator_observation_accounts
.iter()
.chain(liquidatee_observation_accounts.iter())
.cloned()
.collect::<Vec<_>>();

let observation_swb_oracles = joined_observation_accounts
.iter()
.filter_map(|&pk| {
banks.get(&pk).and_then(|bank| {
if bank.oracle_adapter.is_switchboard_pull() {
Some(bank.oracle_adapter.address)
} else {
None
}
})
})
.collect::<Vec<_>>();

let crank_ix = if !observation_swb_oracles.is_empty() {
if let Ok(crank_data) = PullFeed::fetch_update_many_ix(
&self.non_blocking_rpc_client,
FetchUpdateManyParams {
feeds: observation_swb_oracles,
payer: self.signer_keypair.pubkey(),
gateway: self.swb_gateway.clone(),
num_signatures: Some(1),
..Default::default()
},
)
.await
{
crank_data.0
} else {
return Err(anyhow::anyhow!("Failed to fetch crank data"));
}
} else {
return Err(anyhow::anyhow!("No crank data available"));
};

let liquidate_ix = make_liquidate_ix(
self.program_id,
self.group,
Expand All @@ -118,7 +179,8 @@ impl LiquidatorAccount {
);

// Double vec implies that is a single bundle
self.transaction_tx.send(vec![vec![liquidate_ix]])?;
self.transaction_tx
.send(vec![vec![crank_ix, liquidate_ix]])?;

Ok(())
}
Expand Down

0 comments on commit 9ce77d6

Please sign in to comment.