Add private swaps
Let users trade between assets without showing anyone their balance.
A private swap trades one asset in a user's private balance for another, in one transaction. The trade is routed across the chain's liquidity, and the output lands back in the user's private balance. Nobody can link it to the user or see what they hold before or after.
Private swaps are live on Robinhood Chain. Configure the client with both assets, as shown in Choose a chain.
Quote#
Show the user what they'll get before they confirm:
const quote = await senddy.quoteSwap(HOODS, USDG, 12_500_000000n);
quote.expectedOut; // what the route quotes, in USDG units
quote.minOut; // the floor the user is protected by
quote.swapFee; // fees on this swap
quote.swapFeeBps;A quote doesn't prove anything and costs nothing. If swaps aren't possible right now it throws with a code such as ROUTE_UNAVAILABLE, which you can show as "Try again in a moment".
Swap#
const result = await senddy.swap(HOODS, USDG, 12_500_000000n, { toleranceBps: 100 });
result.txHash;
result.sharesOut; // what arrived in the private balance| Option | What it does |
|---|---|
toleranceBps | How far below the quote the user will accept. 100 is 1% |
deadlineSecs | How long the swap may wait before it expires |
exactAmount | Sell exactly the amount given, with no rounding |
If the price moves past the tolerance, the swap fails and nothing is spent. If the route fills above the floor, the surplus stays in the user's private balance.
Show a review screen#
As with payments, split the call so the proof is made while the user reads:
const prepared = await senddy.prepareSwap(HOODS, USDG, amount, { toleranceBps: 100 });
// show rate, fees, minimum received…
const result = await senddy.submitSwap(prepared);What's private, and what isn't#
| Visible onchain? | |
|---|---|
| Who swapped | No |
| The user's balance before and after | No |
| That a swap of this size happened on this pair | Yes, as with any trade that touches public liquidity |
The swap has to meet public liquidity, so the trade itself is visible. It can't be tied to the user, and their holdings stay hidden.
Earn on swaps#
You can charge a fee on every swap your users make. See Set your fee.