This project implements a hybrid oracle system that combines ETH/USD price and S&P 500 index data. It consists of Python scripts for data collection and model training, and a Node.js script for automated oracle updates.
-
Python Scripts
train_model.py
: Machine learning model trainingmodel_validation.py
: Advanced model validation and diagnosticspredict_and_monitor.py
: Real-time price monitoring and predictions
-
Node.js Script
update_oracle.js
: Smart contract oracle updater
- Hybrid model combining ETH and S&P 500 data
- Advanced feature engineering
- Multiple model comparison (XGBoost, LightGBM)
- Hyperparameter optimization
- Cross-validation with time series split
class HybridIndexPredictor:
def __init__(self):
self.models = {
'xgboost': XGBRegressor(),
'lightgbm': LGBMRegressor()
}
- Volatility calculations
- Moving averages (7, 14, 30 days)
- Momentum indicators
- Price ratios and correlations
- R-squared score
- RMSE (Root Mean Square Error)
- MAE (Mean Absolute Error)
- MAPE (Mean Absolute Percentage Error)
class ModelValidator:
def check_residuals(self)
def temporal_stability(self)
def feature_importance_analysis()
- Residual analysis
- Heteroscedasticity tests
- Autocorrelation checks
- SHAP value analysis
- Feature importance plots
- Residual distribution
- Temporal stability graphs
- SHAP summary plots
- Real-time ETH price monitoring via CoinGecko
- S&P 500 data from Alpha Vantage
- Automated alerts system
- Historical data tracking
# Environment variables
COINGECKO_API_URL=
ALPHAVANTAGE_API_KEY=
ALERT_THRESHOLD=0.05 # 5% threshold
The Node.js script automates updates to the HybridOracle smart contract by monitoring ETH/USD and S&P 500 index values.
-
OracleUpdater Class
- Update logic management
- Smart contract interaction
- Data validation
-
Configuration System
.env
for sensitive variables- Configurable update thresholds
- Retry parameters
-
Logging System
- Updates logged to
oracle_updates.json
- Detailed console logs
- Transaction tracking
- Updates logged to
async getLatestDataFromCSV()
- Reads
historical_data.csv
- Parses ETH and S&P 500 values
- Data validation
async checkContractState()
- Contract pause check
- Wallet permissions
- Operation security
async checkUpdateNeeded(latestData)
- On-chain data comparison
- Variation threshold (5%)
- External alerts via
alert.json
-
Data Validation
- Null value checks
- Format validation
- Boundary controls
-
Transaction Management
- Gas buffer (20%)
- Confirmation waiting
- Event verification
- Python 3.8+
- Node.js v14+
- npm or yarn
- Ethereum node access
# Create virtual environment
python -m venv venv
source venv/bin/activate # Unix
venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Install dependencies
npm install
# Configure environment
cp .env.example .env
# Edit .env with your values
python train_model.py
python model_validation.py
python predict_and_monitor.py
npm start
# or
node update_oracle.js
# API Configuration
WEB3_PROVIDER_URL=https://mainnet.infura.io/v3/your_key
WALLET_PRIVATE_KEY=your_private_key
CONTRACT_ADDRESS=your_contract_address
COINGECKO_API_URL=https://api.coingecko.com/api/v3
ALPHAVANTAGE_API_KEY=your_key
# Update Parameters
UPDATE_THRESHOLD=0.05
MAX_RETRIES=3
RETRY_DELAY=5000
-
Reading Errors
- Missing CSV file
- Invalid data format
-
Blockchain Errors
- Failed transactions
- Insufficient gas
- Incorrect nonce
-
Validation Errors
- Out-of-bounds data
- Contract paused
- Insufficient permissions
- Multiple transaction attempts
- Exponential delays
- Detailed error logs
- Check error logs
- Monitor gas costs
- Validate update thresholds
- Review model performance
- Adjust thresholds based on market
- Optimize gas parameters
- Update dependencies
- Retrain model periodically
- Fork the repository
- Create a feature branch
- Submit a Pull Request
- Open issues for bugs
- Consult smart contract documentation
- Check logs for debugging
MIT License