Skip to content

Implement twap oracle pool registration #115

@0x-r4bbit

Description

@0x-r4bbit

Implement the register_pool instruction for the TWAP oracle program. When a new AMM pool is created, the oracle program is notified via a chained call from the AMM's NewDefinition instruction and sets up all accounts needed to begin accumulating price data for that pool.

Background

Every AMM pool needs three oracle-owned accounts before the oracle can start tracking its price:

  • A Registered Feed Account storing feed metadata
  • An Observation Account holding the ring buffer of accumulator snapshots
  • A Price Account holding the canonical published price that consumers read

These are created once at pool creation and never recreated. Registration is triggered automatically via a chained call from AMM
NewDefinition

Accounts

Registered Feed Account (PDA, created)

Derived from (oracle_program_id, pool_id). Fixed-size. Stores:

  • pool_id: AccountId — the AMM pool account to read from
  • base_asset: AccountId — canonical identifier for token A
  • quote_asset: AccountId — canonical identifier for token B
  • max_tick_delta: i32 — per-pool manipulation floor, initialised from Oracle Config default
  • last_recorded_tick: i32 — initialised to the pool's opening tick, used by update_accumulator for truncation delta computation

Observation Account (PDA, created)

Derived from (oracle_program_id, pool_id). Variable-size (resizable in a follow-up issue for cardinality expansion). Stores:

  • write_pointer: u16 — index of next slot to write
  • cardinality: u16 — number of allocated slots (initialised to 1)
  • observations: [(timestamp: u64, tick_cumulative: i64); cardinality]

**^ This account struct might need a different shape based on what Rust allows for

Price Account (PDA, created)

Derived from (oracle_program_id, base_asset, quote_asset, source_id). Stores the canonical OraclePriceAccount struct (defined as a
standalone SPEL IDL artefact — see related issues). Initialised with zeroed price and timestamp — consumers must treat a zero price as
unavailable until publish_price has been called at least once.

Steps:

  1. Read current_tick, last_observation_timestamp, definition_token_a_id, definition_token_b_id from the pool account
  2. Create Registered Feed Account with last_recorded_tick = current_tick and max_tick_delta from Oracle Config
  3. Create Observation Account with cardinality 1, initialised to (timestamp=now, tick_cumulative=0)
  4. Create Price Account with zeroed price and timestamp, source_id = oracle_program_id, base_asset = definition_token_a_id, quote_asset =
    definition_token_b_id

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions