gmsol_solana_utils/make_bundle_builder/
estimate_fee.rs

1use std::ops::Deref;
2
3use crate::bundle_builder::{BundleBuilder, BundleOptions};
4use solana_sdk::signer::Signer;
5
6use super::MakeBundleBuilder;
7
8/// Estimate Execution Fee.
9pub struct EstimateFee<T> {
10    builder: T,
11    compute_unit_price_micro_lamports: Option<u64>,
12}
13
14impl<T> EstimateFee<T> {
15    /// Estiamte fee before building the transaction.
16    pub fn new(builder: T, compute_unit_price_micro_lamports: Option<u64>) -> Self {
17        Self {
18            builder,
19            compute_unit_price_micro_lamports,
20        }
21    }
22}
23
24/// Set Execution Fee.
25pub trait SetExecutionFee {
26    /// Whether the execution fee needed to be estiamted.
27    fn is_execution_fee_estimation_required(&self) -> bool {
28        true
29    }
30
31    /// Set execution fee.
32    fn set_execution_fee(&mut self, lamports: u64) -> &mut Self;
33}
34
35impl<'a, C: Deref<Target = impl Signer> + Clone, T> MakeBundleBuilder<'a, C> for EstimateFee<T>
36where
37    T: SetExecutionFee,
38    T: MakeBundleBuilder<'a, C>,
39{
40    async fn build_with_options(
41        &mut self,
42        options: BundleOptions,
43    ) -> crate::Result<BundleBuilder<'a, C>> {
44        let mut tx = self.builder.build_with_options(options.clone()).await?;
45
46        if self.builder.is_execution_fee_estimation_required() {
47            let lamports = tx
48                .estimate_execution_fee(self.compute_unit_price_micro_lamports)
49                .await?;
50            self.builder.set_execution_fee(lamports);
51            tracing::info!(%lamports, "execution fee estimated");
52            tx = self.builder.build_with_options(options).await?;
53        }
54
55        Ok(tx)
56    }
57}