The clearing rule
Every auction clears all of its orders at once, at one rate. The rule is written in SableMarket._clear and tested against a brute-force reference on random books.
Definitions
For a rate r, let offered(r) be the USDC offered at minimum rates at or under r, and asked(r) the USDC asked at maximum rates at or over r. The volume a rate can clear is min(offered(r), asked(r)).
The rule
- Over every rate at which there is an order, find the largest volume V*. If V* is zero, nothing fills and every order is refunded.
- The rates that reach V* form one interval [r_lo, r_hi]. The clearing rate is r* = floor((r_lo + r_hi) / 2), in basis points.
- Offers fill from the lowest minimum rate up, bids from the highest maximum rate down, until V* is reached on each side. The level where a side crosses V* is shared pro rata; claims there are allocated in sequence so the level's fills sum exactly to its share, each within one unit of its pro rata amount.
- Every fill, on both sides, is at r*. Everything not filled is refunded.
Why it holds
- The interval is one piece. offered rises with r and asked falls, so for rates a < m < b, min(offered(m), asked(m)) ≥ min(V(a), V(b)): the rates reaching the maximum are contiguous, and r* (between two of them) also reaches it.
- Nobody is filled outside their limit. A lender fills only at levels at or under the crossing level, which is at or under r*; a borrower only at levels at or over r*. Lenders receive r* ≥ their minimum; borrowers pay r* ≤ their maximum.
- Fills conserve. offered(r*) ≥ V* and asked(r*) ≥ V*, so both walks reach V*; lender fills and borrower fills each sum to V* exactly. Refunds are amount minus fill. The contract holds exactly what it owes after every call (a stateful invariant of the test suite).
- Borrowers owe at least what notes promise. Interest on loans rounds up and on notes rounds down, so a cohort's debts always cover its notes.
Worked example
| Order | Limit | USDC |
|---|---|---|
| Lender A | min 3.00 % | 600,000 |
| Lender B | min 3.50 % | 400,000 |
| Lender C | min 5.50 % | 250,000 |
| Borrower X | max 6.00 % | 500,000 |
| Borrower Y | max 5.00 % | 500,000 |
| Borrower Z | max 3.20 % | 300,000 |
Volume at each order rate: 3.00 %: 600,000; 3.20 %: 600,000; 3.50 %: 1,000,000; 5.00 %: 1,000,000; 5.50 %: 500,000; 6.00 %: 500,000. The maximum, 1,000,000, is reached from 3.50 % to 5.00 %, so r* = 4.25 %. A, B, X and Y fill in full at 4.25 %; C and Z are refunded. A 30-day note on 600,000 is 602,095.89.
Timing
Orders placed during UTC day d-1 join auction d, which closes at d × 86400. settle(d) is open to anyone from the close for 6 hours; an auction nobody settles in that time can be voided by anyone and every order is refunded in full.