Docs · Concepts
Batches
How time is cut into batches, and the path every action takes through one, from place to open, clear and settle, with reclaim for an action that was never opened.
A batch is a fixed stretch of time. Everything placed into it stays unreadable until it closes, then opens at once and settles at one set of prices. Orders, supplies, redeems, borrows, perp opens and closes, and vault deposits and withdrawals all go through a batch.
The clock
Batches are numbered from 0 and have one length, batch_len, set in the program's Config. Batch n covers
[genesis_ts + n · batch_len, genesis_ts + (n + 1) · batch_len)and the end of that interval is its close time, close_ts. GET /v1/config returns the length as batchLengthSeconds, and GET /v1/clock returns the current batch, its close time and the number of sealed actions in it.
Each batch is tied to one round of the drand quicknet beacon: the first round published at or after close_ts. The beacon's signature for that round is the key that opens the batch. It does not exist before the round's time, and it is public from that moment.
What goes into a batch
| Action | Locks | Settles into |
|---|---|---|
| Order | The asset being sold | The asset bought at the clearing price, and any unfilled remainder |
| Supply | The asset supplied | Shares of the pool |
| Redeem | Shares of the pool | The asset, at the pool's share price |
| Borrow | Collateral, in a position | The borrowed asset |
| Perp open | Collateral, in a position | Nothing more: the position is the result |
| Perp close | The position's collateral | Collateral plus or minus the result |
| Vault deposit | The vault's assets | Shares of the vault |
| Vault withdraw | Shares of the vault | The vault's assets |
The four steps
1Place
An action locks its funds in a note and seals its terms to the batch’s close.
2Open
Buy 12.5limit 182.60Sell 4.0limit 182.10Buy 30.0limit 182.45Sell 18.2limit 182.90Sell 7.1limit 182.30Buy 2.4limit 181.95When the timelock round is out, every action in the batch opens at once.
3Clear
Buy 12.5at 182.40Sell 4.0at 182.40Buy 30.0at 182.40Sell 18.2not filledSell 7.1at 182.40Buy 2.4not filledOne price per market, the one that matches the most volume.
4Settle
fill → ownerfill → ownerfill → ownerback → ownerfill → ownerback → ownerEach action pays out to its owner’s commitment. The owner can be offline.
Place
Your client builds the action, proves it, and sends it to the relayer. Nobody's wallet signs: the proof is the authorization, and the relayer pays the fee.
The proof shows that you own the balance, share or position being spent, that the new action note holds exactly the locked amount, and that its terms hash to a commitment. The program stores the commitment in a new Action account for the batch and moves the notes in the pool.
Alongside goes a timelock ciphertext: the action's terms and the keys of its action holder, encrypted to the batch's drand round. The program does not read it. It is what lets anyone settle the action after the round without you.
The chain now shows that an action entered batch n, and a commitment. The program refuses place once the clock is within place_margin of the close, and the action then goes into the next batch.
Open
When the round is published, anyone can decrypt a batch's ciphertexts and call open for each action with its terms. The program hashes the posted terms, requires the hash to equal the stored commitment, and records them. No beacon signature is verified on chain: terms that do not match the commitment are refused, so only the true terms can be posted.
An opened action shows its kind, side, target, amounts and limit. It shows no owner. Terms that are well formed but cannot be executed, such as an unknown or paused market, are recorded as Invalid.
Clear
Once every action is opened, or the open window has passed, anyone calls clear for each market, pool, perp market and vault the batch touched.
- A spot market gets one clearing price, computed from its opened orders and its liquidity vault's curve. See Clearing at one price.
- A pool accrues interest, then nets the batch's supplies, redeems and borrows into one change of its totals.
- A perp market reads the oracle once, updates funding, and adds and removes the batch's opens and closes from open interest.
- A vault mints shares for deposits and pays out withdrawals.
The program computes each result from the opened actions itself, so the caller chooses nothing.
Settle
Anyone calls settle for each opened action. The program computes what the action is owed from the batch's results, and a proof fixes every output to the receive holder named in the action's terms. Your fill arrives as notes only your keys open, whether or not you are online.
An order settles into the asset bought and a refund of whatever did not fill. A supply settles into shares. A borrow settles into the borrowed asset. An action the pool, market or vault could not take is Refused: nothing moves. You take a refused redeem's shares back with reclaim, and you close the position a refused borrow or perp open would have activated to get its collateral back.
Mesa's keeper opens, clears and settles every batch. Each of those instructions is open to anyone, so a batch does not depend on the keeper to finish.
Reclaim
An action that is still sealed when the open window ends, open_window after the close, was never opened. The same holds for an action recorded as Invalid. Its owner calls reclaim with a proof of ownership and what the action locked returns: funds to their balance, or shares as shares.
A redeem or a vault withdrawal that the batch Refused is reclaimed the same way, as soon as the batch has cleared, and the shares return. So is a refused perp close: the position returns, still open with its collateral and entry terms and a new deadline. A borrow or perp open that was refused or never opened locked its collateral in a position, and closing that position returns it.
States
An action's account holds one of four states. Settling or reclaiming an action closes its account and returns the rent to whoever paid it.
| State | Meaning |
|---|---|
Sealed | Placed. Only its commitment is public. |
Opened | Its terms are posted and match the commitment. |
Invalid | Its terms opened but cannot be executed. Reclaimable. |
Refused | An action the batch could not take: a borrow or a redeem beyond the pool's cash, a perp open beyond the market's open interest limit, a perp close whose gain the perps vault cannot pay, a vault withdrawal too small to pay out. |
The API reports a batch as a whole in one of five states.
| State | Meaning |
|---|---|
open | Taking actions. The record shows the count of sealed actions, the countdown and the drand round it is sealed to. |
sealed | Closed, and the round is not yet opened on chain. |
opening | Actions are being opened. |
cleared | Every target has its result. |
settled | Every action is paid out. |
What a batch leaves behind
The Batch account keeps, for each market, pool, perp market and vault it touched: the count of opened actions, the clearing price, the matched base and quote volume, the vault's trade, the pool's deltas and the perp oracle price. Each settlement also emits the action's opened terms with its result. Together they are the batch record at /batches.