ethereum-mysql
Design Purpose:
ethereum-mysql
is a pragmatic Rust library that provides type-safe, ergonomic wrappers for Ethereum types (Address, U256, FixedBytes, Bytes) for seamless integration with SQLx and relational databases (MySQL, PostgreSQL, SQLite). The crate is designed for real-world Web3/data engineering scenarios, focusing on:
- String-based storage: All types are stored as human-readable hex strings (e.g.
0x...
) in the database, ensuring maximum compatibility and easy inspection. - Type safety: Compile-time and runtime validation for all Ethereum types, eliminating manual string parsing and validation in business logic.
- API ergonomics: Direct arithmetic, comparison, and conversion with Rust primitives for U256, and compile-time address macros for zero-cost, safe usage.
- No binary mode: Binary column support is intentionally omitted; for binary storage, use the official alloy types directly or wait for native support in your stack.
- Minimal, focused features: Only the most practical and widely-used Ethereum types and operations are supported, with optional serde integration.
Features
- Multi-database support: MySQL, PostgreSQL, SQLite via SQLx
- Zero-cost abstractions: Wraps
alloy::primitives
types (Address
,U256
,FixedBytes
,Bytes
) - Type-safe wrappers:
SqlAddress
,SqlU256
,SqlFixedBytes
,SqlBytes
for direct DB integration - Arithmetic operations: Full arithmetic and comparison for
SqlU256
(U256), including with Rust primitives - Type conversions: Seamless conversion between wrappers and Rust integer types
- Compile-time macros: Create addresses and hashes at compile time with
sqladdress!
,sqlhash!
- Constants: Pre-defined constants like
SqlAddress::ZERO
,SqlU256::ZERO
,SqlU256::ETHER
- Decimal utilities: Parse and format Ethereum amounts with
parse_sether
/format_sether
- Serde support: Optional JSON serialization with serde
- SQLx native: Implements
sqlx_core::Type
,sqlx_core::Encode
, andsqlx_core::Decode
for all wrappers - Pure Rust: No C dependencies, async-first, production ready
- Common 32-byte types
SqlHash
andSqlTopicHash
are exported as type aliases forSqlFixedBytes<32>
Database Column Types
All types are stored as strings (hex with 0x
prefix) for maximum compatibility:
Type | Recommended Column Type |
---|---|
SqlAddress | VARCHAR(42) |
SqlU256 | VARCHAR(66) |
SqlFixedBytes | VARCHAR(2+2*N) |
SqlBytes | TEXT |
- For PostgreSQL, use
TEXT
for all string types. - For MySQL/SQLite, use
VARCHAR
as above.
U256 String Encoding
- All U256/FixedBytes types are written as lowercase hex strings with
0x
prefix (e.g.0x1234...
). - Reading supports both
0x
-prefixed hex and decimal strings (but hex is recommended for consistency). - For predictable sorting/comparison, always store as hex.
Key Advantages
🛡️ Type Safety & Validation
All wrappers provide compile-time and runtime validation. No more manual string parsing or error-prone conversions in your business logic.
⚡ Zero Conversion Overhead
Directly bind and retrieve types from SQLx queries—no intermediate conversions or manual parsing required.
🔥 Intuitive Arithmetic
Direct arithmetic and comparison between SqlU256
and Rust primitives, e.g. balance * 25 / 10000
.
🚀 API-Ready
Request structs can use SqlAddress
/SqlU256
directly for automatic validation and type safety in web APIs.
Quick Start
Add to your Cargo.toml
:
= "3.1.1"
Example Usage
Address Creation
use ;
use FromStr;
let zero = ZERO;
let addr = sqladdress!;
let addr2 = from_str.unwrap;
const ADMIN: SqlAddress = sqladdress!;
U256 Usage & Ethereum Constants
use ;
use FromStr;
// Basic usage
let small = from;
let large = from;
let zero = ZERO;
let from_decimal = from_str.unwrap;
let from_hex = from_str.unwrap;
assert_eq!;
// Ethereum amount calculations
let one_ether = ETHER; // 10^18 wei
let half_ether = ETHER / 2;
let gas_price = from; // 20 gwei
// Decimal string parsing and formatting
let amount = parse_sether.unwrap; // Parse 1.5 ETH
assert_eq!;
// Arithmetic operations
let a = from;
let b = from;
let sum = a + b;
let product = a * b;
let doubled = a * 2;
let tripled = 3 * a;
Hash and FixedBytes Creation
use ;
// Create various sized hashes at compile time
const TX_HASH: = sqlhash!;
const FUNCTION_SELECTOR: = sqlhash!;
const TOPIC: = sqlhash!;
SQLx Integration (String Mode)
use ;
use MySqlPool;
use FromStr;
async
JSON Serialization (with serde)
use ;
use ;
let user = User ;
let json = to_string?;
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.