A Rust-based blockchain oracle service that mines usage and revenue data from external APIs and submits batched reports to smart contracts.
The service consists of three main components:
-
API Miner: Periodically fetches usage and revenue data from external APIs with configurable pagination and delay mechanisms to ensure data consistency.
-
Database Layer: PostgreSQL database storing revenue reports, usage reports, and mining state tracking with proper indexing for efficient queries.
-
Blockchain Client: Handles batched submission of aggregated data to smart contracts using the Alloy library for Ethereum interactions.
-
REST API: Provides endpoints for querying artifact revenue and usage data. (In Version 1 of oracle, they are just placeholders)
External APIs → API Miner → PostgreSQL → Blockchain Client → Smart Contract
↓
REST API (Query Interface)
The service runs three concurrent tasks:
- Mining Task: Fetches data from external APIs every 5 minutes with a 2-minute delay buffer
- Batching Task: Submits accumulated data to blockchain every 10 minutes in batches of 40 records
- API Server: Serves HTTP requests on port 8080
- Docker and Docker Compose
- Environment variables for sensitive configuration
Copy .env.example as .env. Override placeholder values in .env with appropriate values
# Create environment file
cp .env.example .env
# Edit .env with your configuration
# Deploy with Docker Compose
docker-compose up -d
# Check logs
docker-compose logs -f oracle-service# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Run PostgreSQL
docker-compose up -d db
# Update config.toml for local database
# Change database.host from "db" to "localhost"
# Run the service
cargo runGET /health- Health checkGET /api/v1/artifacts/{address}/six-month-revenue- Get 6-month revenue for artifactGET /api/v1/artifacts/{address}/total-usage- Get total usage for artifact
Key configuration parameters in config.toml:
- Mining Interval: 300 seconds (5 minutes)
- Mining Delay: 120 seconds buffer for data consistency
- Batch Size: 40 records per blockchain transaction
- Batch Interval: 600 seconds (10 minutes)
- Bootstrap Lookback: 86400 seconds (24 hours) for initial data mining
- External API Stability: The external API provides consistent data format and maintains reasonable uptime
- Blockchain Connectivity: Reliable RPC endpoint access with sufficient gas for batch transactions
- Data Consistency: 2-minute delay buffer is sufficient for external API data settlement
- Artifact Addresses: All artifact addresses are valid Ethereum addresses (42 characters, 0x-prefixed)
- Database Persistence: PostgreSQL data persists across service restarts via Docker volumes
- Check the working end-to-end with actual API with Sentient Chat data.
- Integrate nats-based indexer as another mining method.
- Implement API functions.
This project is licensed under the Apache 2.0 License. See the LICENSE-APACHE file for the details.