revm_interpreter/interpreter_action/
create_inputs.rs

1pub use crate::primitives::CreateScheme;
2use crate::primitives::{Address, Bytes, TxEnv, TxKind, U256};
3use std::boxed::Box;
4
5/// Inputs for a create call.
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct CreateInputs {
9    /// Caller address of the EVM.
10    pub caller: Address,
11    /// The create scheme.
12    pub scheme: CreateScheme,
13    /// The value to transfer.
14    pub value: U256,
15    /// The init code of the contract.
16    pub init_code: Bytes,
17    /// The gas limit of the call.
18    pub gas_limit: u64,
19}
20
21impl CreateInputs {
22    /// Creates new create inputs.
23    pub fn new(tx_env: &TxEnv, gas_limit: u64) -> Option<Self> {
24        let TxKind::Create = tx_env.transact_to else {
25            return None;
26        };
27
28        Some(CreateInputs {
29            caller: tx_env.caller,
30            scheme: CreateScheme::Create,
31            value: tx_env.value,
32            init_code: tx_env.data.clone(),
33            gas_limit,
34        })
35    }
36
37    /// Returns boxed create inputs.
38    pub fn new_boxed(tx_env: &TxEnv, gas_limit: u64) -> Option<Box<Self>> {
39        Self::new(tx_env, gas_limit).map(Box::new)
40    }
41
42    /// Returns the address that this create call will create.
43    pub fn created_address(&self, nonce: u64) -> Address {
44        match self.scheme {
45            CreateScheme::Create => self.caller.create(nonce),
46            CreateScheme::Create2 { salt } => self
47                .caller
48                .create2_from_code(salt.to_be_bytes(), &self.init_code),
49        }
50    }
51}