Skip to content

feat(amm): create TWAP price observations on behalf of the pool#148

Open
0x-r4bbit wants to merge 1 commit into
mainfrom
feat/amm-create-observations-account
Open

feat(amm): create TWAP price observations on behalf of the pool#148
0x-r4bbit wants to merge 1 commit into
mainfrom
feat/amm-create-observations-account

Conversation

@0x-r4bbit

Copy link
Copy Markdown
Collaborator

Add a CreatePriceObservations instruction that registers a TWAP price-observations account for a pool over a time window, via a chained call to the configured TWAP oracle program. The pool acts as the price source: the AMM authorizes it with its pool PDA seed so the oracle ties the feed to that pool.

The feed's initial tick is read from the pool's authoritative CurrentTickAccount (validated against its pool-derived PDA) rather than being supplied by the caller, so the feed cannot be seeded at a forged price — mirroring what RecordTick does. The clock is verified to be the canonical 1-block LEZ clock, and creation is rejected if the observations account already exists.

To support the chained call, AmmConfig and the Initialize instruction are extended with a twap_oracle_program_id that the instruction reads.

@0x-r4bbit 0x-r4bbit requested review from 3esmit and gravityblast June 18, 2026 07:05
@0x-r4bbit 0x-r4bbit force-pushed the feat/amm-create-observations-account branch from 5ac79c9 to 4c9ef12 Compare June 18, 2026 14:12
@0x-r4bbit 0x-r4bbit changed the base branch from feat/amm-config to main June 18, 2026 14:12
@0x-r4bbit 0x-r4bbit requested a review from Copilot June 19, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds AMM support for creating TWAP oracle price-observations accounts on behalf of an AMM pool, using the pool PDA as the authorized price source and seeding the initial tick from the oracle-managed CurrentTickAccount rather than trusting caller input.

Changes:

  • Added CreatePriceObservations instruction + AMM implementation that validates config/pool/tick/clock inputs and issues a chained call into the configured TWAP oracle program.
  • Extended AmmConfig (and Initialize) to persist twap_oracle_program_id, enabling the AMM to discover the oracle program for chained calls.
  • Added integration and unit tests, plus updated IDL and dependency wiring to include the TWAP oracle program.

Reviewed changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
programs/integration_tests/tests/amm.rs Deploys TWAP oracle in test harness and adds integration tests for creating/rejecting observations seeded from the pool’s current tick.
programs/integration_tests/Cargo.toml Adds TWAP oracle core + methods dependencies for integration tests.
programs/amm/src/tests.rs Updates AMM test fixtures to include twap_oracle_program_id in AmmConfig.
programs/amm/src/lib.rs Registers the new create_price_observations module.
programs/amm/src/initialize.rs Extends initialize flow to store twap_oracle_program_id in config and updates unit tests.
programs/amm/src/create_price_observations.rs New implementation that validates inputs and performs the chained call into the TWAP oracle program.
programs/amm/methods/guest/src/bin/amm.rs Exposes create_price_observations and updates initialize ABI to accept TWAP oracle program id.
programs/amm/methods/guest/Cargo.lock Lockfile updates for new transitive dependencies (clock + TWAP oracle core).
programs/amm/core/src/lib.rs Adds CreatePriceObservations to the instruction enum and extends AmmConfig schema.
programs/amm/Cargo.toml Adds clock_core + twap_oracle_core dependencies to support the new instruction.
Cargo.lock Workspace lockfile updates for new dependencies.
artifacts/amm-idl.json Updates IDL to reflect new instruction and initialize arg.

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

Add a `CreatePriceObservations` instruction that registers a TWAP
price-observations account for a pool over a time window, via a chained
call to the configured TWAP oracle program. The pool acts as the price
source: the AMM authorizes it with its pool PDA seed so the oracle ties
the feed to that pool.

The feed's initial tick is read from the pool's authoritative
`CurrentTickAccount` (validated against its pool-derived PDA) rather than
being supplied by the caller, so the feed cannot be seeded at a forged
price — mirroring what `RecordTick` does. The clock is verified to be the
canonical 1-block LEZ clock, and creation is rejected if the observations
account already exists.

To support the chained call, `AmmConfig` and the `Initialize` instruction
are extended with a `twap_oracle_program_id` that the instruction reads.
@0x-r4bbit 0x-r4bbit force-pushed the feat/amm-create-observations-account branch from 4c9ef12 to b245910 Compare June 19, 2026 14:57
@0x-r4bbit 0x-r4bbit requested a review from Copilot June 19, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 13 changed files in this pull request and generated 1 comment.

Comment on lines 254 to 260
pub struct AmmConfig {
/// Program ID of the Token Program the AMM issues chained calls to.
pub token_program_id: ProgramId,
/// Program ID of the TWAP oracle program the AMM issues chained calls to.
pub twap_oracle_program_id: ProgramId,
/// Admin authority allowed to change this configuration via `UpdateConfig`.
pub authority: AccountId,
// The initial tick comes from the pool's authoritative CurrentTickAccount, not from the
// caller. Verifying its PDA ties it to this exact pool, so the seed tick cannot be forged.
assert_eq!(
current_tick_account.account_id,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not ready yet, I assume this PR don't expect full functionality? Because it seems that no AMM instruction currently creates or update the pool current-tick account, so creating an observation would fail here or use a stale value.

The tests implemented on the PR bypass this by using force_insert_account.

If you want to implement it on this PR, than on AMM side you would want the tick lifecycle implemented there, so it can properly call create price observations.

@3esmit 3esmit left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved with comments, confim this is the scope of the implementation.

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.

4 participants