tycho-simulation 0.477.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::{collections::HashMap, fmt};

use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use tycho_common::{models::protocol::GetAmountOutParams, Bytes};

use crate::{
    book::{levels::Levels, wire::SkipInvalidEntries},
    rfq::errors::RFQError,
    serde_helpers::evm_address,
};

/// The error Hashflow reports on a rejected request, sent as an object alongside HTTP 200.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HashflowError {
    pub code: u64,
    pub message: String,
}

/// The codes below are the ones Hashflow has been seen to answer with. The venue publishes no
/// catalogue of them — the numbered vocabulary in its documentation belongs to the market-maker
/// WebSocket API, not to the taker endpoints these reach — and one code covers several
/// conditions, so what a code not listed here means is unknown, and telling two of them apart
/// can need the message.
impl HashflowError {
    /// No maker quotes the request: the one it was addressed to declined it, or is unknown to
    /// the API.
    const NO_MAKER_SUPPORTS_REQUEST: u64 = 82;

    /// The amount asked for is above what the maker quotes.
    const EXCEEDS_SUPPORTED_AMOUNTS: u64 = 76;

    /// The amount asked for is below the maker's first level.
    const BELOW_MINIMUM_AMOUNT: u64 = 79;

    /// Whether this is a maker declining the trade rather than the request being wrong: nobody
    /// quotes it, or the amount is outside the range the maker it went to supports. All three are
    /// answers about one trade at one moment, which the same ladder quotes at another size.
    pub fn declines_the_trade(&self) -> bool {
        matches!(
            self.code,
            Self::NO_MAKER_SUPPORTS_REQUEST |
                Self::EXCEEDS_SUPPORTED_AMOUNTS |
                Self::BELOW_MINIMUM_AMOUNT
        )
    }

    /// Whether this is Hashflow stating it does not price the chain it was asked about.
    ///
    /// The code it answers here with covers an unknown market maker and an unknown chain type
    /// too, so what it refused is in the message alone.
    pub fn refuses_the_chain(&self) -> bool {
        self.message
            .starts_with("Invalid chainId")
    }
}

/// The envelope Hashflow answers a request it would not serve with, on either book endpoint.
/// A body it does serve carries no `error` at all, so parsing one is what tells the two apart.
#[derive(Debug, Clone, Deserialize)]
pub struct HashflowFailure {
    pub error: HashflowError,
}

