Skip to main content

cbe_sdk/
fee.rs

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