solomka_sdk/
fee.rs

1use crate::native_token::sol_to_lamports;
2
3/// A fee and its associated compute unit limit
4#[derive(Debug, Default, Clone)]
5pub struct FeeBin {
6    /// maximum compute units for which this fee will be charged
7    pub limit: u64,
8    /// fee in lamports
9    pub fee: u64,
10}
11
12/// Information used to calculate fees
13#[derive(Debug, Clone)]
14pub struct FeeStructure {
15    /// lamports per signature
16    pub lamports_per_signature: u64,
17    /// lamports_per_write_lock
18    pub lamports_per_write_lock: u64,
19    /// Compute unit fee bins
20    pub compute_fee_bins: Vec<FeeBin>,
21}
22
23impl FeeStructure {
24    pub fn new(
25        sol_per_signature: f64,
26        sol_per_write_lock: f64,
27        compute_fee_bins: Vec<(u64, f64)>,
28    ) -> Self {
29        let compute_fee_bins = compute_fee_bins
30            .iter()
31            .map(|(limit, sol)| FeeBin {
32                limit: *limit,
33                fee: sol_to_lamports(*sol),
34            })
35            .collect::<Vec<_>>();
36        FeeStructure {
37            lamports_per_signature: sol_to_lamports(sol_per_signature),
38            lamports_per_write_lock: sol_to_lamports(sol_per_write_lock),
39            compute_fee_bins,
40        }
41    }
42
43    pub fn get_max_fee(&self, num_signatures: u64, num_write_locks: u64) -> u64 {
44        num_signatures
45            .saturating_mul(self.lamports_per_signature)
46            .saturating_add(num_write_locks.saturating_mul(self.lamports_per_write_lock))
47            .saturating_add(
48                self.compute_fee_bins
49                    .last()
50                    .map(|bin| bin.fee)
51                    .unwrap_or_default(),
52            )
53    }
54}
55
56impl Default for FeeStructure {
57    fn default() -> Self {
58        Self::new(0.000005, 0.0, vec![(1_400_000, 0.0)])
59    }
60}
61
62#[cfg(RUSTC_WITH_SPECIALIZATION)]
63impl ::solana_frozen_abi::abi_example::AbiExample for FeeStructure {
64    fn example() -> Self {
65        FeeStructure::default()
66    }
67}