Skip to content

feat(amm): refresh TWAP current tick on add/remove liquidity#150

Open
0x-r4bbit wants to merge 1 commit into
feat/amm-create-currenttick-accountfrom
feat/amm-update-tick-account
Open

feat(amm): refresh TWAP current tick on add/remove liquidity#150
0x-r4bbit wants to merge 1 commit into
feat/amm-create-currenttick-accountfrom
feat/amm-update-tick-account

Conversation

@0x-r4bbit

Copy link
Copy Markdown
Collaborator

Extend add_liquidity and remove_liquidity to chain UpdateCurrentTick to the oracle, matching swaps and sync_reserves. Each takes the current-tick and clock accounts, reads the TWAP program ID from config, validates the clock and current-tick PDA, and after computing the post-op pool chains an UpdateCurrentTick carrying the post-op spot price with the pool as the authorized price source.

Every reserve-mutating instruction now keeps the current tick in sync, so current_tick == tick(reserves) holds after each operation. Proportional add/remove preserve the price, so the tick is unchanged for them, but the refresh still runs and lands on the correct value.

@0x-r4bbit 0x-r4bbit requested review from 3esmit and gravityblast June 18, 2026 13:53
@0x-r4bbit 0x-r4bbit force-pushed the feat/amm-create-currenttick-account branch from fa05bae to ca59f7d Compare June 18, 2026 14:13
Extend add_liquidity and remove_liquidity to chain UpdateCurrentTick to
the oracle, matching swaps and sync_reserves. Each takes the current-tick
and clock accounts, reads the TWAP program ID from config, validates the
clock and current-tick PDA, and after computing the post-op pool chains an
UpdateCurrentTick carrying the post-op spot price with the pool as the
authorized price source.

Every reserve-mutating instruction now keeps the current tick in sync, so
current_tick == tick(reserves) holds after each operation. Proportional
add/remove preserve the price, so the tick is unchanged for them, but the
refresh still runs and lands on the correct value.

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 extends AMM reserve-mutating instructions to keep the TWAP oracle “current tick” synchronized by chaining an oracle UpdateCurrentTick call after add/remove liquidity (and also after sync_reserves), aligning behavior with swaps and ensuring current_tick == tick(reserves) after each operation.

Changes:

  • Add current_tick_account + canonical clock accounts to add/remove liquidity and swap instructions; validate PDAs and chain UpdateCurrentTick with the post-op spot price.
  • Extend sync_reserves to take config, validate clock/current-tick PDA, and chain UpdateCurrentTick after reconciling reserves.
  • Update unit tests + integration tests and AMM IDL to reflect new accounts and to assert tick refresh behavior.

Reviewed changes

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

Show a summary per file
File Description
programs/integration_tests/tests/amm.rs Seeds a current-tick account into test state; updates tx account lists; adds sync-reserves integration test + tick assertions.
programs/amm/src/tests.rs Updates call helpers to include current tick + clock; asserts oracle UpdateCurrentTick chained call is emitted.
programs/amm/src/sync.rs Adds config/current-tick/clock validation; chains UpdateCurrentTick based on synced spot price.
programs/amm/src/swap.rs Adds current-tick/clock validation; appends UpdateCurrentTick chained call using post-swap price.
programs/amm/src/remove.rs Adds current-tick/clock validation; appends UpdateCurrentTick chained call using post-remove price.
programs/amm/src/add.rs Adds current-tick/clock validation; appends UpdateCurrentTick chained call using post-add price.
programs/amm/methods/guest/src/bin/amm.rs Updates instruction interfaces to pass current-tick/clock and new sync-reserves config arg.
artifacts/amm-idl.json Updates IDL account lists for modified instructions (incl. sync_reserves).

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

Comment on lines +322 to +323
/// The pool's TWAP current-tick account, owned by the oracle program, holding `tick`. Seeded
/// into state so swap/sync can refresh it via a chained UpdateCurrentTick call.
Comment on lines +977 to +979
// Seed the pool's current-tick account so swaps and syncs can refresh it. Its initial value is
// the tick of the opening reserves; swap/sync tests assert it is updated to the new price.
let initial_tick = twap_oracle_core::price_to_tick(amm_core::spot_price_q64_64(

@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 suggestions:

  • update amm/core/src/lib.rs comments that describe the changed required accounts.
  • add one successful exact-output integration test that executes the transaction and checks the stored CurrentTickAccount changed to the post-swap spot price.

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.

3 participants