Skip to content

Commit

Permalink
chore: fix some clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
LevBeta committed Jun 28, 2024
1 parent 07ab1a9 commit 6a6aff3
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 61 deletions.
16 changes: 10 additions & 6 deletions src/cli/entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use crate::{
rebalancer::Rebalancer,
transaction_manager::{BatchTransactions, TransactionManager},
};
use log::info;
use log::{error, info};
use std::collections::HashMap;
use yellowstone_grpc_client::GeyserGrpcClient;

pub async fn run_liquidator(config: Eva01Config) -> anyhow::Result<()> {
info!("Starting eva01 liquidator!");
Expand Down Expand Up @@ -56,22 +55,27 @@ pub async fn run_liquidator(config: Eva01Config) -> anyhow::Result<()> {
}

tokio::task::spawn(async move {
GeyserService::connect(
if let Err(e) = GeyserService::connect(
config.general_config.get_geyser_service_config(),
accounts_to_track,
config.general_config.marginfi_program_id,
liquidator_tx,
rebalancer_tx,
)
.await;
.await
{
error!("Failed to connect to geyser service: {:?}", e);
}
});

tokio::task::spawn(async move {
let _ = transaction_manager.start().await;
transaction_manager.start().await;
});

tokio::task::spawn(async move {
let _ = rebalancer.start().await;
if let Err(e) = rebalancer.start().await {
error!("Failed to start rebalancer: {:?}", e);
}
});

liquidator.start().await?;
Expand Down
21 changes: 4 additions & 17 deletions src/geyser.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::utils::account_update_to_account;
use crossbeam::channel::Sender;
use futures::channel::mpsc::SendError;
use futures::{SinkExt, StreamExt};
use futures::StreamExt;
use log::{error, info};
use marginfi::state::marginfi_account::MarginfiAccount;
use solana_program::pubkey::Pubkey;
use solana_sdk::account::Account;
use std::{collections::HashMap, mem::size_of};
use yellowstone_grpc_client::{GeyserGrpcClient, GeyserGrpcClientError};
use yellowstone_grpc_client::GeyserGrpcClient;
use yellowstone_grpc_proto::prelude::*;

const MARGIN_ACCOUNT_SIZE: usize = size_of::<MarginfiAccount>() + 8;
Expand Down Expand Up @@ -66,7 +65,7 @@ impl GeyserService {
let sub_req =
Self::build_geyser_subscribe_request(&tracked_accounts_vec, &marginfi_program_id);

let (subscribe_tx, mut stream) = client.subscribe_with_request(Some(sub_req)).await?;
let (_, mut stream) = client.subscribe_with_request(Some(sub_req)).await?;

while let Some(msg) = stream.next().await {
match msg {
Expand All @@ -78,7 +77,7 @@ impl GeyserService {
{
if let Ok(account) = account_update_to_account(update_account) {
if let Ok(account_owner_pk) =
Pubkey::try_from(account.owner.clone())
Pubkey::try_from(account.owner)
{
if account_owner_pk == marginfi_program_id
&& update_account.data.len() == MARGIN_ACCOUNT_SIZE
Expand Down Expand Up @@ -179,15 +178,3 @@ impl GeyserService {
request
}
}

#[derive(Debug, thiserror::Error)]
pub enum GeyserServiceError {
#[error("Generic error")]
GenericError,
#[error("Geyser client error: {0}")]
GeyserServiceError(#[from] GeyserGrpcClientError),
#[error("Error parsing account: {0}")]
AnyhowError(#[from] anyhow::Error),
#[error("Error sending message to geyser: {0}")]
SendError(#[from] SendError),
}
1 change: 0 additions & 1 deletion src/liquidator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ impl Liquidator {
&account.asset_bank,
&account.liab_bank,
account.asset_amount,
self.general_config.get_tx_config(),
&account.banks,
)
.await
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod liquidator;
mod rebalancer;

/// Wrappers around marginfi structs
#[warn(clippy::type_complexity)]
mod wrappers;

/// Utilities used by Eva01
Expand Down
17 changes: 4 additions & 13 deletions src/rebalancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ use marginfi::{
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_sdk::{
account_info::IntoAccountInfo,
commitment_config::CommitmentConfig,
signature::read_keypair_file,
transaction::{self, VersionedTransaction},
account_info::IntoAccountInfo, commitment_config::CommitmentConfig,
signature::read_keypair_file, transaction::VersionedTransaction,
};
use std::{
cmp::min,
Expand Down Expand Up @@ -353,7 +351,6 @@ impl Rebalancer {
.unwrap(),
withdraw_amount.to_num(),
Some(withdraw_all),
self.general_config.get_tx_config(),
&self.banks,
)?;

Expand Down Expand Up @@ -391,7 +388,6 @@ impl Rebalancer {
.unwrap(),
token_balance.to_num(),
Some(repay_all),
self.general_config.get_tx_config(),
)?;

Ok(())
Expand All @@ -416,12 +412,8 @@ impl Rebalancer {
.get_address_for_mint(bank.bank.mint)
.unwrap();

self.liquidator_account.deposit(
bank,
token_address,
balance.to_num(),
self.general_config.get_tx_config(),
)?;
self.liquidator_account
.deposit(bank, token_address, balance.to_num())?;

Ok(())
}
Expand Down Expand Up @@ -521,7 +513,6 @@ impl Rebalancer {
.unwrap(),
amount,
Some(withdrawl_all),
self.general_config.get_tx_config(),
&self.banks,
)?;

Expand Down
4 changes: 2 additions & 2 deletions src/sender.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{config::GeneralConfig, wrappers::marginfi_account::TxConfig};
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_sdk::signature::{read_keypair_file, Signature};
use solana_sdk::signature::Signature;
use solana_sdk::{
commitment_config::CommitmentConfig,
compute_budget::ComputeBudgetInstruction,
Expand Down
1 change: 1 addition & 0 deletions src/token_account_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl TokenAccountManager {
}
}

#[allow(dead_code)]
fn get_liquidator_seed(signer: Pubkey, mint: Pubkey, seed: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();

Expand Down
9 changes: 5 additions & 4 deletions src/transaction_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::config::GeneralConfig;
use crossbeam::channel::Receiver;
use futures::stream::FuturesUnordered;
use jito_protos::searcher::{
searcher_service_client::SearcherServiceClient, GetTipAccountsRequest,
NextScheduledLeaderRequest, SubscribeBundleResultsRequest,
Expand Down Expand Up @@ -33,6 +32,7 @@ const LEADERSHIP_THRESHOLD: u64 = 2;
const SLEEP_DURATION: std::time::Duration = std::time::Duration::from_millis(500);

/// Manages transactions for the liquidator and rebalancer
#[allow(dead_code)]
pub struct TransactionManager {
rx: Receiver<BatchTransactions>,
keypair: Keypair,
Expand Down Expand Up @@ -94,7 +94,9 @@ impl TransactionManager {
pub async fn start(&mut self) {
for instructions in self.rx.iter() {
for instructions in instructions {
self.send_agressive_tx(instructions);
if let Err(e) = self.send_agressive_tx(instructions) {
error!("Failed to send transaction: {:?}", e);
}
}
}
}
Expand All @@ -119,7 +121,7 @@ impl TransactionManager {
)
.await
{
return Err(anyhow::anyhow!("Failed to send transaction"));
return Err(anyhow::anyhow!("Failed to send transaction: {:?}", e));
}

Ok(())
Expand Down Expand Up @@ -209,7 +211,6 @@ impl TransactionManager {
self.is_jito_leader
.store(num_slots <= LEADERSHIP_THRESHOLD, Ordering::Relaxed);
}
Ok(())
}

async fn get_tip_accounts(&mut self) -> anyhow::Result<Vec<String>> {
Expand Down
3 changes: 1 addition & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ impl<'a> BankAccountWithPriceFeedEva<'a> {
.filter(|balance| balance.active);

active_balances
.enumerate()
.map(move |(_, balance)| {
.map(move |balance| {
let bank = banks
.get(&balance.bank_pk)
.ok_or_else(|| anyhow::anyhow!("Bank not found"))?
Expand Down
16 changes: 2 additions & 14 deletions src/wrappers/liquidator_account.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
use super::{
bank::BankWrapper,
marginfi_account::{MarginfiAccountWrapper, TxConfig},
};
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, TransactionManager},
transaction_manager::BatchTransactions,
};
use crossbeam::channel::Sender;
use log::info;
use marginfi::state::{marginfi_account::MarginfiAccount, marginfi_group::BankVaultType};
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_sdk::{
signature::{read_keypair_file, Keypair},
signer::Signer,
transaction,
};
use std::{collections::HashMap, sync::Arc};

/// Wraps the liquidator account into a dedicated strecture
pub struct LiquidatorAccount {
pub account_wrapper: MarginfiAccountWrapper,
pub signer_keypair: Arc<Keypair>,
rpc_client: Arc<RpcClient>,
program_id: Pubkey,
token_program: Pubkey,
group: Pubkey,
Expand Down Expand Up @@ -52,7 +45,6 @@ impl LiquidatorAccount {
Ok(Self {
account_wrapper,
signer_keypair,
rpc_client: Arc::new(rpc_client),
program_id,
token_program,
group,
Expand All @@ -66,7 +58,6 @@ impl LiquidatorAccount {
asset_bank: &BankWrapper,
liab_bank: &BankWrapper,
asset_amount: u64,
send_cfg: TxConfig,
banks: &HashMap<Pubkey, BankWrapper>,
) -> anyhow::Result<()> {
let liquidator_account_address = self.account_wrapper.address;
Expand Down Expand Up @@ -122,7 +113,6 @@ impl LiquidatorAccount {
token_account: Pubkey,
amount: u64,
withdraw_all: Option<bool>,
send_cfg: TxConfig,
banks: &HashMap<Pubkey, BankWrapper>,
) -> anyhow::Result<()> {
let marginfi_account = self.account_wrapper.address;
Expand Down Expand Up @@ -171,7 +161,6 @@ impl LiquidatorAccount {
token_account: &Pubkey,
amount: u64,
repay_all: Option<bool>,
send_cfg: TxConfig,
) -> anyhow::Result<()> {
let marginfi_account = self.account_wrapper.address;

Expand Down Expand Up @@ -201,7 +190,6 @@ impl LiquidatorAccount {
bank: &BankWrapper,
token_account: Pubkey,
amount: u64,
send_cfg: TxConfig,
) -> anyhow::Result<()> {
let marginfi_account = self.account_wrapper.address;

Expand Down
4 changes: 2 additions & 2 deletions src/wrappers/marginfi_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl MarginfiAccountWrapper {
.balances
.iter()
.filter(|b| matches!(b.get_side(), Some(BalanceSide::Liabilities)) && b.active)
.filter_map(|b| Some((b.liability_shares.into(), b.bank_pk)))
.map(|b| (b.liability_shares.into(), b.bank_pk))
.collect::<Vec<_>>()
}

Expand Down Expand Up @@ -103,7 +103,7 @@ impl MarginfiAccountWrapper {
.balances
.iter()
.filter(|b| matches!(b.get_side(), Some(BalanceSide::Assets)) & b.active)
.filter_map(|b| Some((b.asset_shares.into(), b.bank_pk)))
.map(|b| (b.asset_shares.into(), b.bank_pk))
.collect::<Vec<_>>()
}

Expand Down

0 comments on commit 6a6aff3

Please sign in to comment.