Plutus: Oracles + PAB + Swap dApp
Topic: Oracle-based smart contract with update & usage logic Includes: On-chain logic, off-chain contract, EmulatorTrace, and PAB integration Tools: Plutus, Haskell, Template Haskell, PAB
🔢 Table of Contents
🔍 What Are Oracles?
⚙️ Oracle Implementation Overview
🧹 Oracle Types and Core Logic
⚖️ Swap Contract (Using the Oracle)
📊 Oracle Lifecycle
📚 Off-Chain Contracts
🔢 Test with EmulatorTrace
✨ PAB Integration
💡 Glossary of Key Terms
📄 Next Steps
1️⃣ 🔍 What Are Oracles?
An oracle bridges off-chain data to the blockchain. Smart contracts on Cardano can't directly access external data like exchange rates, weather, or random numbers. Oracles solve this by publishing such data on-chain in a secure and verifiable way.
Examples:
ADA/USD exchange rates
Weather data for insurance contracts
Randomness for games
Sports match results
2️⃣ ⚙️ Oracle Implementation Overview
We implement an oracle that provides an ADA/USD exchange rate using:
⚫ A UTxO carrying the exchange rate in its datum
🏷️ An NFT to uniquely identify the "true" oracle output
✍️ A validator supporting two actions:
Update
(by the oracle operator)Use
(by contracts that read the value)
3️⃣ 🧹 Oracle Types and Core Logic
🔹 Oracle
Record (Parameters)
Oracle
Record (Parameters)data Oracle = Oracle
{ oSymbol :: CurrencySymbol
, oOperator :: PubKeyHash
, oFee :: Integer
, oAsset :: AssetClass
}
🔹 Redeemer
data OracleRedeemer = Update | Use
🔹 Validator: mkOracleValidator
mkOracleValidator
mkOracleValidator :: Oracle -> Integer -> OracleRedeemer -> ScriptContext -> Bool
Key checks:
🔒 NFT must be present in both input/output
🛎️ If
Update
: Signed by operator & datum is valid✅ If
Use
: Oracle value unchanged & fee paid
4️⃣ ⚖️ Swap Contract (Using the Oracle)
Users deposit ADA at a swap script address. Another user can buy ADA by providing the required USDT (price calculated using the oracle).
👤 Datum: PubKeyHash (seller)
➞ Redeemer: ()
(unit)
()
(unit)🔹 Swap Validator: mkSwapValidator
mkSwapValidator
mkSwapValidator :: Oracle -> Address -> PubKeyHash -> () -> ScriptContext -> Bool
Key logic:
If signed by seller: refund allowed
If used by buyer:
Requires oracle input
Enforces price based on oracle value
Checks correct amount of USDT paid to seller
5️⃣ 📊 Oracle Lifecycle
1
Mint NFT
Oracle
—
Token identifies oracle
2
Start oracle
Oracle
Update
Initial value set
3
Update value
Oracle
Update
Datum updated
4
Use value
Any contract
Use
Oracle value consumed
6️⃣ 📚 Off-Chain Contracts
🔹 startOracle
startOracle
Mints NFT
Constructs Oracle parameters
Uses
forgeContract
🔹 updateOracle
updateOracle
Looks up UTxO with NFT
Consumes it, creates a new one with updated datum
Paid fees can be collected by oracle operator
🔹 runOracle
runOracle
Combines
startOracle
and loopsupdateOracle
🔹 offerSwap
, useSwap
, retrieveSwaps
offerSwap
, useSwap
, retrieveSwaps
offerSwap
: Locks ADA at swap addressuseSwap
: Uses oracle to calculate USDT price and executes swapretrieveSwaps
: Seller reclaims their ADA if not swapped
7️⃣ 🔢 Test with EmulatorTrace
🏋️ Actors:
Wallet 1: Oracle operator
Wallets 2–5: Swap participants
🔹 Test Flow:
Oracle is started, value initialized
Swaps are offered
Oracle is used to complete swaps
Oracle is updated, fees collected
8️⃣ ✨ PAB Integration
🔹 oracle-pab.hs
oracle-pab.hs
Starts web server
Initializes oracle, swaps, and distributes tokens
Writes contract instance IDs to disk
🔹 oracle-client.hs
oracle-client.hs
Reads oracle instance ID
Fetches live rate (e.g., from CoinMarketCap)
Calls update endpoint every few seconds
🔹 swap-client.hs
swap-client.hs
CLI tool for Wallets
Endpoints:
Offer
Retrieve
Use
View funds
9️⃣ 💡 Glossary of Key Terms
Oracle
Bridge between off-chain data and smart contracts
NFT
Uniquely identifies a UTxO (oracle instance)
Datum
Stores data like price in UTxO
Redeemer
Input to a script that describes action taken
UTxO
Unspent Transaction Output
Validator
On-chain function that enforces logic
PAB
Plutus Application Backend for real-world deployment
AssetClass
Tuple of CurrencySymbol + TokenName
🔣 10. 📄 Next Steps
📚 Build a front-end UI for the swap dApp
✨ Extend oracle to support median-of-n feeds
⚖️ Add slashing for invalid data via staking
🎯 Deploy on Cardano testnet using real oracle feeds
Last updated