diff --git a/crates/node/src/node_implementations/geth.rs b/crates/node/src/node_implementations/geth.rs index bc9ac8f..ac588fd 100644 --- a/crates/node/src/node_implementations/geth.rs +++ b/crates/node/src/node_implementations/geth.rs @@ -131,7 +131,11 @@ impl GethNode { } pub async fn new_existing(private_key: &str, rpc_port: u16) -> anyhow::Result { - use alloy::{primitives::FixedBytes, signers::local::PrivateKeySigner}; + use alloy::{ + primitives::FixedBytes, + providers::{Provider, ProviderBuilder}, + signers::local::PrivateKeySigner, + }; let key_str = private_key.trim().strip_prefix("0x").unwrap_or(private_key.trim()); let key_bytes = alloy::hex::decode(key_str) @@ -152,6 +156,13 @@ impl GethNode { let address = signer.address(); let wallet = Arc::new(EthereumWallet::new(signer)); + let connection_string = format!("http://localhost:{}", rpc_port); + + let chain_id = ProviderBuilder::new() + .connect_http(connection_string.parse()?) + .get_chain_id() + .await + .context("Failed to query chain ID from RPC")?; let node = Self { connection_string: format!("http://localhost:{}", rpc_port), @@ -160,7 +171,7 @@ impl GethNode { logs_directory: PathBuf::new(), geth: PathBuf::new(), id: 0, - chain_id: 1337, + chain_id, handle: None, start_timeout: Duration::from_secs(0), wallet, @@ -174,6 +185,8 @@ impl GethNode { Ok(node) } + /// Ensure that the given address has at least 1000 ETH, funding it from the node's managed + /// account if necessary. async fn ensure_funded(&self, address: Address) -> anyhow::Result<()> { use alloy::{ primitives::utils::{format_ether, parse_ether}, diff --git a/crates/node/src/node_implementations/substrate.rs b/crates/node/src/node_implementations/substrate.rs index 7c7dde6..51835de 100644 --- a/crates/node/src/node_implementations/substrate.rs +++ b/crates/node/src/node_implementations/substrate.rs @@ -80,6 +80,7 @@ pub struct SubstrateNode { wallet: Arc, nonce_manager: CachedNonceManager, provider: OnceCell>>, + chain_id: alloy::primitives::ChainId, } impl SubstrateNode { @@ -129,11 +130,16 @@ impl SubstrateNode { wallet: wallet.clone(), nonce_manager: Default::default(), provider: Default::default(), + chain_id: CHAIN_ID, } } pub async fn new_existing(private_key: &str, rpc_port: u16) -> anyhow::Result { - use alloy::{primitives::FixedBytes, signers::local::PrivateKeySigner}; + use alloy::{ + primitives::FixedBytes, + providers::{Provider, ProviderBuilder}, + signers::local::PrivateKeySigner, + }; let key_str = private_key.trim().strip_prefix("0x").unwrap_or(private_key.trim()); let key_bytes = alloy::hex::decode(key_str) @@ -153,13 +159,21 @@ impl SubstrateNode { .map_err(|e| anyhow::anyhow!("Failed to create signer from private key: {}", e))?; let wallet = Arc::new(EthereumWallet::new(signer)); + let rpc_url = format!("http://localhost:{}", rpc_port); + + // Query the chain ID from the RPC + let chain_id = ProviderBuilder::new() + .connect_http(rpc_url.parse()?) + .get_chain_id() + .await + .context("Failed to query chain ID from RPC")?; Ok(Self { id: 0, node_binary: PathBuf::new(), eth_proxy_binary: PathBuf::new(), export_chainspec_command: String::new(), - rpc_url: format!("http://localhost:{}", rpc_port), + rpc_url, base_directory: PathBuf::new(), logs_directory: PathBuf::new(), substrate_process: None, @@ -167,6 +181,7 @@ impl SubstrateNode { wallet, nonce_manager: Default::default(), provider: Default::default(), + chain_id, }) } @@ -383,7 +398,7 @@ impl SubstrateNode { construct_concurrency_limited_provider::( self.rpc_url.as_str(), FallbackGasFiller::new(u64::MAX, 5_000_000_000, 1_000_000_000), - ChainIdFiller::new(Some(CHAIN_ID)), + ChainIdFiller::new(Some(self.chain_id)), NonceFiller::new(self.nonce_manager.clone()), self.wallet.clone(), )