pub struct AnvilNode { /* private fields */ }anvil only.Expand description
§Community Testcontainers Implementation for Foundry Anvil
This is a community implementation of the Testcontainers interface for Foundry Anvil.
Anvil is Foundry’s fast local Ethereum node for development and testing. It’s an ideal tool for rapid iteration on smart contract development and provides a clean, lightweight alternative to running a full node.
§Example
use testcontainers_modules::{
anvil::{AnvilNode, ANVIL_PORT},
testcontainers::runners::AsyncRunner,
};
// Start an Anvil node
let node = AnvilNode::default().start().await?;
// Get the RPC endpoint URL
let host_port = node.get_host_port_ipv4(ANVIL_PORT).await?;
let rpc_url = format!("http://localhost:{host_port}");
// Use with your favorite Ethereum library (alloy, ethers, web3, etc.)
// let provider = Provider::try_from(rpc_url)?;§Advanced Configuration
use testcontainers_modules::anvil::AnvilNode;
// Configure chain ID and forking
let node = AnvilNode::default()
.with_chain_id(1337)
.with_fork_url("https://eth.llamarpc.com")
.with_fork_block_number(18_000_000)
.start().await?;§Usage
The endpoint of the container is intended to be injected into your provider configuration, so that you can easily run tests against a local Anvil instance.
To use the latest Foundry image, you can use the latest() method:
let node = AnvilNode::latest().start().await?;Users can use a specific Foundry image in their code with ImageExt::with_tag.
use testcontainers::core::ImageExt;
let node = AnvilNode::default().with_tag("nightly").start().await?;Implementations§
Source§impl AnvilNode
impl AnvilNode
Sourcepub fn with_chain_id(self, chain_id: u64) -> Self
pub fn with_chain_id(self, chain_id: u64) -> Self
Specify the chain ID - this will be Ethereum Mainnet by default
Sourcepub fn with_fork_url(self, fork_url: impl Into<String>) -> Self
pub fn with_fork_url(self, fork_url: impl Into<String>) -> Self
Sourcepub fn with_fork_block_number(self, block_number: u64) -> Self
pub fn with_fork_block_number(self, block_number: u64) -> Self
Sourcepub fn with_state_mount(self, host_dir: impl AsRef<Path>) -> Self
pub fn with_state_mount(self, host_dir: impl AsRef<Path>) -> Self
Mount a host directory for anvil state at /state inside the container
Use this method to bind mount a host directory to the container’s /state directory.
This allows you to persist state across container restarts.
§Arguments
host_dir- Path on the host machine to mount (will be mounted to/statein container)
§Note
When using this method, specify container paths (starting with /state/) in
with_load_state_path and with_dump_state_path.
§Example
let temp_dir = std::env::temp_dir().join("anvil-state");
let node = AnvilNode::default()
.with_state_mount(&temp_dir)
.with_dump_state_path("/state/state.json")
.start().await?;Sourcepub fn with_load_state_path(self, path: impl Into<String>) -> Self
pub fn with_load_state_path(self, path: impl Into<String>) -> Self
Configure Anvil to initialize from a previously saved state snapshot.
Equivalent to passing --load-state <PATH>.
§Arguments
path- Path to the state file (container-internal path, typically/state/...if usingwith_state_mount)
§Example
let node = AnvilNode::default()
.with_state_mount("/host/path/to/state")
.with_load_state_path("/state/state.json")
.start().await?;Sourcepub fn with_dump_state_path(self, path: impl Into<String>) -> Self
pub fn with_dump_state_path(self, path: impl Into<String>) -> Self
Configure Anvil to dump the state on exit to the given file or directory.
Equivalent to passing --dump-state <PATH>.
§Arguments
path- Path where state should be saved (container-internal path, typically/state/...if usingwith_state_mount)
§Example
let node = AnvilNode::default()
.with_state_mount("/host/path/to/state")
.with_dump_state_path("/state/state.json")
.start().await?;Sourcepub fn with_state_interval(self, seconds: u64) -> Self
pub fn with_state_interval(self, seconds: u64) -> Self
Configure periodic state persistence interval in seconds.
Equivalent to passing --state-interval <SECONDS>.
§Example
let node = AnvilNode::default()
.with_state_mount("/host/path/to/state")
.with_dump_state_path("/state/state.json")
.with_state_interval(30) // Save every 30 seconds
.start().await?;Trait Implementations§
Source§impl Image for AnvilNode
impl Image for AnvilNode
Source§fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>>
fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>>
CMD this image needs to be created with.Source§fn entrypoint(&self) -> Option<&str>
fn entrypoint(&self) -> Option<&str>
Source§fn env_vars(
&self,
) -> impl IntoIterator<Item = (impl Into<Cow<'_, str>>, impl Into<Cow<'_, str>>)>
fn env_vars( &self, ) -> impl IntoIterator<Item = (impl Into<Cow<'_, str>>, impl Into<Cow<'_, str>>)>
Source§fn mounts(&self) -> impl IntoIterator<Item = &Mount>
fn mounts(&self) -> impl IntoIterator<Item = &Mount>
Source§fn expose_ports(&self) -> &[ContainerPort]
fn expose_ports(&self) -> &[ContainerPort]
Source§fn tag(&self) -> &str
fn tag(&self) -> &str
Source§fn ready_conditions(&self) -> Vec<WaitFor>
fn ready_conditions(&self) -> Vec<WaitFor>
Source§fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer>
fn copy_to_sources(&self) -> impl IntoIterator<Item = &CopyToContainer>
Source§fn exec_after_start(
&self,
cs: ContainerState,
) -> Result<Vec<ExecCommand>, TestcontainersError>
fn exec_after_start( &self, cs: ContainerState, ) -> Result<Vec<ExecCommand>, TestcontainersError>
Source§fn exec_before_ready(
&self,
cs: ContainerState,
) -> Result<Vec<ExecCommand>, TestcontainersError>
fn exec_before_ready( &self, cs: ContainerState, ) -> Result<Vec<ExecCommand>, TestcontainersError>
Auto Trait Implementations§
impl Freeze for AnvilNode
impl RefUnwindSafe for AnvilNode
impl Send for AnvilNode
impl Sync for AnvilNode
impl Unpin for AnvilNode
impl UnwindSafe for AnvilNode
Blanket Implementations§
Source§impl<T, I> AsyncRunner<I> for T
impl<T, I> AsyncRunner<I> for T
Source§fn start<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<ContainerAsync<I>, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
fn start<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<ContainerAsync<I>, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
ContainerAsync.Source§fn pull_image<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<ContainerRequest<I>, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
fn pull_image<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<ContainerRequest<I>, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<RI, I> ImageExt<I> for RI
impl<RI, I> ImageExt<I> for RI
Source§fn with_cmd(
self,
cmd: impl IntoIterator<Item = impl Into<String>>,
) -> ContainerRequest<I>
fn with_cmd( self, cmd: impl IntoIterator<Item = impl Into<String>>, ) -> ContainerRequest<I>
Source§fn with_name(self, name: impl Into<String>) -> ContainerRequest<I>
fn with_name(self, name: impl Into<String>) -> ContainerRequest<I>
{domain}/{owner}/{image}).
Can be used to specify a custom registry or owner.Source§fn with_tag(self, tag: impl Into<String>) -> ContainerRequest<I>
fn with_tag(self, tag: impl Into<String>) -> ContainerRequest<I>
Source§fn with_container_name(self, name: impl Into<String>) -> ContainerRequest<I>
fn with_container_name(self, name: impl Into<String>) -> ContainerRequest<I>
Source§fn with_platform(self, platform: impl Into<String>) -> ContainerRequest<I>
fn with_platform(self, platform: impl Into<String>) -> ContainerRequest<I>
Source§fn with_network(self, network: impl Into<String>) -> ContainerRequest<I>
fn with_network(self, network: impl Into<String>) -> ContainerRequest<I>
Source§fn with_label(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> ContainerRequest<I>
fn with_label( self, key: impl Into<String>, value: impl Into<String>, ) -> ContainerRequest<I>
Source§fn with_labels(
self,
labels: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> ContainerRequest<I>
fn with_labels( self, labels: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> ContainerRequest<I>
Source§fn with_env_var(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> ContainerRequest<I>
fn with_env_var( self, name: impl Into<String>, value: impl Into<String>, ) -> ContainerRequest<I>
Source§fn with_host(
self,
key: impl Into<String>,
value: impl Into<Host>,
) -> ContainerRequest<I>
fn with_host( self, key: impl Into<String>, value: impl Into<Host>, ) -> ContainerRequest<I>
Source§fn with_hostname(self, hostname: impl Into<String>) -> ContainerRequest<I>
fn with_hostname(self, hostname: impl Into<String>) -> ContainerRequest<I>
Source§fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>
Source§fn with_copy_to(
self,
target: impl Into<CopyTargetOptions>,
source: impl Into<CopyDataSource>,
) -> ContainerRequest<I>
fn with_copy_to( self, target: impl Into<CopyTargetOptions>, source: impl Into<CopyDataSource>, ) -> ContainerRequest<I>
Source§fn with_mapped_port(
self,
host_port: u16,
container_port: ContainerPort,
) -> ContainerRequest<I>
fn with_mapped_port( self, host_port: u16, container_port: ContainerPort, ) -> ContainerRequest<I>
Source§fn with_ulimit(
self,
name: &str,
soft: i64,
hard: Option<i64>,
) -> ContainerRequest<I>
fn with_ulimit( self, name: &str, soft: i64, hard: Option<i64>, ) -> ContainerRequest<I>
Source§fn with_privileged(self, privileged: bool) -> ContainerRequest<I>
fn with_privileged(self, privileged: bool) -> ContainerRequest<I>
Source§fn with_cap_add(self, capability: impl Into<String>) -> ContainerRequest<I>
fn with_cap_add(self, capability: impl Into<String>) -> ContainerRequest<I>
Source§fn with_cap_drop(self, capability: impl Into<String>) -> ContainerRequest<I>
fn with_cap_drop(self, capability: impl Into<String>) -> ContainerRequest<I>
Source§fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I>
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I>
Source§fn with_userns_mode(self, userns_mode: &str) -> ContainerRequest<I>
fn with_userns_mode(self, userns_mode: &str) -> ContainerRequest<I>
Source§fn with_shm_size(self, bytes: u64) -> ContainerRequest<I>
fn with_shm_size(self, bytes: u64) -> ContainerRequest<I>
Source§fn with_startup_timeout(self, timeout: Duration) -> ContainerRequest<I>
fn with_startup_timeout(self, timeout: Duration) -> ContainerRequest<I>
Source§fn with_working_dir(self, working_dir: impl Into<String>) -> ContainerRequest<I>
fn with_working_dir(self, working_dir: impl Into<String>) -> ContainerRequest<I>
/.Source§fn with_log_consumer(
self,
log_consumer: impl LogConsumer + 'static,
) -> ContainerRequest<I>
fn with_log_consumer( self, log_consumer: impl LogConsumer + 'static, ) -> ContainerRequest<I>
Source§fn with_user(self, user: impl Into<String>) -> ContainerRequest<I>
fn with_user(self, user: impl Into<String>) -> ContainerRequest<I>
Source§fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest<I>
fn with_readonly_rootfs(self, readonly_rootfs: bool) -> ContainerRequest<I>
Source§fn with_security_opt(
self,
security_opt: impl Into<String>,
) -> ContainerRequest<I>
fn with_security_opt( self, security_opt: impl Into<String>, ) -> ContainerRequest<I>
Source§fn with_ready_conditions(
self,
ready_conditions: Vec<WaitFor>,
) -> ContainerRequest<I>
fn with_ready_conditions( self, ready_conditions: Vec<WaitFor>, ) -> ContainerRequest<I>
Source§fn with_health_check(self, health_check: Healthcheck) -> ContainerRequest<I>
fn with_health_check(self, health_check: Healthcheck) -> ContainerRequest<I>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T, I> SyncRunner<I> for T
impl<T, I> SyncRunner<I> for T
Source§fn start(self) -> Result<Container<I>, TestcontainersError>
fn start(self) -> Result<Container<I>, TestcontainersError>
Container.