tycho-simulation 0.470.1

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
//! The provider half of the Metric book feed: fetches one complete book per poll from the
//! `api.metric.xyz` v1 API.
//!
//! Endpoint reference: <https://docs.metric.xyz/RSm94m71kqtGICv4iKRj/developers/api>

use std::mem;

use chrono::DateTime;
use tracing::{debug, warn};

use crate::{
    book::{component::pair_book, BookFeedConfig, BookSnapshot, Books, ReceivedAt},
    pamm::protocols::metric::{
        client::MetricClient, models::MetricMetadata, state::MetricState, PROTOCOL_TYPE,
    },
    snapshot_feed::{errors::FeedError, http::HttpSource},
};

/// Fetches one complete Metric book per poll: every quotable pool between the configured tokens
/// whose USD TVL clears the threshold, as simulate-ready books. `Debug` output omits the API key.
#[derive(derive_more::Debug, Clone)]
pub struct MetricBookSource {
    /// The protocol system stamped on this source's components: the venue's own, or its
    /// fallback-router one for a feed built with `with_fallback_router()`.
    pub protocol_system: &'static str,
    pub book_config: BookFeedConfig,
    /// Reads the venue's pool metadata and quotes.
    pub client: MetricClient,
}

impl HttpSource for MetricBookSource {
    type Snapshot = BookSnapshot<ReceivedAt>;

    /// A metadata failure fails the poll, and so does losing every pool: a book that reads as
    /// nothing withdraws every pair the venue was serving, which is an outage rather than a
    /// poll that worked. Losing some of the pools only drops those from this book.
    async fn fetch(&self) -> Result<BookSnapshot<ReceivedAt>, FeedError> {
        let metadata = self.client.fetch_metadata().await?;

        let quotable: Vec<MetricMetadata> = metadata
            .into_iter()
            .filter(|pool| {
                self.book_config
                    .pair_tokens(&pool.token0, &pool.token1)
                    .is_some() &&
                    // v1 metadata carries the USD TVL directly, so no cross-pool price
                    // normalization is needed.
                    self.book_config
                        .clears_min_tvl(pool.tvl_fiat.unwrap_or(0.0), &pool.pool_address)
            })
            .collect();

        let polled = self
            .client
            .fetch_bid_ask_each(quotable)
            .await;
        let requested = polled.len();
        let mut failures: Vec<FeedError> = Vec::new();
        let mut books = Books::new();
        for (pool, bid_ask) in polled {
            let bid_ask = match bid_ask {
                Ok(bid_ask) => bid_ask,
                Err(e) => {
                    warn!(pool = %pool.pool_address, error = %e, "skipping pool, bid/ask fetch failed");
                    failures.push(e);
                    continue;
                }
            };
            let Some((token0, token1)) = self
                .book_config
                .pair_tokens(&pool.token0, &pool.token1)
            else {
                continue;
            };
            if !bid_ask.is_quotable() {
                debug!(pool = %pool.pool_address, "skipping pool, not quotable");
                continue;
            }

            let component_id = pool.pool_address.clone();
            let updated_at = DateTime::from_timestamp(bid_ask.server_ts as i64, 0);
            let state = MetricState {
                base_token: token0.clone(),
                quote_token: token1.clone(),
                metadata: pool,
                bid_ask,
            };
            books.insert(pair_book(
                self.protocol_system,
                PROTOCOL_TYPE,
                self.book_config.chain,
                component_id,
                state,
                updated_at,
            ));
        }
        if requested > 0 && failures.len() == requested {
            return Err(every_pool_failed(failures));
        }
        Ok(BookSnapshot::received_now(books))
    }
}

/// What a poll reports when none of the pools it asked answered: the pools' own verdict where
/// they agree on it, and an unreachable venue where they do not. One pool calling its failure
/// fatal is about that pool; every pool calling it fatal is about the venue, and nothing is
/// gained by polling on. The individual failures are logged as they arrive.
fn every_pool_failed(failures: Vec<FeedError>) -> FeedError {
    let summary =
        format!("none of Metric's {} quotable pools answered its bid/ask request", failures.len());
    let mut failures = failures.into_iter();
    let Some(first) = failures.next() else {
        return FeedError::Connection(summary);
    };
    if failures.all(|other| mem::discriminant(&other) == mem::discriminant(&first)) {
        first.in_context(summary)
    } else {
        FeedError::Connection(summary)
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, str::FromStr, sync::Arc};

    use num_bigint::BigUint;
    use rstest::rstest;
    use tycho_common::{
        models::{token::Token, Chain},
        Bytes,
    };

    use super::*;
    use crate::{
        book::test_token_map,
        evm::protocol::utils::bytes_to_address,
        pamm::protocols::metric::{
            client::DEFAULT_BID_ASK_CONCURRENCY, feed::MetricFeedBuilder, FALLBACK_PROTOCOL_SYSTEM,
            PROTOCOL_SYSTEM,
        },
        snapshot_feed::http::test_support::spawn_http_server,
    };

    const FIRST_POOL: &str = "0x1111111111111111111111111111111111111111";
    const SECOND_POOL: &str = "0x2222222222222222222222222222222222222222";

    /// How the venue answers a key it has stopped accepting. It is about this request, so a
    /// later poll may be served.
    const REFUSED_KEY: (&str, &str) = ("401 Unauthorized", r#"{"message":"invalid api key"}"#);

    /// How the venue answers for a chain it does not serve, which no later poll reaches either.
    const UNSERVED_CHAIN: (&str, &str) = ("404 Not Found", r#"{"error": "Unknown chainId: 1"}"#);

    fn pool_json(pool: &str, tvl_fiat: Option<f64>) -> serde_json::Value {
        serde_json::json!({
            "poolAddress": pool,
            "token0": weth().address.to_string(),
            "token1": usdc().address.to_string(),
            "tvlFiat": tvl_fiat,
        })
    }

    fn metadata_page(pools: &[serde_json::Value], next_offset: Option<u64>) -> String {
        serde_json::json!({ "data": pools, "nextOffset": next_offset }).to_string()
    }

    /// The bid/ask body of a healthy, quotable pool; `unhealthy` flips the provider status.
    fn bid_ask_json(server_ts: u64, unhealthy: bool) -> String {
        let mut body = serde_json::json!({
            "bidAdj": q64(3000).to_string(),
            "askAdj": q64(3010).to_string(),
            "totalToken0Available": "1000000000000000000",
            "totalToken1Available": "3000000000",
            "serverTs": server_ts,
            "depth": {
                "asks": [{
                    "binIdx": 0,
                    "price": q64(3010).to_string(),
                    "cumulativeVolume": "3000000000",
                    "cumulativeInputVolume": "1000000000000000000"
                }],
                "bids": []
            }
        });
        if unhealthy {
            body["priceProviderStatus"] = serde_json::json!("degraded");
        }
        body.to_string()
    }

    /// The `/bid_ask` target of `pool` on Ethereum as the client requests it: the chain's own
    /// prefix, then the pool's checksummed address.
    fn bid_ask_target(pool: &str) -> String {
        let checksummed = bytes_to_address(&Bytes::from_str(pool).unwrap())
            .unwrap()
            .to_checksum(None);
        format!("/public/v1/evm/1/{checksummed}/bid_ask")
    }

    /// A source against the Metric deployment at `base_url`, with an empty token universe and no
    /// TVL floor.
    fn source(chain: Chain, base_url: &str, api_key: &str) -> MetricBookSource {
        MetricBookSource {
            protocol_system: PROTOCOL_SYSTEM,
            book_config: BookFeedConfig {
                chain,
                tokens: Arc::new(HashMap::new()),
                min_tvl_usd: 0.0,
            },
            client: MetricClient::new(
                Some(base_url),
                chain.try_id().unwrap(),
                api_key.to_string(),
                DEFAULT_BID_ASK_CONCURRENCY,
            ),
        }
    }

    fn weth() -> Token {
        let address = Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap();
        Token::new(&address, "WETH", 18, 0, &[], Chain::Ethereum, 100)
    }

    fn usdc() -> Token {
        let address = Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap();
        Token::new(&address, "USDC", 6, 0, &[], Chain::Ethereum, 100)
    }

    fn q64(price: u64) -> BigUint {
        BigUint::from(price) << 64usize
    }

    #[tokio::test]
    async fn a_fallback_source_labels_its_components_for_the_fallback_router() {
        const POOL: &str = "0x1111111111111111111111111111111111111111";
        const SERVER_TS: u64 = 1_700_000_000;
        let page = metadata_page(&[pool_json(POOL, Some(1_000.0))], None);
        let server = spawn_http_server(move |request| {
            if request
                .target
                .starts_with("/public/v1/evm/1/metadata?")
            {
                return Some(("200 OK", page.clone()));
            }
            Some(("200 OK", bid_ask_json(SERVER_TS, false)))
        })
        .await;
        let mut source = MetricBookSource {
            protocol_system: FALLBACK_PROTOCOL_SYSTEM,
            ..source(Chain::Ethereum, &server.url(), "test_key")
        };
        source.book_config.tokens = Arc::new(test_token_map(&[
            (&weth().address, "WETH", 18),
            (&usdc().address, "USDC", 6),
        ]));

        let books = source.fetch().await.unwrap().books;

        let book = books.values().next().expect("one book");
        assert_eq!(book.component.protocol_system, "fallback:rfq:metric");
        // A Metric component is identified by its pool address, whatever the system labels it.
        assert_eq!(book.component.id.to_string(), POOL);
    }

    #[tokio::test]
    async fn fetch_books_drops_failed_and_unquotable_pools_but_keeps_the_rest() {
        const HEALTHY: &str = "0x1111111111111111111111111111111111111111";
        const FAILING: &str = "0x2222222222222222222222222222222222222222";
        const UNQUOTABLE: &str = "0x3333333333333333333333333333333333333333";
        const BELOW_FLOOR: &str = "0x4444444444444444444444444444444444444444";
        const UNPRICED: &str = "0x5555555555555555555555555555555555555555";
        const SERVER_TS: u64 = 1_700_000_000;
        let page = metadata_page(
            &[
                pool_json(HEALTHY, Some(1_000.0)),
                pool_json(FAILING, Some(1_000.0)),
                pool_json(UNQUOTABLE, Some(1_000.0)),
                pool_json(BELOW_FLOOR, Some(99.0)),
                pool_json(UNPRICED, None),
            ],
            None,
        );
        let server = spawn_http_server(move |request| {
            if request
                .target
                .starts_with("/public/v1/evm/1/metadata?")
            {
                return Some(("200 OK", page.clone()));
            }
            if request.target == bid_ask_target(HEALTHY) {
                return Some(("200 OK", bid_ask_json(SERVER_TS, false)));
            }
            if request.target == bid_ask_target(FAILING) {
                return Some(("500 Internal Server Error", "boom".to_string()));
            }
            if request.target == bid_ask_target(UNQUOTABLE) {
                return Some(("200 OK", bid_ask_json(SERVER_TS, true)));
            }
            // Pools filtered out on metadata must never get here; answering them as healthy
            // makes such a slip show up as an extra emitted book.
            Some(("200 OK", bid_ask_json(SERVER_TS, false)))
        })
        .await;
        let endpoint = server.url();
        let mut source = source(Chain::Ethereum, &endpoint, "key");
        source.book_config.tokens = Arc::new(test_token_map(&[
            (&weth().address, "WETH", 18),
            (&usdc().address, "USDC", 6),
        ]));
        source.book_config.min_tvl_usd = 100.0;

        let books = source.fetch().await.unwrap().books;

        let healthy_id = Bytes::from_str(HEALTHY)
            .unwrap()
            .to_string();
        assert_eq!(books.keys().collect::<Vec<_>>(), vec![&healthy_id]);
        let book = &books[&healthy_id];
        assert_eq!(book.component.id, Bytes::from(healthy_id.as_str()));
        assert_eq!(book.updated_at, DateTime::from_timestamp(SERVER_TS as i64, 0));
    }

    #[tokio::test]
    async fn metadata_failure_fails_the_poll() {
        let server =
            spawn_http_server(|_| Some(("500 Internal Server Error", "boom".to_string()))).await;
        let endpoint = server.url();
        let source = source(Chain::Ethereum, &endpoint, "key");

        let result = source.fetch().await;

        assert!(matches!(result, Err(FeedError::Connection(_))), "got {result:?}");
    }

    /// A key the venue has stopped accepting answers every pool the same way. The empty book
    /// that leaves would withdraw every pair while reporting a poll that worked, so the feed's
    /// failure budget would never see the outage. What the pools answered decides the failure:
    /// a chain the venue has stopped serving is a chain no later poll reaches either, while one
    /// pool alone saying so is about that pool, whatever the pool beside it answered.
    #[rstest]
    #[case::every_pool_on_a_refused_key(REFUSED_KEY, REFUSED_KEY, false)]
    #[case::every_pool_on_an_unserved_chain(UNSERVED_CHAIN, UNSERVED_CHAIN, true)]
    #[case::one_pool_of_two_refusing_the_chain(UNSERVED_CHAIN, REFUSED_KEY, false)]
    #[tokio::test]
    async fn losing_every_pool_fails_the_poll(
        #[case] first_answer: (&'static str, &'static str),
        #[case] second_answer: (&'static str, &'static str),
        #[case] fatal: bool,
    ) {
        let page = metadata_page(
            &[pool_json(FIRST_POOL, Some(1_000.0)), pool_json(SECOND_POOL, Some(1_000.0))],
            None,
        );
        let server = spawn_http_server(move |request| {
            if request
                .target
                .starts_with("/public/v1/evm/1/metadata?")
            {
                return Some(("200 OK", page.clone()));
            }
            let (status, body) = if request.target == bid_ask_target(FIRST_POOL) {
                first_answer
            } else {
                second_answer
            };
            Some((status, body.to_string()))
        })
        .await;
        let endpoint = server.url();
        let mut source = source(Chain::Ethereum, &endpoint, "revoked");
        source.book_config.tokens = Arc::new(test_token_map(&[
            (&weth().address, "WETH", 18),
            (&usdc().address, "USDC", 6),
        ]));

        let result = source.fetch().await;

        let error = result.expect_err("a poll that lost every pool must fail");
        assert_eq!(error.is_fatal(), fatal, "{error}");
        assert!(
            error
                .to_string()
                .contains("none of Metric's 2 quotable pools"),
            "{error}"
        );
    }

    /// One poll fetches every pool's bid/ask and shares a single `request_timeout`, so the
    /// default concurrency has to keep a full poll inside that budget at the pool count Metric
    /// actually serves. Base is the chain with pools. The token universe is stubbed from the
    /// metadata itself: what this measures is the poll, not the pricing.
    #[tokio::test]
    #[ignore = "hits Metric's public API; requires METRIC_API_KEY"]
    async fn live_metric_poll_fits_the_request_timeout_at_the_default_concurrency() {
        let api_key = std::env::var("METRIC_API_KEY").expect("METRIC_API_KEY not set");
        let mut source = source(Chain::Base, "https://api.metric.xyz", &api_key);
        let metadata = source
            .client
            .fetch_metadata()
            .await
            .unwrap();
        assert!(!metadata.is_empty(), "Metric serves no Base pools");

        let tokens = metadata
            .iter()
            .flat_map(|pool| [pool.token0.clone(), pool.token1.clone()])
            .map(|address| {
                let token = Token::new(&address, "TKN", 18, 0, &[], Chain::Base, 100);
                (address, token)
            })
            .collect();
        source.book_config =
            BookFeedConfig { chain: Chain::Base, tokens: Arc::new(tokens), min_tvl_usd: 0.0 };

        let budget = MetricFeedBuilder::default_feed_config().request_timeout;
        let started = std::time::Instant::now();
        let books = source.fetch().await.unwrap().books;
        let elapsed = started.elapsed();

        assert!(!books.is_empty(), "no quotable pool among the {} on Base", metadata.len());
        println!(
            "{} pools, {} books, {elapsed:?} of a {budget:?} budget",
            metadata.len(),
            books.len()
        );
        assert!(elapsed < budget, "a poll of {} pools took {elapsed:?}", metadata.len());
    }
}