You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
💰 The implementation we'll be looking at is a **price** oracle. Price oracles are one of the most common and critical types of oracles in DeFi, as they enable smart contracts to make decisions based on real-world asset prices. Our whitelist price oracle collects price reports from multiple trusted sources (instances of `SimpleOracle`) and returns their median value.
104
104
105
-
🗂️ The whitelist oracle contracts are located in `packages/hardhat/contracts/00_Whitelist/`. Go check them out and reference the following descriptions.
106
-
107
-
### Core Components
108
-
109
-
1. 🔗 **SimpleOracle (`SimpleOracle.sol`)**
110
-
111
-
- Basic oracle contract that allows price updates from the contract owner (We have commented out the onlyOwner modifier to allow you to impersonate the owner)
112
-
113
-
- Each SimpleOracle instance represents one trusted data source
114
-
115
-
2. 🏛️ **WhitelistOracle (`WhitelistOracle.sol`)**
116
-
117
-
- Central contract that aggregates multiple SimpleOracle contracts
118
-
119
-
- Only the owner can add/remove SimpleOracle contracts from the whitelist (We have commented out the onlyOwner modifier to allow you to impersonate the owner)
120
-
121
-
- Uses median calculation for consensus among whitelisted sources
122
-
123
-
- Filters out stale data to ensure freshness
124
-
125
-
- Simple architecture but requires trust in the whitelist authority
🧭 Let's understand how oracle systems are built from simple components and then aggregated for better reliability. We'll examine both the basic building block (SimpleOracle) and how multiple simple oracles can be combined into a more robust system (WhitelistOracle).
105
+
🧭 Let's understand how this oracle system works. We'll examine both the basic building block (SimpleOracle) and how multiple simple oracles can be combined into a more robust system (WhitelistOracle).
👊 **Manual Testing**: Try manually changing the price of individual SimpleOracle contracts and adding new oracle nodes to see how the aggregated price changes:
255
+
👊 **Manual Testing**: Notice how the onlyOwner modifiers are commented out to allow you to have full control. Try manually changing the price of individual SimpleOracle contracts and adding new oracle nodes to see how the aggregated price changes:
282
256
283
257
1.**Change Prices**: Use the frontend to modify individual oracle prices
284
258
@@ -304,7 +278,7 @@ yarn simulate:whitelist
304
278
- Look at these examples "in the wild" from early DeFi: [Simple Oracle](https://github.com/dapphub/ds-value), [Whitelist Oracle](https://github.com/sky-ecosystem/medianizer)
🧭 Now let's explore a decentralized oracle that uses economic incentives to ensure honest behavior. Nodes stake ETH to participate and can be slashed for bad behavior. We will also issue rewards in the form of an ERC20 token called ORA to incentivise participation in the system.
310
284
@@ -407,13 +381,26 @@ yarn simulate:staking
407
381
- Understand how economic incentives drive honest behavior
408
382
- See how slashing mechanisms enforce data freshness
409
383
- Observe the decentralized nature of the system
410
-
- Recognize the trade-offs and risks associated with type of oracle
384
+
- Recognize the trade-offs and risks associated with this type of oracle
385
+
- Oracles that require staking include [Chainlink](https://chain.link) and [PYTH](https://www.pyth.network/)
411
386
412
387
---
413
388
414
-
## Checkpoint 4: 🧠 Optimistic Oracle Architecture
389
+
## Checkpoint 3: 🧠 Optimistic Oracle Architecture
390
+
391
+
🤿 Now let's dive into the most sophisticated of this challenge's three designs: the **Optimistic Oracle**. Unlike the previous two designs that focus on price data, this one will handle any type of binary (true/false) question about real-world events.
392
+
393
+
🎯 **What makes it "optimistic"?** The system assumes proposals are correct unless someone disputes them. This creates a game-theoretic mechanism where economic incentives encourage honest behavior while providing strong security guarantees through dispute resolution.
394
+
395
+
💡 **Key Innovation**: Instead of requiring constant active participation from multiple parties (like staking oracles), optimistic oracles only require intervention when something goes wrong. This makes them highly efficient for events that don't need frequent updates.
396
+
397
+
🔍 **Real-World Applications**:
398
+
-**Cross-chain bridges**: "Did transaction X happen on chain Y?"
399
+
-**Insurance claims**: "Did flight ABC get delayed by more than 2 hours?"
400
+
-**Prediction markets**: "Did candidate X win the election?"
401
+
-**DeFi protocols**: "Did token X reach price Y on date Z?"
415
402
416
-
🧭 Before coding, understand the flow at a glance.
403
+
🧭 Before coding, let's understand the flow at a glance.
417
404
418
405
**Roles**:
419
406
-**asserter**: posts an assertion + reward
@@ -429,23 +416,28 @@ yarn simulate:staking
429
416
- Reward + bonds flow to the winner; a fixed fee goes to the decider in disputes
O-->>Winner: claimDisputedReward() -> reward + both bonds - decider fee
440
+
end
449
441
end
450
442
```
451
443
@@ -455,19 +447,25 @@ sequenceDiagram
455
447
- Before a specific deadline
456
448
- With a reward
457
449
450
+
🦗 If no one answers before the end of the assertion window, the asserter can claim a refund.
451
+
458
452
💡 If someone knows the answer within the correct time then they **propose** the answer, posting a bond. This bond is a risk to them because if their answer is thought to be wrong by someone else then they might lose it. This keeps people economically tied to the **proposals** they make.
459
453
460
-
⏳ Then if no one **disputes** the proposal before the dispute window is over then the proposal is considered to be true. The dispute window should give anyone ample time to submit a dispute.
454
+
⏳ Then if no one **disputes** the proposal before the dispute window is over then the proposal is considered to be true, and the proposer may claim the reward and their bond. The dispute window should give anyone ample time to submit a dispute.
461
455
462
456
⚖️ If someone does **dispute** during the dispute window then they must also post a bond equal to the proposer's bond. This kicks the assertion out of any particular timeline and puts it in a state where it is waiting for a decision from the **decider**. Once the decider contract has **settled** the assertion, the winner can claim the reward and both of the bonds that were posted, subtracting a small fee that goes to the decider.
463
457
464
458
🧑⚖️ Now, as we mentioned earlier, this oracle has a role called the **decider**. For this example it is just a simple contract that anyone can call to settle disputes. One could imagine in a live oracle you would want something more robust such as a group of people who vote to settle disputes.
465
459
466
460
🔗 Look at how [UMA](https://uma.xyz/) does this with their Optimistic Oracle (OO). **This contract is based UMA's OO design**.
👩💻 Now it's (finally) time to build! Unlike the previous checkpoints where you explored existing implementations, this section challenges you to implement the optimistic oracle system from scratch. You'll write the core functions that handle assertions, proposals, disputes, and settlements.
465
+
466
+
🎯 **Your Mission**: Complete the missing function implementations in the `OptimisticOracle.sol` contract. The contract skeleton is already provided with all the necessary structs, events, and modifiers - you just need to fill in the logic.
469
467
470
-
⚙️ Now let's implement the most sophisticated oracle design: the optimistic oracle. This system allows anyone to propose outcomes and others to dispute them, with a decider contract resolving disputes. This is the main implementation challenge where you'll build the core functionality from scratch.
468
+
🧪 **Testing Strategy**: Each function you implement can be tested individually using the provided test suite. Run `yarn test` after implementing each function to verify your solution works correctly.
471
469
472
470
🔍 Open the `packages/hardhat/contracts/02_Optimistic/OptimisticOracle.sol` file to implement the optimistic oracle functionality.
473
471
@@ -971,7 +969,7 @@ yarn simulate:optimistic
971
969
972
970
```
973
971
974
-
🤖 This will start automated bots that create assertions, propose outcomes, dispute proposals, and settle via the decider, so you can observe rewards, bonds, fees, and timing windows in a realistic flow.
972
+
🤖 This will start automated bots that create assertions, propose outcomes, and dispute proposals, so you can observe rewards, bonds, fees, and timing windows in a realistic flow. It is up to you to settle disputes!
975
973
976
974
### 🥅 Goals:
977
975
@@ -983,7 +981,7 @@ yarn simulate:optimistic
983
981
- Bond amounts are properly validated
984
982
---
985
983
986
-
## Checkpoint 6: 🔍 Oracle Comparison & Trade-offs
984
+
## Checkpoint 5: 🔍 Oracle Comparison & Trade-offs
987
985
988
986
🧠 Now let's analyze the strengths and weaknesses of each oracle design.
|**Security**| Low (trusted authority) | Medium (economic incentives) | High (dispute resolution) |
995
-
|**Decentralization**| Low | High |Medium|
993
+
|**Decentralization**| Low | High |Depends on Decider Implementation|
996
994
|**Cost**| Low | Medium | High |
997
995
|**Complexity**| Simple | Medium | Complex |
998
996
@@ -1024,7 +1022,7 @@ yarn simulate:optimistic
1024
1022
1025
1023
- ✅ Can be used for any type of data (not just prices)
1026
1024
1027
-
- ✴️ Decider role is the weakest link and should be carefully implemented
1025
+
- ✴️ Decider role is the weakest link and should be carefully implemented though it is up to the consuming application whether it wants to wait for a resolution or post another assertion and hope a proposal passes without dispute
1028
1026
1029
1027
- ❌ Higher latency
1030
1028
@@ -1040,7 +1038,7 @@ Each oracle design solves different problems:
1040
1038
1041
1039
---
1042
1040
1043
-
## Checkpoint 7: 💾 Deploy your contracts! 🛰
1041
+
## Checkpoint 6: 💾 Deploy your contracts! 🛰
1044
1042
1045
1043
🎉 Well done on building the optimistic oracle system! Now, let's get it on a public testnet.
1046
1044
@@ -1058,7 +1056,7 @@ Each oracle design solves different problems:
1058
1056
1059
1057
---
1060
1058
1061
-
## Checkpoint 8: 🚢 Ship your frontend! 🚁
1059
+
## Checkpoint 7: 🚢 Ship your frontend! 🚁
1062
1060
1063
1061
✏️ Edit your frontend config in `packages/nextjs/scaffold.config.ts` to change the `targetNetwork` to `chains.sepolia` (or your chosen deployed network).
1064
1062
@@ -1091,7 +1089,7 @@ For production-grade applications, it's recommended to obtain your own API keys
1091
1089
1092
1090
---
1093
1091
1094
-
## Checkpoint 9: 📜 Contract Verification
1092
+
## Checkpoint 8: 📜 Contract Verification
1095
1093
1096
1094
📝 Run the `yarn verify --network your_network` command to verify your optimistic oracle contracts on Etherscan 🛰.
1097
1095
@@ -1105,7 +1103,7 @@ For production-grade applications, it's recommended to obtain your own API keys
1105
1103
1106
1104
> 💬 Problems, questions, comments on the stack? Post them to the [🏗 scaffold-eth developers chat](https://t.me/joinchat/F7nCRK3kI93PoCOk)
1107
1105
1108
-
## Checkpoint 10: More On Oracles
1106
+
## Checkpoint 9: More On Oracles
1109
1107
1110
1108
Oracles are fundamental infrastructure for the decentralized web. They enable smart contracts to interact with real-world data, making blockchain applications truly useful beyond simple token transfers.
0 commit comments