Skip to content

21pounder/rust-chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-chain

A Rust HTTP API service for interacting with Ethereum smart contracts, built with Axum and ethers-rs.

中文文档

Overview

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.

Tech Stack

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

Architecture

┌──────────────────────────────────┐
│          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)    │
       └──────────────────────┘

API Endpoints

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

Getting Started

Prerequisites

  • Rust (edition 2024)
  • Foundry (for smart contract development)

Build & Run

# Build the Rust project
cargo build

# Run the server
cargo run

Smart Contract Development

# 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

Deploy Contract

# 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 --broadcast

Project Structure

rust-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

License

MIT

About

Block chain demo. Based on Rust, Solidity, Ethereum. Never try to steal my Bitcoin. (=^-ω-^=)

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors