Skip to content

Derive swap direction from signed input holding - #208

Merged
0x-r4bbit merged 1 commit into
mainfrom
amm-signed-input-swap-v2
Jul 21, 2026
Merged

Derive swap direction from signed input holding#208
0x-r4bbit merged 1 commit into
mainfrom
amm-signed-input-swap-v2

Conversation

@3esmit

@3esmit 3esmit commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #207

  • Refactor AMM swap instructions to use user_holding_in and user_holding_out.
  • Derive swap direction from the signed input holding's token definition and remove token_definition_id_in.
  • Mark only user_holding_in as signer in the guest instruction surface and regenerated AMM IDL.
  • Add role validation for non-pool input tokens, wrong output definitions, duplicate input/output accounts, and missing input signatures.

Breaking Changes

  • SwapExactInput and SwapExactOutput no longer accept token_definition_id_in.
  • Swap callers must pass the debited user holding first and the recipient holding second.

Copilot AI review requested due to automatic review settings June 29, 2026 14:22

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 refactors the AMM swap interface to derive swap direction from the signed “input” user holding account (instead of a separate token_definition_id_in argument), updates the guest instruction surface + IDL accordingly, and adds/updates tests to validate the new account ordering and role constraints.

Changes:

  • Remove token_definition_id_in from SwapExactInput / SwapExactOutput and derive direction from user_holding_in’s token definition.
  • Update guest instruction accounts so only user_holding_in is marked as signer, and reorder swap accounts to (user_holding_in, user_holding_out).
  • Add/adjust tests to cover wrong output definitions, duplicate in/out holdings, and signer expectations.

Reviewed changes

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

Show a summary per file
File Description
programs/integration_tests/tests/amm.rs Updates swap instruction construction/account ordering and adds integration coverage for signer behavior.
programs/amm/src/tests.rs Extends unit tests to validate new input/output holding validation rules and removes old token_definition_id_in plumbing.
programs/amm/src/swap.rs Implements swap direction derivation + user account validation based on signed input holding and output definition matching.
programs/amm/methods/guest/src/bin/amm.rs Updates guest instruction interface to (user_holding_in signer, user_holding_out) and removes token_definition_id_in.
programs/amm/core/src/lib.rs Updates instruction docs and removes token_definition_id_in fields from the core instruction enum.
artifacts/amm-idl.json Regenerates IDL to reflect renamed accounts, signer flag change, and removed argument.

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

Comment thread programs/amm/src/swap.rs Outdated
Comment thread programs/integration_tests/tests/amm.rs Outdated

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

Comment thread programs/integration_tests/tests/amm.rs
@3esmit
3esmit requested review from 0x-r4bbit and gravityblast June 29, 2026 17:04
@3esmit
3esmit force-pushed the amm-signed-input-swap-v2 branch 3 times, most recently from bea6d64 to 7d1ed57 Compare July 3, 2026 12:42
@3esmit 3esmit self-assigned this Jul 14, 2026
@3esmit 3esmit added program: amm breaking Breaking change to on-chain shape or signatures labels Jul 14, 2026
@3esmit

3esmit commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator Author

Approved.

… input

Replace the `token_definition_id_in` argument with a role-based account
interface: swaps now take a `user_input_holding` and a `user_output_holding`
instead of positional token-A/token-B holdings. Direction is derived from the
input holding's own token definition, and the input slot is a framework-level
`#[account(signer)]` so authorization is enforced before execution rather than
delegated solely to the downstream token transfer. The output holding only
receives and needs no signature.

This removes the ambiguity the previous arg-based model carried (the arg could
disagree with the signed holding) and makes the IDL express the signing rule:
`user_input_holding` is `signer: true`, `user_output_holding` is `signer: false`.

The two user-holding post-states are echoed in the guest's declared slot order
(input, then output); the framework matches post-states to accounts by
position, so the internal A/B mapping used for reserve bookkeeping must not leak
into the returned order.

BREAKING CHANGE: The AMM swap instruction interface changed and the guest
ImageID/ProgramId changes as a result.
@0x-r4bbit
0x-r4bbit force-pushed the amm-signed-input-swap-v2 branch from adff15b to 860b48c Compare July 21, 2026 07:04
@0x-r4bbit
0x-r4bbit merged commit cca063c into main Jul 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Breaking change to on-chain shape or signatures program: amm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor AMM swaps to use one signed input holding

3 participants