Anvil Test Container
A Python library for managing Anvil Ethereum test containers with security and ease of use in mind. This library provides a clean interface for running and interacting with Anvil instances in Docker containers, making it perfect for testing Ethereum smart contracts and DApps.
Features
This library offers several key features for Ethereum development and testing:
Core Functionality
- Automated container lifecycle management
- Ethereum chain forking and manipulation
- Secure command execution
- Transaction handling
- Block time control
Advanced Features
- Chain state snapshots and restoration
- Health monitoring and diagnostics
- Container log access
- Resource cleanup and management
- Environment variable handling
Security
- Input validation for Ethereum addresses
- Command injection protection
- Secure transaction handling
- Safe environment variable management
- Resource isolation
Installation
You can install the library using pip:
pip install anvil-testcontainer
Or with Poetry (recommended):
poetry add anvil-testcontainer
Prerequisites
- Python 3.8 or higher
- Docker installed and running
- Access to an Ethereum node for forking (e.g., Infura, Alchemy)
Quick Start
Here's a simple example to get you started:
from anvil_testcontainer import AnvilContainer
with AnvilContainer("<rpc provider url eg https://eth-mainnet.alchemyapi.io/v2..>") as anvil:
web3 = anvil.get_web3()
print(f"Current block: {web3.eth.block_number}")
anvil.move_time(86400)
Advanced Usage
Configuration
Use the ContainerConfig class for more control:
from anvil_testcontainer import AnvilContainer, ContainerConfig
config = ContainerConfig(
fork_url="<provider URL>",
fork_block_number=14000000,
image="ghcr.io/foundry-rs/foundry:nightly",
port=8545,
timeout=60,
env_vars={"ETHERSCAN_API_KEY": "your-key"}
)
with AnvilContainer(config) as anvil:
snapshot_id = anvil.create_snapshot()
tx_hash = anvil.send_transaction(
from_address="0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
to_address="0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
value=100000000000000000 # 0.1 ETH
)
receipt = anvil.get_web3().eth.wait_for_transaction_receipt(tx_hash)
anvil.revert_snapshot(snapshot_id)
Health Monitoring
Monitor container health and access logs:
with AnvilContainer(config) as anvil:
# Check container health
if anvil.verify_health():
print("Container is healthy")
# Access container logs
logs = anvil.get_logs()
print(logs)
API Reference
AnvilContainer
The main class for container management.
Basic Methods
start(): Start the containerstop(): Stop the containerget_web3(): Get Web3 instancemove_time(seconds: int): Advance blockchain timereset_fork(block_number: int): Reset to specific block
Advanced Methods
create_snapshot(): Create chain state snapshotrevert_snapshot(snapshot_id: str): Restore chain statesend_transaction(...): Execute transactionverify_health(): Check container healthget_logs(): Retrieve container logs
ContainerConfig
Configuration class for container initialization.
ContainerConfig(
fork_url: str,
fork_block_number: Optional[int] = None,
image: str = "ghcr.io/foundry-rs/foundry:nightly",
port: int = 8545,
timeout: int = 60,
env_vars: Optional[Dict[str, str]] = None
)
Error Handling
The library provides custom exceptions for better error handling:
from anvil_testcontainer import ValidationError
try:
with AnvilContainer(config) as anvil:
anvil.send_transaction(
from_address="invalid_address",
to_address="0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
value=1000000000000000000
)
except ValidationError as e:
print(f"Validation failed: {e}")
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch
- Make your changes
- Add or update tests
- Submit a pull request
Development Setup
git clone https://github.com/epappas/anvil-testcontainer.git
cd anvil-testcontainer
poetry install
poetry run pytest
Testing
Run the test suite:
poetry run pytest
With coverage:
poetry run pytest --cov=anvil_testcontainer
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support, please:
- Check the documentation
- Search existing issues
- Create a new issue if needed
Acknowledgments
- Foundry team for Anvil
- OpenZeppelin for security best practices
- Web3.py team for Ethereum interaction capabilities
Metadata
Release files for anvil-testcontainer 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| anvil_testcontainer-0.1.6.tar.gz | 9.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| anvil_testcontainer-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.3 kB
Release files / anvil_testcontainer-0.1.6.tar.gz
| Download URL | anvil_testcontainer-0.1.6.tar.gz |
|---|---|
| Size | 9.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1d64b45bde3ad5a1129ab9b45425a4f8ab515f4b1c6abe8397b0b1756eabf19c
|
|
BLAKE2b-256 checksum How to use checksums |
eeae7e099ac479bb792140b0c3573b3cb61c15d1879aca6bdc8702c1055d5b2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2
|
Release files / anvil_testcontainer-0.1.6-py3-none-any.whl
| Download URL | anvil_testcontainer-0.1.6-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3814913d092264bc05da3b7dd98135543d662dccf2af37b180725e4dee5da18b
|
|
BLAKE2b-256 checksum How to use checksums |
660011a94f23965f9a717ac79d79afadfbc56dd5ede2a7c9faaa8ee86f0f7818
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2
|
Security for anvil-testcontainer 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.