foundry_evm_networks/arbitrum.rs
1//! Minimal Arbitrum system contract compatibility helpers.
2
3use alloy_chains::Chain;
4use alloy_primitives::{Address, Bytes, U256, hex};
5
6/// ArbSys system contract address.
7pub const ARB_SYS_ADDRESS: Address = Address::with_last_byte(0x64);
8
9/// `ArbSys.arbBlockNumber()` selector.
10pub const ARB_BLOCK_NUMBER_SELECTOR: [u8; 4] = hex!("a3b1b31d");
11
12/// Gas Nitro charges inside `ArbSys.arbBlockNumber()`: opening the ArbOS state reads its version
13/// from storage (800, the EIP-2200 `SLOAD` cost), and copying the 32-byte result costs 3.
14pub const ARB_BLOCK_NUMBER_GAS_COST: u64 = 803;
15
16/// Transaction type of ArbOS internal transactions, such as the `StartBlock` transaction that opens
17/// every block.
18pub const ARBITRUM_INTERNAL_TX_TYPE: u8 = 0x6a;
19
20/// Returns whether `chain_id` is an Arbitrum chain.
21pub fn is_arbitrum_chain(chain_id: u64) -> bool {
22 Chain::from_id(chain_id).is_arbitrum()
23}
24
25/// Returns the ABI-encoded result for `ArbSys.arbBlockNumber()`.
26pub fn arb_block_number_output(block_number: u64) -> Bytes {
27 U256::from(block_number).to_be_bytes::<32>().into()
28}
29
30/// Returns the gas cost and ABI-encoded result for `ArbSys.arbBlockNumber()`.
31pub fn arb_block_number_call(gas_limit: u64, block_number: u64) -> Option<(u64, Bytes)> {
32 (gas_limit >= ARB_BLOCK_NUMBER_GAS_COST)
33 .then(|| (ARB_BLOCK_NUMBER_GAS_COST, arb_block_number_output(block_number)))
34}