Skip to content

refactor(amm): consolidate the two client FFIs into one JSON crate - #236

Open
0x-r4bbit wants to merge 1 commit into
mainfrom
refactor/amm-ffi
Open

refactor(amm): consolidate the two client FFIs into one JSON crate#236
0x-r4bbit wants to merge 1 commit into
mainfrom
refactor/amm-ffi

Conversation

@0x-r4bbit

Copy link
Copy Markdown
Collaborator

The AMM host FFI was split across two crates with two ABI styles: the typed-C amm_client_ffi (swap primitives, under programs/) and the JSON/wire amm_client (new-position flow, under apps/). Fold both into a single amm_client crate exposing one JSON C ABI, and delete programs/amm/client-ffi.

Rust:

  • Re-express the swap path as api/swap.rs operations on the existing call::<T> dispatch — swap_pair, resolve_pool, swap_plan, program_id — reusing pair::derive_pair (no more duplicated PDA derivation) and risc0_zkvm::serde for the SwapExactInput words (the same encoding the guest decodes). The account list and signer flags stay byte-identical to the old typed path.
  • Generate a single header (include/amm_client.h) covering all ops via cbindgen; bump cbindgen 0.27 -> 0.28 for #[unsafe(no_mangle)] support.

C++:

  • Extend the AmmClient wrapper with the four swap ops.
  • Add SwapRuntime (mirrors NewPositionRuntime): reads accounts through the wallet, drives the swap ops, submits the transaction.
  • AmmUiBackend swap methods now delegate to SwapRuntime, dropping ~390 lines of typed-FFI and byte-twiddling. program_id becomes a JSON op, and the swap clock is derived via derive_pair (clock_core::CLOCK_01) instead of a hardcoded base58 literal — same account, verified.

@0x-r4bbit
0x-r4bbit requested review from 3esmit and gravityblast July 27, 2026 14:02
#endif // __cplusplus

#endif
#endif /* AMM_CLIENT_H */

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.

THe changes in this file are here because the headers are not autogenerated (this was also done in the amm_client ffi from the swap fuctionality PR)

The AMM host FFI was split across two crates with two ABI styles: the
typed-C `amm_client_ffi` (swap primitives, under programs/) and the
JSON/wire `amm_client` (new-position flow, under apps/). Fold both into a
single `amm_client` crate exposing one JSON C ABI, and delete
`programs/amm/client-ffi`.

Rust:
- Re-express the swap path as `api/swap.rs` operations on the existing
  `call::<T>` dispatch — swap_pair, resolve_pool, swap_plan, program_id —
  reusing `pair::derive_pair` (no more duplicated PDA derivation) and
  `risc0_zkvm::serde` for the SwapExactInput words (the same encoding the
  guest decodes). The account list and signer flags stay byte-identical
  to the old typed path.
- Generate a single header (`include/amm_client.h`) covering all ops via
  cbindgen; bump cbindgen 0.27 -> 0.28 for `#[unsafe(no_mangle)]` support.

C++:
- Extend the `AmmClient` wrapper with the four swap ops.
- Add `SwapRuntime` (mirrors `NewPositionRuntime`): reads accounts through
  the wallet, drives the swap ops, submits the transaction.
- `AmmUiBackend` swap methods now delegate to `SwapRuntime`, dropping ~390
  lines of typed-FFI and byte-twiddling. `program_id` becomes a JSON op,
  and the swap clock is derived via `derive_pair` (clock_core::CLOCK_01)
  instead of a hardcoded base58 literal — same account, verified.

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 consolidates the AMM host-side FFI into a single amm_client Rust crate exposing a JSON-based C ABI, removing the legacy typed-C amm_client_ffi crate and migrating the AMM UI’s swap flow to use the unified JSON ops.

Changes:

  • Remove programs/amm/client-ffi (typed ABI) and update the workspace/flake to build and export only amm_client.
  • Add swap-related JSON ops to apps/amm/client (swap_pair, resolve_pool, swap_plan, program_id) and export them via the C ABI/header.
  • Update the AMM UI backend to route swap orchestration through a new SwapRuntime using the unified AmmClient JSON calls.

Reviewed changes

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

Show a summary per file
File Description
programs/amm/client-ffi/src/swap.rs Removed legacy typed-FFI swap instruction-word builder.
programs/amm/client-ffi/src/pool.rs Removed legacy typed-FFI pool/config decode helpers.
programs/amm/client-ffi/src/pda.rs Removed legacy typed-FFI PDA + program-id helpers.
programs/amm/client-ffi/src/lib.rs Removed legacy typed-FFI C ABI surface.
programs/amm/client-ffi/Cargo.toml Removed legacy typed-FFI crate manifest.
programs/amm/client-ffi/amm_client_ffi.h Removed legacy generated header.
flake.nix Switch Nix packaging to build/export only amm_client (and its header).
Cargo.toml Remove programs/amm/client-ffi from workspace members.
Cargo.lock Drop amm_client_ffi package; bump cbindgen to 0.28; add deps for consolidated crate.
apps/amm/src/SwapRuntime.h New swap orchestration runtime interface for the UI.
apps/amm/src/SwapRuntime.cpp New implementation that reads accounts, builds swap plan, and submits tx.
apps/amm/src/AmmUiBackend.h Remove typed-FFI header usage; add SwapRuntime member.
apps/amm/src/AmmUiBackend.cpp Replace typed-FFI swap/resolvePool/program-id code with JSON client + SwapRuntime.
apps/amm/src/AmmClient.h Extend AmmClient interface with swap/program-id operations.
apps/amm/src/AmmClient.cpp Wire new AmmClient methods to corresponding amm_* FFI entrypoints.
apps/amm/metadata.json Remove amm_client_ffi external library dependency.
apps/amm/flake.nix Update docs/comments to refer to consolidated amm_client crate.
apps/amm/CMakeLists.txt Remove amm_client_ffi external lib; add SwapRuntime sources.
apps/amm/client/src/lib.rs Re-export newly added swap/program-id APIs from amm_client.
apps/amm/client/src/ffi.rs Export new amm_swap_* and amm_program_id JSON ops via C ABI.
apps/amm/client/src/api/swap.rs New pure swap/program-id operations powering the unified client ABI.
apps/amm/client/src/api/request.rs Add request types for swap/pool/program-id operations.
apps/amm/client/src/api/mod.rs Register and expose swap/program-id API functions.
apps/amm/client/include/amm_client.h Update generated header to include new exported functions and C++ compatibility.
apps/amm/client/cbindgen.toml Update include guard and enable cpp_compat.
apps/amm/client/Cargo.toml Add risc0-binfmt runtime dep and cbindgen build-dependency.
apps/amm/client/build.rs Generate header into include/amm_client.h and adjust rerun triggers.

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

Comment on lines +85 to +90
Ok(json!({
"exists": true,
"reserveA": pool.reserve_a.to_string(),
"reserveB": pool.reserve_b.to_string(),
"feeBps": fee_bps,
}))
Comment on lines +45 to +49
pub(super) fn swap_pair(request: SwapPairRequest) -> Result<Value, String> {
let amm_program = parse_program_id(&request.amm_program_id)?;
let token_in = account_id_from_hex(&request.token_in_id, "token in id")?;
let token_out = account_id_from_hex(&request.token_out_id, "token out id")?;
if token_in == token_out {
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.

2 participants