Skip to content

feat(twap-oracle): implement create_price_feed instruction#128

Open
0x-r4bbit wants to merge 1 commit into
mainfrom
feat/twap_register_feed
Open

feat(twap-oracle): implement create_price_feed instruction#128
0x-r4bbit wants to merge 1 commit into
mainfrom
feat/twap_register_feed

Conversation

@0x-r4bbit
Copy link
Copy Markdown
Collaborator

@0x-r4bbit 0x-r4bbit commented May 28, 2026

Adds the CreatePriceObservations instruction to the TWAP oracle program.
The instruction initialises a PriceObservations PDA for a given price
source account and time window, writing the initial tick and timestamp
as the first entry.

Key design decisions:

  • Per-window accounts: each (price_source, window_duration) pair maps to
    a distinct PriceObservations PDA. The window duration is baked into the
    PDA seed so a single price source can support multiple TWAP windows
    (24h, 7d, 30d) at independent sampling rates without sharing a buffer.

  • window_duration not stored on struct: it is implicit in the PDA address.
    Any reader that located the account already knows the window duration
    used to derive it. Storing it would be redundant.

  • Authorization is implicit: the PriceObservations PDA is derived from
    the price source account ID, so is_authorized = true on the price source
    proves the caller controls it without a redundant authority field.

  • Impersonation is prevented by the PDA check: passing a controlled price
    source with a victim's observations account ID fails immediately because
    the computed PDA (from the attacker's source) does not match.

Closes #126

@0x-r4bbit 0x-r4bbit requested review from 3esmit and gravityblast May 28, 2026 08:48
@0x-r4bbit 0x-r4bbit force-pushed the feat/twap_register_feed branch 2 times, most recently from 643adef to 0b97584 Compare May 28, 2026 08:58
@0x-r4bbit 0x-r4bbit requested a review from Copilot May 28, 2026 10:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements TWAP oracle “price feed registration” by adding a CreatePriceFeed instruction that initializes a PriceObservations PDA for a given price source and records the initial tick + timestamp, replacing the prior no-op instruction.

Changes:

  • Add CreatePriceFeed instruction in twap_oracle_core plus PriceObservations/ObservationEntry data model and PDA helpers.
  • Implement program-side create_price_feed logic and expose it via the guest method entrypoint.
  • Update dependencies, locks, and regenerated IDL artifacts accordingly.

Reviewed changes

Copilot reviewed 10 out of 16 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
programs/twap_oracle/src/noop.rs Removes the obsolete no-op instruction implementation.
programs/twap_oracle/src/lib.rs Switches exported module from noop to create_price_feed.
programs/twap_oracle/src/create_price_feed.rs Adds the instruction implementation and unit tests for initializing PriceObservations.
programs/twap_oracle/methods/guest/src/bin/twap_oracle.rs Exposes the new create_price_feed instruction through the guest entrypoint.
programs/twap_oracle/methods/guest/Cargo.toml Adds clock_core dependency for the guest build.
programs/twap_oracle/methods/guest/Cargo.lock Locks clock_core and bumps transitive deps for the guest build.
programs/twap_oracle/core/src/lib.rs Adds CreatePriceFeed, PriceObservations, ObservationEntry, capacity constant, and PDA derivation helpers.
programs/twap_oracle/core/Cargo.toml Enables workspace lints and adds risc0-zkvm dependency used for PDA seed hashing.
programs/twap_oracle/Cargo.toml Adds clock_core dependency and enables workspace lints for the program crate.
programs/token/methods/guest/Cargo.lock Transitive dependency lockfile updates.
programs/stablecoin/methods/guest/Cargo.lock Transitive dependency lockfile updates.
programs/ata/methods/guest/Cargo.lock Transitive dependency lockfile updates.
programs/amm/methods/guest/Cargo.lock Transitive dependency lockfile updates.
Cargo.lock Workspace lockfile updates reflecting new/updated dependencies.
artifacts/twap_oracle-idl.json Regenerated TWAP oracle IDL reflecting create_price_feed and new account/type definitions.
artifacts/stablecoin-idl.json Regenerated stablecoin IDL reflecting newly visible types from twap_oracle_core.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread programs/twap_oracle/core/src/lib.rs
Comment thread programs/twap_oracle/src/create_price_observations.rs
Adds the CreatePriceObservations instruction to the TWAP oracle program.
The instruction initialises a PriceObservations PDA for a given price
source account and time window, writing the initial tick and timestamp
as the first entry.

Key design decisions:

- Per-window accounts: each (price_source, window_duration) pair maps to
  a distinct PriceObservations PDA. The window duration is baked into the
  PDA seed so a single price source can support multiple TWAP windows
  (24h, 7d, 30d) at independent sampling rates without sharing a buffer.

- window_duration not stored on struct: it is implicit in the PDA address.
  Any reader that located the account already knows the window duration
  used to derive it. Storing it would be redundant.

- Authorization is implicit: the PriceObservations PDA is derived from
  the price source account ID, so is_authorized = true on the price source
  proves the caller controls it without a redundant authority field.

- Impersonation is prevented by the PDA check: passing a controlled price
  source with a victim's observations account ID fails immediately because
  the computed PDA (from the attacker's source) does not match.

Closes #126
@0x-r4bbit 0x-r4bbit force-pushed the feat/twap_register_feed branch from e36903f to c59316b Compare May 29, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement TWAP oracle price feed creation

2 participants