feat(amm): create TWAP price observations on behalf of the pool#148
feat(amm): create TWAP price observations on behalf of the pool#1480x-r4bbit wants to merge 1 commit into
Conversation
5ac79c9 to
4c9ef12
Compare
There was a problem hiding this comment.
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
CreatePriceObservationsinstruction + AMM implementation that validates config/pool/tick/clock inputs and issues a chained call into the configured TWAP oracle program. - Extended
AmmConfig(andInitialize) to persisttwap_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.
4c9ef12 to
b245910
Compare
| 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, |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Approved with comments, confim this is the scope of the implementation.
Add a
CreatePriceObservationsinstruction 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 whatRecordTickdoes. 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,
AmmConfigand theInitializeinstruction are extended with atwap_oracle_program_idthat the instruction reads.