alloy_eip7928/
storage_change.rs

1//! Contains the [`StorageChange`] struct, which represents a single storage write operation within
2//! a transaction.
3
4use crate::BlockAccessIndex;
5use alloy_primitives::U256;
6
7/// Represents a single storage write operation within a transaction.
8#[derive(Debug, Clone, Default, PartialEq, Eq)]
9#[cfg_attr(feature = "rlp", derive(alloy_rlp::RlpEncodable, alloy_rlp::RlpDecodable))]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
12#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14pub struct StorageChange {
15    /// Index of the bal that stores the performed write.
16    #[cfg_attr(feature = "serde", serde(alias = "txIndex", with = "crate::quantity"))]
17    pub block_access_index: BlockAccessIndex,
18    /// The new value written to the storage slot.
19    #[cfg_attr(feature = "serde", serde(alias = "postValue"))]
20    pub new_value: U256,
21}
22
23impl StorageChange {
24    /// Creates a new `StorageChange`.
25    #[inline]
26    pub const fn new(block_access_index: BlockAccessIndex, new_value: U256) -> Self {
27        Self { block_access_index, new_value }
28    }
29
30    /// Returns true if the new value is zero.
31    #[inline]
32    pub fn is_zero(&self) -> bool {
33        self.new_value.is_zero()
34    }
35
36    /// Returns true if this change was made by the given transaction.
37    #[inline]
38    pub const fn is_from_tx(&self, block_index: BlockAccessIndex) -> bool {
39        self.block_access_index == block_index
40    }
41
42    /// Returns a copy with a different storage value.
43    #[inline]
44    pub const fn with_value(&self, value: U256) -> Self {
45        Self { block_access_index: self.block_access_index, new_value: value }
46    }
47}