data_anchor_client/fees/
fee_strategy.rs

1use solana_client::nonblocking::rpc_client::RpcClient;
2use solana_sdk::{
3    compute_budget::ComputeBudgetInstruction, instruction::Instruction, pubkey::Pubkey,
4};
5
6use super::{Fee, Priority};
7use crate::BloberClientResult;
8
9/// The strategy to use for calculating the fees for transactions.
10#[derive(Debug, Clone, Copy)]
11pub enum FeeStrategy {
12    /// Use a fixed fee for all transactions.
13    Fixed(Fee),
14    /// Calculate a reasonable fee based on the recent fees in the network and a given priority.
15    BasedOnRecentFees(Priority),
16}
17
18impl Default for FeeStrategy {
19    fn default() -> Self {
20        Self::BasedOnRecentFees(Priority::default())
21    }
22}
23
24impl FeeStrategy {
25    /// Creates a transaction for setting the compute unit price for a transaction based on recent prioritization fees.
26    ///
27    /// # Arguments
28    /// - `client`: The RPC client to use for looking up recent prioritization fees.
29    /// - `mutable_accounts`: The addresses of the accounts that are mutable in the transaction (and thus need exclusive locks).
30    pub async fn set_compute_unit_price(
31        &self,
32        client: &RpcClient,
33        mutable_accounts: &[Pubkey],
34        use_helius: bool,
35    ) -> BloberClientResult<Instruction> {
36        let compute_unit_price = match self {
37            Self::Fixed(fee) => fee.prioritization_fee_rate,
38            Self::BasedOnRecentFees(priority) => {
39                priority
40                    .get_priority_fee_estimate(client, mutable_accounts, use_helius)
41                    .await?
42            }
43        };
44        Ok(ComputeBudgetInstruction::set_compute_unit_price(
45            compute_unit_price.0,
46        ))
47    }
48}