Skip to main content

foundry_common/
constants.rs

1//! Commonly used constants.
2
3use alloy_eips::Typed2718;
4use alloy_network::AnyTxEnvelope;
5use alloy_primitives::{Address, B256, Signature, address};
6use std::time::Duration;
7
8/// The dev chain-id, inherited from hardhat
9pub const DEV_CHAIN_ID: u64 = 31337;
10
11/// The first four bytes of the call data for a function call specifies the function to be called.
12pub const SELECTOR_LEN: usize = 4;
13
14/// Maximum size in bytes (0x6000) that a contract can have.
15pub const CONTRACT_MAX_SIZE: usize = 24576;
16
17/// Default request timeout for http requests
18///
19/// Note: this is only used so that connections, that are discarded on the server side won't stay
20/// open forever. We assume some nodes may have some backoff baked into them and will delay some
21/// responses. This timeout should be a reasonable amount of time to wait for a request.
22pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(45);
23
24/// Alchemy free tier cups: <https://docs.alchemy.com/reference/pricing-plans>
25pub const ALCHEMY_FREE_TIER_CUPS: u64 = 330;
26
27/// Logged when an error is indicative that the user is trying to fork from a non-archive node.
28pub const NON_ARCHIVE_NODE_WARNING: &str = "\
29It looks like you're trying to fork from an older block with a non-archive node which is not \
30supported. Please try to change your RPC url to an archive node if the issue persists.";
31
32/// Arbitrum L1 sender address of the first transaction in every block.
33/// `0x00000000000000000000000000000000000a4b05`
34pub const ARBITRUM_SENDER: Address = address!("0x00000000000000000000000000000000000a4b05");
35
36/// The system address, the sender of the first transaction in every block:
37/// `0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001`
38///
39/// See also <https://github.com/ethereum-optimism/optimism/blob/65ec61dde94ffa93342728d324fecf474d228e1f/specs/deposits.md#l1-attributes-deposited-transaction>
40pub const OPTIMISM_SYSTEM_ADDRESS: Address = address!("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001");
41
42/// The system address, the sender of the first transaction in every block:
43pub const MONAD_SYSTEM_ADDRESS: Address = address!("0x6f49a8F621353f12378d0046E7d7e4b9B249DC9e");
44
45/// MegaETH system address for `Set Slots` in the MegaETH oracle.
46///
47/// Transactions from this sender are submitted with gas price 0.
48///
49/// See: <https://mega.etherscan.io/address/0xa887dcb9d5f39ef79272801d05abdf707cfbbd1d>
50pub const MEGA_SYSTEM_ADDRESS: Address = address!("0xa887dcb9d5f39ef79272801d05abdf707cfbbd1d");
51
52/// Transaction identifier of System transaction types
53pub const SYSTEM_TRANSACTION_TYPE: u8 = 126;
54
55/// Default user agent set as the header for requests that don't specify one.
56pub const DEFAULT_USER_AGENT: &str = concat!("foundry/", env!("CARGO_PKG_VERSION"));
57
58/// Prefix for auto-generated type bindings using `forge bind-json`.
59pub const TYPE_BINDING_PREFIX: &str = "string constant schema_";
60
61/// Returns whether the sender is a known L2 system sender that is the first tx in every block.
62///
63/// Transactions from these senders usually don't have a any fee information OR set absurdly high fees that exceed the gas limit (See: <https://github.com/foundry-rs/foundry/pull/10608>)
64///
65/// See: [ARBITRUM_SENDER], [OPTIMISM_SYSTEM_ADDRESS], [MONAD_SYSTEM_ADDRESS], [MEGA_SYSTEM_ADDRESS]
66/// and [Address::ZERO]
67pub fn is_known_system_sender(sender: Address) -> bool {
68    [
69        ARBITRUM_SENDER,
70        OPTIMISM_SYSTEM_ADDRESS,
71        MONAD_SYSTEM_ADDRESS,
72        MEGA_SYSTEM_ADDRESS,
73        Address::ZERO,
74    ]
75    .contains(&sender)
76}
77
78pub fn is_impersonated_tx(tx: &AnyTxEnvelope) -> bool {
79    if let AnyTxEnvelope::Ethereum(tx) = tx {
80        return is_impersonated_sig(tx.signature(), tx.ty());
81    }
82    false
83}
84
85pub fn is_impersonated_sig(sig: &Signature, ty: u8) -> bool {
86    let impersonated_sig =
87        Signature::from_scalars_and_parity(B256::with_last_byte(1), B256::with_last_byte(1), false);
88    if ty != SYSTEM_TRANSACTION_TYPE
89        && (sig == &impersonated_sig || sig.r() == impersonated_sig.r())
90    {
91        return true;
92    }
93    false
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_constant_sender() {
102        let arb = address!("0x00000000000000000000000000000000000a4b05");
103        assert_eq!(arb, ARBITRUM_SENDER);
104        let base = address!("0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001");
105        assert_eq!(base, OPTIMISM_SYSTEM_ADDRESS);
106    }
107}