tycho-simulation 0.525.0

Provides tools for interacting with protocol states, calculating spot prices, and quoting token swaps.
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Fixtures shared by the BaiBai state and feed tests: recordings of the Base deployment, the
//! states they describe, and a JSON-RPC node that serves one.

use std::{collections::HashMap, sync::Arc};

use alloy::{
    primitives::{aliases::U48, Address, U256},
    providers::{
        bindings::IMulticall3::{
            aggregate3Call, getBlockNumberCall, getChainIdCall, getCurrentBlockTimestampCall,
            Result as Call3Result,
        },
        MULTICALL3_ADDRESS,
    },
    sol_types::{SolCall, SolValue},
};
use serde::Deserialize;
use tycho_common::{
    models::{token::Token, Chain},
    Bytes,
};

use crate::{
    book::BookFeedConfig,
    pamm::protocols::baibai::{
        math::{Knot, Side},
        reader::{
            availableLiquidityCall, cUnitCall, curveBookCall, custodianCall, knotCall, pairCall,
            quoteTokenCall, ttlCall, validUntilCall, PairView, SideView, IMPLEMENTATION_SLOT,
        },
        state::BaibaiState,
        Deployment, BASE_DEPLOYMENT, BASE_LISTED_BASES, BASE_ROUTER,
    },
    snapshot_feed::http::test_support::{spawn_http_server, MockHttpServer, MockRequest},
};

/// Recorded by `testdata/record_fixture.py` at four Base blocks. The first, after the router's
/// zero-fee whitelist and the Entrypoint upgrade, has every cursor at zero. The other three
/// predate both and carry the default fee of 50 millionths: every cursor at zero, then WETH's ask
/// partly filled, then its bid partly filled.
const RECORDINGS: [&str; 4] = [
    include_str!("testdata/live_53545000.json"),
    include_str!("testdata/live_53234728.json"),
    include_str!("testdata/live_53234783.json"),
    include_str!("testdata/live_53236603.json"),
];

#[derive(Debug, Clone, Deserialize)]
pub(super) struct Fixture {
    pub block: u64,
    pub timestamp: u64,
    pub ttl: u64,
    pub c_unit: U256,
    pub available_quote: U256,
    pub pairs: Vec<FixturePair>,
}

#[derive(Debug, Clone, Deserialize)]
pub(super) struct FixturePair {
    pub base: Address,
    pub valid_until: u64,
    pub available_base: U256,
    pub fee_pips: u32,
    pub pair: FixtureView,
    pub cases: Vec<FixtureCase>,
}

#[derive(Debug, Clone, Deserialize)]
pub(super) struct FixtureView {
    pub seq: u64,
    pub fill_seq: u64,
    pub last_update_at: u64,
    pub mid: U256,
    pub q_unit: U256,
    pub ask: FixtureSide,
    pub bid: FixtureSide,
}

#[derive(Debug, Clone, Deserialize)]
pub(super) struct FixtureSide {
    pub spread_bps: i16,
    pub depth_bps: u16,
    pub filled: U256,
    pub knots: Vec<[u64; 2]>,
}

/// One recorded quote: `quoteFor`'s net output for the router and the raw curve's output.
#[derive(Debug, Clone, Deserialize)]
pub(super) struct FixtureCase {
    pub sell_base: bool,
    pub amount_in: U256,
    pub amount_out: U256,
    pub curve_out: U256,
}

/// Every recording.
pub(super) fn fixtures() -> Vec<Fixture> {
    RECORDINGS
        .iter()
        .map(|recording| serde_json::from_str(recording).unwrap())
        .collect()
}

/// The 53,545,000 recording, with every cursor at zero and the router's fee at zero.
pub(super) fn fixture() -> Fixture {
    serde_json::from_str(RECORDINGS[0]).unwrap()
}

/// The 53,234,728 recording, with every cursor at zero, which charges the router 50 millionths.
pub(super) fn fee_charging_fixture() -> Fixture {
    serde_json::from_str(RECORDINGS[1]).unwrap()
}

pub(super) fn bytes(address: Address) -> Bytes {
    Bytes::from(address.to_vec())
}

