File size: 9,428 Bytes
86deab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Testnet Deployment Guide (Phase 2 Task 5)

**Document:** Testnet deployment of QuantumRandomnessOracle contracts  
**Last Updated:** 2026-03-23  
**Status:** Ready for deployment

---

## Overview

This guide covers the deployment of the `QuantumRandomnessOracle` smart contract to major testnets and the integration with the QCrypt RNG API for end-to-end validation.

---

## Target Testnets

| Chain | Testnet | Chain ID | RPC URL | Explorer |
|-------|---------|----------|---------|----------|
| Ethereum | Sepolia | 11155111 | `https://rpc.sepolia.org` | [sepolia.etherscan.io](https://sepolia.etherscan.io) |
| Polygon | Amoy | 80002 | `https://rpc.amoy.polygon.technology` | [amoy.polygonscan.com](https://amoy.polygonscan.com) |
| BSC | BSC Testnet | 97 | `https://data-seed-prebsc-1-s1.binance.org:8545` | [testnet.bscscan.com](https://testnet.bscscan.com) |
| Avalanche | Fuji | 43113 | `https://api.avax-test.network/ext/bc/C/rpc` | [testnet.snowtrace.io](https://testnet.snowtrace.io) |
| Fantom | Fantom Testnet | 4002 | `https://rpc.testnet.fantom.network` | [testnet.ftmscan.com](https://testnet.ftmscan.com) |

---

## Prerequisites

### 1. Node.js and Dependencies

```bash
cd quantum-oracle/contracts
npm install
```

### 2. Environment Variables

Create a `.env` file in `quantum-oracle/contracts/`:

```bash
# Deployment private key (DO NOT use production keys on testnets)
PRIVATE_KEY=your_testnet_deployer_private_key

# RPC URLs (optional, defaults are configured)
SEPOLIA_RPC_URL=https://rpc.sepolia.org
POLYGON_AMOY_RPC_URL=https://rpc.amoy.polygon.technology
BSC_TESTNET_RPC_URL=https://data-seed-prebsc-1-s1.binance.org:8545
AVALANCHE_FUJI_RPC_URL=https://api.avax-test.network/ext/bc/C/rpc
FANTOM_TESTNET_RPC_URL=https://rpc.testnet.fantom.network

# Block explorer API keys (for verification)
ETHERSCAN_API_KEY=your_etherscan_api_key
POLYGONSCAN_API_KEY=your_polygonscan_api_key
BSCSCAN_API_KEY=your_bscscan_api_key
SNOWTRACE_API_KEY=your_snowtrace_api_key
FTMSCAN_API_KEY=your_ftmscan_api_key
```

### 3. Fund Deployer Account

Obtain testnet tokens for each network:

- **Sepolia ETH:** [faucet.sepolia.org](https://faucet.sepolia.org) or [sepoliafaucet.com](https://sepoliafaucet.com)
- **Polygon Amoy MATIC:** [faucet.polygon.technology](https://faucet.polygon.technology)
- **BSC Testnet BNB:** [testnet.binance.org/faucet](https://testnet.binance.org/faucet)
- **Avalanche Fuji AVAX:** [faucet.avax.network](https://faucet.avax.network)
- **Fantom Testnet FTM:** [faucet.fantom.network](https://faucet.fantom.network)

**Recommended funding:** 0.1-0.5 native tokens per network for deployment and testing.

---

## Deployment Steps

### Option 1: Deploy to All Testnets (Recommended)

```bash
cd quantum-oracle/contracts
npx hardhat run scripts/deploy-all-testnets.js --network sepolia
```

This script:
1. Deploys to all 5 testnets sequentially
2. Waits for 5 confirmations per deployment
3. Verifies contracts on block explorers (if API keys provided)
4. Saves deployment info to `deployments/` folder

### Option 2: Deploy to Single Network

```bash
# Deploy to Sepolia
npx hardhat run scripts/deploy.js --network sepolia

# Deploy to Polygon Amoy
npx hardhat run scripts/deploy.js --network polygonAmoy

# Deploy to BSC Testnet
npx hardhat run scripts/deploy.js --network bscTestnet
```

### Option 3: Deploy to Specific Networks

```bash
# Deploy to selected networks only
npx hardhat run scripts/deploy-all-testnets.js --network sepolia sepolia polygonAmoy bscTestnet
```

---

## Contract Addresses

**After deployment, update this table:**

| Chain | Contract | Address | Deployment TX | Verified |
|-------|----------|---------|---------------|----------|
| Sepolia | QuantumRandomnessOracle | `TBD` | `TBD` | ☐ |
| Polygon Amoy | QuantumRandomnessOracle | `TBD` | `TBD` | ☐ |
| BSC Testnet | QuantumRandomnessOracle | `TBD` | `TBD` | ☐ |
| Avalanche Fuji | QuantumRandomnessOracle | `TBD` | `TBD` | ☐ |
| Fantom Testnet | QuantumRandomnessOracle | `TBD` | `TBD` | ☐ |

Deployment artifacts are saved to `quantum-oracle/contracts/deployments/`:
- `deployment-{network}.json` - Individual network deployment
- `deployment-summary.json` - Combined summary
- `DEPLOYMENT_REPORT.md` - Human-readable report

---

## API Configuration

### Update Environment Variables

Add deployed contract addresses to your `.env` file in the main project root:

```bash
# Oracle contract addresses (testnets)
ORACLE_CONTRACT_SEPOLIA=0x...
ORACLE_CONTRACT_POLYGON_AMOY=0x...
ORACLE_CONTRACT_BSC_TESTNET=0x...
ORACLE_CONTRACT_AVALANCHE_FUJI=0x...
ORACLE_CONTRACT_FANTOM_TESTNET=0x...

# Testnet RPC URLs (for API backend)
TESTNET_RPC_SEPOLIA=https://rpc.sepolia.org
TESTNET_RPC_POLYGON_AMOY=https://rpc.amoy.polygon.technology
TESTNET_RPC_BSC_TESTNET=https://data-seed-prebsc-1-s1.binance.org:8545
TESTNET_RPC_AVALANCHE_FUJI=https://api.avax-test.network/ext/bc/C/rpc
TESTNET_RPC_FANTOM_TESTNET=https://rpc.testnet.fantom.network

# Chain IDs
TESTNET_CHAIN_ID_SEPOLIA=11155111
TESTNET_CHAIN_ID_POLYGON_AMOY=80002
TESTNET_CHAIN_ID_BSC_TESTNET=97
TESTNET_CHAIN_ID_AVALANCHE_FUJI=43113
TESTNET_CHAIN_ID_FANTOM_TESTNET=4002
```

### Update `app/config.py`

Add testnet configuration to the settings:

```python
# Testnet Oracle Configuration
testnet_oracle_contracts = {
    "sepolia": os.getenv("ORACLE_CONTRACT_SEPOLIA", ""),
    "polygon_amoy": os.getenv("ORACLE_CONTRACT_POLYGON_AMOY", ""),
    "bsc_testnet": os.getenv("ORACLE_CONTRACT_BSC_TESTNET", ""),
    "avalanche_fuji": os.getenv("ORACLE_CONTRACT_AVALANCHE_FUJI", ""),
    "fantom_testnet": os.getenv("ORACLE_CONTRACT_FANTOM_TESTNET", ""),
}
```

---

## End-to-End Validation

### 1. Configure Chain in API

Use the `/oracle/fulfillment/configure-chain` endpoint:

```bash
# Configure Sepolia
curl -X POST "http://localhost:8000/api/v2/oracle/fulfillment/configure-chain" \
  -d "chain=ethereum" \
  -d "rpc_url=https://rpc.sepolia.org" \
  -d "private_key=YOUR_TEST_KEY" \
  -d "explorer_url=https://sepolia.etherscan.io" \
  -d "chain_id=11155111" \
  -d "currency_symbol=SepoliaETH"
```

### 2. Create Oracle Request

```bash
curl -X POST "http://localhost:8000/api/v2/oracle/fulfillment/request" \
  -d "chain=ethereum" \
  -d "contract_address=0xYOUR_CONTRACT_ADDRESS" \
  -d "num_bytes=32" \
  -d "num_qubits=16" \
  -d "async_fulfillment=true"
```

### 3. Check Request Status

```bash
curl "http://localhost:8000/api/v2/oracle/fulfillment/status/{request_id}"
```

### 4. Verify On-Chain

Check the transaction on the block explorer:
- Commitment submission transaction
- Reveal transaction (after 2-block delay)
- Randomness value matches API response

### Validation Checklist

- [ ] Contract deployed and verified on block explorer
- [ ] API can connect to testnet RPC
- [ ] Commitment transaction submitted successfully
- [ ] Reveal transaction submitted after delay
- [ ] Randomness delivered to requester contract
- [ ] On-chain randomness matches API-generated value
- [ ] Gas costs within expected range (< 0.01 ETH per fulfillment)

---

## Gas Requirements

Estimated gas costs for oracle operations:

| Operation | Gas Limit | Sepolia (gwei) | Cost (ETH) |
|-----------|-----------|----------------|------------|
| Commit | ~50,000 | 20 | 0.001 |
| Reveal | ~100,000 | 20 | 0.002 |
| **Total** | **~150,000** | **20** | **~0.003** |

**Note:** Actual gas costs vary by network congestion and contract complexity.

---

## Troubleshooting

### Deployment Fails

**Error: "insufficient funds for gas * price + value"**
- Solution: Fund deployer account with more testnet tokens

**Error: "nonce too low"**
- Solution: Wait for pending transactions to confirm or increase gas price

**Error: "contract creation code storage out of gas"**
- Solution: Increase gas limit in deployment script

### API Cannot Connect

**Error: "Could not detect network"**
- Solution: Verify RPC URL is correct and accessible

**Error: "invalid sender"**
- Solution: Ensure private key matches the account funding transactions

### Fulfillment Fails

**Error: "Commitment not yet submitted"**
- Solution: Check that commit transaction was confirmed before reveal

**Error: "Commit-reveal delay not elapsed"**
- Solution: Wait 2 blocks between commit and reveal

**Error: "Commitment mismatch"**
- Solution: Verify randomness value matches the commitment hash

---

## Security Notes

⚠️ **Testnet Only:** These deployments are for testing and development. Do not use testnet contracts or keys in production.

⚠️ **Private Key Security:** Never commit private keys to version control. Use environment variables or secure key management.

⚠️ **Gas Limits:** Set appropriate gas limits to prevent front-running and MEV attacks in production.

---

## Next Steps

After successful testnet deployment and validation:

1. **Mainnet Deployment:** Deploy to production networks (Ethereum, Polygon, BSC, etc.)
2. **Security Audit:** Complete security review per `docs/next-phase/SECURITY_AUDIT_CHECKLIST.md`
3. **Monitoring:** Set up alerts for oracle fulfillment failures
4. **Documentation:** Update user-facing docs with contract addresses

---

## References

- [Hardhat Documentation](https://hardhat.org/docs)
- [Ethers.js Documentation](https://docs.ethers.org)
- [Chainlist](https://chainlist.org) - Network RPCs and Chain IDs
- [QCrypt Oracle Service](../../app/blockchain/oracle_service.py)

---

*Last updated: 2026-03-23*