This PR implements comprehensive Chainlink oracle integration for the NEPA decentralized utility payment platform, enabling real-time external data feeds including exchange rates, utility rates, and external API validation.
- Real-time exchange rate feeds for cryptocurrency pairs (ETH/USD, BTC/USD, USDC/USD)
- Fiat currency pairs (NGN/USD, EUR/USD, GBP/USD)
- Configurable decimal precision and reliability scoring
- Automatic price feed updates every 5 minutes
- Real-time utility rates for electricity, water, and gas
- Region-specific rate management (e.g., electricity_LAGOS)
- Per-unit consumption billing with live rates
- Hourly update scheduling for utility rates
- Range validation for price bounds and reasonable limits
- Decimal precision checking with tolerance for floating-point conversions
- Timestamp freshness validation (configurable max age)
- Reliability score filtering (minimum 70% by default)
- Cached data fallback when primary oracle fails
- Configurable fallback enablement
- Multiple data source support architecture
- Graceful degradation for service continuity
- Automated price feed updates (5-minute intervals, configurable)
- Utility rate updates (1-hour intervals, configurable)
- Configurable update frequencies
- Timestamp-based update tracking
- Per-call cost limits (default: 0.001 XLM)
- Daily spending limits with automatic reset
- Comprehensive cost tracking and analytics
- Budget optimization and spending controls
- Success rate tracking (0-100 score calculation)
- Response time monitoring and averaging
- Historical performance metrics
- Quality assessment based on multiple factors
src/oracle.rs- Complete OracleManager contract implementationsrc/tests.rs- Comprehensive test suite (15+ test cases)ORACLE_INTEGRATION_DOCUMENTATION.md- Detailed technical documentationBUILD_INSTRUCTIONS.md- Build, deployment, and testing guide
src/lib.rs- Enhanced NepaBillingContract with oracle integrationCargo.toml- Updated dependencies for oracle functionality
- OracleManager Contract - Manages all oracle operations
- PriceFeed Structure - Handles exchange rate data
- UtilityRate Structure - Manages utility rate information
- Reliability System - Tracks oracle performance and reliability
- Cost Management - Controls oracle call costs
- Fallback Mechanisms - Provides backup data sources
// Real-time currency conversion billing
pay_bill_with_oracle(env, from, token_address, meter_id, amount, currency, use_exchange_rate)
// Consumption-based billing with live utility rates
pay_utility_bill(env, from, token_address, meter_id, kwh_consumed, utility_type, region, currency)// Price feed management
add_price_feed(env, admin, feed_id, price_feed)
update_price_feed(env, feed_id, new_price, timestamp)
get_price_feed(env, feed_id)
// Utility rate management
add_utility_rate(env, admin, rate_id, utility_rate)
update_utility_rate(env, rate_id, new_rate, timestamp)
get_utility_rate(env, rate_id)
// Oracle statistics and monitoring
get_oracle_stats(env)
should_update_oracles(env)pub struct PriceFeed {
pub feed_address: Address, // Chainlink feed contract address
pub base_asset: String, // Base currency (e.g., "ETH")
pub quote_asset: String, // Quote currency (e.g., "USD")
pub decimals: u32, // Decimal precision
pub last_updated: u64, // Last update timestamp
pub price: i128, // Current price with decimals
pub reliability_score: u8, // Reliability score (0-100)
}pub struct UtilityRate {
pub utility_type: String, // Type of utility (electricity, water, gas)
pub rate_per_kwh: i128, // Rate per unit of consumption
pub currency: String, // Currency code
pub region: String, // Geographic region
pub last_updated: u64, // Last update timestamp
pub reliability_score: u8, // Reliability score (0-100)
}- ✅ Oracle initialization and configuration
- ✅ Price feed management and updates
- ✅ Utility rate management and updates
- ✅ Data validation mechanisms
- ✅ Fallback functionality
- ✅ Reliability scoring
- ✅ Cost management
- ✅ Enhanced billing operations
- ✅ Error handling and edge cases
All tests pass successfully, validating:
- Correct data flow and validation
- Proper error handling for invalid data
- Reliability scoring accuracy
- Cost management enforcement
- Fallback mechanism functionality
- Admin-only functions: Oracle configuration and feed management
- Public functions: Rate retrieval and billing operations
- Authentication: All state-changing operations require authentication
- Input validation: All oracle data is validated before use
- Range checking: Prevents extreme or invalid values
- Timestamp verification: Ensures data freshness
- Reliability filtering: Rejects low-quality data
- Spending limits: Prevents runaway oracle costs
- Per-call limits: Caps individual call costs
- Daily budgets: Controls overall spending
- Emergency stops: Can disable oracle calls if needed
- Optimized storage patterns for oracle data
- Fixed-point arithmetic for precise calculations
- Efficient update scheduling to minimize unnecessary calls
- Batch operations for multiple oracle updates
- Minimal gas footprint for billing operations
# Build for development
cargo build
# Build for release (optimized)
cargo build --release
# Run tests
cargo test
# Generate WASM contract
cargo build --release --target wasm32-unknown-unknownlet config = OracleConfig {
max_age_seconds: 300, // 5 minutes
min_reliability_score: 70, // 70% minimum reliability
fallback_enabled: true, // Enable fallbacks
cost_limit_per_call: 1000000, // 0.001 XLM per call
};- Complete API documentation with usage examples
- Architecture overview with data structures and workflows
- Security guidelines and best practices
- Build and deployment instructions
- Troubleshooting guide for common issues
The architecture supports future improvements:
- Multi-oracle aggregation
- Machine learning rate predictions
- Dynamic pricing models
- Cross-chain oracle support
- Advanced analytics and insights
This oracle integration enables NEPA to:
- Provide accurate billing with real-time utility rates and exchange rates
- Support multi-currency payments with seamless conversions
- Ensure reliability through multiple fallback mechanisms
- Control costs with intelligent spending management
- Maintain transparency with auditable data sources and validation
- All acceptance criteria implemented
- Comprehensive test coverage
- Documentation complete
- Security considerations addressed
- Performance optimized
- Build instructions provided
- Code follows project conventions
- Ready for production deployment
- Closes #22 - Oracle Integration for External Data
Tech Stack: Chainlink, Soroban Rust, Stellar Blockchain Files Changed: 6 files, 1663 insertions(+), 1 deletion(-) Test Coverage: 15+ comprehensive test cases