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 = "bincode")]
24use crate::SysvarSerialize;
25#[cfg(feature = "serde")]
26use serde_derive::{Deserialize, Serialize};
27pub use solana_sdk_ids::sysvar::fees::{check_id, id, ID};
28#[cfg(target_os = "solana")]
29use {solana_define_syscall::definitions, solana_program_entrypoint::SUCCESS};
30use {
31    solana_fee_calculator::FeeCalculator, solana_get_sysvar::GetSysvar,
32    solana_sdk_macro::CloneZeroed, solana_sysvar_id::impl_deprecated_sysvar_id,
33};
34
35impl_deprecated_sysvar_id!(Fees);
36
37/// Transaction fees.
38#[deprecated(
39    since = "1.9.0",
40    note = "Please do not use, will no longer be available in the future"
41)]
42#[repr(C)]
43#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
44#[cfg_attr(feature = "wincode", derive(wincode::SchemaWrite, wincode::SchemaRead))]
45#[derive(Debug, CloneZeroed, Default, PartialEq, Eq)]
46pub struct Fees {
47    pub fee_calculator: FeeCalculator,
48}
49
50/// Serialized size of `Fees` sysvar account.
51pub const SIZE: usize = size_of::<Fees>();
52const _: () = assert!(SIZE == 8);
53
54impl Fees {
55    pub fn new(fee_calculator: &FeeCalculator) -> Self {
56        #[allow(deprecated)]
57        Self {
58            fee_calculator: *fee_calculator,
59        }
60    }
61}
62
63// DEPRECATED: This impl is only for the deprecated Fees sysvar and should be
64// removed once Fees is no longer in use. It uses the old-style direct syscall
65// approach instead of the new sol_get_sysvar syscall.
66impl GetSysvar for Fees {
67    fn get() -> Result<Self, solana_program_error::ProgramError> {
68        #[cfg(target_os = "solana")]
69        {
70            let mut fees = Self::default();
71            let fees_addr = &mut fees as *mut _ as *mut u8;
72            let result = unsafe { definitions::sol_get_fees_sysvar(fees_addr) };
73
74            match result {
75                SUCCESS => Ok(fees),
76                _ => Err(solana_program_error::ProgramError::UnsupportedSysvar),
77            }
78        }
79
80        #[cfg(not(target_os = "solana"))]
81        {
82            Err(solana_program_error::ProgramError::UnsupportedSysvar)
83        }
84    }
85}
86
87#[cfg(feature = "bincode")]
88impl SysvarSerialize for Fees {}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn test_size_of() {
96        assert_eq!(
97            wincode::serialized_size(&Fees::default()).unwrap() as usize,
98            SIZE,
99        );
100    }
101
102    #[test]
103    fn test_clone() {
104        let fees = Fees {
105            fee_calculator: FeeCalculator {
106                lamports_per_signature: 1,
107            },
108        };
109        let cloned_fees = fees.clone();
110        assert_eq!(cloned_fees, fees);
111    }
112}