Skip to main content

alloy_eip7928/
balance_change.rs

1//! Contains the [`BalanceChange`] struct, which represents a post balance for an account.
2//! Single balance change: `tx_index` -> `post_balance`
3
4use crate::BlockAccessIndex;
5use alloy_primitives::U256;
6
7/// This struct is used to track the balance changes of accounts in a block.
8#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
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 BalanceChange {
15    /// The index of bal that stores balance change.
16    #[cfg_attr(
17        feature = "serde",
18        serde(rename = "index", alias = "blockAccessIndex", alias = "txIndex")
19    )]
20    pub block_access_index: BlockAccessIndex,
21    /// The post-transaction balance of the account.
22    #[cfg_attr(feature = "serde", serde(rename = "value", alias = "postBalance"))]
23    pub post_balance: U256,
24}
25
26impl BalanceChange {
27    /// Creates a new [`BalanceChange`].
28    pub const fn new(block_access_index: BlockAccessIndex, post_balance: U256) -> Self {
29        Self { block_access_index, post_balance }
30    }
31
32    /// Returns the bal index.
33    #[inline]
34    pub const fn block_access_index(&self) -> BlockAccessIndex {
35        self.block_access_index
36    }
37
38    /// Returns the post-transaction balance.
39    #[inline]
40    pub const fn post_balance(&self) -> U256 {
41        self.post_balance
42    }
43}