stratus/eth/primitives/
account.rs

1use display_json::DebugAsJson;
2use revm::primitives::KECCAK_EMPTY;
3use revm::state::Bytecode;
4
5use crate::alias::RevmAccountInfo;
6use crate::alias::RevmAddress;
7use crate::eth::primitives::Address;
8use crate::eth::primitives::Nonce;
9use crate::eth::primitives::Wei;
10
11/// Ethereum account (wallet or contract).
12///
13/// TODO: group bytecode, code_hash, static_slot_indexes and mapping_slot_indexes into a single bytecode struct.
14#[derive(DebugAsJson, Clone, Default, PartialEq, Eq, fake::Dummy, serde::Deserialize, serde::Serialize)]
15pub struct Account {
16    /// Immutable address of the account.
17    pub address: Address,
18
19    /// Current nonce of the account. Changes every time a transaction is sent.
20    pub nonce: Nonce,
21
22    /// Current balance of the account. Changes when a transfer is made or the account pays a fee for executing a transaction.
23    pub balance: Wei,
24
25    /// Contract bytecode. Present only if the account is a contract.
26    #[dummy(default)]
27    pub bytecode: Option<Bytecode>,
28}
29
30impl Account {
31    /// Creates a new empty account.
32    pub fn new_empty(address: Address) -> Self {
33        Self::new_with_balance(address, Wei::ZERO)
34    }
35
36    /// Creates a new account with initial balance.
37    pub fn new_with_balance(address: Address, balance: Wei) -> Self {
38        Self {
39            address,
40            nonce: Nonce::ZERO,
41            balance,
42            bytecode: None,
43        }
44    }
45}
46
47// -----------------------------------------------------------------------------
48// Conversions: Other -> Self
49// -----------------------------------------------------------------------------
50impl From<(RevmAddress, RevmAccountInfo)> for Account {
51    fn from(value: (RevmAddress, RevmAccountInfo)) -> Self {
52        let (address, info) = value;
53
54        Self {
55            address: address.into(),
56            nonce: info.nonce.into(),
57            balance: info.balance.into(),
58            bytecode: info.code,
59        }
60    }
61}
62
63// -----------------------------------------------------------------------------
64// Conversions: Self -> Other
65// -----------------------------------------------------------------------------
66
67impl From<&Account> for RevmAccountInfo {
68    fn from(value: &Account) -> Self {
69        Self {
70            nonce: value.nonce.into(),
71            balance: value.balance.into(),
72            code_hash: KECCAK_EMPTY,
73            code: value.bytecode.as_ref().cloned(),
74        }
75    }
76}
77
78// -----------------------------------------------------------------------------
79// Utilities
80// -----------------------------------------------------------------------------
81
82/// Accounts to be used only in development-mode.
83pub fn test_accounts() -> Vec<Account> {
84    use hex_literal::hex;
85    [
86        hex!("f39fd6e51aad88f6f4ce6ab8827279cfffb92266"), // ALICE
87        hex!("70997970c51812dc3a010c7d01b50e0d17dc79c8"), // BOB
88        hex!("3c44cdddb6a900fa2b585dd299e03d12fa4293bc"), // CHARLIE
89        hex!("15d34aaf54267db7d7c367839aaf71a00a2c6a65"), // DAVE
90        hex!("9965507d1a55bcc2695c58ba16fb37d819b0a4dc"), // EVE
91        hex!("976ea74026e726554db657fa54763abd0c3a0aa9"), // FERDIE
92        hex!("e45b176cad7090a5cf70b69a73b6def9296ba6a2"), // ?
93    ]
94    .into_iter()
95    .map(|address| Account {
96        address: address.into(),
97        balance: Wei::TEST_BALANCE,
98        ..Account::default()
99    })
100    .collect()
101}