Skip to main content

solana_sysvar/
fees.rs

1//! Current cluster fees.
2//!
3//! The _fees sysvar_ provides access to the [`Fees`] type, which contains the
4//! current [`FeeCalculator`].
5//!
6//! [`Fees`] implements [`crate::Sysvar::get`] and can be loaded efficiently without
7//! passing the sysvar account ID to the program.
8//!
9//! This sysvar is deprecated and will not be available in the future.
10//! Transaction fees should be determined with the [`getFeeForMessage`] RPC
11//! method. For additional context see the [Comprehensive Compute Fees
12//! proposal][ccf].
13//!
14//! [`getFeeForMessage`]: https://solana.com/docs/rpc/http/getfeeformessage
15//! [ccf]: https://docs.solanalabs.com/proposals/comprehensive-compute-fees
16//!
17//! See also the Solana [documentation on the fees sysvar][sdoc].
18//!
19//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#fees
20
21#![allow(deprecated)]
22
23#[cfg(feature = "serde")]
24use serde_derive::{Deserialize, Serialize};
25pub use solana_sdk_ids::sysvar::fees::{check_id, id, ID};
26#[cfg(target_os = "solana")]
27use {solana_define_syscall::definitions, solana_program_entrypoint::SUCCESS};
28use {
29    solana_fee_calculator::FeeCalculator, solana_get_sysvar::GetSysvar,
30    solana_sdk_macro::CloneZeroed, solana_sysvar_id::impl_deprecated_sysvar_id,
31};
32
33impl_deprecated_sysvar_id!(Fees);
34
35/// Transaction fees.
36#[deprecated(
37    since = "1.9.0",
38    note = "Please do not use, will no longer be available in the future"
39)]
40#[repr(C)]
41#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
42#[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
43#[derive(Debug, CloneZeroed, Default, PartialEq, Eq)]
44pub struct Fees {
45    pub fee_calculator: FeeCalculator,
46}
47
48/// Serialized size of `Fees` sysvar account.
49pub const SIZE: usize = size_of::<Fees>();
50const _: () = assert!(SIZE == 8);
51
52impl Fees {
53    pub fn new(fee_calculator: &FeeCalculator) -> Self {
54        #[allow(deprecated)]
55        Self {
56            fee_calculator: *fee_calculator,
57        }
58    }
59}
60
61// DEPRECATED: This impl is only for the deprecated Fees sysvar and should be
62// removed once Fees is no longer in use. It uses the old-style direct syscall
63// approach instead of the new sol_get_sysvar syscall.
64impl GetSysvar for Fees {
65    fn get() -> Result<Self, solana_program_error::ProgramError> {
66        #[cfg(target_os = "solana")]
67        {
68            let mut fees = Self::default();
69            let fees_addr = &mut fees as *mut _ as *mut u8;
70            let result = unsafe { definitions::sol_get_fees_sysvar(fees_addr) };
71
72            match result {
73                SUCCESS => Ok(fees),
74                _ => Err(solana_program_error::ProgramError::UnsupportedSysvar),
75            }
76        }
77
78        #[cfg(not(target_os = "solana"))]
79        {
80            Err(solana_program_error::ProgramError::UnsupportedSysvar)
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_size_of() {
91        assert_eq!(
92            wincode::serialized_size(&Fees::default()).unwrap() as usize,
93            SIZE,
94        );
95    }
96
97    #[test]
98    fn test_clone() {
99        let fees = Fees {
100            fee_calculator: FeeCalculator {
101                lamports_per_signature: 1,
102            },
103        };
104        let cloned_fees = fees.clone();
105        assert_eq!(cloned_fees, fees);
106    }
107}