Skip to main content

aztec_core/
fee.rs

1use serde::{Deserialize, Serialize};
2
3/// Gas consumption broken down by DA and L2 components.
4#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct Gas {
7    /// Data availability gas consumed.
8    pub da_gas: u64,
9    /// L2 execution gas consumed.
10    pub l2_gas: u64,
11}
12
13/// Per-unit gas fee prices.
14#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct GasFees {
17    /// Fee per unit of DA gas.
18    pub fee_per_da_gas: u128,
19    /// Fee per unit of L2 gas.
20    pub fee_per_l2_gas: u128,
21}
22
23/// Gas limits and fee caps for a transaction.
24#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct GasSettings {
27    /// Maximum gas allowed for the main execution phase.
28    pub gas_limits: Option<Gas>,
29    /// Maximum gas allowed for the teardown phase.
30    pub teardown_gas_limits: Option<Gas>,
31    /// Maximum fee per gas unit the sender is willing to pay.
32    pub max_fee_per_gas: Option<GasFees>,
33    /// Maximum priority fee per gas unit (tip).
34    pub max_priority_fee_per_gas: Option<GasFees>,
35}
36
37impl Default for GasSettings {
38    /// Returns sensible defaults matching the TS `GasSettings.default()`.
39    fn default() -> Self {
40        use crate::constants::*;
41        Self {
42            gas_limits: Some(Gas {
43                da_gas: DEFAULT_DA_GAS_LIMIT,
44                l2_gas: DEFAULT_L2_GAS_LIMIT,
45            }),
46            teardown_gas_limits: Some(Gas {
47                da_gas: DEFAULT_TEARDOWN_DA_GAS_LIMIT,
48                l2_gas: DEFAULT_TEARDOWN_L2_GAS_LIMIT,
49            }),
50            max_fee_per_gas: Some(GasFees {
51                fee_per_da_gas: 1,
52                fee_per_l2_gas: 1,
53            }),
54            max_priority_fee_per_gas: Some(GasFees::default()),
55        }
56    }
57}
58
59impl Gas {
60    pub fn new(da_gas: u64, l2_gas: u64) -> Self {
61        Self { da_gas, l2_gas }
62    }
63
64    pub fn empty() -> Self {
65        Self::default()
66    }
67
68    pub fn add(&self, other: &Gas) -> Gas {
69        Gas {
70            da_gas: self.da_gas + other.da_gas,
71            l2_gas: self.l2_gas + other.l2_gas,
72        }
73    }
74}
75
76#[cfg(test)]
77#[allow(clippy::panic)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn gas_settings_default_has_sensible_values() {
83        let settings = GasSettings::default();
84        let gl = settings.gas_limits.unwrap();
85        assert_eq!(gl.da_gas, crate::constants::DEFAULT_DA_GAS_LIMIT);
86        assert_eq!(gl.l2_gas, crate::constants::DEFAULT_L2_GAS_LIMIT);
87        assert!(settings.teardown_gas_limits.is_some());
88        assert!(settings.max_fee_per_gas.is_some());
89    }
90
91    #[test]
92    fn gas_settings_roundtrip() {
93        let settings = GasSettings {
94            gas_limits: Some(Gas {
95                da_gas: 1,
96                l2_gas: 2,
97            }),
98            teardown_gas_limits: Some(Gas {
99                da_gas: 3,
100                l2_gas: 4,
101            }),
102            max_fee_per_gas: Some(GasFees {
103                fee_per_da_gas: 5,
104                fee_per_l2_gas: 6,
105            }),
106            max_priority_fee_per_gas: None,
107        };
108
109        let json = match serde_json::to_string(&settings) {
110            Ok(json) => json,
111            Err(err) => panic!("serializing GasSettings should succeed: {err}"),
112        };
113        let decoded: GasSettings = match serde_json::from_str(&json) {
114            Ok(decoded) => decoded,
115            Err(err) => panic!("deserializing GasSettings should succeed: {err}"),
116        };
117        assert_eq!(decoded, settings);
118    }
119}