mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-09 23:38:10 -05:00
Co-authored-by: sergey-melnychuk <sergey-melnychuk@users.noreply.github.com> Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
2.2 KiB
2.2 KiB
Full Contract State Example
This example demonstrates how to extract the complete state of a specific contract from the reth database.
What it does
The example shows how to:
- Connect to a reth database - Uses the recommended builder pattern to create a read-only provider
- Get basic account information - Retrieves balance, nonce, and code hash for the contract address
- Get contract bytecode - Fetches the actual contract bytecode if the account is a contract
- Iterate through all storage slots - Uses database cursors to efficiently retrieve all storage key-value pairs
Prerequisites
- A reth database with some data (you can sync a node or use a pre-synced database)
- Set the
RETH_DATADIRenvironment variable to point to your reth data directory - Set the
CONTRACT_ADDRESSenvironment variable to provide target contract address
Usage
# Set your reth data directory
export RETH_DATADIR="/path/to/your/reth/datadir"
# Set target contract address
export CONTRACT_ADDRESS="0x0..."
# Run the example
cargo run --example full-contract-state
Code Structure
The example consists of:
ContractStatestruct - Holds all contract state informationextract_contract_statefunction - Main function that extracts contract statemainfunction - Sets up the provider and demonstrates usage
Key Concepts
Provider Pattern
The example uses reth's provider pattern:
ProviderFactory- Creates database connectionsDatabaseProvider- Provides low-level database accessStateProvider- Provides high-level state access
Database Cursors
For efficient storage iteration, the example uses database cursors:
cursor_dup_read- Creates a cursor for duplicate key tablesseek_exact- Positions cursor at specific keynext_dup- Iterates through duplicate entries
Output
The example will print:
- Contract address
- Account balance
- Account nonce
- Code hash
- Number of storage slots
- All storage key-value pairs
Error Handling
The example includes proper error handling:
- Returns
Noneif the contract doesn't exist - Uses
ProviderResultfor database operation errors - Gracefully handles missing bytecode or storage