Files
lez-programs/apps/amm/src/SwapRuntime.h
T
r4bbit 737b2f674a refactor(amm): consolidate the two client FFIs into one JSON crate
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.
2026-08-03 15:37:37 +02:00

47 lines
1.7 KiB
C++

#pragma once
#include <QJsonObject>
#include <QString>
#include <QVariantMap>
#include "ActiveNetwork.h"
class AmmClient;
class WalletProvider;
// Off-chain orchestration for the Swap view: reads accounts through the wallet,
// drives the amm_client swap ops, and submits the swap transaction. The AMM
// domain logic lives in the amm_client crate; this class only glues account
// reads and submission to it. Mirrors NewPositionRuntime.
class SwapRuntime {
public:
SwapRuntime(WalletProvider* wallet, AmmClient* client);
// { exists, reserveA, reserveB, feeBps } for the (tokenIn, tokenOut) pool.
// reserveA/reserveB follow the pool's canonical def order (the caller maps
// them to sell/buy). exists=false when the pool is absent or has no liquidity.
QVariantMap resolvePool(const QString& tokenInId,
const QString& tokenOutId,
const ActiveNetworkSnapshot& network);
// Builds and submits a SwapExactInput transaction; returns the native tx
// hash on success, an empty string on any failure.
QString swap(const QString& tokenInId,
const QString& tokenOutId,
const QString& userInputHoldingId,
const QString& userOutputHoldingId,
const QString& amountInDecimal,
const QString& minOutDecimal,
const QString& deadlineMs,
const ActiveNetworkSnapshot& network,
bool walletOpen);
private:
// Derives the config account id (config_id) and reads it. Returns an empty
// object only when the config_id op itself fails.
QJsonObject readConfig(const ActiveNetworkSnapshot& network) const;
WalletProvider* m_wallet;
AmmClient* m_client;
};