A production-ready Rust implementation of the OIF Protocol Solver with abstract trait architecture and dependency injection.
This project implements a modular, abstract architecture using Rust traits for maximum flexibility, testability, and maintainability.
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Origin Chain │ │ Rust Solver │ │ Destination Chain│
│ (TheCompact) │◄──►│ (Abstract) │◄──►│ (CoinFiller) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
┌─────────────────────────┐
│ ContractFactory │
│ (Entry Point) │
└──────────┬──────────────┘
│
┌─────────────────────────┐
│ FinalizationOrchestrator│
│ (Coordination) │
└──────────┬──────────────┘
│
┌───────────────┴───────────────┐
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ CallDataEncoder │ │ ExecutionEngine │
│ (Abstract) │ │ (Abstract) │
└─────────┬───────┘ └─────────┬───────┘
│ │
┌─────────▼───────┐ ┌─────────▼───────┐
│ FoundryEncoder │ │ AlloyExecutor │
│ (Foundry cast) │ │ (Alloy providers)│
└─────────────────┘ └─────────────────┘
# Build the project
cargo build
# Run all tests (14/14 passing)
cargo test
# Run with default configuration
cargo run// Use default implementations
let orchestrator = FinalizationOrchestrator::new(abi_provider, config)?;
// Or inject custom implementations
let custom_encoder = Arc::new(MyCustomEncoder::new());
let custom_executor = Arc::new(MyCustomExecutor::new());
let orchestrator = FinalizationOrchestrator::new_with_traits(
custom_encoder,
custom_executor,
config
);CallDataEncoder: Abstract interface for ABI encodingExecutionEngine: Abstract interface for blockchain executionOrderExecutor: High-level order processing interface
struct MockEncoder;
impl CallDataEncoder for MockEncoder {
fn encode_finalize_call(&self, order: &Order) -> Result<Vec<u8>> {
// Mock implementation for testing
}
}src/contracts/encoding/
├── mod.rs # Trait exports
├── traits.rs # CallDataEncoder trait
└── foundry_encoder.rs # Foundry cast implementation
Features:
- Abstract Interface:
CallDataEncodertrait - Foundry Integration: Uses
cast abi-encodefor compatibility - TypeScript Compatibility: Generates identical calldata (selector:
0xdd1ff485)
src/contracts/execution/
├── mod.rs # Trait exports
├── traits.rs # ExecutionEngine trait
└── alloy_executor.rs # Alloy implementation
Features:
- Abstract Interface:
ExecutionEnginetrait - Multi-Chain Support: Origin and destination chain execution
- Gas Management: Automatic gas estimation and optimization
src/contracts/operations/
└── settlement.rs # FinalizationOrchestrator
Features:
- Modular Coordination: Combines encoding + execution
- Dependency Injection: Accepts abstract trait implementations
- Order Processing: Complete finalization workflow
src/contracts/
└── factory.rs # ContractFactory (updated)
Features:
- Simplified Interface: Uses
FinalizationOrchestrator - Backward Compatibility: Legacy methods preserved
- Integration Tests: 5/5 tests passing
cargo testResults: 14/14 tests passing ✅
- 2/2 FoundryEncoder tests
- 3/3 AlloyExecutor tests
- 4/4 Settlement tests
- 5/5 Factory tests
- Unit Tests: Individual component testing
- Integration Tests: Cross-component interaction
- Trait Testing: Abstract interface validation
- End-to-End: Complete finalization workflow
| Method | Path | Description |
|---|---|---|
| GET | / |
API information |
| GET | /api/v1/health |
Health check |
| POST | /api/v1/orders |
Submit new order |
| GET | /api/v1/orders/{id} |
Get order status |
| POST | /api/v1/orders/{id}/finalize |
Manual finalization |
| GET | /api/v1/queue |
View processing queue |
# config/local.toml
[server]
host = "0.0.0.0"
port = 3000
[solver]
private_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
finalization_delay_seconds = 30
[chains.origin]
rpc_url = "http://localhost:8545"
chain_id = 31337
[chains.destination]
rpc_url = "http://localhost:8546"
chain_id = 31338
[contracts]
the_compact = "0x..."
settler_compact = "0x..."
coin_filler = "0x..."
[monitoring]
enabled = true
check_interval_seconds = 60
[persistence]
enabled = true
data_file = "data/orders.json"export SOLVER_PRIVATE_KEY="0x..."
export ORIGIN_RPC_URL="http://localhost:8545"
export DESTINATION_RPC_URL="http://localhost:8546"┌─────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Order │───►│ CallDataEncoder │───►│ Encoded Data │
│ Submission │ │ (Abstract) │ │ (ABI bytes) │
└─────────────┘ └─────────────────┘ └─────────────────┘
│
┌─────────────┐ ┌─────────────────┐ ┌───────▼─────────┐
│ Transaction │◄───│ ExecutionEngine │◄───│ FinalizationOrch│
│ Receipt │ │ (Abstract) │ │ estrator │
└─────────────┘ └─────────────────┘ └─────────────────┘
cargo build # Compile
cargo test # Run all tests
cargo run # Start server
cargo check # Quick syntax check
cargo clippy # Linting
cargo fmt # Formatting# Install development tools
cargo install cargo-watch
# Auto-reload development
cargo watch -x test # Auto-test on changes
cargo watch -x run # Auto-run on changessrc/
├── main.rs # Application entry point
├── server.rs # HTTP server
├── config.rs # Configuration management
│
├── contracts/ # Blockchain layer
│ ├── mod.rs # Module exports
│ ├── factory.rs # ContractFactory (updated)
│ │
│ ├── abi/ # ABI management
│ │ ├── mod.rs
│ │ └── definitions.rs # Centralized function signatures
│ │
│ ├── encoding/ # Abstract encoding
│ │ ├── mod.rs # Trait exports
│ │ ├── traits.rs # CallDataEncoder trait
│ │ └── foundry_encoder.rs # Foundry implementation
│ │
│ ├── execution/ # Abstract execution
│ │ ├── mod.rs # Trait exports
│ │ ├── traits.rs # ExecutionEngine trait
│ │ └── alloy_executor.rs # Alloy implementation
│ │
│ └── operations/ # Orchestration
│ └── settlement.rs # FinalizationOrchestrator
│
├── models/ # Data structures
│ ├── mod.rs
│ ├── order.rs # Order models
│ └── mandate.rs # Mandate outputs
│
├── services/ # Business logic
│ ├── mod.rs
│ ├── cross_chain.rs # Cross-chain operations
│ ├── finalization.rs # Order finalization
│ └── monitoring.rs # Event monitoring
│
├── storage/ # Data persistence
│ ├── mod.rs
│ └── memory.rs # In-memory storage
│
└── handlers/ # HTTP endpoints
├── mod.rs
├── health.rs # Health check
├── orders.rs # Order API
└── queue.rs # Queue status
- Trait-Based Design: Maximum flexibility and testability
- Dependency Injection: Easy component swapping
- Modular Components: Clear separation of concerns
- Type Safety: Compile-time guarantees
- Error Handling: Comprehensive error management
- Logging: Structured logging with
tracing - Configuration: Flexible TOML + environment variables
- Testing: 14/14 tests passing with full coverage
- Multi-Chain: Origin and destination chain support
- ABI Compatibility: TypeScript-compatible encoding
- Gas Optimization: Intelligent gas estimation
- Transaction Management: Robust transaction handling
pub struct AlloyEncoder {
// Implementation using pure Alloy
}
impl CallDataEncoder for AlloyEncoder {
fn encode_finalize_call(&self, order: &Order) -> Result<Vec<u8>> {
// Pure Alloy implementation
}
fn description(&self) -> &str {
"AlloyEncoder: Pure Alloy ABI encoding"
}
}pub struct Web3Executor {
// Implementation using web3 library
}
impl ExecutionEngine for Web3Executor {
async fn send_transaction(&self, call_data: Vec<u8>, to: Address, gas: GasParams) -> Result<String> {
// web3 implementation
}
}let custom_orchestrator = FinalizationOrchestrator::new_with_traits(
Arc::new(AlloyEncoder::new()),
Arc::new(Web3Executor::new()),
config
);-
Prerequisites
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Clone and Build
git clone <repository> cd oif-solver-rust cargo build cargo test # Verify 14/14 tests pass
-
Configuration
cp config/local.toml.example config/local.toml # Edit with your settings -
Run
cargo run
-
Test Integration
curl http://localhost:3000/api/v1/health
- Test Coverage: 14/14 tests (100% core functionality)
- Compilation: Clean build with zero errors
- Memory: Efficient Arc-based sharing
- Type Safety: Full compile-time validation
- Modularity: Easy component swapping
✅ Monolithic → Modular: Complete architectural transformation
✅ Concrete → Abstract: Trait-based design patterns
✅ Rigid → Flexible: Dependency injection support
✅ Hard to Test → Testable: Mock-friendly interfaces
✅ Coupled → Decoupled: Clear component boundaries
This implementation demonstrates production-grade Rust architecture with modern design patterns, comprehensive testing, and maximum extensibility.