impl fmt::Display for HashflowError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} (code {})", self.message, self.code)
    }
}

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HashflowPriceLevelsResponse {
    pub status: String, // "success" or "fail"
    #[serde_as(deserialize_as = "Option<SkipInvalidEntries>")]
    pub levels: Option<HashMap<String, Vec<HashflowMarketMakerLevels>>>,
    pub error: Option<HashflowError>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HashflowMarketMakerLevels {
    pub pair: HashflowPair,
    #[serde(with = "hashflow_levels")]
    pub levels: Levels,
}

/// Hashflow's wire form of a ladder: `{"q": "<quantity>", "p": "<price>"}` objects with decimal
/// strings.
mod hashflow_levels {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use serde_with::{serde_as, DisplayFromStr};

    use crate::book::levels::{Levels, PriceLevel};

    #[serde_as]
    #[derive(Serialize, Deserialize)]
    struct WireLevel {
        #[serde_as(as = "DisplayFromStr")]
        q: f64,
        #[serde_as(as = "DisplayFromStr")]
        p: f64,
    }

    pub fn serialize<S: Serializer>(levels: &Levels, serializer: S) -> Result<S::Ok, S::Error> {
        levels
            .iter()
            .map(|level| WireLevel { q: level.quantity, p: level.price })
            .collect::<Vec<_>>()
            .serialize(serializer)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Levels, D::Error> {
        let levels = Vec::<WireLevel>::deserialize(deserializer)?
            .into_iter()
            .map(|level| PriceLevel { price: level.p, quantity: level.q })
            .collect();
        Levels::new(levels).map_err(serde::de::Error::custom)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowPair {
    #[serde(deserialize_with = "evm_address::deserialize")]
    pub base_token: Bytes,
    #[serde(deserialize_with = "evm_address::deserialize")]
    pub quote_token: Bytes,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowMarketMakersResponse {
    pub market_makers: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowQuoteRequest {
    pub source: String,
    pub base_chain: HashflowChain,
    pub quote_chain: HashflowChain,
    pub rfqs: Vec<HashflowRFQ>,
    pub calldata: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowChain {
    chain_type: String,
    chain_id: u64,
}

impl HashflowChain {
    /// The chain as Hashflow's quote requests name it: an EVM chain id.
    pub fn evm(chain_id: u64) -> Self {
        HashflowChain { chain_type: "evm".to_string(), chain_id }
    }
}

/// Optional fields are left out of the request rather than sent as `null`.
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowRFQ {
    pub base_token: String,
    pub quote_token: String,
    // Decimal amount (e.g. "1000000" for 1 USDT)
    pub base_token_amount: Option<String>,
    // Decimal amount (e.g. "1000000" for 1 USDT)
    pub quote_token_amount: Option<String>,
    pub trader: String,
    pub effective_trader: Option<String>,
    /// The makers the RFQ goes to; a quote from any other is only possible as the API's
    /// fallback, which `options` disables.
    pub market_makers: Vec<String>,
    pub options: HashflowRFQOptions,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowRFQOptions {
    /// Without it, the API answers a failed maker with the next best one. That maker's liquidity
    /// is a component of its own, which the same route may already be filling, so its quote could
    /// draw on the same depth twice, at a price the requesting state did not simulate.
    pub do_not_retry_with_other_makers: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowQuoteResponse {
    pub status: String,
    pub error: Option<HashflowError>,
    rfq_id: String,
    internal_rfq_ids: Option<Vec<String>>,
    pub quotes: Option<Vec<HashflowQuote>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowQuote {
    pub quote_data: HashflowQuoteData,
    pub signature: Bytes,
    pub target_contract: Option<Bytes>,
    pub value: Option<String>,
}

impl HashflowQuote {
    pub fn validate(
        &self,
        params: &GetAmountOutParams,
        expected_effective_trader: &Bytes,
    ) -> Result<(), RFQError> {
        if self.quote_data.base_token != params.token_in {
            return Err(RFQError::FatalError(format!(
                "Base token mismatch: expected {}, got {}",
                params.token_in, self.quote_data.base_token
            )));
        }
        if self.quote_data.quote_token != params.token_out {
            return Err(RFQError::FatalError(format!(
                "Quote token mismatch: expected {}, got {}",
                params.token_out, self.quote_data.quote_token
            )));
        }
        if self.quote_data.trader != params.receiver {
            return Err(RFQError::FatalError(format!(
                "Trader address mismatch: expected {}, got {}",
                params.receiver, self.quote_data.trader
            )));
        }
        let effective_trader = self
            .quote_data
            .effective_trader
            .clone()
            .unwrap_or_else(|| self.quote_data.trader.clone());
        if &effective_trader != expected_effective_trader {
            return Err(RFQError::FatalError(format!(
                "Effective trader mismatch: expected {expected_effective_trader}, got {effective_trader}"
            )));
        }
        if self.quote_data.base_token_amount != params.amount_in.to_string() {
            return Err(RFQError::FatalError(format!(
                "Base token amount mismatch: expected {}, got {}",
                params.amount_in, self.quote_data.base_token_amount
            )));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HashflowQuoteData {
    pub base_token: Bytes,
    pub quote_token: Bytes,
    // Decimal amount (e.g. "1000000" for 1 USDT)
    pub base_token_amount: String,
    // Decimal amount (e.g. "1000000" for 1 USDT)
    pub quote_token_amount: String,
    pub trader: Bytes,
    pub effective_trader: Option<Bytes>,
    #[serde(rename = "txid")]
    pub tx_id: Bytes,
    pub pool: Bytes,
    pub quote_expiry: u64,
    pub nonce: u64,
    pub external_account: Option<Bytes>,
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;
    use crate::book::levels::PriceLevel;

    /// A maker quoting a ladder Hashflow got wrong goes quiet for the poll; the makers that
    /// priced their pairs correctly still publish.
    #[test]
    fn a_maker_with_a_malformed_ladder_does_not_cost_the_others() {
        let json = r#"{"status":"success","levels":{
                "mm1":[{"pair":{"baseToken":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quoteToken":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"},"levels":[{"q":"1","p":"0"}]}],
                "mm2":[{"pair":{"baseToken":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quoteToken":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"},"levels":[{"q":"1","p":"3000"}]}]
            }}"#;

        let response: HashflowPriceLevelsResponse = serde_json::from_str(json).unwrap();

        let levels = response.levels.unwrap();
        assert!(levels["mm1"].is_empty());
        assert_eq!(levels["mm2"].len(), 1);
    }

    /// Nothing readable in the response is a parsing failure, not an empty book: a published
    /// snapshot is complete, so an empty one withdraws every pair the venue had.
    #[test]
    fn a_response_with_no_readable_maker_is_an_error() {
        let json = r#"{"status":"success","levels":{"mm1":[{"pair":{"baseToken":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quoteToken":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"},"levels":[{"q":"1","p":"0"}]}]}}"#;

        assert!(serde_json::from_str::<HashflowPriceLevelsResponse>(json).is_err());
    }

    /// Hashflow answers a rejected RFQ with HTTP 200 and an `error` object, so the quote response
    /// has to deserialize that shape rather than only the successful one.
    #[test]
    fn deserialize_rejected_quote_response() {
        let body = r#"{"status":"fail","rfqId":"0x2250000000000000000000000000000f",
            "error":{"code":82,"message":"No maker supports this request"}}"#;

        let response: HashflowQuoteResponse = serde_json::from_str(body).unwrap();

        assert_eq!(response.status, "fail");
        assert_eq!(response.error.unwrap().to_string(), "No maker supports this request (code 82)");
    }

    #[test]
    fn levels_round_trip_through_the_wire_form() {
        let json = r#"{"pair":{"baseToken":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quoteToken":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"},"levels":[{"q":"1","p":"3000"},{"q":"2.5","p":"2999"}]}"#;

        let mm_levels: HashflowMarketMakerLevels = serde_json::from_str(json).unwrap();

        assert_eq!(
            &*mm_levels.levels,
            &[
                PriceLevel { quantity: 1.0, price: 3000.0 },
                PriceLevel { quantity: 2.5, price: 2999.0 },
            ]
        );
        assert_eq!(
            mm_levels.pair.base_token,
            Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()
        );
        let json = serde_json::to_string(&mm_levels).unwrap();
        assert!(json.contains(r#"{"q":"1","p":"3000"}"#), "{json}");

        // The adapter hands the decoded levels to `Levels::new`, so its validation applies.
        let invalid = json.replace(r#""p":"3000""#, r#""p":"0""#);
        assert!(serde_json::from_str::<HashflowMarketMakerLevels>(&invalid).is_err());
    }

    #[cfg(test)]
    mod hashflow_quote_validate_tests {
        use num_bigint::BigUint;
        use tycho_common::models::protocol::GetAmountOutParams;

        use super::*;

        fn hex_to_bytes(hex: &str) -> Bytes {
            Bytes::from_str(hex).unwrap()
        }

        fn quote_data() -> HashflowQuoteData {
            HashflowQuoteData {
                base_token: hex_to_bytes("0x1111111111111111111111111111111111111111"),
                quote_token: hex_to_bytes("0x2222222222222222222222222222222222222222"),
                base_token_amount: "1000".to_string(),
                quote_token_amount: "2000".to_string(),
                trader: hex_to_bytes("0x3333333333333333333333333333333333333333"),
                effective_trader: Some(hex_to_bytes("0x6666666666666666666666666666666666666666")),
                tx_id: hex_to_bytes("0x4444444444444444444444444444444444444444"),
                pool: hex_to_bytes("0x5555555555555555555555555555555555555555"),
                quote_expiry: 123456,
                nonce: 1,
                external_account: None,
            }
        }

        fn params() -> GetAmountOutParams {
            GetAmountOutParams {
                amount_in: BigUint::from(1000u32),
                token_in: hex_to_bytes("0x1111111111111111111111111111111111111111"),
                token_out: hex_to_bytes("0x2222222222222222222222222222222222222222"),
                sender: hex_to_bytes("0x6666666666666666666666666666666666666666"),
                receiver: hex_to_bytes("0x3333333333333333333333333333333333333333"),
                origin: None,
            }
        }

        fn expected_effective_trader() -> Bytes {
            hex_to_bytes("0x6666666666666666666666666666666666666666")
        }

        fn quote() -> HashflowQuote {
            HashflowQuote {
                quote_data: quote_data(),
                signature: hex_to_bytes("0x7777777777777777777777777777777777777777"),
                target_contract: None,
                value: None,
            }
        }

        #[test]
        fn validate_success() {
            let quote = quote();
            let params = params();
            assert!(quote
                .validate(&params, &expected_effective_trader())
                .is_ok());
        }

        #[test]
        fn validate_base_token_mismatch() {
            let mut quote = quote();
            quote.quote_data.base_token =
                hex_to_bytes("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Base token mismatch"));
        }

        #[test]
        fn validate_quote_token_mismatch() {
            let mut quote = quote();
            quote.quote_data.quote_token =
                hex_to_bytes("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Quote token mismatch"));
        }

        #[test]
        fn validate_trader_mismatch() {
            let mut quote = quote();
            quote.quote_data.trader = hex_to_bytes("0xabcdefabcdefabcdefabcdefabcdefabcdefabcd");
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Trader address mismatch"));
        }

        #[test]
        fn validate_effective_trader_mismatch() {
            let mut quote = quote();
            quote.quote_data.effective_trader =
                Some(hex_to_bytes("0xabcdefabcdefabcdefabcdefabcdefabcdefabcd"));
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Effective trader mismatch"));
        }

        #[test]
        fn validate_effective_trader_absent() {
            // An answer without an effectiveTrader is not tied to the address we requested
            // (Hashflow ties it to the trader instead), so validation must reject it.
            let mut quote = quote();
            quote.quote_data.effective_trader = None;
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Effective trader mismatch"));
        }

        #[test]
        fn validate_base_token_amount_mismatch() {
            let mut quote = quote();
            quote.quote_data.base_token_amount = "9999".to_string();
            let params = params();
            let err = quote
                .validate(&params, &expected_effective_trader())
                .unwrap_err();
            assert!(format!("{err:?}").contains("Base token amount mismatch"));
        }
    }
}