pub(super) fn weth() -> Address {
    BASE_LISTED_BASES[0]
}

pub(super) fn cbbtc() -> Address {
    BASE_LISTED_BASES[1]
}

pub(super) fn token(address: Address) -> Token {
    let (symbol, decimals) = match address {
        a if a == weth() => ("WETH", 18),
        a if a == cbbtc() => ("cbBTC", 8),
        a if a == BASE_DEPLOYMENT.quote => ("USDC", 6),
        _ => ("TKN", 18),
    };
    Token::new(&bytes(address), symbol, decimals, 0, &[Some(10_000)], Chain::Base, 100)
}

/// The recorded deployment's tokens on Base, with no TVL floor.
pub(super) fn book_config() -> BookFeedConfig {
    let tokens: HashMap<Bytes, Token> = [weth(), cbbtc(), BASE_DEPLOYMENT.quote]
        .into_iter()
        .map(|address| (bytes(address), token(address)))
        .collect();
    BookFeedConfig { chain: Chain::Base, tokens: Arc::new(tokens), min_tvl_usd: 0.0 }
}

fn side(side: &FixtureSide) -> Side {
    Side {
        spread_bps: side.spread_bps,
        depth_bps: side.depth_bps,
        filled: side.filled,
        knots: side
            .knots
            .iter()
            .map(|[q, extra]| Knot { q: *q, extra: *extra })
            .collect(),
    }
}

/// The state the recording describes for `pair`.
pub(super) fn state(fixture: &Fixture, pair: &FixturePair) -> BaibaiState {
    BaibaiState {
        base_token: token(pair.base),
        quote_token: token(BASE_DEPLOYMENT.quote),
        mid: pair.pair.mid,
        q_unit: pair.pair.q_unit,
        c_unit: fixture.c_unit,
        ask: side(&pair.pair.ask),
        bid: side(&pair.pair.bid),
        available: [pair.available_base, fixture.available_quote],
        fee_pips: pair.fee_pips,
    }
    .validated()
    .unwrap()
}

/// The 53,545,000 recording's WETH pair.
pub(super) fn weth_state() -> BaibaiState {
    let fixture = fixture();
    state(&fixture, &fixture.pairs[0])
}

/// The entrypoint's taker fee view as it answers on chain: written out here rather than taken from
/// the reader, so a reader asking with another selector finds no answer, as it would on chain.
pub(super) const TAKER_FEE_SELECTOR: [u8; 4] = [0xa5, 0xcb, 0xd1, 0xb7];

/// A JSON-RPC node serving a recording, with knobs for what a test wants it to get wrong. Like
/// the chain, the custodian reverts for an address that is not a token, and a revert in a call
/// that may not fail reverts the whole `aggregate3`.
#[derive(Clone)]
pub(super) struct FakeNode {
    pub fixture: Fixture,
    /// What the entrypoint answers `curveBook()` with.
    pub entrypoint_curve_book: Address,
    /// Overrides the block timestamp, to age every curve.
    pub timestamp: u64,
    /// Addresses with no token behind them, which the custodian reverts for. Any other token the
    /// recording does not list reads as zero custody.
    pub not_tokens: Vec<Address>,
    /// The chain Multicall3 reports.
    pub chain_id: u64,
    /// Selectors of the deployment's getters that revert, as after an upgrade that removed them.
    pub reverting: Vec<[u8; 4]>,
    /// The block a read sees, to replay an older one.
    pub block: u64,
    /// The only taker the entrypoint expects its fee asked for.
    pub taker: Address,
    /// The implementation each proxy's EIP-1967 slot holds, keyed by proxy.
    pub implementations: HashMap<Address, Address>,
    /// Fails every `eth_getStorageAt`, as a node that cannot serve one.
    pub storage_fails: bool,
}

impl FakeNode {
    pub(super) fn new() -> Self {
        let fixture = fixture();
        Self {
            timestamp: fixture.timestamp,
            block: fixture.block,
            fixture,
            entrypoint_curve_book: BASE_DEPLOYMENT.curve_book,
            not_tokens: Vec::new(),
            chain_id: BASE_DEPLOYMENT.chain.id(),
            reverting: Vec::new(),
            taker: BASE_ROUTER,
            implementations: BASE_DEPLOYMENT
                .proxies()
                .into_iter()
                .map(|(_, proxy, implementation)| (proxy, implementation))
                .collect(),
            storage_fails: false,
        }
    }

