kabu_evm_utils/
evm_env.rs

1use alloy::consensus::BlockHeader;
2use alloy::network::TransactionBuilder;
3use alloy::primitives::{Address, U256};
4use alloy::rpc::types::TransactionRequest;
5use alloy_eips::Typed2718;
6use alloy_primitives::TxKind;
7use alloy_rpc_types::{AccessList, Transaction, TransactionTrait};
8use alloy_rpc_types_eth::Header;
9use either::Either;
10use lazy_static::lazy_static;
11use revm::context::{BlockEnv, TxEnv};
12use revm::context_interface::block::BlobExcessGasAndPrice;
13
14lazy_static! {
15    static ref COINBASE: Address = "0x1f9090aaE28b8a3dCeaDf281B0F12828e676c326".parse().unwrap();
16}
17/*
18pub async fn env_fetch_for_block<P: Provider<T, N>, T: Transport + Clone, N: Network>(provider: P, BlockID: BlockId) -> Result<Env> {
19    let block = provider.get_block_by_number()
20}
21
22 */
23
24pub fn tx_req_to_env<T: Into<TransactionRequest>>(tx: T) -> TxEnv {
25    let tx: TransactionRequest = tx.into();
26    TxEnv {
27        tx_type: tx.transaction_type.unwrap_or_default(),
28        caller: tx.from.unwrap_or_default(),
29        kind: tx.kind().unwrap_or_default(),
30        gas_limit: tx.gas.unwrap_or_default(),
31        gas_price: tx.max_fee_per_gas.unwrap_or_default(),
32        value: tx.value.unwrap_or_default(),
33        data: tx.input.input().cloned().unwrap_or_default(),
34        nonce: tx.nonce.unwrap_or_default(),
35        chain_id: tx.chain_id(),
36        access_list: tx.access_list.unwrap_or_default(),
37        gas_priority_fee: tx.max_priority_fee_per_gas,
38        blob_hashes: tx.blob_versioned_hashes.unwrap_or_default(),
39        max_fee_per_blob_gas: tx.max_fee_per_blob_gas.unwrap_or_default(),
40        authorization_list: tx.authorization_list.unwrap_or_default().into_iter().map(Either::Left).collect(),
41    }
42}
43
44pub fn header_to_block_env<H: BlockHeader>(header: &H) -> BlockEnv {
45    BlockEnv {
46        number: U256::from(header.number()),
47        beneficiary: header.beneficiary(),
48        timestamp: U256::from(header.timestamp()),
49        gas_limit: header.gas_limit(),
50        basefee: header.base_fee_per_gas().unwrap_or_default(),
51        difficulty: header.difficulty(),
52        prevrandao: Some(header.parent_hash()),
53        blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(header.excess_blob_gas().unwrap_or_default(), 3338477u64)),
54    }
55}
56
57pub fn block_env_from_block_header(block_header: &Header) -> BlockEnv {
58    BlockEnv {
59        number: U256::from(block_header.number),
60        beneficiary: block_header.beneficiary,
61        timestamp: U256::from(block_header.timestamp),
62        gas_limit: block_header.gas_limit,
63        basefee: block_header.base_fee_per_gas.unwrap_or_default(),
64        difficulty: block_header.difficulty,
65        prevrandao: Some(block_header.parent_hash),
66        blob_excess_gas_and_price: None,
67    }
68}
69
70pub fn tx_evm_env_from_tx<T: Into<Transaction>>(tx: T) -> TxEnv {
71    let tx = tx.into();
72
73    TxEnv {
74        tx_type: tx.inner.tx_type().ty(),
75        caller: tx.inner.signer(),
76        gas_limit: tx.gas_limit(),
77        gas_price: tx.max_fee_per_gas(),
78        kind: match tx.to() {
79            Some(to_address) => TxKind::Call(to_address),
80            None => TxKind::Create,
81        },
82        value: tx.value(),
83        data: tx.input().clone(),
84        nonce: tx.nonce(),
85        chain_id: tx.chain_id(),
86        access_list: AccessList::default(),
87        gas_priority_fee: tx.max_priority_fee_per_gas(),
88        blob_hashes: Vec::new(),
89        max_fee_per_blob_gas: 0,
90        authorization_list: vec![],
91    }
92}