Skip to main content

Swap API

SwapClient enables atomic swaps between EBEMT tokens (e.g., ebUSD ↔ ebEUR).

Setup

import { createSwapClient, RATE_SCALE } from '@zk-privacy/eb-sdk';

const swapClient = createSwapClient({
publicClient: client.getPublicClient(),
walletClient: walletClient,
swapRegistry: '0x667d6c4d1e69399a8b881b474100dccf73ce42a0',
prover: client.getProver(),
});

Reading Offers

const offer = await swapClient.getOffer(1n);
const offers = await swapClient.getActiveOffers(ebUsdAddress, ebEurAddress);
const best = await swapClient.getBestOffer(ebUsdAddress, ebEurAddress);

Rate Calculations

Rates use 1e6 precision. 1_000_000n = 1:1, 1_100_000n = 1.1:1.

const receive = swapClient.calculateReceiveAmount(giveAmount, rate);
const give = swapClient.calculateGiveAmount(receiveAmount, rate);

Maker Operations

const { offerId, txHash } = await swapClient.createOffer({
tokenGive: ebUsdAddress,
tokenReceive: ebEurAddress,
rate: 1_100_000n,
makerBPK: wallet.BPK,
});

await swapClient.updateRate(offerId, 1_150_000n);
await swapClient.cancelOffer(offerId);

Executing Swaps

Both parties generate proofs with authorizedCaller set to SwapRegistry, then submit atomically:

const takerProof = await wallet.EUR.prepareTransfer(
makerBPK, eurAmount,
{ authorizedCaller: BigInt(swapRegistryAddress) }
);

const makerProof = await makerWallet.USD.prepareTransfer(
wallet.BPK, usdAmount,
{ authorizedCaller: BigInt(swapRegistryAddress) }
);

const toBytes32 = (v: bigint) =>
('0x' + v.toString(16).padStart(64, '0')) as `0x${string}`;

await swapClient.executeAtomicSwap({
offerId,
takerGives: eurAmount,
takerReceives: usdAmount,
takerProof: takerProof.proofHex,
takerInputs: takerProof.publicInputs.map(toBytes32),
makerProof: makerProof.proofHex,
makerInputs: makerProof.publicInputs.map(toBytes32),
});