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)πΉ Redeemer
πΉ Validator: mkOracleValidator
mkOracleValidatorKey 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
mkSwapValidatorKey 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
startOracleMints NFT
Constructs Oracle parameters
Uses
forgeContract
πΉ updateOracle
updateOracleLooks up UTxO with NFT
Consumes it, creates a new one with updated datum
Paid fees can be collected by oracle operator
πΉ runOracle
runOracleCombines
startOracleand loopsupdateOracle
πΉ offerSwap, useSwap, retrieveSwaps
offerSwap, useSwap, retrieveSwapsofferSwap: 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.hsStarts web server
Initializes oracle, swaps, and distributes tokens
Writes contract instance IDs to disk
πΉ oracle-client.hs
oracle-client.hsReads oracle instance ID
Fetches live rate (e.g., from CoinMarketCap)
Calls update endpoint every few seconds
πΉ swap-client.hs
swap-client.hsCLI 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