feat(amm): refresh TWAP current tick on add/remove liquidity#150
Open
0x-r4bbit wants to merge 1 commit into
Open
feat(amm): refresh TWAP current tick on add/remove liquidity#1500x-r4bbit wants to merge 1 commit into
0x-r4bbit wants to merge 1 commit into
Conversation
fa05bae to
ca59f7d
Compare
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.
533ade8 to
2c0bb72
Compare
There was a problem hiding this comment.
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+ canonicalclockaccounts to add/remove liquidity and swap instructions; validate PDAs and chainUpdateCurrentTickwith the post-op spot price. - Extend
sync_reservesto takeconfig, validate clock/current-tick PDA, and chainUpdateCurrentTickafter 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
approved these changes
Jun 19, 2026
3esmit
left a comment
Collaborator
There was a problem hiding this comment.
Approved with suggestions:
- update
amm/core/src/lib.rscomments that describe the changed required accounts. - add one successful exact-output integration test that executes the transaction and checks the stored
CurrentTickAccountchanged to the post-swap spot price.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.