Batch swap matcher

Traders who want to swap Bitcoin, Ethereum and Monero are matched with each other at one fair price per coin, with Kraken’s live market as the fallback. Add some traders, run the batch, and compare it with the alternatives.

Live prices

Streamed from Kraken by the Java price server over WebSocket. Bid = what you can sell for, ask = what you pay to buy.

Connecting to price server…
Bitcoin (BTC)
—
waiting for data
Ethereum (ETH)
—
waiting for data
Monero (XMR)
—
waiting for data

Who wants to trade

Fake traders for the demo. Each one gives up an amount of one coin for another and says how far from the market price they will go (−1% = “I accept up to 1% less than market”).

Scenarios:
NameGivesAmountWantsTolerance %WorthWorst rate acceptedAlone on Kraken
No traders yet. Pick a scenario above or add one.
up to 10 per batch

How it works

The idea in four steps.

  1. Collect orders into a batch. Instead of filling each order the moment it arrives, the system gathers everyone who wants to swap over a short window. Each person says what they give, what they want, and the worst rate they will accept.
  2. Find who can swap with whom. If Alice has Monero and wants Bitcoin while Erin has Bitcoin and wants Monero, they can simply swap: a coincidence of wants. Often there is no direct partner, but there is a loop: XMR → BTC → ETH → XMR. A ring trade lets all of them swap at once.
  3. Set one fair price per coin. The algorithm picks a single price for each coin that clears the whole batch at once. Everyone buying Bitcoin pays the same price, whoever they are and whenever they joined.
  4. Use Kraken only for the leftovers. If more of one coin is offered than wanted, Kraken takes the rest at its real, live price. So the batch is never worse than going to Kraken directly, and whatever is matched between traders skips Kraken’s spread and fees.

Why this is the best possible outcome

What “best” means, and how each claim is checked on every run.

1. Never worse than Kraken

Kraken sits inside every batch as a participant, quoting its real bid, ask and fees. Any trader whose order Kraken would fill gets at least Kraken’s price. Usually more, because matching with another trader avoids the spread and the fee on both legs.

2. One price for everyone

There is a single clearing price per coin, and prices are consistent across pairs, so there is no arbitrage loop inside the batch. Nobody pays more than anyone else for the same thing, and nobody ever trades past the limit they set.

3. The market clears

If the price beats your limit, your whole order is filled. Only the side with more on offer than anyone wants can be partly filled, and only at exactly its own limit. Economists call this a competitive equilibrium: the outcome of a perfectly fair auction.

4. No one can beat it

No group of traders could leave the batch, trade among themselves, and all come out ahead. A standard theorem says an equilibrium has this property (it is in the core). The page checks it two ways: a certificate that verifies the equilibrium conditions, and a brute-force search over every group of traders.

5. The most value among fair outcomes

Several sets of prices can satisfy all of the above. The optimiser (SCIP, a global solver) picks the one where traders gain the most in total above their limits, and proves no better one exists rather than settling for a good guess.

Compare: the alternatives fail

Kraken alone charges a spread and a fee on every leg; Monero to Ethereum pays both twice. Greedy matching settles the best loop first and prices each loop separately, so the same coin trades at different prices. The checklist above shows, on your own data, a group that could walk away from each of them.

Questions a client usually asks

Greedy matching gave one trader more. Isn’t greedy better for them?
Only by charging someone else more for the same coin. Greedy prices each ring separately, so two people buying Bitcoin in the same batch can pay different prices. The checklist names a group who would walk away from that result: they could trade among themselves and all do better. With one uniform price that cannot happen. Being fair to everyone means the lucky trader in a greedy match gets the market price instead.
Why is someone only partly filled, at exactly their limit?
When more of a coin is offered than the batch and Kraken’s price can absorb, the sellers of that coin are the “long side”. The price falls until it reaches the limit of the marginal seller, exactly as in a stock exchange’s opening auction. They still trade at a price they agreed to; they just are not the ones setting a better one.
What does “gain” mean in the charts?
For each trader: the value of what they received, minus the least they said they would accept for what they gave, valued in USD at Kraken’s mid price. It measures how much better than their own limit each person did, which lets you add up results across coins and people.
Are the prices real?
When the badge says “Live from Kraken”, yes: the Java server streams Kraken’s public ticker for BTC/USD, ETH/USD and XMR/USD. If Kraken cannot be reached it switches to clearly labelled simulated prices so the demo still works, and goes back to live data as soon as Kraken is reachable again. The traders are always fake.
Does this actually move coins?
No. This demo decides who trades what at which price. Settling it for real needs custody or atomic swaps, which for Monero and Bitcoin exist only between two parties, not for rings. That is a separate piece of work.
How big can a batch be?
The optimiser solves these batches in milliseconds and handles far more orders. The demo caps a batch at 10 traders so the brute-force “no group can beat it” check, which tries every group, stays quick enough to run live. The equilibrium certificate proves the same thing at any size.