data_anchor_client/fees/
microlamports.rs

1use super::Lamports;
2
3/// 10^-6 lamports, only used for prioritization fee calculations.
4#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
5pub struct MicroLamports(pub(crate) u64);
6
7impl MicroLamports {
8    /// Zero micro-lamports.
9    pub const ZERO: Self = MicroLamports(0);
10    /// Minimum recommended fee for a transaction. Based on https://docs.helius.dev/solana-apis/priority-fee-api#helius-priority-fee-api
11    pub const MIN: Self = MicroLamports(10_000);
12
13    /// Create an instance of `MicroLamports` from a given value.
14    pub fn new(value: u64) -> Self {
15        MicroLamports(value)
16    }
17
18    /// Extracts the inner value.
19    pub fn into_inner(self) -> u64 {
20        self.0
21    }
22
23    /// Multiplies the inner value by the given value, returning `None` if the result would overflow.
24    pub fn checked_mul(&self, rhs: u64) -> Option<Self> {
25        self.0.checked_mul(rhs).map(MicroLamports)
26    }
27
28    /// Divides the inner value by the given value, returning `None` if `rhs` == 0.
29    pub fn checked_div(&self, rhs: u64) -> Option<Self> {
30        self.0.checked_div(rhs).map(MicroLamports)
31    }
32
33    /// Divides the inner value from the given value, returning `None` if the result would underflow.
34    pub fn checked_div_self(&self, rhs: Self) -> Option<u64> {
35        self.0.checked_div(rhs.0)
36    }
37
38    /// Adds the inner value to the given value, returning `None` if the result would overflow.
39    pub fn checked_add(&self, rhs: Self) -> Option<Self> {
40        self.0.checked_add(rhs.0).map(MicroLamports)
41    }
42
43    /// Subtracts the given value from the inner value, returning `None` if the result would underflow.
44    pub fn checked_sub(&self, rhs: Self) -> Option<Self> {
45        self.0.checked_sub(rhs.0).map(MicroLamports)
46    }
47}
48
49impl From<Lamports> for MicroLamports {
50    fn from(value: Lamports) -> Self {
51        // Can't overflow because MicroLamports is u64 and Lamports is u32.
52        MicroLamports(value.0 as u64 * 1_000_000)
53    }
54}