Repository navigation
Feat twap oracle storage 1176 - #1195
Chucks1093 merged 12 commits into
Conversation
|
@somotochukwu-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
34faf60 to
7e9a8bd
Compare
dbc017d to
0c51c7a
Compare
|
Please add |
✅ PR Ready for ReviewDue to a recent malware incident affecting our repositories, all PRs are required to pass automated security checks before being reviewed for merging. This PR has passed all automated checks:
Next step: Please share this PR link in our Telegram group so a maintainer can review and merge it: https://t.me/c/accesslayerorg/8514 |
Summary
This PR adds a Time-Weighted Average Price (TWAP) oracle to the bonding curve contract by recording price snapshots on every trade and maintaining a bounded historical record for efficient on-chain price averaging. The implementation introduces cumulative price tracking, configurable snapshot retention through a ring buffer, and a public
get_twapquery that returns the average price over a requested time window while gracefully handling limited historical data.The TWAP oracle provides a more manipulation-resistant price reference than relying on a single spot price by weighting each recorded price according to the amount of time it remained active before the next snapshot.
Problem
The bonding curve currently exposes only instantaneous trade prices, which can fluctuate significantly between transactions. Consumers that require a more stable price reference—such as analytics, integrations, or future protocol components—have no native way to retrieve an average price over time.
Without historical price accumulation:
Solution
This implementation introduces an internal TWAP mechanism that continuously accumulates price history as trades occur.
Price snapshot storage
Ring buffer behavior
Time-weighted accumulation
Each interval contributes to the average according to how long that price remained active.
The TWAP is computed as:
Where:
price_iis the recorded price.Δt_iis the elapsed time until the next snapshot.total window timeis the effective averaging period.TWAP query
Adds:
get_twap(window_seconds)Behavior:
Events
Emits a
PriceSnapshotevent on every trade containing:This enables off-chain consumers to track oracle updates without reconstructing state manually.
Implementation Details
get_twap(window_seconds)interface.PriceSnapshotevent emission during trade execution.Edge Cases Covered
Testing
Tests validate:
PriceSnapshotevent emission with expected values on every trade.Acceptance Criteria Checklist
get_twapreturns accurate results for any window within available historyPriceSnapshotevent emitted correctly on every tradeCloses #1176