Oracles
What are Price Oracles?
A price oracle is any tool used to view price information about a given asset. When you check token prices on your phone, you're using your phone as a price oracle. Similarly, the app on your phone gets this data from various sources that aggregate and display price information to you - these are all part of the oracle system.
The Oracle Challenge in DeFi
When building applications that interact with Zealous Swap or other DeFi protocols on Kaspa, developers need reliable on-chain price data. This raises an important question: what's the best way to get accurate, manipulation-resistant price information directly on the blockchain?
Many oracle designs across DeFi have been implemented with varying degrees of security, leading to high-profile exploits where the oracle was the primary attack vector.
Zealous Swap's Oracle Solution
Zealous Swap implements a secure, decentralized price oracle system based on the battle-tested TWAP architecture, providing reliable price data for the entire Kaspa ecosystem.
How Our Oracle Works
Beginning-of-Block Price Measurement: Every token pair measures the market price at the beginning of each block, before any trades take place. This price is difficult to manipulate because it's set by the last transaction in the previous block.
Cumulative Price Tracking: Zealous Swap records this price in a cumulative-price variable weighted by the time this price existed. This creates a running sum of prices throughout the entire history of the contract.
Time-Weighted Average Price (TWAP): External contracts can use this data to calculate accurate time-weighted average prices across any time interval.
Using the TWAP Oracle
The TWAP is calculated by:
Reading the cumulative price from a token pair at the start of your desired period
Reading it again at the end of your period
Finding the difference between these values
Dividing by the time elapsed
This gives you a manipulation-resistant average price for that specific time period.
Practical Applications
Zealous Swap's oracle enables:
Other dApps to access reliable price data
Lending protocols to calculate accurate collateral values
Derivatives platforms to determine settlement prices
Smart contracts to make decisions based on market conditions
Oracle Security and Manipulation Resistance
The cost of manipulating Zealous Swap's oracle increases with:
The amount of liquidity in the pool (higher liquidity = more costly to attack)
The length of the time period being averaged (longer period = more costly to attack)
For example, manipulating a 1-hour TWAP by 5% would require an attacker to move the price by 5% in every block for an entire hour, resulting in significant losses to arbitrage and fees.
Usage Tips
For a 10-minute TWAP, sample the cumulative price once every 10 minutes
For a 1-week TWAP, sample once every week
Longer time periods provide greater manipulation resistance but less responsiveness to recent price changes
Higher liquidity pools provide more accurate and secure price feeds
Developer Integration
Zealous Swap's oracle functionality is accessible to any developer building on Kaspa. By supporting reliable price feeds, we aim to foster a robust DeFi ecosystem where developers can build secure financial applications with confidence.
Last updated