Skip to main content

tycho_simulation/evm/protocol/checkable/
mod.rs

1//! What the property checks in `tycho-test` need from protocol states.
2//!
3//! The generic `ProtocolSim` properties hold for every state. A state the checks run on implements
4//! [`CheckableState`]: the kind of limit its venue has, which states the venue can hold, and the
5//! amounts where the state's own behaviour changes. Behind the `proptest` feature, [`generator`]
6//! generates states to check.
7
8use num_bigint::BigUint;
9use tycho_common::Bytes;
10
11#[cfg(feature = "proptest")]
12pub mod generator;
13
14/// What kind of bound a direction's `get_limits` reports.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum LimitKind {
17    /// `max_input` is the largest input the venue accepts (a vault cap, a redemption limit, the
18    /// end of the liquidity range), whichever side of the swap the venue bounds. One unit more,
19    /// `get_amount_out` errors and execution reverts.
20    Hard,
21    /// The venue has no bound on the input, so `max_input` is a recommended size, as the
22    /// `ProtocolSim::get_limits` documentation allows. Twice it still quotes.
23    Soft,
24    /// The venue has a bound, and `max_input` stays below it because `get_limits` cannot report
25    /// the largest input the venue accepts, such as a bound on the output without an exact
26    /// inverse. One unit past it, the quote and the execution still succeed.
27    UnderCap,
28}
29
30/// What the checks ask of a state they hold, decoded or generated.
31pub trait CheckableState {
32    /// The kind of bound `get_limits(sell_token, buy_token)` reports in this state.
33    fn limit_kind(&self, sell_token: &Bytes, buy_token: &Bytes) -> LimitKind;
34
35    /// Whether the state is one the venue can hold on chain, with what is wrong when it is not.
36    fn check_valid(&self) -> Result<(), String>;
37
38    /// Amounts to quote `get_amount_out(_, sell_token, buy_token)` at in this state besides the
39    /// ones every state is quoted at: where its own behaviour changes, such as the smallest input
40    /// that buys any output. None by default.
41    fn amounts(&self, _sell_token: &Bytes, _buy_token: &Bytes) -> Vec<BigUint> {
42        Vec::new()
43    }
44}