1use crate::{eip4844, eip4844::DATA_GAS_PER_BLOB, eip7691};
4
5#[cfg(feature = "serde")]
7const DEFAULT_BLOB_FEE_GETTER: fn() -> u128 = || eip4844::BLOB_TX_MIN_BLOB_GASPRICE;
8#[cfg(feature = "serde")]
9const IS_DEFAULT_BLOB_FEE: fn(&u128) -> bool = |&x| x == eip4844::BLOB_TX_MIN_BLOB_GASPRICE;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct BlobParams {
15 #[cfg_attr(feature = "serde", serde(rename = "target"))]
17 pub target_blob_count: u64,
18 #[cfg_attr(feature = "serde", serde(rename = "max"))]
20 pub max_blob_count: u64,
21 #[cfg_attr(feature = "serde", serde(rename = "baseFeeUpdateFraction"))]
23 pub update_fraction: u128,
24 #[cfg_attr(
29 feature = "serde",
30 serde(default = "DEFAULT_BLOB_FEE_GETTER", skip_serializing_if = "IS_DEFAULT_BLOB_FEE")
31 )]
32 pub min_blob_fee: u128,
33}
34
35impl BlobParams {
36 pub const fn cancun() -> Self {
38 Self {
39 target_blob_count: eip4844::TARGET_BLOBS_PER_BLOCK,
40 max_blob_count: eip4844::MAX_BLOBS_PER_BLOCK as u64,
41 update_fraction: eip4844::BLOB_GASPRICE_UPDATE_FRACTION,
42 min_blob_fee: eip4844::BLOB_TX_MIN_BLOB_GASPRICE,
43 }
44 }
45
46 pub const fn prague() -> Self {
48 Self {
49 target_blob_count: eip7691::TARGET_BLOBS_PER_BLOCK_ELECTRA,
50 max_blob_count: eip7691::MAX_BLOBS_PER_BLOCK_ELECTRA,
51 update_fraction: eip7691::BLOB_GASPRICE_UPDATE_FRACTION_PECTRA,
52 min_blob_fee: eip4844::BLOB_TX_MIN_BLOB_GASPRICE,
53 }
54 }
55
56 pub const fn max_blob_gas_per_block(&self) -> u64 {
60 self.max_blob_count * DATA_GAS_PER_BLOB
61 }
62
63 pub const fn target_blob_gas_per_block(&self) -> u64 {
67 self.target_blob_count * DATA_GAS_PER_BLOB
68 }
69
70 #[inline]
73 pub const fn next_block_excess_blob_gas(
74 &self,
75 excess_blob_gas: u64,
76 blob_gas_used: u64,
77 ) -> u64 {
78 (excess_blob_gas + blob_gas_used)
79 .saturating_sub(eip4844::DATA_GAS_PER_BLOB * self.target_blob_count)
80 }
81
82 #[inline]
84 pub const fn calc_blob_fee(&self, excess_blob_gas: u64) -> u128 {
85 eip4844::fake_exponential(self.min_blob_fee, excess_blob_gas as u128, self.update_fraction)
86 }
87}