refactor(amm): consolidate the two client FFIs into one JSON crate - #236
Open
0x-r4bbit wants to merge 1 commit into
Open
refactor(amm): consolidate the two client FFIs into one JSON crate#2360x-r4bbit wants to merge 1 commit into
0x-r4bbit wants to merge 1 commit into
Conversation
0x-r4bbit
commented
Jul 27, 2026
| #endif // __cplusplus | ||
|
|
||
| #endif | ||
| #endif /* AMM_CLIENT_H */ |
Collaborator
Author
There was a problem hiding this comment.
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.
0x-r4bbit
force-pushed
the
refactor/amm-ffi
branch
from
July 27, 2026 19:37
0f40479 to
8e8d89b
Compare
There was a problem hiding this comment.
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 onlyamm_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
SwapRuntimeusing the unifiedAmmClientJSON 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 { |
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.
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/wireamm_client(new-position flow, under apps/). Fold both into a singleamm_clientcrate exposing one JSON C ABI, and deleteprograms/amm/client-ffi.Rust:
api/swap.rsoperations on the existingcall::<T>dispatch — swap_pair, resolve_pool, swap_plan, program_id — reusingpair::derive_pair(no more duplicated PDA derivation) andrisc0_zkvm::serdefor the SwapExactInput words (the same encoding the guest decodes). The account list and signer flags stay byte-identical to the old typed path.include/amm_client.h) covering all ops via cbindgen; bump cbindgen 0.27 -> 0.28 for#[unsafe(no_mangle)]support.C++:
AmmClientwrapper with the four swap ops.SwapRuntime(mirrorsNewPositionRuntime): reads accounts through the wallet, drives the swap ops, submits the transaction.AmmUiBackendswap methods now delegate toSwapRuntime, dropping ~390 lines of typed-FFI and byte-twiddling.program_idbecomes a JSON op, and the swap clock is derived viaderive_pair(clock_core::CLOCK_01) instead of a hardcoded base58 literal — same account, verified.