Skip to content

Add SyncReserves Instruction For AMM Pools#36

Closed
3esmit wants to merge 2 commits intologos-blockchain:mainfrom
3esmit:feat/amm-sync-reserves
Closed

Add SyncReserves Instruction For AMM Pools#36
3esmit wants to merge 2 commits intologos-blockchain:mainfrom
3esmit:feat/amm-sync-reserves

Conversation

@3esmit
Copy link
Copy Markdown
Collaborator

@3esmit 3esmit commented Apr 8, 2026

Summary

This PR adds a SyncReserves instruction to the AMM so pool reserves can be explicitly refreshed from current vault balances.

The new path makes reserve reconciliation an intentional AMM action and covers the donation case where vault balances grow beyond the reserves currently stored in pool state.

Changes

  • add SyncReserves to the AMM instruction enum
  • add the guest entrypoint for sync_reserves
  • add the sync implementation that rewrites stored reserves from vault balances
  • add shared vault balance helpers used by the sync path
  • add AMM unit tests for donation sync and under-collateralized vault failures
  • update the checked-in AMM IDL to include the new instruction

Testing

  • cargo +nightly fmt --all -- --check
  • taplo fmt --check .
  • RISC0_SKIP_BUILD=1 cargo +1.94.0 clippy --workspace --all-targets -- -D warnings
  • RISC0_DEV_MODE=1 cargo +1.94.0 test --workspace --exclude integration_tests
  • RISC0_DEV_MODE=1 cargo +1.94.0 test -p integration_tests

Notes

  • SyncReserves updates only AMM reserve bookkeeping and does not transfer tokens.
  • The instruction rejects under-collateralized vault states instead of silently lowering reserves.
  • Coverage includes the case where syncing before a later liquidity add reduces donation-driven mispricing.

Copilot AI review requested due to automatic review settings April 8, 2026 22:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new SyncReserves AMM instruction that refreshes a pool’s stored reserves from the current vault balances, enabling explicit reconciliation (e.g., after token donations) while rejecting under-collateralized vault states.

Changes:

  • Introduces SyncReserves in amm_core::Instruction and wires a guest entrypoint.
  • Implements reserve syncing logic and shared vault-balance parsing helpers.
  • Adds unit tests covering donation syncing and under-collateralized vault failures; updates the checked-in AMM IDL.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
amm/src/vault_utils.rs Adds shared helpers to read fungible token holding balances from vault accounts.
amm/src/sync.rs Implements sync_reserves to update pool reserve bookkeeping from vault balances (with under-collateralization checks).
amm/src/lib.rs Exposes the new sync module and adds internal vault_utils module.
amm/src/tests.rs Adds unit tests for donation-driven reserve syncing and failure cases.
amm/methods/guest/src/bin/amm.rs Adds guest entrypoint for the new sync_reserves instruction.
amm/core/src/lib.rs Extends the AMM instruction enum with SyncReserves.
amm/amm-idl.json Updates the IDL to include the new sync_reserves instruction.

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

@3esmit 3esmit force-pushed the feat/amm-sync-reserves branch from 652e932 to 079a3f8 Compare April 9, 2026 03:26
@3esmit 3esmit requested a review from Copilot April 9, 2026 03:28
@3esmit 3esmit self-assigned this Apr 9, 2026
@3esmit 3esmit linked an issue Apr 9, 2026 that may be closed by this pull request
Copy link
Copy Markdown

Copilot AI left a comment

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 6 out of 7 changed files in this pull request and generated 1 comment.


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

"accounts": [
{
"name": "pool",
"writable": false,
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

In sync_reserves, the pool account is mutated (reserves are rewritten), but the IDL marks the pool account as "writable": false. If IDL consumers use this flag to build transactions, they may pass the pool as read-only and the instruction will fail when trying to write. Consider setting pool.writable to true for sync_reserves (and aligning other instructions similarly if the writable field is intended to be authoritative).

Suggested change
"writable": false,
"writable": true,

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This looks directionally correct: sync_reserves does rewrite the pool, so writable: false is misleading for that instruction. One nuance is that this does not appear isolated to sync_reserves; the checked-in generated IDLs currently mark every account as non-writable, including other mutating instructions, so this is likely a broader IDL-generation issue rather than a one-off JSON typo.

let (_, vault_b_balance) = read_fungible_holding(vault_b, &vault_b_context);

(vault_a_balance, vault_b_balance)
}
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.

As already mentioned in the other PR, let's not put this here, and instead move it into core and maybe have a utils.rs there. src/ is for program functions.

@0x-r4bbit
Copy link
Copy Markdown
Collaborator

Landed via #40

@0x-r4bbit 0x-r4bbit closed this Apr 9, 2026
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.

Add SyncReserves Instruction For AMM Pools

3 participants