|
| 1 | +use anchor_lang::AccountDeserialize; |
| 2 | +use anyhow::Result; |
| 3 | +use jito_priority_fee_distribution_sdk::PriorityFeeDistributionAccount; |
| 4 | +use jito_tip_distribution_sdk::TipDistributionAccount; |
| 5 | +use log::info; |
| 6 | +use solana_client::{ |
| 7 | + nonblocking::rpc_client::RpcClient, |
| 8 | + rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig}, |
| 9 | +}; |
| 10 | +use solana_sdk::pubkey::Pubkey; |
| 11 | + |
| 12 | +pub struct TipDistributionStats { |
| 13 | + pub account_pubkey: Pubkey, |
| 14 | + pub validator_vote_account: Pubkey, |
| 15 | + pub total_lamports: u64, |
| 16 | + pub is_priority_fee: bool, |
| 17 | + pub validator_commission_bps: u16, |
| 18 | +} |
| 19 | + |
| 20 | +pub async fn get_tip_distribution_stats( |
| 21 | + rpc_client: &RpcClient, |
| 22 | + tip_distribution_program_id: &Pubkey, |
| 23 | + priority_fee_distribution_program_id: &Pubkey, |
| 24 | + epoch: u64, |
| 25 | +) -> Result<()> { |
| 26 | + info!("Fetching tip distribution accounts for epoch {}...", epoch); |
| 27 | + |
| 28 | + let rpc_client_with_timeout = |
| 29 | + RpcClient::new_with_timeout(rpc_client.url(), std::time::Duration::from_secs(1800)); |
| 30 | + |
| 31 | + let tip_distribution_accounts = get_tip_distribution_accounts_for_epoch( |
| 32 | + &rpc_client_with_timeout, |
| 33 | + tip_distribution_program_id, |
| 34 | + epoch, |
| 35 | + ) |
| 36 | + .await?; |
| 37 | + |
| 38 | + let priority_fee_distribution_accounts = get_priority_fee_distribution_accounts_for_epoch( |
| 39 | + &rpc_client_with_timeout, |
| 40 | + priority_fee_distribution_program_id, |
| 41 | + epoch, |
| 42 | + ) |
| 43 | + .await?; |
| 44 | + |
| 45 | + info!( |
| 46 | + "Found {} tip distribution accounts and {} priority fee distribution accounts", |
| 47 | + tip_distribution_accounts.len(), |
| 48 | + priority_fee_distribution_accounts.len() |
| 49 | + ); |
| 50 | + |
| 51 | + let mut all_stats = Vec::new(); |
| 52 | + |
| 53 | + for (pubkey, account) in tip_distribution_accounts { |
| 54 | + let stats = |
| 55 | + process_tip_distribution_account(&rpc_client_with_timeout, &pubkey, &account, false) |
| 56 | + .await?; |
| 57 | + all_stats.push(stats); |
| 58 | + } |
| 59 | + |
| 60 | + for (pubkey, account) in priority_fee_distribution_accounts { |
| 61 | + let stats = process_priority_fee_distribution_account( |
| 62 | + &rpc_client_with_timeout, |
| 63 | + &pubkey, |
| 64 | + &account, |
| 65 | + true, |
| 66 | + ) |
| 67 | + .await?; |
| 68 | + all_stats.push(stats); |
| 69 | + } |
| 70 | + |
| 71 | + print_tip_distribution_summary(&all_stats, epoch); |
| 72 | + |
| 73 | + Ok(()) |
| 74 | +} |
| 75 | + |
| 76 | +async fn get_tip_distribution_accounts_for_epoch( |
| 77 | + rpc_client: &RpcClient, |
| 78 | + tip_distribution_program_id: &Pubkey, |
| 79 | + epoch: u64, |
| 80 | +) -> Result<Vec<(Pubkey, TipDistributionAccount)>> { |
| 81 | + let accounts = rpc_client |
| 82 | + .get_program_accounts_with_config( |
| 83 | + tip_distribution_program_id, |
| 84 | + RpcProgramAccountsConfig { |
| 85 | + filters: None, |
| 86 | + account_config: RpcAccountInfoConfig { |
| 87 | + encoding: Some(solana_account_decoder::UiAccountEncoding::Base64), |
| 88 | + ..RpcAccountInfoConfig::default() |
| 89 | + }, |
| 90 | + ..RpcProgramAccountsConfig::default() |
| 91 | + }, |
| 92 | + ) |
| 93 | + .await?; |
| 94 | + |
| 95 | + let mut result = Vec::new(); |
| 96 | + for (pubkey, account) in accounts { |
| 97 | + if let Ok(tip_distribution_account) = |
| 98 | + TipDistributionAccount::try_deserialize(&mut account.data.as_slice()) |
| 99 | + { |
| 100 | + if tip_distribution_account.epoch_created_at == epoch { |
| 101 | + result.push((pubkey, tip_distribution_account)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Ok(result) |
| 107 | +} |
| 108 | + |
| 109 | +async fn get_priority_fee_distribution_accounts_for_epoch( |
| 110 | + rpc_client: &RpcClient, |
| 111 | + priority_fee_distribution_program_id: &Pubkey, |
| 112 | + epoch: u64, |
| 113 | +) -> Result<Vec<(Pubkey, PriorityFeeDistributionAccount)>> { |
| 114 | + let accounts = rpc_client |
| 115 | + .get_program_accounts_with_config( |
| 116 | + priority_fee_distribution_program_id, |
| 117 | + RpcProgramAccountsConfig { |
| 118 | + filters: None, |
| 119 | + account_config: RpcAccountInfoConfig { |
| 120 | + encoding: Some(solana_account_decoder::UiAccountEncoding::Base64), |
| 121 | + ..RpcAccountInfoConfig::default() |
| 122 | + }, |
| 123 | + ..RpcProgramAccountsConfig::default() |
| 124 | + }, |
| 125 | + ) |
| 126 | + .await?; |
| 127 | + |
| 128 | + let mut result = Vec::new(); |
| 129 | + for (pubkey, account) in accounts { |
| 130 | + if let Ok(priority_fee_distribution_account) = |
| 131 | + PriorityFeeDistributionAccount::try_deserialize(&mut account.data.as_slice()) |
| 132 | + { |
| 133 | + if priority_fee_distribution_account.epoch_created_at == epoch { |
| 134 | + result.push((pubkey, priority_fee_distribution_account)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + Ok(result) |
| 140 | +} |
| 141 | + |
| 142 | +async fn process_tip_distribution_account( |
| 143 | + rpc_client: &RpcClient, |
| 144 | + account_pubkey: &Pubkey, |
| 145 | + tip_distribution_account: &TipDistributionAccount, |
| 146 | + is_priority_fee: bool, |
| 147 | +) -> Result<TipDistributionStats> { |
| 148 | + // Get the account data to calculate total lamports |
| 149 | + let account_info = rpc_client.get_account(account_pubkey).await?; |
| 150 | + let rent_exempt_amount = rpc_client |
| 151 | + .get_minimum_balance_for_rent_exemption(account_info.data.len()) |
| 152 | + .await?; |
| 153 | + let total_lamports = account_info.lamports.saturating_sub(rent_exempt_amount); |
| 154 | + |
| 155 | + Ok(TipDistributionStats { |
| 156 | + account_pubkey: *account_pubkey, |
| 157 | + validator_vote_account: tip_distribution_account.validator_vote_account, |
| 158 | + total_lamports, |
| 159 | + is_priority_fee, |
| 160 | + validator_commission_bps: tip_distribution_account.validator_commission_bps, |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +async fn process_priority_fee_distribution_account( |
| 165 | + rpc_client: &RpcClient, |
| 166 | + account_pubkey: &Pubkey, |
| 167 | + priority_fee_distribution_account: &PriorityFeeDistributionAccount, |
| 168 | + is_priority_fee: bool, |
| 169 | +) -> Result<TipDistributionStats> { |
| 170 | + // Get the account data to calculate total lamports |
| 171 | + let account_info = rpc_client.get_account(account_pubkey).await?; |
| 172 | + let rent_exempt_amount = rpc_client |
| 173 | + .get_minimum_balance_for_rent_exemption(account_info.data.len()) |
| 174 | + .await?; |
| 175 | + let total_lamports = account_info.lamports.saturating_sub(rent_exempt_amount); |
| 176 | + |
| 177 | + Ok(TipDistributionStats { |
| 178 | + account_pubkey: *account_pubkey, |
| 179 | + validator_vote_account: priority_fee_distribution_account.validator_vote_account, |
| 180 | + total_lamports, |
| 181 | + is_priority_fee, |
| 182 | + validator_commission_bps: priority_fee_distribution_account.validator_commission_bps, |
| 183 | + }) |
| 184 | +} |
| 185 | + |
| 186 | +fn print_tip_distribution_summary(stats: &[TipDistributionStats], epoch: u64) { |
| 187 | + info!("\n=== Epoch {} Tip Distribution Statistics ===", epoch); |
| 188 | + info!( |
| 189 | + "{:<50} {:<15} {:<10} {:<10}", |
| 190 | + "Account", "Total (SOL)", "Type", "Commission" |
| 191 | + ); |
| 192 | + info!("{:-<85}", ""); |
| 193 | + |
| 194 | + let mut total_total = 0u64; |
| 195 | + |
| 196 | + for stat in stats { |
| 197 | + let total_sol = stat.total_lamports as f64 / 1_000_000_000.0; |
| 198 | + let account_type = if stat.is_priority_fee { |
| 199 | + "Priority" |
| 200 | + } else { |
| 201 | + "Tip" |
| 202 | + }; |
| 203 | + let commission_pct = stat.validator_commission_bps as f64 / 100.0; |
| 204 | + |
| 205 | + info!( |
| 206 | + "{:<50} {:<15.6} {:<10} {:<10.2}", |
| 207 | + format!("{}", stat.account_pubkey), |
| 208 | + total_sol, |
| 209 | + account_type, |
| 210 | + commission_pct |
| 211 | + ); |
| 212 | + |
| 213 | + total_total += stat.total_lamports; |
| 214 | + } |
| 215 | + |
| 216 | + info!("{:-<85}", ""); |
| 217 | + let total_total_sol = total_total as f64 / 1_000_000_000.0; |
| 218 | + |
| 219 | + info!( |
| 220 | + "{:<50} {:<15.6} {:<10} {:<10}", |
| 221 | + "TOTAL", total_total_sol, "ALL", "" |
| 222 | + ); |
| 223 | +} |
0 commit comments