1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Hanji on Base: central limit order books whose depth is virtual liquidity placed by a quoter
//! around each swap, quoted and executed through one proxy orderbook per market.
//!
//! Reference: <https://docs.hanji.io/for-developers/liquidity-source-integration>
//!
//! # Where the books come from
//!
//! The feed serves the markets `BASE_DEPLOYMENT` lists, each with the contracts and tokens its
//! fills were recorded against. Every poll reads them all over JSON-RPC, at one block in one
//! `Multicall3.aggregate3` (see `reader.rs`). Hanji's aggregator API (`/aggregators/markets`)
//! lists more markets, but the feed does not read it: a market it adds can only be served once
//! its fills are recorded and its contracts pinned here, and the live test
//! `test_live_deployment_lists_served_markets` reports one the deployment lacks.
//! Events cannot rebuild these books: the quoter places its orders inside the swap and removes them
//! after, so no event ever records them.
//!
//! # Whose book
//!
//! A proxy fills a caller against the orders of one of two quoters. The transaction's sender and
//! callers on the proxy's whitelist (`whitelisted(address)`) get its FastQuoter
//! (`0x598877e528437c0b4429e910d0bba850708cd98c` on the three ETH and cbBTC markets); any other
//! contract, such as Tycho's router, gets a second quoter
//! (`0x611c491d84469c4498b2c4ffd56fd3adc7ce5a1f`) whose book is wider: at Base block 64,593,995
//! its best bid sat 2.0 bps under FastQuoter's on ETH/USDC, 2.1 bps on cbBTC/USDC and 6.2 bps on
//! cbBTC/ETH. FastQuoterHelper, which Hanji's documentation points aggregators to, reports only
//! FastQuoter's book. The feed reads both quoters' books, and whether its taker (Tycho's router
//! unless [`HanjiFeedBuilder::taker`] says otherwise) is whitelisted, and publishes the book the
//! proxy will fill the taker against. Hanji whitelisted Tycho's router on every Base market at
//! about block 62,515,500 (2026-11-26), so it is filled against FastQuoter's book.
//! The tokenized-equity markets name no second quoter and run other implementations, and their
//! fills and gas were not recorded, so the deployment does not list them. A paused
//! proxy reverts every fill while its quoters still report a book, so a paused market is left out.
//!
//! # One inventory, re-priced on every fill
//!
//! One FastQuoter and one contract quoter serve all three ETH and cbBTC markets, from one shared
//! inventory, and after every fill they place a new book on all three, at worse prices for a
//! second fill that takes the same token out. Measured on a fork of Base block 61,711,413: after a
//! cbBTC/USDC sell of about $2k a second cbBTC/USDC sell filled 1.5 bps under the levels the first
//! left, and 10.8 bps under after one of about $18k; after the $18k sell, selling ETH on ETH/USDC
//! filled 10.7 bps under its book. Depth came back each time, so the executor fills such a swap in
//! full, and only the route's `minAmountOut` meets the difference. No state here models it: a
//! simulated swap leaves the side it walked empty, so a route cannot use the same market twice in
//! one direction, but a route through two of these markets that takes the same token out of both
//! is over-quoted by about 0.75 bps per $1k the first hop took.
//!
//! # Freshness
//!
//! A fleet of keeper addresses pushes the quoters' prices several times a second (selector
//! `0xae7e8d81`). The book is empty from about 3 s after the last push, measured at Base block
//! 61,595,000 with `eth_call` time overrides. The feed publishes only reads whose block trails the
//! wall clock by little more than Base's whole-second timestamps explain, and withdraws a book
//! soon after the poll that read it, so a book is served about as long as the quoters keep it
//! without a newer price (`feed.rs` has the bound). A swap that lands after the book went empty
//! fills nothing, and the executor reverts it. The quoters also price off four Base pools seen in
//! their call traces (two Uniswap V3, one PancakeSwap V3 and one Aerodrome CL, whose TWAP they
//! read), so a route that trades through one of those before Hanji meets a different Hanji book
//! than the one simulated.
//!
//! # How a market fills
//!
//! A market trades token X (the base) against token Y (the quote). Prices are Y units per X unit
//! (`shares`), both scaled by the market's `scalingFactorTokenX`/`scalingFactorTokenY`. Recorded
//! at Base block 64,593,995 (`testdata/`) for the transaction's sender, for a contract off the
//! whitelist and for Tycho's whitelisted router, with each output measured as the caller's balance
//! gain, and matched wei for wei by [`HanjiState`] against the book each one gets:
//!
//! - Selling X sends `placeOrder` with `floor(amountIn / scalingX)` shares. They fill best bid
//! first, and the taker receives `(value - fee) * scalingY`, where `value` is the sum of `shares
//! * price` over the levels filled and `fee = ceil(value * rate / 1e18)`.
//! - Buying X sends `placeMarketOrderWithTargetValue` with a target of `floor(amountIn /
//! scalingY)`. It buys, best ask first, the most shares whose `value + fee` fits in the target,
//! and the taker receives `shares * scalingX`.
//!
//! The rate is `totalAggressiveCommissionRate + passiveOrderPayoutRate` from `getConfig()`; the
//! fee rounds up, where the documented formula reads as rounding down. `passiveOrderPayoutRate`
//! was zero on every recorded market, so whether a taker pays it is not measured. The proxy pulls
//! only what it fills and does not revert when its book runs out; tycho-execution's executor
//! reverts a fill short of the input instead. Within that, the input left over below one share
//! (selling) or one share's cost (buying) stays with the caller, which for a swap through Tycho's
//! router is the router, and a native input the proxy does not use is refunded to it. The quotes
//! are the exact fill of the input; the leftover is not returned to the user.
//!
//! # Native ETH
//!
//! On a market that trades ETH (`supportsNativeEth`), the ETH side is always paid out as native
//! ETH, whichever side it is, and taken as `msg.value`. Components therefore carry the chain's
//! native token (the zero address) in place of WETH, which the API lists for it.
//!
//! # What this integration depends on
//!
//! The proxies and the underlying CLOBs are UUPS proxies whose shells are verified on Sourcify;
//! their implementations and both quoters are not source-verified (checked 2026-11-26), and each
//! market's proxy runs its own implementation. One EOA,
//! `0xA24D2aF7B9d58579225800B32111D71fb34643C9`, owns them all and upgrades them without a
//! timelock: ETH/USDC's proxy was upgraded at Base blocks 56,481,785 and 59,531,598, cbBTC/ETH's at
//! 59,531,787, and Hanji says it adjusts the on-chain quoting logic from time to time. Beside what
//! Hanji documents (the API's market list, `getConfig()`, `assembleOrderbookFromOrders` and the
//! two swap methods), the feed calls three unnamed getters of the proxies, identified by what they
//! return (`reader.rs`), and `whitelisted(address)`; it does not call the API. The fill rules and
//! the choice of quoter were measured rather than read from source, so an upgrade can break the
//! quotes without failing any read. The feed therefore pins what the fills were recorded against:
//! the implementation each proxy and CLOB ran, and that of the LPManager
//! (`0x69a9f156d5902191dce331ab348f3e9e96e48b22`), a UUPS proxy of the same owner that holds the
//! quoters' shared inventory and is an immutable of both (`Deployment::shared`); the CLOB each
//! proxy names; and the two quoters every market names (`Deployment::quoters`). The quoters are
//! not proxies, and neither they nor the contracts FastQuoter embeds have a `DELEGATECALL`, so
//! their code cannot change; the owner can only point a proxy at another CLOB or quoter, which
//! every read checks. Every poll reads the implementations from their EIP-1967 slots, in one batch
//! at the read's block: a market whose proxy or CLOB runs another implementation, or that names
//! another CLOB or other quoters, is withheld until the pins are updated, and every market is
//! withheld while the LPManager runs another. The fixture records the implementations it ran
//! against, and the tests check the pins against it, so updating a pin means re-recording the
//! fixture with `testdata/record_fixture.py`, whose fills the state tests check across sizes.
use ;
use ;
use Chain;
pub use ;
pub use HanjiState;
/// Protocol system stamped on every component this integration emits; tycho-execution keys
/// Hanji's encoder on it.
pub const PROTOCOL_SYSTEM: &str = "pamm:hanji";
/// Component type stamped on every market this integration emits.
pub const PROTOCOL_TYPE: &str = "hanji_orderbook";
/// Static attribute holding the market's proxy orderbook, which the encoder calls.
pub const ORDERBOOK_ATTRIBUTE: &str = "orderbook";
/// One chain's Hanji markets and what they all depend on.
pub
/// The two quoters a proxy fills its callers against (see the module docs).
pub
/// Base's wrapped native token, which `getConfig()` names for a native side.
const BASE_WETH: Address = address!;
const BASE_USDC: Address = address!;
const BASE_CBBTC: Address = address!;
/// The CLOB implementation all three markets' CLOBs run.
const BASE_CLOB_IMPLEMENTATION: Address = address!;
/// Hanji's LPManager on Base, which holds the quoters' shared inventory.
const BASE_LP_MANAGER: Address = address!;
/// The Base deployment, as recorded in the fixture at Base block 64,593,995 (2026-12-01).
///
/// Each market's gas covers what the proxy call costs a contract caller once its refund is paid
/// back, measured with `debug_traceCall` of both swap methods sent through a forwarding contract
/// (the `net_gas` of the recording's "contract" and "router" cases). Most of the gas is the quoter
/// placing and removing its virtual orders around the fill and re-reading the pools it prices
/// from; clearing those orders earns a refund of 10% to 14% of the call's gas, under the 20% of a
/// transaction's gas a refund may pay back. In the recording, after the refund, a first-level fill
/// cost at most 919,606 on ETH/USDC, 1,097,234 on cbBTC/USDC and 1,367,593 on cbBTC/ETH, and each
/// further level at most 229,796, 275,491 and 224,922 more, averaged over the levels a fill
/// touched; the figures sit 3% above those. Tycho's router, whitelisted and served from
/// FastQuoter, and a contract off the whitelist cost within 3% of each other, so the model does not
/// tell the two apart. Re-measure when the recorded gas stops fitting
/// (`test_gas_covers_every_recorded_contract_fill`). The router's own transfers and approval are
/// added on top by tycho-execution's gas estimator.
pub const BASE_DEPLOYMENT: Deployment = Deployment ;
/// Tycho's default router on Base, the taker the books are read for unless
/// [`HanjiFeedBuilder::taker`] sets another.
pub const BASE_ROUTER: Address = address!;