Skip to content

Commit

Permalink
fix: various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
losman0s committed Sep 6, 2024
1 parent 57ac2a1 commit 5555d83
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
17 changes: 13 additions & 4 deletions src/liquidator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use marginfi::{
constants::EXP_10_I80F48,
state::{
marginfi_account::{BalanceSide, MarginfiAccount, RequirementType},
marginfi_group::{Bank, RiskTier},
marginfi_group::{Bank, BankOperationalState, RiskTier},
price::{
OraclePriceFeedAdapter, OraclePriceType, OracleSetup, PriceBias,
SwitchboardPullPriceFeed,
Expand Down Expand Up @@ -492,9 +492,11 @@ impl Liquidator {
RequirementType::Maintenance,
)?;

debug!("Account {:?}", account.address);
debug!("Health {:?}", maintenance_health);
debug!("Liquidator profit {:?}", liquidator_profit);
if liquidator_profit > self.config.min_profit {
debug!("Account {:?}", account.address);
debug!("Health {:?}", maintenance_health);
debug!("Liquidator profit {:?}", liquidator_profit);
}

Ok((max_liquidatable_asset_amount, liquidator_profit))
}
Expand Down Expand Up @@ -780,6 +782,13 @@ impl Liquidator {
continue;
}

if !matches!(
bank.bank.config.operational_state,
BankOperationalState::Operational
) {
continue;
}

let value = match balance_side {
BalanceSide::Liabilities => bank
.calc_value(share.0, BalanceSide::Liabilities, requirement_type)
Expand Down
10 changes: 8 additions & 2 deletions src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::wrappers::marginfi_account::TxConfig;
use log::{error, info};
use serde::Deserialize;
use solana_client::rpc_client::{RpcClient, SerializableTransaction};
use solana_client::rpc_config::RpcSimulateTransactionConfig;
use solana_client::rpc_config::{RpcSendTransactionConfig, RpcSimulateTransactionConfig};
use solana_sdk::signature::Signature;
use solana_sdk::{
commitment_config::CommitmentConfig,
Expand Down Expand Up @@ -165,7 +165,13 @@ impl TransactionSender {
}

(0..cfg.spam_times).try_for_each(|_| {
rpc.send_transaction(transaction)?;
rpc.send_transaction_with_config(
transaction,
RpcSendTransactionConfig {
skip_preflight: true,
..Default::default()
},
)?;
Ok::<_, Box<dyn Error>>(())
})?;

Expand Down
31 changes: 12 additions & 19 deletions src/token_account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,20 @@ use std::{
sync::{Arc, RwLock},
};

use anchor_lang::accounts::program;
use anchor_spl::associated_token;
use crossbeam::epoch::Owned;
use log::{debug, error, info};
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
use sha2::{Digest, Sha256};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
account::Account,
pubkey::Pubkey,
signature::Keypair,
signer::{SeedDerivable, Signer},
};
use solana_sdk::{account::Account, pubkey::Pubkey, signature::Keypair, signer::Signer};

use crate::{
sender::{SenderCfg, TransactionSender},
utils::{batch_get_multiple_accounts, BatchLoadingConfig},
};

const TOKEN_ACCOUNT_SEED: &[u8] = b"liquidator_ta";
const MAX_INIT_TA_IXS: usize = 4;
// const TOKEN_ACCOUNT_SEED: &[u8] = b"liquidator_ta";
const MAX_INIT_TA_IXS: usize = 10;

#[derive(Debug, Clone, thiserror::Error)]
pub enum TokenAccountManagerError {
Expand Down Expand Up @@ -230,15 +223,15 @@ fn get_liquidator_seed(signer: Pubkey, mint: Pubkey, seed: &[u8]) -> [u8; 32] {
hasher.finalize().into()
}

fn get_keypair_for_token_account(
signer: Pubkey,
mint: Pubkey,
seed: &[u8],
) -> Result<Keypair, TokenAccountManagerError> {
let keypair_seed = get_liquidator_seed(signer, mint, seed);
Keypair::from_seed(&keypair_seed)
.map_err(|_| TokenAccountManagerError::SetupFailed("Keypair::from_seed failed"))
}
// fn get_keypair_for_token_account(
// signer: Pubkey,
// mint: Pubkey,
// seed: &[u8],
// ) -> Result<Keypair, TokenAccountManagerError> {
// let keypair_seed = get_liquidator_seed(signer, mint, seed);
// Keypair::from_seed(&keypair_seed)
// .map_err(|_| TokenAccountManagerError::SetupFailed("Keypair::from_seed failed"))
// }

fn get_address_for_token_account(
signer: Pubkey,
Expand Down
22 changes: 11 additions & 11 deletions src/wrappers/liquidator_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ use super::{bank::BankWrapper, marginfi_account::MarginfiAccountWrapper};
use crate::{
config::GeneralConfig,
marginfi_ixs::{make_deposit_ix, make_liquidate_ix, make_repay_ix, make_withdraw_ix},
sender::{SenderCfg, TransactionSender},
transaction_manager::{BatchTransactions, RawTransaction},
};
use crossbeam::channel::Sender;
use marginfi::state::{marginfi_account::MarginfiAccount, marginfi_group::BankVaultType};
use rayon::vec;
use solana_client::{
nonblocking::rpc_client::RpcClient as NonBlockingRpcClient, rpc_client::RpcClient,
};
use solana_program::pubkey::Pubkey;
use solana_sdk::{
compute_budget::ComputeBudgetInstruction,
signature::{read_keypair_file, Keypair},
signer::Signer,
};
Expand Down Expand Up @@ -140,8 +137,8 @@ impl LiquidatorAccount {
})
.collect::<Vec<_>>();

let (crank_ix, crank_lut) = if !observation_swb_oracles.is_empty() {
if let Ok(crank_data) = PullFeed::fetch_update_many_ix(
let crank_data = if !observation_swb_oracles.is_empty() {
if let Ok((ix, luts)) = PullFeed::fetch_update_many_ix(
&self.non_blocking_rpc_client,
FetchUpdateManyParams {
feeds: observation_swb_oracles,
Expand All @@ -153,12 +150,12 @@ impl LiquidatorAccount {
)
.await
{
crank_data
Some((ix, luts))
} else {
return Err(anyhow::anyhow!("Failed to fetch crank data"));
}
} else {
return Err(anyhow::anyhow!("No crank data available"));
None
};

let liquidate_ix = make_liquidate_ix(
Expand All @@ -181,10 +178,13 @@ impl LiquidatorAccount {
asset_amount,
);

self.transaction_tx.send(vec![
RawTransaction::new(vec![crank_ix]).with_lookup_tables(crank_lut),
RawTransaction::new(vec![liquidate_ix]),
])?;
let mut bundle = vec![];
if let Some((crank_ix, crank_lut)) = crank_data {
bundle.push(RawTransaction::new(vec![crank_ix]).with_lookup_tables(crank_lut));
}
bundle.push(RawTransaction::new(vec![liquidate_ix]));

self.transaction_tx.send(bundle)?;

Ok(())
}
Expand Down

0 comments on commit 5555d83

Please sign in to comment.