Skip to content

Commit

Permalink
feat: add anvil_setChainId API
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Dec 10, 2024
1 parent 7d0b689 commit 0805a56
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ impl<S> ForkStorage<S> {
let mut mutator = self.inner.write().unwrap();
mutator.raw_storage.store_factory_dep(hash, bytecode)
}
pub fn set_chain_id(&mut self, id: L2ChainId) {
self.chain_id = id;
let mut mutator = self.inner.write().unwrap();
if let Some(fork) = &mut mutator.fork {
fork.set_chain_id(id)
}
}
}

/// Trait that provides necessary data when
Expand Down Expand Up @@ -776,6 +783,12 @@ impl ForkDetails {
pub fn set_rpc_url(&mut self, url: String) {
self.fork_source = Box::new(HttpForkSource::new(url, self.cache_config.clone()));
}

// Sets fork's chain id.
pub fn set_chain_id(&mut self, id: L2ChainId) {
self.chain_id = id;
self.overwrite_chain_id = Some(id);
}
}

/// Serializable representation of [`ForkStorage`]'s state.
Expand Down
8 changes: 8 additions & 0 deletions src/namespaces/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ pub trait AnvilNamespaceT {
/// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
#[rpc(name = "anvil_setStorageAt")]
fn set_storage_at(&self, address: Address, slot: U256, value: U256) -> RpcResult<bool>;

/// Sets the chain id.
///
/// # Arguments
///
/// * `id` - The chain id to be set.
#[rpc(name = "anvil_setChainId")]
fn set_chain_id(&self, id: u32) -> RpcResult<()>;
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand Down
9 changes: 9 additions & 0 deletions src/node/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,13 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> AnvilNames
})
.into_boxed_future()
}

fn set_chain_id(&self, id: u32) -> RpcResult<()> {
self.set_chain_id(id)
.map_err(|err| {
tracing::error!("failed setting chain id: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(err))
})
.into_boxed_future()
}
}
10 changes: 10 additions & 0 deletions src/node/in_memory_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
}
Ok(())
}

pub fn set_chain_id(&self, id: u32) -> Result<()> {
let mut inner = self.inner
.write()
.map_err(|_| anyhow::anyhow!("Failed to acquire write lock"))?;

inner.config.update_chain_id(Some(id));
inner.fork_storage.set_chain_id(id.into());
Ok(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit 0805a56

Please sign in to comment.