use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use tycho_common::{models::Chain, Bytes};
use crate::book::{
levels::{Levels, PriceLevel},
wire::SkipInvalidEntries,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NativeOrderbookSide {
Bid,
Ask,
}
fn deserialize_levels<'de, D>(deserializer: D) -> Result<Levels, D::Error>
where
D: serde::Deserializer<'de>,
{
let levels = Vec::<[f64; 2]>::deserialize(deserializer)?
.into_iter()
.map(|[quantity, price]| PriceLevel { quantity, price })
.collect();
Levels::new(levels).map_err(serde::de::Error::custom)
}
fn deserialize_non_negative_f64<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = f64::deserialize(deserializer)?;
if !value.is_finite() || value < 0.0 {
return Err(serde::de::Error::custom("expected a non-negative finite number"))
}
Ok(value)
}
#[serde_as]
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct NativeOrderbook(
#[serde_as(deserialize_as = "SkipInvalidEntries")] pub Vec<NativeOrderbookEntry>,
);
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct NativeOrderbookEntry {
pub base_address: Bytes,
pub quote_address: Bytes,
#[serde(deserialize_with = "deserialize_non_negative_f64")]
pub minimum_in_base: f64,
pub side: NativeOrderbookSide,
#[serde(deserialize_with = "deserialize_levels")]
pub levels: Levels,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct NativeBookSide {
pub levels: Levels,
pub minimum_in: f64,
pub minimum_out: f64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NativePriceData {
pub base_address: Bytes,
pub quote_address: Bytes,
pub bids: NativeBookSide,
pub asks: NativeBookSide,
}
impl NativePriceData {
pub fn calculate_tvl(&self, quote_price_data: Option<&NativePriceData>) -> Option<f64> {
let bid_tvl = self.bids.levels.notional();
let ask_tvl = self.asks.levels.notional();
let total_tvl = match (self.bids.levels.is_empty(), self.asks.levels.is_empty()) {
(false, false) => bid_tvl.midpoint(ask_tvl),
(false, true) => bid_tvl,
(true, false) => ask_tvl,
(true, true) => 0.0,
};
if !total_tvl.is_finite() {
return None
}
if let Some(quote_data) = quote_price_data {
let price_of_quote_token = quote_data.get_mid_price(total_tvl, &self.quote_address)?;
let converted_tvl = total_tvl * price_of_quote_token;
return converted_tvl
.is_finite()
.then_some(converted_tvl)
}
Some(total_tvl)
}
pub fn get_mid_price(&self, amount: f64, sell_token: &Bytes) -> Option<f64> {
if sell_token != &self.base_address && sell_token != &self.quote_address {
return None;
}
let (bids, asks) = (&self.bids.levels, &self.asks.levels);
let (bids_price, asks_price) = if sell_token == &self.quote_address {
(bids.invert().average_price(amount), asks.invert().average_price(amount))
} else {
(bids.average_price(amount), asks.average_price(amount))
};
match (bids_price, asks_price) {
(Some(bid), Some(ask)) => Some(bid.midpoint(ask)),
(Some(bid), None) => Some(bid),
(None, Some(ask)) => Some(ask),
(None, None) => None,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct FirmQuoteRequest {
pub from_address: String,
pub src_chain: NativeSupportedChain,
pub dst_chain: NativeSupportedChain,
pub token_in: String,
pub token_out: String,
pub amount_wei: String,
pub version: u32,
pub allow_multihop: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct WidgetFee {
pub signer: String,
pub fee_recipient: String,
pub fee_rate: f64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct TxRequest {
pub target: String,
pub calldata: String,
pub value: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct FirmQuoteOrder {
pub pool: String,
pub signer: String,
pub recipient: String,
pub seller_token: String,
pub buyer_token: String,
pub effective_seller_token_amount: String,
pub seller_token_amount: String,
pub buyer_token_amount: String,
pub deadline_timestamp: u64,
pub nonce: u64,
pub quote_id: String,
pub multi_hop: bool,
pub signature: String,
pub external_swap_calldata: String,
pub amount_out_minimum: String,
pub widget_fee: WidgetFee,
pub widget_fee_signature: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct FirmQuoteResponse {
pub success: bool,
pub orders: Vec<FirmQuoteOrder>,
pub widget_fee: WidgetFee,
pub widget_fee_signature: String,
pub recipient: String,
pub amount_in: String,
pub amount_out: String,
pub amount_out_before_fee: String,
pub fallback_swap_data_array: Option<serde_json::Value>,
pub token_transfer_fee_on_percent: f64,
pub tx_request: TxRequest,
pub source: Vec<u32>,
pub error_message: String,
#[serde(rename = "router_version")]
pub router_version: String,
pub amount_in_offset: u32,
pub amount_out_minimum_offset: u32,
}
#[derive(Debug, Clone, Deserialize)]
pub struct NativeApiErrorResponse {
pub code: u64,
pub message: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NativeSupportedChain {
Ethereum,
Bsc,
Arbitrum,
Base,
Robinhood,
}
impl TryFrom<Chain> for NativeSupportedChain {
type Error = String;
fn try_from(chain: Chain) -> Result<Self, Self::Error> {
match chain {
Chain::Ethereum => Ok(NativeSupportedChain::Ethereum),
Chain::Bsc => Ok(NativeSupportedChain::Bsc),
Chain::Arbitrum => Ok(NativeSupportedChain::Arbitrum),
Chain::Base => Ok(NativeSupportedChain::Base),
Chain::Robinhood => Ok(NativeSupportedChain::Robinhood),
unserved => Err(format!("Native does not serve {unserved}")),
}
}
}
impl NativeSupportedChain {
pub fn as_str(self) -> &'static str {
match self {
NativeSupportedChain::Ethereum => "ethereum",
NativeSupportedChain::Bsc => "bsc",
NativeSupportedChain::Arbitrum => "arbitrum",
NativeSupportedChain::Base => "base",
NativeSupportedChain::Robinhood => "robinhood",
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
fn addr(address: &str) -> Bytes {
Bytes::from_str(address).unwrap()
}
fn side(levels: Vec<PriceLevel>) -> NativeBookSide {
NativeBookSide { levels: Levels::new(levels).unwrap(), minimum_in: 0.0, minimum_out: 0.0 }
}
#[test]
fn an_entry_native_got_wrong_leaves_the_orderbook_standing() {
let json = r#"[
{"base_address":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quote_address":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","minimum_in_base":0,"side":"bid","levels":[[1,0]]},
{"base_address":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quote_address":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","minimum_in_base":0,"side":"ask","levels":[[2,3000]]}
]"#;
let NativeOrderbook(entries) = serde_json::from_str(json).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].side, NativeOrderbookSide::Ask);
}
#[test]
fn an_orderbook_with_no_readable_entry_is_an_error() {
let json = r#"[{"base_address":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","quote_address":"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","minimum_in_base":0,"side":"bid","levels":[[1,0]]}]"#;
assert!(serde_json::from_str::<NativeOrderbook>(json).is_err());
}
#[test]
fn deserializes_native_relay_orderbook_entry() {
let json = r#"{
"base_symbol": "WETH",
"quote_symbol": "USDT",
"base_address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"quote_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"minimum_in_base": 0,
"side": "bid",
"levels": [[0, 0], [0.0001, 3213.12345], [12.75786733219471, 3210.15]]
}"#;
let entry: NativeOrderbookEntry = serde_json::from_str(json).unwrap();
assert_eq!(entry.side, NativeOrderbookSide::Bid);
assert_eq!(entry.levels.len(), 2);
assert_eq!(entry.levels[0].quantity, 0.0001);
assert_eq!(entry.levels[0].price, 3213.12345);
}
#[test]
fn rejects_negative_orderbook_minimum() {
let result = serde_json::from_value::<NativeOrderbookEntry>(serde_json::json!({
"base_address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"quote_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"minimum_in_base": -1,
"side": "bid",
"levels": [[1, 2_000]],
}));
assert!(result.is_err());
}
#[test]
fn calculates_tvl_as_average_bid_ask_quote_value() {
let price_data = NativePriceData {
base_address: addr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
quote_address: addr("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
bids: side(vec![
PriceLevel { quantity: 1.0, price: 2000.0 },
PriceLevel { quantity: 2.0, price: 1999.0 },
]),
asks: side(vec![
PriceLevel { quantity: 1.5, price: 2001.0 },
PriceLevel { quantity: 1.0, price: 2002.0 },
]),
};
let tvl = price_data
.calculate_tvl(None)
.expect("TVL should be finite");
assert!((tvl - 5500.75).abs() < 0.01);
}
#[test]
fn normalizes_tvl_through_quote_token_market() {
let tamara = addr("0x1234567890123456789012345678901234567890");
let usdc = addr("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
let price_data_eth_tamara = NativePriceData {
base_address: addr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
quote_address: tamara.clone(),
bids: side(vec![PriceLevel { quantity: 3.0, price: 100.0 }]),
asks: side(vec![PriceLevel { quantity: 3.0, price: 100.0 }]),
};
let price_data_tamara_usdc = NativePriceData {
base_address: tamara,
quote_address: usdc,
bids: side(vec![PriceLevel { quantity: 300.0, price: 9.0 }]),
asks: side(vec![PriceLevel { quantity: 300.0, price: 11.0 }]),
};
assert_eq!(
price_data_eth_tamara.calculate_tvl(Some(&price_data_tamara_usdc)),
Some(3000.0)
);
}
#[test]
fn calculates_and_normalizes_tvl_for_one_sided_bid_books() {
let tamara = addr("0x1234567890123456789012345678901234567890");
let price_data_eth_tamara = NativePriceData {
base_address: addr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
quote_address: tamara.clone(),
bids: side(vec![PriceLevel { quantity: 3.0, price: 100.0 }]),
asks: NativeBookSide::default(),
};
let price_data_tamara_usdc = NativePriceData {
base_address: tamara,
quote_address: addr("0xA0b86991c6218b36c1d19d4a2e9Eb0cE3606eB48"),
bids: side(vec![PriceLevel { quantity: 300.0, price: 10.0 }]),
asks: NativeBookSide::default(),
};
assert_eq!(price_data_eth_tamara.calculate_tvl(None), Some(300.0));
assert_eq!(
price_data_eth_tamara.calculate_tvl(Some(&price_data_tamara_usdc)),
Some(3000.0)
);
}
#[test]
fn rejects_non_finite_derived_tvl() {
let weth = addr("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
let tamara = addr("0x1234567890123456789012345678901234567890");
let usdc = addr("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
let book = |base_address, quote_address, quantity, price| NativePriceData {
base_address,
quote_address,
bids: side(vec![PriceLevel { quantity, price }]),
asks: NativeBookSide::default(),
};
let overflowing_book = book(weth.clone(), usdc.clone(), 1e308, 2.0);
let finite_book = book(weth, tamara.clone(), 1.0, 2.0);
let overflowing_conversion = book(tamara, usdc, 2.0, 1e308);
assert_eq!(overflowing_book.calculate_tvl(None), None);
assert_eq!(finite_book.calculate_tvl(Some(&overflowing_conversion)), None);
}
}