Exact-input execution

Derive a single-bin fill, compose fills across the directional walk, and reproduce the contract’s two-bin output down to the last base atom.

On this page

The fill function

Write R for the remaining gross input in atoms, A for available output atoms and r for the fee in basis points. Let D = 10,000. For a buy, set (u, v) = (S, pᵢ) to convert quote atoms into base atoms. For a sell, use (u, v) = (pᵢ, S). ObeliskMath.fill computes two successive integer quotients.

n = ⌊R · (D − r) / D⌋
y = ⌊n · u / v⌋

If A = 0 or y = 0, the walk continues with the full remaining input. If 0 < y < A, the bin consumes R and releases y. When y ≥ A, the bin releases exactly A. Its charge is the smallest gross input that can produce this many output atoms.

nrequired = ⌈A · v / u⌉
spent = ⌈nrequired · D / (D − r)⌉

Both ceilings matter. The inner ceiling inverts conversion into output atoms. The outer ceiling inverts the fee deduction. Equality with available depth also takes this branch, so unneeded input atoms remain available for the next bin.

Worked order across two bins

Take P = 100 tUSDC/WHYPE, r = 20 bps and an active index of 10. Bin 10 has 1 WHYPE available, bin 11 has 2 WHYPE. The trader submits exactly 150 tUSDC for base. These values describe a reproducible fixture.

A two-bin buy consumes the first unit of depthAt a reference of 100 tUSDC the marginal price is 100.50 for the first WHYPE and 101.50 for the next two. The example order stops at 1.484729 WHYPE. BIN PRICE · tUSDC / WHYPE 101.50100.50 order ends 01 3 CUMULATIVE BASE OUTPUT · WHYPE
The horizontal extent of a step measures available base inventory. The highlighted portion is consumed by a 150 tUSDC order. Heights exclude the input fee.
BintUSDC per WHYPEGross input (tUSDC)Output (WHYPE)
10100.50100.7014031
11101.5049.2985970.484729054187192118

At bin 10 the least gross input covering 100,500,000 net atoms is 100,701,403. At bin 11 the remaining 49,298,597 gross atoms yield 49,199,999 net atoms after flooring. Total output is 1.484729054187192118 WHYPE. The pool commits active index 11.

Execution bounds

previewSwap returns aggregate output, the reference used and the last bin. The canonical pool reads the relay at preview and again during execution. Supply the reviewed reference as expectedPrice and choose a positive minOut. With a 50 bps output tolerance, the example gives the following lower bound.

minOut = ⌊out · 9,950 / 10,000⌋
= 1,477,305,408,916,256,157 base atoms

The reference guard separately requires |P − Pexpected| ≤ ⌊Pexpected/200⌋ with a positive expected value. The output guard also captures changes in available depth between preview and inclusion. Either failure reverts the entire swap. Exhausting the walk with even one unspent input atom raises InsufficientLiquidity, including when the residual is too small to produce an output atom.

Reversing the direction

Start again at bin 10 with reference 100 tUSDC/WHYPE. Suppose bin 10 has no quote and bin 9 holds 100 tUSDC. Selling 1 WHYPE skips bin 10 and reaches bin 9 at 99.50. After the 20 bps fee the pool converts 0.998 WHYPE, releasing exactly 99.301000 tUSDC. The fee is 0.002 WHYPE and the new active index is 9.

A subsequent buy begins at bin 9 and can consume its newly acquired WHYPE. Reversing the trade immediately therefore encounters the same gross midpoint while both legs pay an input fee. Follow the fee into the reserves →