ethereum-mysql
Ethereum Address wrapper for seamless SQLx database integration.
This crate provides SQL-compatible wrappers for Ethereum types, specifically designed for the SQLx async SQL toolkit. It supports multiple databases (MySQL, PostgreSQL, SQLite) through SQLx's feature system.
Features
- Multi-database support: MySQL, PostgreSQL, SQLite via SQLx
- Zero-cost abstractions: Wraps
alloy::primitives
types - Compile-time macros: Create addresses at compile time with
sqladdress!
- Constants: Pre-defined useful constants like
SqlAddress::ZERO
- Serde support: Optional JSON serialization with serde
- SQLx native: Implements
sqlx::Type
,sqlx::Encode
, andsqlx::Decode
- Pure Rust: No C dependencies, works with SQLx's pure Rust philosophy
Quick Start
Add to your Cargo.toml
:
# Basic usage (no default features)
= "1.0.0"
# Enable specific databases
= { = "1.0.0", = ["mysql"] }
= { = "1.0.0", = ["postgres", "serde"] }
# Enable all features
= { = "1.0.0", = ["full"] }
Feature Flags
mysql
- MySQL/MariaDB support via SQLxpostgres
- PostgreSQL support via SQLxsqlite
- SQLite support via SQLxserde
- JSON serialization supportfull
- Enable all features above
Usage Examples
Basic Address Creation
use ;
use FromStr;
// Use the zero address constant
let zero = ZERO;
// Create from macro (compile-time validation)
let addr = sqladdress!;
// Create from string (runtime parsing)
let addr2 = from_str.unwrap;
// Create constant addresses
const ADMIN: SqlAddress = sqladdress!;
SQLx Database Integration
use SqlAddress;
use MySqlPool;
use FromStr;
async
JSON Serialization (with serde feature)
use SqlAddress;
use ;
let user = User ;
let json = to_string?;
let parsed: User = from_str?;
Database Schema Examples
MySQL
INT AUTO_INCREMENT PRIMARY KEY,
wallet_address VARCHAR(42) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_wallet (wallet_address)
);
(
id
PostgreSQL
SERIAL PRIMARY KEY,
wallet_address VARCHAR(42) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW
);
(wallet_address);
(
id
SQLite
INTEGER PRIMARY KEY AUTOINCREMENT,
wallet_address TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
(wallet_address);
(
id
Why SQLx?
This library is built specifically for SQLx because:
- Pure Rust: No C dependencies or ORM overhead
- Compile-time safety: SQL queries are checked at compile time
- Async-first: Built for modern async Rust applications
- Multi-database: Single API for multiple database backends
- Performance: Zero-cost abstractions and prepared statements
Requirements
- Rust 1.75+ (2024 edition)
- SQLx 0.8+
- alloy 1.0+
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.