    pub(super) async fn spawn(self) -> MockHttpServer {
        spawn_http_server(move |request| Some(("200 OK", self.answer(request)))).await
    }

    /// Answers one request or a batch of them, each by its method.
    fn answer(&self, request: &MockRequest) -> String {
        let request: serde_json::Value = serde_json::from_str(&request.body).unwrap();
        let answer = match request.as_array() {
            Some(batch) => batch
                .iter()
                .map(|request| self.answer_one(request))
                .collect(),
            None => self.answer_one(&request),
        };
        answer.to_string()
    }

    fn answer_one(&self, request: &serde_json::Value) -> serde_json::Value {
        match request["method"].as_str() {
            Some("eth_call") => self.answer_call(request),
            Some("eth_getStorageAt") => self.answer_storage(request),
            method => panic!("the fake node does not serve {method:?}"),
        }
    }

    /// The implementation slot of a proxy, the only storage a read asks for.
    fn answer_storage(&self, request: &serde_json::Value) -> serde_json::Value {
        let params = &request["params"];
        assert_eq!(params[2], "latest", "a read asks for the latest block");
        let slot: U256 = serde_json::from_value(params[1].clone()).unwrap();
        assert_eq!(
            slot,
            U256::from_be_bytes(IMPLEMENTATION_SLOT.0),
            "only the implementation slot"
        );
        if self.storage_fails {
            let error = serde_json::json!({ "code": -32000, "message": "header not found" });
            return serde_json::json!({ "jsonrpc": "2.0", "id": request["id"], "error": error });
        }
        let proxy: Address = serde_json::from_value(params[0].clone()).unwrap();
        let implementation = self
            .implementations
            .get(&proxy)
            .copied()
            .unwrap_or_default();
        let result = implementation.into_word();
        serde_json::json!({ "jsonrpc": "2.0", "id": request["id"], "result": result })
    }

    fn answer_call(&self, request: &serde_json::Value) -> serde_json::Value {
        let block = &request["params"][1];
        assert_eq!(*block, "latest", "a read asks for the latest block");
        let input = request["params"][0]["input"]
            .as_str()
            .or_else(|| request["params"][0]["data"].as_str())
            .unwrap();
        let input = alloy::primitives::hex::decode(input).unwrap();
        let calls = aggregate3Call::abi_decode(&input)
            .unwrap()
            .calls;
        let mut answers = Vec::with_capacity(calls.len());
        for call in &calls {
            let answer = self.call(call.target, &call.callData);
            if answer.is_none() && !call.allowFailure {
                let error = serde_json::json!({ "code": 3, "message": "Multicall3: call failed" });
                return serde_json::json!({ "jsonrpc": "2.0", "id": request["id"], "error": error });
            }
            answers.push(Call3Result {
                success: answer.is_some(),
                returnData: answer.unwrap_or_default().into(),
            });
        }
        let encoded = aggregate3Call::abi_encode_returns(&answers);
        let result = format!("0x{}", alloy::primitives::hex::encode(encoded));
        serde_json::json!({ "jsonrpc": "2.0", "id": request["id"], "result": result })
    }

    fn pair(&self, base: Address) -> Option<&FixturePair> {
        self.fixture
            .pairs
            .iter()
            .find(|pair| pair.base == base)
    }

