# Oracle Publisher Service - Status and Configuration

**Date**: $(date)  
**VMID**: 3500  
**Status**: ⚠️ Configured but not running

---

## ✅ Current Status

### Configuration Files

- **.env File**: ✅ Fixed and configured
  - Location: `/opt/oracle-publisher/.env`
  - Status: Clean configuration with all required variables
  - Missing: `PRIVATE_KEY` (must be transmitter account)

- **Systemd Service**: ✅ Created
  - Location: `/etc/systemd/system/oracle-publisher.service`
  - Status: Loaded but not started
  - User: `oracle`
  - Working Directory: `/opt/oracle-publisher`

- **Python Script**: ✅ Copied
  - Location: `/opt/oracle-publisher/oracle_publisher.py`
  - Status: Present

- **Python Environment**: ✅ Exists
  - Location: `/opt/oracle-publisher/venv`
  - Status: Virtual environment created

---

## ⚠️ What's Missing

### 1. Private Key Configuration

The `.env` file is missing the `PRIVATE_KEY` variable. This must be set to an account that is authorized as a transmitter on the oracle contract.

**To set the private key:**

```bash
ssh root@192.168.11.10
pct exec 3500 -- bash
cd /opt/oracle-publisher
# Edit .env file
nano .env
# Add line: PRIVATE_KEY=0x...
# Save and exit
```

**Important**: The account must be authorized as a transmitter on the oracle aggregator contract (`0x99b3511a2d315a497c8112c1fdd8d508d4b1e506`).

---

## 📋 Current Configuration

```bash
# Oracle Contract Addresses
AGGREGATOR_ADDRESS=0x99b3511a2d315a497c8112c1fdd8d508d4b1e506
ORACLE_ADDRESS=0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6

# RPC Configuration
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
```

---

## 🚀 Starting the Service

Once the `PRIVATE_KEY` is configured:

### 1. Enable and Start Service

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

### 2. Check Status

```bash
pct exec 3500 -- systemctl status oracle-publisher
```

### 3. View Logs

```bash
# Follow logs in real-time
pct exec 3500 -- journalctl -u oracle-publisher -f

# View recent logs
pct exec 3500 -- journalctl -u oracle-publisher -n 50
```

### 4. Verify Oracle Updates

```bash
# Query oracle to check if price is being updated
cast call 0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6 \
  "latestRoundData()" \
  --rpc-url https://rpc-http-pub.d-bis.org

# Parse the result (answer field is in 8 decimals)
# Divide answer by 1e8 to get USD price
```

---

## 🔍 Troubleshooting

### Service Won't Start

1. **Check logs**:
   ```bash
   pct exec 3500 -- journalctl -u oracle-publisher -n 50
   ```

2. **Check configuration**:
   ```bash
   pct exec 3500 -- cat /opt/oracle-publisher/.env
   ```

3. **Verify Python script exists**:
   ```bash
   pct exec 3500 -- ls -la /opt/oracle-publisher/oracle_publisher.py
   ```

4. **Test Python script manually**:
   ```bash
   pct exec 3500 -- su - oracle -c "cd /opt/oracle-publisher && source venv/bin/activate && python oracle_publisher.py"
   ```

### Authorization Errors

If you see "Aggregator: only transmitter" errors:

1. **Verify account is transmitter**:
   - Check oracle contract for authorized transmitter addresses
   - Ensure PRIVATE_KEY matches one of the authorized accounts

2. **Check account balance**:
   - Transmitter account needs ETH for gas fees
   - Check balance: `cast balance <address> --rpc-url <rpc-url>`

### Price Not Updating

1. **Check service is running**:
   ```bash
   pct exec 3500 -- systemctl status oracle-publisher
   ```

2. **Check data sources**:
   - Verify CoinGecko API is accessible
   - Check for rate limiting

3. **Check deviation threshold**:
   - Price only updates if change > 0.5% (DEVIATION_THRESHOLD)
   - For testing, you can lower this temporarily

---

## 📊 Monitoring

### Metrics Endpoint

The service exposes Prometheus metrics on port 8000:

```bash
# Access metrics (from inside container)
curl http://localhost:8000/metrics

# Or from host (if port is forwarded)
curl http://192.168.11.68:8000/metrics
```

### Key Metrics

- `oracle_updates_sent_total` - Total number of updates sent
- `oracle_update_errors_total` - Total number of errors
- `oracle_current_price` - Current oracle price
- `oracle_price_deviation` - Price deviation from last update

---

## ✅ Next Steps

1. **Set PRIVATE_KEY** in `/opt/oracle-publisher/.env`
2. **Verify transmitter authorization** on oracle contract
3. **Start service**: `systemctl start oracle-publisher`
4. **Monitor logs** to verify updates are working
5. **Verify oracle contract** is being updated with new prices

---

## 📝 Scripts Available

- **Configure Service**: `./scripts/configure-oracle-publisher-service.sh`
- **Update Prices Manually**: `./scripts/update-all-oracle-prices.sh` (requires transmitter account)

---

**Last Updated**: $(date)

