Repository navigation
Feat twap oracle storage 1176 - #1195
Open
somotochukwu-dev wants to merge 12 commits into
Open
somotochukwu-dev wants to merge 12 commits into
somotochukwu-dev wants to merge 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! 🚀 |
somotochukwu-dev
force-pushed
the
feat-twap-oracle-storage-1176
branch
from
October 26, 2026 23:51
34faf60 to
7e9a8bd
Compare
somotochukwu-dev
force-pushed
the
feat-twap-oracle-storage-1176
branch
from
October 27, 2026 11:05
dbc017d to
0c51c7a
Compare
Member
|
Please add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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