Alloy in Action is a comprehensive repository designed to demonstrate practical methods for integrating Rust applications with blockchain technologies. Serving as the foundation for the "Alloy in Action" blog series, this project guides you through various facets of Rust-based blockchain client development, offering hands-on examples and in-depth explanations to enhance your understanding and proficiency.
- Overview
- Getting Started
- Rust Projects
- Solidity Smart Contracts
- Running the Examples
- Contributing
- License
This repository is designed to showcase how Rust can be effectively used to interact with Ethereum smart contracts. Through a series of Rust sub-projects, you will learn how to:
- Deploy Solidity smart contracts
- Interact with contract functions and state
- Handle events and custom errors
- Automate contract interactions using Rust macros
- Extend functionalities with advanced Rust features
Ensure you have the following installed on your system:
- Rust (version 1.82 or later)
- Foundry (for Solidity development and testing)
- Anvil (local Ethereum node for testing)
- Cargo (comes with Rust)
- Git
-
Clone the Repository
git clone --recurse-submodules https://github.com/eierina/alloy-in-action.git cd alloy-in-action
-
Install Dependencies
Ensure you have Rust and Foundry installed. You can install Foundry by following the official guide.
-
Set Up Environment Variables
Check the
.env
file is in the root directory and correctly configured. See environment configuration section. -
Start Anvil
Launch a local Ethereum node using Anvil:
anvil
Anvil will start on
http://127.0.0.1:8545
by default.
The .env
file is located in the root directory with the following non-sensitive content:
# Private key for the first default Anvil account
ANVIL_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
# RPC URL for the Anvil local Ethereum node
ANVIL_RPC_URL=http://127.0.0.1:8545
# Optional: Chain ID for the Anvil network
ANVIL_CHAIN_ID=31337
The first Rust sub-project demonstrates how to:
- Deploy a Solidity smart contract
- Interact with contract functions (reading and writing state)
- Handle events and decode logs
- Manage custom contract errors
- Utilize the
sol!
macro to generate Rust types from Solidity contracts
The related blog post can be found here.
Additional examples and be added as the series progresses.
The solidity-smart-contracts
directory contains:
- SampleContract.sol: A sample Solidity contract used in the Rust examples.
- Tests: Solidity tests to ensure contract correctness.
Additional contracts and tests will be added as the series progresses.
Navigate to the 01-deploy-interact-decode
directory and run the Rust project:
cd 01-deploy-interact-decode
cargo run
Ensure that Anvil is running and the .env
file is properly configured.
Contributions are welcome! Feel free to open issues or submit pull requests to enhance the project.
This project is licensed under the MIT License.