A Rust HTTP API service for interacting with Ethereum smart contracts, built with Axum and ethers-rs.
rust-chain provides a lightweight RESTful API that reads on-chain state from an Ethereum Counter smart contract. It demonstrates how to bridge traditional web services with blockchain data using Rust's async ecosystem.
| Layer | Technology | Purpose |
|---|---|---|
| HTTP Server | Axum 0.8 | Async web framework with type-safe routing |
| Async Runtime | Tokio | Multi-threaded async executor |
| Blockchain | ethers-rs v2 | Ethereum JSON-RPC client & ABI bindings |
| Smart Contract | Solidity ^0.8.20 | On-chain Counter contract |
| Contract Tooling | Foundry | Build, test & deploy Solidity contracts |
| Error Handling | thiserror | Ergonomic custom error types |
| Logging | tracing | Structured, async-aware diagnostics |
| Serialization | serde + serde_json | JSON request/response handling |
┌──────────────────────────────────┐
│ HTTP Clients │
│ GET /number GET /block │
└──────────────┬───────────────────┘
│
┌───────▼────────┐
│ Axum Router │
│ (routes.rs) │
└───────┬─────────┘
│
┌───────▼─────────┐
│ Counter Wrapper │
│ (counter.rs) │
└───────┬──────────┘
│
┌───────▼──────────────┐
│ ethers-rs Provider │
│ + ABI Code Gen │
└───────┬───────────────┘
│
┌───────▼──────────────┐
│ Ethereum JSON-RPC │
│ (Local / Testnet) │
└──────────────────────┘
| Method | Path | Description |
|---|---|---|
GET |
/number |
Returns the current counter value from the smart contract |
GET |
/block |
Returns the latest block number from the connected chain |
# Build the Rust project
cargo build
# Run the server
cargo run# Compile contracts
cd contracts && forge build
# Run all tests (includes fuzz tests)
cd contracts && forge test
# Run a specific test
cd contracts && forge test --match-test test_Increment# Set your private key (never commit this)
export PRIVATE_KEY=<your-private-key>
# Deploy to a local Anvil node
cd contracts && forge script script/Counter.s.sol --rpc-url http://127.0.0.1:8545 --broadcastrust-chain/
├── src/
│ ├── main.rs # Entry point
│ ├── lib.rs # Module exports
│ ├── counter.rs # Contract wrapper (abigen! bindings)
│ ├── routes.rs # Axum HTTP handlers + error types
│ └── counter.json # Contract ABI definition
├── contracts/
│ ├── src/Counter.sol # Solidity smart contract
│ ├── test/Counter.t.sol# Contract tests (unit + fuzz)
│ └── script/Counter.s.sol # Deployment script
├── Cargo.toml
└── foundry.toml
MIT