Skip to content

Add a time-weighted average price function that computes the TWAP over a configurable ledger window #802

Description

@Chucks1093

Summary

The bonding curve spot price can be manipulated by a single large trade. A TWAP function that records price snapshots at configurable intervals and returns the average over a window of ledgers would provide a manipulation-resistant price reference for external integrations.

Scope

  • Record a price_snapshot entry (price, ledger) after every buy and sell in a persistent ring buffer of up to 100 entries
  • Add get_twap(key_id: BytesN<32>, window_ledgers: u32) as a read-only view function
  • Compute TWAP as the simple average of all snapshots within the specified ledger window
  • Return the spot price when fewer than 2 snapshots exist within the window
  • Bump the TTL of the ring buffer entries on every read and write

Acceptance Criteria

  • TWAP computed correctly as average of snapshots within the window
  • Spot price returned when fewer than 2 snapshots exist in the window
  • Ring buffer capped at 100 entries, oldest overwritten first
  • TTL bumped on ring buffer reads and writes
  • Function never panics regardless of snapshot count

ETA: 24 hours


Coordinate on Telegram

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions