tycho-simulation 0.551.0

Provides tools for interacting with protocol states, calculating spot prices, and quoting token swaps.
Documentation
//! The Hanji markets the feed serves, each pinned to what its fills were recorded against.

use alloy::primitives::Address;
use serde::{Deserialize, Serialize};
use tycho_common::Bytes;

/// One market the feed reads: its proxy orderbook and CLOB with the implementation each must
/// run, its two tokens as the contracts name them, token X (the base) then token Y (the quote),
/// which side if any is the native token, and the gas of its fills.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Market {
    /// For logs and failure messages, e.g. `ETH/USDC`.
    pub name: &'static str,
    pub orderbook: Address,
    pub orderbook_implementation: Address,
    /// The CLOB the quoters place their orders on.
    pub clob: Address,
    pub clob_implementation: Address,
    pub token_x: Address,
    pub token_y: Address,
    /// The side `getConfig()` names by the wrapped native token and the proxy pays and takes as
    /// native ETH.
    pub native: NativeSide,
    /// What its fills cost a contract caller (see [`super::BASE_DEPLOYMENT`]).
    pub gas: FillGas,
}

/// What the proxy's swap costs a contract caller once the refund the quoter earns by clearing its
/// virtual orders is paid back, by how many price levels the fill touches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct FillGas {
    /// A fill from the first level only.
    pub first_level: u64,
    /// What each further level adds.
    pub per_further_level: u64,
}

impl FillGas {
    /// The gas of a fill that touches `levels` levels.
    pub(super) fn of(self, levels: usize) -> u64 {
        let further = u64::try_from(levels.saturating_sub(1)).unwrap_or(u64::MAX);
        self.first_level.saturating_add(
            self.per_further_level
                .saturating_mul(further),
        )
    }
}

/// Which side of a market, if any, the native token is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSide {
    None,
    X,
    Y,
}

impl Market {
    /// The market's base and quote as components carry them: `native` (the chain's native token)
    /// for the native side, the contract's address for the other.
    pub(super) fn published_tokens(&self, native: &Bytes) -> (Bytes, Bytes) {
        let side = |token: Address, is_native: bool| {
            if is_native {
                native.clone()
            } else {
                Bytes::from(token.to_vec())
            }
        };
        (
            side(self.token_x, self.native == NativeSide::X),
            side(self.token_y, self.native == NativeSide::Y),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pamm::protocols::hanji::test_support::{CBBTC, USDC};

    /// The native side is published as the chain's native token, the other side as is.
    #[test]
    fn test_published_tokens_native_side() {
        let native = Bytes::from(vec![0; 20]);
        let market = Market {
            name: "cbBTC/ETH",
            orderbook: Address::repeat_byte(1),
            orderbook_implementation: Address::repeat_byte(2),
            clob: Address::repeat_byte(3),
            clob_implementation: Address::repeat_byte(4),
            token_x: CBBTC,
            token_y: Address::repeat_byte(0x42),
            native: NativeSide::Y,
            gas: FillGas { first_level: 1, per_further_level: 1 },
        };
        let usdc_market = Market { token_y: USDC, native: NativeSide::None, ..market };

        let (base, quote) = market.published_tokens(&native);

        assert_eq!(base, Bytes::from(CBBTC.to_vec()));
        assert_eq!(quote, native);
        assert_eq!(usdc_market.published_tokens(&native).1, Bytes::from(USDC.to_vec()));
    }
}