# Oracle Publisher Service - Setup Complete

**Date**: $(date)  
**VMID**: 3500  
**Status**: ✅ **Configured and Started**

---

## ✅ Completed Tasks

### 1. Configuration Files
- ✅ Fixed corrupted `.env` file
- ✅ Configured all oracle addresses
- ✅ Set data sources (CoinGecko, Binance)
- ✅ Configured update intervals and thresholds
- ✅ Set PRIVATE_KEY (transmitter account)

### 2. Python Script
- ✅ Copied `oracle_publisher.py` to container
- ✅ Set correct permissions (755)
- ✅ Fixed ownership (oracle:oracle)

### 3. Python Environment
- ✅ Verified virtual environment exists
- ✅ Installed required packages (web3, eth-account, requests, etc.)

### 4. Systemd Service
- ✅ Created service file
- ✅ Enabled service
- ✅ Started service

---

## 📋 Current Configuration

```bash
# Oracle Contracts
AGGREGATOR_ADDRESS=0x99b3511a2d315a497c8112c1fdd8d508d4b1e506
ORACLE_ADDRESS=0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6

# Network
RPC_URL=http://192.168.11.250:8545
WS_URL=ws://192.168.11.250:8546
CHAIN_ID=138

# Update Settings
UPDATE_INTERVAL=60
HEARTBEAT_INTERVAL=60
DEVIATION_THRESHOLD=0.5

# Data Sources
DATA_SOURCE_1_URL=https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd
DATA_SOURCE_1_PARSER=coingecko
DATA_SOURCE_2_URL=https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT
DATA_SOURCE_2_PARSER=binance

# Metrics
METRICS_PORT=8000
METRICS_ENABLED=true
```

---

## 🔍 Service Status

### Check Status

```bash
ssh root@192.168.11.10 "pct exec 3500 -- systemctl status oracle-publisher"
```

### View Logs

```bash
# Follow logs in real-time
ssh root@192.168.11.10 "pct exec 3500 -- journalctl -u oracle-publisher -f"

# View recent logs
ssh root@192.168.11.10 "pct exec 3500 -- journalctl -u oracle-publisher -n 50"
```

### Verify Oracle Updates

```bash
# Query oracle for latest price
cast call 0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6 \
  "latestRoundData()" \
  --rpc-url https://rpc-http-pub.d-bis.org

# The answer field (in 8 decimals) represents the ETH/USD price
# Divide by 1e8 to get USD price
```

### Check Metrics

```bash
# Access Prometheus metrics
ssh root@192.168.11.10 "pct exec 3500 -- curl -s http://localhost:8000/metrics | grep oracle"
```

---

## 🔄 Service Management

### Start Service

```bash
ssh root@192.168.11.10 "pct exec 3500 -- systemctl start oracle-publisher"
```

### Stop Service

```bash
ssh root@192.168.11.10 "pct exec 3500 -- systemctl stop oracle-publisher"
```

### Restart Service

```bash
ssh root@192.168.11.10 "pct exec 3500 -- systemctl restart oracle-publisher"
```

### Enable Auto-Start

```bash
ssh root@192.168.11.10 "pct exec 3500 -- systemctl enable oracle-publisher"
```

---

## 📊 Expected Behavior

The Oracle Publisher service will:

1. **Fetch Prices** every 60 seconds from:
   - CoinGecko API (primary)
   - Binance API (fallback)

2. **Calculate Median Price** from multiple sources

3. **Check Deviation** - Only update if price change > 0.5%

4. **Update Oracle Contract** with new price if needed

5. **Expose Metrics** on port 8000 for monitoring

---

## 🐛 Troubleshooting

### Service Not Running

```bash
# Check status
ssh root@192.168.11.10 "pct exec 3500 -- systemctl status oracle-publisher"

# Check logs for errors
ssh root@192.168.11.10 "pct exec 3500 -- journalctl -u oracle-publisher -n 50"
```

### Authorization Errors

If you see "Aggregator: only transmitter":
- Verify PRIVATE_KEY account is authorized as transmitter
- Check account has sufficient ETH for gas fees

### Price Not Updating

1. Check service is running
2. Check logs for errors
3. Verify data sources are accessible
4. Check deviation threshold (only updates if change > 0.5%)
5. Verify oracle contract is being updated

### Python Errors

```bash
# Test Python script manually
ssh root@192.168.11.10 "pct exec 3500 -- su - oracle -c 'cd /opt/oracle-publisher && source venv/bin/activate && python oracle_publisher.py'"
```

---

## 📈 Monitoring

### Key Metrics

- `oracle_updates_sent_total` - Total updates sent to blockchain
- `oracle_update_errors_total` - Total errors encountered
- `oracle_current_price` - Current oracle price (USD)
- `oracle_price_deviation` - Price deviation from last update (%)

### Log Monitoring

Monitor logs for:
- Successful price updates
- Transaction confirmations
- API errors from data sources
- Authorization errors

---

## ✅ Verification Checklist

- [x] Service file created and configured
- [x] .env file configured with all settings
- [x] PRIVATE_KEY set (transmitter account)
- [x] Python script copied and has correct permissions
- [x] Python packages installed
- [x] Service started and running
- [ ] Service logs show successful operation
- [ ] Oracle contract receiving price updates
- [ ] Metrics endpoint accessible

---

## 📚 Related Documentation

- `docs/ORACLE_UPDATE_AUTHORIZATION.md` - Authorization requirements
- `docs/METAMASK_USD_PRICE_FIX.md` - MetaMask integration
- `docs/UPDATE_ALL_ORACLE_PRICES.md` - Manual update guide
- `docs/ORACLE_PUBLISHER_SERVICE_STATUS.md` - Status and troubleshooting

---

**Last Updated**: $(date)  
**Status**: ✅ Service configured and started

