A batch has one answer: one price for every token in it
The first order opens a window of 2 seconds, and orders and liquidity collect in it. Then the whole batch is solved as one problem. The solution is a price vector, and every fill follows from it.
What a batch is
An order says: sell this much of one token for another, at this limit or better. For one batch window, 10 slots, every order placed is folded into the batch’s input hash. When the window ends the input is fixed, and nobody can add to it, reorder it or take anything out.
The result of a batch is one price per token, measured in SOL. An order selling token A for token B trades at the ratio of the two prices, and every order in the batch that sells A for B gets that same ratio. Because the price is uniform, each order’s fill follows from the price vector alone, so the chain settles any order from the posted result.
The four conditions
A price vector is the clearing of a batch when all four hold. A solver searches for it, and the guest program checks it.
Conservation
For every token, what orders and liquidity pay in equals what they are paid out, less fees.
Limits
An order fills only at its limit or better, and an order strictly better than the cleared price fills in full.
Liquidity on its curve
Each position ends where its range and the cleared prices put it.
Maximal
No other price vector inside the band fills more volume.
Each price also has to sit inside a band around the token’s last proven price. The band bounds how far one batch can move a price, and it is what lets the program measure the value a batch moves before any proof exists.
A worked batch
This is batch 1 of the guest’s test fixture, the batch whose proof the program verifies in its tests. It holds two orders between SOL and a test token, and one liquidity position on the pair. Prices are written as atoms of the test token per lamport of SOL.
| Order | Sells | Limit | Fills | Clearing fee | Receives |
|---|---|---|---|---|---|
| A | 200,000,000 lamports of SOL | at least 95 atoms per lamport | 100% | 19,411,205 | 19,391,794,237 atoms |
| B | 12,000,000,000 atoms of the test token | at most 105 atoms per lamport | 100% | 123,639 | 123,516,286 lamports |
The last proven price was 100. At that price order A would be owed 20,000,000,000 atoms, and order B brings only 12,000,000,000. The rest has to come from liquidity, and liquidity gives up the test token only as its price rises along the curve. The batch balances at 97.056 atoms per lamport: the test token cleared at 0.0103033 lamports per atom.
Both orders are in the money at 97.056, so both fill in full. A is owed 19,411,205,442 atoms before the fee. B supplies 12,000,000,000 of them and the position pays out 7,411,205,442. In the other token, A puts in 200,000,000 lamports, B is owed 123,639,925, and the position takes in 76,360,074, with one lamport left in the vault as rounding. Each order pays the clearing fee, 0.1% in this fixture, out of what it receives.
The position trades at the cleared price, which is better for it than its own curve. The difference, about 1,094,773 lamports, is credited to the position along with the pair’s liquidity fee of about 3,707,456 atoms. In a pool that difference is what an arbitrageur is paid for the same move.
The guest checks this clearing in 136,583 RISC-V cycles, and its proof is 260 bytes.
Why one price per token
Two orders in the same batch get the same price whoever arrived first. Arriving in front of someone gains nothing, because there is no earlier price to take.
Liquidity trades only at the cleared price. When the outside price moves, the next batch clears at the new price and each position moves along its curve to it. The difference between the curve and the cleared price, which a pool would have paid to an arbitrageur, stays with the positions.
An order between two tokens with no direct counterparty still fills. The prices are per token, so the batch only has to balance in each token, and the other orders and positions carry it across.
Clearing the whole market at once is the convex routing problem with one shared price vector, solved once for everyone. Angeris, Chitra, Evans and Boyd, 2022.