    /// The answer to one call, or `None` where the chain reverts.
    fn call(&self, target: Address, data: &[u8]) -> Option<Vec<u8>> {
        let Deployment { entrypoint, curve_book, custodian, quote, .. } = BASE_DEPLOYMENT;
        let Ok(selector) = <[u8; 4]>::try_from(&data[..4]) else {
            panic!("a call to {target} carries no selector");
        };
        if target != MULTICALL3_ADDRESS && self.reverting.contains(&selector) {
            return None;
        }
        let answer = match (target, selector) {
            (t, getBlockNumberCall::SELECTOR) if t == MULTICALL3_ADDRESS => {
                U256::from(self.block).abi_encode()
            }
            (t, getChainIdCall::SELECTOR) if t == MULTICALL3_ADDRESS => {
                U256::from(self.chain_id).abi_encode()
            }
            (t, getCurrentBlockTimestampCall::SELECTOR) if t == MULTICALL3_ADDRESS => {
                U256::from(self.timestamp).abi_encode()
            }
            (t, curveBookCall::SELECTOR) if t == entrypoint => {
                self.entrypoint_curve_book.abi_encode()
            }
            (t, custodianCall::SELECTOR) if t == entrypoint => custodian.abi_encode(),
            (t, quoteTokenCall::SELECTOR) if t == entrypoint || t == curve_book => {
                quote.abi_encode()
            }
            (t, ttlCall::SELECTOR) if t == curve_book => self.fixture.ttl.abi_encode(),
            (t, cUnitCall::SELECTOR) if t == curve_book => self.fixture.c_unit.abi_encode(),
            (t, pairCall::SELECTOR) if t == curve_book => {
                let base = pairCall::abi_decode(data).unwrap().base;
                pairCall::abi_encode_returns(&self.view(base))
            }
            (t, validUntilCall::SELECTOR) if t == curve_book => {
                let base = validUntilCall::abi_decode(data)
                    .unwrap()
                    .base;
                self.pair(base)
                    .map_or(0, |pair| pair.valid_until)
                    .abi_encode()
            }
            (t, knotCall::SELECTOR) if t == curve_book => {
                let call = knotCall::abi_decode(data).unwrap();
                // Like the curve book, an index past the side's knots reverts
                // (`InvalidLevelIndex`), and so does any index of an unlisted base.
                let pair = self.pair(call.base)?;
                let side = if call.side == 0 { &pair.pair.ask } else { &pair.pair.bid };
                let [q, extra] = side
                    .knots
                    .get(call.index.to::<usize>())?;
                (U48::from(*q), U48::from(*extra)).abi_encode_params()
            }
            (t, _) if t == entrypoint && data[..4] == TAKER_FEE_SELECTOR => {
                let (taker, base) = <(Address, Address)>::abi_decode_params(&data[4..]).unwrap();
                assert_eq!(taker, self.taker, "the books are priced for the configured taker");
                // Only Tycho's router is whitelisted; other callers pay the venue default.
                let whitelisted = self
                    .pair(base)
                    .filter(|_| taker == BASE_ROUTER);
                whitelisted
                    .map_or(50u32, |pair| pair.fee_pips)
                    .abi_encode()
            }
            (t, availableLiquidityCall::SELECTOR) if t == custodian => {
                let token = availableLiquidityCall::abi_decode(data)
                    .unwrap()
                    .token;
                if token == quote {
                    self.fixture
                        .available_quote
                        .abi_encode()
                } else if self.not_tokens.contains(&token) {
                    return None;
                } else {
                    self.pair(token)
                        .map_or(U256::ZERO, |pair| pair.available_base)
                        .abi_encode()
                }
            }
            _ => panic!("the fake node does not answer {selector:?} on {target}"),
        };
        Some(answer)
    }

    fn view(&self, base: Address) -> PairView {
        let empty = SideView { spreadBps: 0, depthBps: 0, filled: U256::ZERO, knotCount: 0 };
        let Some(pair) = self.pair(base) else {
            return PairView {
                seq: 0,
                fillSeq: 0,
                lastUpdateAt: 0,
                mid: Default::default(),
                qUnit: U256::ZERO,
                ask: empty.clone(),
                bid: empty,
            };
        };
        let side = |side: &FixtureSide| SideView {
            spreadBps: side.spread_bps,
            depthBps: side.depth_bps,
            filled: side.filled,
            knotCount: u8::try_from(side.knots.len()).unwrap(),
        };
        PairView {
            seq: pair.pair.seq,
            fillSeq: pair.pair.fill_seq,
            lastUpdateAt: pair.pair.last_update_at,
            mid: pair.pair.mid.to(),
            qUnit: pair.pair.q_unit,
            ask: side(&pair.pair.ask),
            bid: side(&pair.pair.bid),
        }
    }
}