# Update All Oracle Prices - Guide

**Date**: $(date)  
**ChainID**: 138  
**Purpose**: Update all oracle contracts with current token prices from CoinGecko

---

## 📋 Overview

This script updates all oracle contracts listed in the token list with current market prices fetched from CoinGecko API.

### Current Token List

| Token | Symbol | Address | Oracle Address | CoinGecko ID |
|-------|--------|---------|----------------|--------------|
| ETH/USD Price Feed | ETH-USD | `0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6` | `0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6` | ethereum |
| Wrapped Ether | WETH | `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2` | `0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6` | ethereum |
| Wrapped Ether v10 | WETH10 | `0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F` | `0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6` | ethereum |
| Chainlink Token | LINK | `0x514910771AF9Ca656af840dff83E8264EcF986CA` | *(no oracle)* | chainlink |

**Note**: 
- WETH and WETH10 use the ETH/USD oracle (1:1 price with ETH)
- LINK token does not have an oracle contract yet - would need separate LINK/USD oracle

---

## 🚀 Usage

### Prerequisites

1. **Private Key**: Set `DEPLOYER_PRIVATE_KEY` environment variable OR pass as argument
2. **Foundry/cast**: Must have `cast` command available
3. **Python3**: Required for price calculations
4. **jq**: Required for JSON parsing
5. **bc**: Required for floating point comparisons

### Basic Usage

```bash
# Using environment variable for private key
export DEPLOYER_PRIVATE_KEY="0x..."
./scripts/update-all-oracle-prices.sh

# Using command line arguments
./scripts/update-all-oracle-prices.sh https://rpc-http-pub.d-bis.org 0x...
```

### Arguments

1. **RPC URL** (optional): Defaults to `https://rpc-http-pub.d-bis.org`
2. **Private Key** (optional): Defaults to `$DEPLOYER_PRIVATE_KEY` environment variable

---

## 📊 How It Works

1. **Read Token List**: Parses `token-lists/lists/dbis-138.tokenlist.json`
2. **Map Tokens to CoinGecko**: Maps token symbols to CoinGecko API IDs
3. **Fetch Prices**: Gets current USD prices from CoinGecko API
4. **Check Oracle Contracts**: Queries current oracle prices
5. **Update if Needed**: Only updates if price difference > 0.5% (to save gas)
6. **Verify Update**: Confirms transaction was successful

### Price Sources

- **ETH, WETH, WETH10**: CoinGecko `ethereum` price
- **LINK**: CoinGecko `chainlink` price (if oracle exists)

---

## 🔧 Configuration

### Token Mappings

Edit the script to add more tokens:

```bash
# Add to TOKEN_MAP
declare -A TOKEN_MAP=(
    ["NEWTOKEN"]="coingecko-id"
)

# Add to ORACLE_ADDRESSES
declare -A ORACLE_ADDRESSES=(
    ["NEWTOKEN"]="0x..."
)
```

### Oracle Update Methods

The script tries these methods in order:
1. `updateAnswer(int256)`
2. `transmit(int256)`
3. `setLatestAnswer(int256)`

---

## ✅ Verification

After running the script, verify prices:

```bash
# Check ETH/USD oracle
cast call 0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6 \
  "latestRoundData()" \
  --rpc-url https://rpc-http-pub.d-bis.org

# Parse result (answer field is in 8 decimals)
# Format: (roundId, answer, startedAt, updatedAt, answeredInRound)
# Divide answer by 1e8 to get USD price
```

### Expected Output

```
=========================================
Update All Oracle Prices
=========================================

[INFO] RPC URL: https://rpc-http-pub.d-bis.org
[INFO] Token List: /home/intlc/projects/proxmox/token-lists/lists/dbis-138.tokenlist.json

[✓] RPC is accessible

[INFO] Tokens to update: ethereum chainlink

[INFO] Fetching prices from CoinGecko...
[✓] Prices fetched successfully

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Updating Oracle Contracts
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

[INFO] ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[INFO] Updating: ETH/USD Price Feed (ETH-USD)
[DETAIL] Oracle Address: 0x3304b747e565a97ec8ac220b0b6a1f6ffdb837e6
[DETAIL] Current Price: $2943.34 USD
[DETAIL] Price in 8 decimals: 294334000000
[INFO] Sending transaction to update oracle...
[✓] Transaction sent using updateAnswer: 0x...
[✓] Oracle updated: $2943.34 USD

=========================================
Summary
=========================================

[✓] Successfully updated: 1 oracle(s)
```

---

## ⚠️ Limitations

1. **Single Oracle Contract**: Currently only ETH/USD oracle exists
   - WETH and WETH10 use the same oracle (correct, as they track ETH 1:1)
   - LINK needs a separate oracle contract for LINK/USD

2. **Gas Costs**: Each update requires a transaction, so costs gas
   - Script skips updates if price change < 0.5% to save gas

3. **CoinGecko Rate Limits**: Free API has rate limits
   - Script fetches all prices in one API call to minimize requests

---

## 🔄 Automatic Updates

For automatic price updates, use the Oracle Publisher Service (VMID 3500):

1. **Configure Oracle Publisher**: Set up service on VMID 3500
2. **Set Update Interval**: Configure to update every 60 seconds
3. **Price Sources**: Configure multiple sources (CoinGecko, Binance, etc.)
4. **Start Service**: Service will automatically update prices

See: `docs/METAMASK_USD_PRICE_FIX.md` for Oracle Publisher setup

---

## 📝 Adding New Tokens

To add a new token oracle:

1. **Deploy Oracle Contract**: Deploy Chainlink-compatible aggregator
2. **Add to Token List**: Add token to `token-lists/lists/dbis-138.tokenlist.json`
3. **Update Script**: Add mappings to `TOKEN_MAP` and `ORACLE_ADDRESSES`
4. **Test**: Run script and verify oracle updates

---

## 🐛 Troubleshooting

### Error: "Private key required"
- Set `DEPLOYER_PRIVATE_KEY` environment variable OR pass as argument

### Error: "RPC is not accessible"
- Check RPC URL is correct and accessible
- Try: `curl -X POST https://rpc-http-pub.d-bis.org -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'`

### Error: "Failed to fetch prices from CoinGecko"
- Check internet connection
- CoinGecko API may be rate-limited (wait and retry)
- Verify CoinGecko IDs are correct

### Error: "Failed to update oracle"
- Check oracle contract address is correct
- Verify contract has `updateAnswer`, `transmit`, or `setLatestAnswer` function
- Ensure private key has sufficient balance for gas
- Check transaction was successful on block explorer

---

**Last Updated**: $(date)

