use std::{any::Any, collections::HashMap};
use num_bigint::BigUint;
use num_traits::Zero;
use serde::{Deserialize, Serialize};
use tycho_common::{
dto::ProtocolStateDelta,
models::token::Token,
simulation::{
errors::{SimulationError, TransitionError},
protocol_sim::{Balances, GetAmountOutResult, ProtocolSim},
},
Bytes,
};
use crate::{
book::{component::PairState, sim},
pamm::protocols::metric::models::{MetricBidAskResponse, MetricDepthBin, MetricMetadata},
};
const METRIC_SWAP_GAS: u64 = 235_000;
#[derive(Clone, derive_more::Debug, Serialize, Deserialize)]
pub struct MetricState {
pub(super) base_token: Token,
pub(super) quote_token: Token,
#[debug("{{ pool_address: {:?}, .. }}", metadata.pool_address)]
pub(super) metadata: MetricMetadata,
#[debug("{{ server_ts: {}, .. }}", bid_ask.server_ts)]
pub(super) bid_ask: MetricBidAskResponse,
}
impl MetricState {
fn direction(
&self,
token_in: &Bytes,
token_out: &Bytes,
) -> Result<MetricDirection, SimulationError> {
if token_in == &self.base_token.address && token_out == &self.quote_token.address {
Ok(MetricDirection::ZeroForOne)
} else if token_in == &self.quote_token.address && token_out == &self.base_token.address {
Ok(MetricDirection::OneForZero)
} else {
Err(SimulationError::InvalidInput(
format!(
"Invalid token addresses. Got in={token_in}, out={token_out}, expected {} / {}",
self.base_token.address, self.quote_token.address
),
None,
))
}
}
fn quoted_price(&self, direction: MetricDirection) -> Result<Option<f64>, SimulationError> {
let price = match direction {
MetricDirection::ZeroForOne => self.bid_ask.bid_price()?,
MetricDirection::OneForZero => self.bid_ask.ask_price()?,
};
Ok(price)
}
fn quote_with_depth(
&self,
direction: MetricDirection,
amount_in: &BigUint,
max_output: &BigUint,
) -> Result<Option<DepthQuote>, SimulationError> {
let bins = match direction {
MetricDirection::ZeroForOne => &self.bid_ask.depth.bids,
MetricDirection::OneForZero => &self.bid_ask.depth.asks,
};
let Some(depth_max_output) = depth_max_output(bins) else {
return Ok(None);
};
let effective_max_output = depth_max_output.min(max_output.clone());
let depth_fill = depth_output_for_input(bins, amount_in, &effective_max_output)?;
Ok(Some(DepthQuote {
amount_out: depth_fill.output,
max_output: effective_max_output,
exhausted: depth_fill.exhausted,
}))
}
}
#[derive(Debug, Clone, Copy)]
enum MetricDirection {
ZeroForOne,
OneForZero,
}
struct DepthQuote {
amount_out: BigUint,
max_output: BigUint,
exhausted: bool,
}
#[derive(Debug)]
struct DepthFill {
output: BigUint,
exhausted: bool,
}
impl PairState for MetricState {
fn base_token(&self) -> &Token {
&self.base_token
}
fn quote_token(&self) -> &Token {
&self.quote_token
}
fn directions(&self) -> Vec<(Bytes, Bytes)> {
let priced =
|direction| matches!(self.quoted_price(direction), Ok(Some(price)) if price > 0.0);
sim::directions(
&self.base_token,
&self.quote_token,
priced(MetricDirection::ZeroForOne),
priced(MetricDirection::OneForZero),
)
}
}
#[typetag::serde]
impl ProtocolSim for MetricState {
fn fee(&self) -> f64 {
0.0
}
fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError> {
let price = match (self.bid_ask.bid_price()?, self.bid_ask.ask_price()?) {
(Some(bid), Some(ask)) => (bid + ask) / 2.0,
(Some(bid), None) => bid,
(None, Some(ask)) => ask,
(None, None) => return Err(SimulationError::RecoverableError("No liquidity".into())),
};
if base.address == self.base_token.address && quote.address == self.quote_token.address {
Ok(price)
} else if base.address == self.quote_token.address &&
quote.address == self.base_token.address
{
Ok(1.0 / price)
} else {
Err(SimulationError::InvalidInput(
format!(
"Invalid token addresses. Got base={}, quote={}, expected {} / {}",
base.address, quote.address, self.base_token.address, self.quote_token.address
),
None,
))
}
}
fn get_amount_out(
&self,
amount_in: BigUint,
token_in: &Token,
token_out: &Token,
) -> Result<GetAmountOutResult, SimulationError> {
sim::require_positive_amount(&amount_in)?;
let direction = self.direction(&token_in.address, &token_out.address)?;
let Some(price) = self.quoted_price(direction)? else {
return Err(SimulationError::InvalidInput(
format!(
"Metric pool quotes no price in this direction. Input {amount_in} cannot be \
filled"
),
None,
));
};
let max_output = match direction {
MetricDirection::ZeroForOne => self.bid_ask.total_token1_available()?,
MetricDirection::OneForZero => self.bid_ask.total_token0_available()?,
};
if let Some(quote) = self.quote_with_depth(direction, &amount_in, &max_output)? {
let res = GetAmountOutResult {
amount: quote.amount_out,
gas: BigUint::from(METRIC_SWAP_GAS),
new_state: self.clone_box(),
};
if quote.exhausted {
return Err(SimulationError::InvalidInput(
format!(
"Metric pool depth exhausted. Input {amount_in} cannot be fully filled; \
tradable depth caps output at {}",
quote.max_output
),
Some(res),
));
}
return Ok(res);
}
let amount_in_human = sim::to_human(&amount_in, token_in.decimals);
let flat_amount_out_human = match direction {
MetricDirection::ZeroForOne => amount_in_human * price,
MetricDirection::OneForZero => amount_in_human / price,
};
let amount_out = sim::to_atomic(flat_amount_out_human, token_out.decimals)?;
let res = GetAmountOutResult {
amount: amount_out
.clone()
.min(max_output.clone()),
gas: BigUint::from(METRIC_SWAP_GAS),
new_state: self.clone_box(),
};
if amount_out > max_output {
return Err(SimulationError::InvalidInput(
format!(
"Metric pool has not enough liquidity. Requested output {amount_out} exceeds \
available {max_output}"
),
Some(res),
));
}
Ok(res)
}
fn get_limits(
&self,
sell_token: Bytes,
buy_token: Bytes,
) -> Result<(BigUint, BigUint), SimulationError> {
let direction = self.direction(&sell_token, &buy_token)?;
let Some(price) = self.quoted_price(direction)? else {
return Err(SimulationError::RecoverableError("No liquidity".into()));
};
let (sell_per_buy, aggregate, bins, sell_decimals, buy_decimals) = match direction {
MetricDirection::ZeroForOne => (
1.0 / price,
self.bid_ask.total_token1_available()?,
&self.bid_ask.depth.bids,
self.base_token.decimals,
self.quote_token.decimals,
),
MetricDirection::OneForZero => (
price,
self.bid_ask.total_token0_available()?,
&self.bid_ask.depth.asks,
self.quote_token.decimals,
self.base_token.decimals,
),
};
if let Some(last_bin) = bins.last() {
if last_bin.cumulative_volume <= aggregate {
return Ok((
last_bin.cumulative_input_volume.clone(),
last_bin.cumulative_volume.clone(),
));
}
}
let buy_limit = aggregate;
let buy_limit_human = sim::to_human(&buy_limit, buy_decimals);
let sell_limit = sim::to_atomic(buy_limit_human * sell_per_buy, sell_decimals)?;
Ok((sell_limit, buy_limit))
}
fn delta_transition(
&mut self,
_delta: ProtocolStateDelta,
_tokens: &HashMap<Bytes, Token>,
_balances: &Balances,
) -> Result<(), TransitionError> {
Err(TransitionError::DecodeError(
"Metric RFQ state is snapshot-based and does not support deltas".into(),
))
}
fn clone_box(&self) -> Box<dyn ProtocolSim> {
Box::new(self.clone())
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn eq(&self, other: &dyn ProtocolSim) -> bool {
if let Some(other_state) = other
.as_any()
.downcast_ref::<MetricState>()
{
self.base_token == other_state.base_token &&
self.quote_token == other_state.quote_token &&
self.metadata == other_state.metadata &&
self.bid_ask == other_state.bid_ask
} else {
false
}
}
}
fn depth_max_output(bins: &[MetricDepthBin]) -> Option<BigUint> {
bins.last()
.map(|bin| bin.cumulative_volume.clone())
}
fn depth_output_for_input(
bins: &[MetricDepthBin],
amount_in: &BigUint,
max_output: &BigUint,
) -> Result<DepthFill, SimulationError> {
if amount_in.is_zero() || max_output.is_zero() {
return Ok(DepthFill { output: BigUint::ZERO, exhausted: !amount_in.is_zero() });
}
let mut previous_output = BigUint::ZERO;
let mut previous_input = BigUint::ZERO;
let mut remaining_input = amount_in.clone();
let mut output = BigUint::ZERO;
for bin in bins {
let cumulative_output = &bin.cumulative_volume;
let cumulative_input = &bin.cumulative_input_volume;
if cumulative_output < &previous_output || cumulative_input < &previous_input {
return Err(SimulationError::RecoverableError(
"Metric depth cumulative volumes are not monotonic".into(),
));
}
let volume_in_bin = cumulative_output - &previous_output;
let input_in_bin = cumulative_input - &previous_input;
previous_output = cumulative_output.clone();
previous_input = cumulative_input.clone();
if volume_in_bin.is_zero() && input_in_bin.is_zero() {
continue;
}
if volume_in_bin.is_zero() || input_in_bin.is_zero() {
return Err(SimulationError::RecoverableError(
"Metric depth bin has inconsistent volume and input".into(),
));
}
let output_capacity = max_output - &output;
if output_capacity.is_zero() {
break;
}
let (fillable_volume, fillable_input) = if volume_in_bin <= output_capacity {
(volume_in_bin, input_in_bin)
} else {
let fillable_input = (&input_in_bin * &output_capacity + &volume_in_bin -
BigUint::from(1u8)) /
&volume_in_bin;
(output_capacity, fillable_input)
};
if remaining_input >= fillable_input {
output += &fillable_volume;
remaining_input -= &fillable_input;
continue;
}
output += &fillable_volume * &remaining_input / &fillable_input;
remaining_input = BigUint::ZERO;
break;
}
Ok(DepthFill { output, exhausted: !remaining_input.is_zero() })
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tycho_common::models::Chain;
use super::*;
use crate::pamm::protocols::metric::models::MetricDepth;
fn big(value: &str) -> BigUint {
value.parse().unwrap()
}
fn weth() -> Token {
Token::new(
&Bytes::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap(),
"WETH",
18,
0,
&[Some(2300)],
Chain::Ethereum,
100,
)
}
fn usdc() -> Token {
Token::new(
&Bytes::from_str("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48").unwrap(),
"USDC",
6,
0,
&[Some(1)],
Chain::Ethereum,
100,
)
}
fn base_weth() -> Token {
Token::new(
&Bytes::from_str("0x4200000000000000000000000000000000000006").unwrap(),
"WETH",
18,
0,
&[Some(2300)],
Chain::Base,
100,
)
}
fn base_usdc() -> Token {
Token::new(
&Bytes::from_str("0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913").unwrap(),
"USDC",
6,
0,
&[Some(1)],
Chain::Base,
100,
)
}
fn state() -> MetricState {
let weth = weth();
let usdc = usdc();
let metadata = MetricMetadata {
pool_address: Bytes::from_str("0xbF48bCf474d57fF82A3215319229e0DE1476A557").unwrap(),
token0: weth.address.clone(),
token1: usdc.address.clone(),
tvl_fiat: Some(3000.0),
pause_level: Some(0),
};
let bid_ask = MetricBidAskResponse {
bid_adj: big("55340232221128654848000"),
ask_adj: big("55524699661865750400000"),
total_token0_available: Some(big("10000000000000000000")),
total_token1_available: Some(big("30000000000")),
server_ts: 100,
price_provider_status: Some("healthy".to_string()),
depth: MetricDepth::default(),
};
MetricState { base_token: weth, quote_token: usdc, metadata, bid_ask }
}
fn state_without_bid() -> MetricState {
let mut state = state();
state.bid_ask.bid_adj = BigUint::zero();
state.bid_ask.depth.asks = vec![depth_bin("1000000000000000000", "3011000000")];
state
}
fn state_without_ask() -> MetricState {
let mut state = state();
state.bid_ask.ask_adj = BigUint::from(u128::MAX);
state.bid_ask.depth.bids = vec![depth_bin("2999000000", "1000000000000000000")];
state
}
#[test]
fn book_without_bid_trades_only_the_ask_side() {
let state = state_without_bid();
let ask = state
.bid_ask
.ask_price()
.unwrap()
.unwrap();
assert!(state.bid_ask.is_quotable());
assert_eq!(
state
.spot_price(&weth(), &usdc())
.unwrap(),
ask
);
assert!(matches!(
state.get_limits(weth().address, usdc().address),
Err(SimulationError::RecoverableError(_))
));
let Err(SimulationError::InvalidInput(msg, None)) =
state.get_amount_out(big("100000000000000000"), &weth(), &usdc())
else {
panic!("selling WETH into a book without a bid must fail");
};
assert!(msg.contains("quotes no price in this direction"), "{msg}");
assert_eq!(
state
.get_limits(usdc().address, weth().address)
.unwrap(),
(big("3011000000"), big("1000000000000000000"))
);
}
#[test]
fn book_without_ask_trades_only_the_bid_side() {
let state = state_without_ask();
let bid = state
.bid_ask
.bid_price()
.unwrap()
.unwrap();
assert!(state.bid_ask.is_quotable());
assert_eq!(
state
.spot_price(&weth(), &usdc())
.unwrap(),
bid
);
assert!(matches!(
state.get_limits(usdc().address, weth().address),
Err(SimulationError::RecoverableError(_))
));
let Err(SimulationError::InvalidInput(msg, None)) =
state.get_amount_out(big("300000000"), &usdc(), &weth())
else {
panic!("buying WETH from a book without an ask must fail");
};
assert!(msg.contains("quotes no price in this direction"), "{msg}");
assert_eq!(
state
.get_limits(weth().address, usdc().address)
.unwrap(),
(big("1000000000000000000"), big("2999000000"))
);
}
#[test]
fn book_without_quotes_has_no_spot_price() {
let mut state = state();
state.bid_ask.bid_adj = BigUint::zero();
state.bid_ask.ask_adj = BigUint::from(u128::MAX);
let Err(SimulationError::RecoverableError(msg)) = state.spot_price(&weth(), &usdc()) else {
panic!("a book without quotes has no spot price");
};
assert_eq!(msg, "No liquidity");
}
#[test]
fn get_amount_out_zero_for_one() {
let state = state();
let result = state
.get_amount_out(
BigUint::from(1_000_000_000_000_000_000u128),
&state.base_token,
&state.quote_token,
)
.unwrap();
assert_eq!(result.amount, BigUint::from(3_000_000_000u64));
}
#[test]
fn get_amount_out_one_for_zero() {
let state = state();
let result = state
.get_amount_out(BigUint::from(3_010_000_000u64), &state.quote_token, &state.base_token)
.unwrap();
assert_eq!(result.amount, BigUint::from(1_000_000_000_000_000_000u128));
}
#[test]
fn get_amount_out_caps_to_available_liquidity() {
let mut state = state();
state.bid_ask.total_token1_available = Some(big("1500000000"));
let err = state
.get_amount_out(
BigUint::from(1_000_000_000_000_000_000u128),
&state.base_token,
&state.quote_token,
)
.unwrap_err();
assert!(matches!(err, SimulationError::InvalidInput(_, Some(_))));
}
#[test]
fn get_amount_out_depth_exhausted_reports_depth_message() {
let mut state = state();
state.bid_ask.depth.bids = vec![MetricDepthBin {
bin_idx: 0,
price: big("53495557813757699686400"),
cumulative_volume: big("3000000000"),
cumulative_input_volume: big("1016949152542372881"),
}];
let err = state
.get_amount_out(
BigUint::from(2_000_000_000_000_000_000u128),
&state.base_token,
&state.quote_token,
)
.unwrap_err();
match err {
SimulationError::InvalidInput(msg, Some(_)) => {
assert!(msg.contains("depth exhausted"), "unexpected message: {msg}");
}
other => panic!("expected InvalidInput, got {other:?}"),
}
}
#[test]
fn get_amount_out_exhausted_returns_exact_cap() {
let mut state = state();
let cap = "6575581573690662958";
state.bid_ask.depth.asks = vec![MetricDepthBin {
bin_idx: 0,
price: big("57184906628499610009600"),
cumulative_volume: big(cap),
cumulative_input_volume: big("20088000000"),
}];
let err = state
.get_amount_out(
BigUint::from(30_000_000_000u64),
&state.quote_token,
&state.base_token,
)
.unwrap_err();
match err {
SimulationError::InvalidInput(msg, Some(res)) => {
assert!(msg.contains("depth exhausted"), "unexpected message: {msg}");
assert_eq!(res.amount, BigUint::from_str(cap).unwrap());
}
other => panic!("expected InvalidInput, got {other:?}"),
}
}
#[test]
fn get_limits_caps_to_depth() {
let mut state = state();
state.bid_ask.depth.bids = vec![MetricDepthBin {
bin_idx: 0,
price: big("53495557813757699686400"),
cumulative_volume: big("1500000000"),
cumulative_input_volume: big("508474576271186440"),
}];
let (sell_limit, buy_limit) = state
.get_limits(state.base_token.address.clone(), state.quote_token.address.clone())
.unwrap();
assert_eq!(buy_limit, BigUint::from(1_500_000_000u64));
assert_eq!(sell_limit, BigUint::from(508_474_576_271_186_440u128));
}
#[test]
fn get_limits_aggregate_truncates_depth() {
let mut state = state();
state.bid_ask.total_token1_available = Some(big("1000000000"));
state.bid_ask.depth.bids = vec![MetricDepthBin {
bin_idx: 0,
price: big("53495557813757699686400"),
cumulative_volume: big("3000000000"),
cumulative_input_volume: big("1016949152542372881"),
}];
let (sell_limit, buy_limit) = state
.get_limits(state.base_token.address.clone(), state.quote_token.address.clone())
.unwrap();
assert_eq!(buy_limit, BigUint::from(1_000_000_000u64));
assert_eq!(sell_limit, BigUint::from(333_333_333_333_333_312u128));
}
#[test]
fn get_limits_uses_aggregate_without_depth() {
let state = state();
let (_, buy_limit) = state
.get_limits(state.base_token.address.clone(), state.quote_token.address.clone())
.unwrap();
assert_eq!(buy_limit, BigUint::from(30_000_000_000u64));
}
#[test]
fn get_amount_out_walks_bid_depth() {
let mut state = state();
state.bid_ask.depth.bids = vec![MetricDepthBin {
bin_idx: 0,
price: big("53495557813757699686400"),
cumulative_volume: big("3000000000"),
cumulative_input_volume: big("1016949152542372881"),
}];
let result = state
.get_amount_out(
BigUint::from(1_000_000_000_000_000_000u128),
&state.base_token,
&state.quote_token,
)
.unwrap();
assert_eq!(result.amount, BigUint::from(2_950_000_000u64));
}
#[test]
fn get_amount_out_walks_ask_depth() {
let mut state = state();
state.bid_ask.depth.asks = vec![MetricDepthBin {
bin_idx: 0,
price: big("57184906628499610009600"),
cumulative_volume: big("1000000000000000000"),
cumulative_input_volume: big("3055000000"),
}];
let result = state
.get_amount_out(BigUint::from(3_000_000_000u64), &state.quote_token, &state.base_token)
.unwrap();
assert!(result.amount < BigUint::from(1_000_000_000_000_000_000u128));
assert!(result.amount > BigUint::from(980_000_000_000_000_000u128));
}
fn depth_bin(cumulative_volume: &str, cumulative_input_volume: &str) -> MetricDepthBin {
MetricDepthBin {
bin_idx: 0,
price: big("53495557813757699686400"),
cumulative_volume: big(cumulative_volume),
cumulative_input_volume: big(cumulative_input_volume),
}
}
#[test]
fn depth_output_for_input_partially_fills_bid_bin() {
let bins = vec![depth_bin("3000000000", "1016949152542372881")];
let fill = depth_output_for_input(
&bins,
&BigUint::from(1_000_000_000_000_000_000u128),
&BigUint::from(3_000_000_000u64),
)
.unwrap();
assert_eq!(fill.output, BigUint::from(2_950_000_000u64));
assert!(!fill.exhausted);
}
#[test]
fn depth_output_for_input_partially_fills_ask_bin() {
let bins = vec![depth_bin("1000000000000000000", "3055000000")];
let fill = depth_output_for_input(
&bins,
&BigUint::from(3_000_000_000u64),
&BigUint::from(1_000_000_000_000_000_000u128),
)
.unwrap();
let expected = BigUint::from(1_000_000_000_000_000_000u128) *
BigUint::from(3_000_000_000u64) /
BigUint::from(3_055_000_000u64);
assert_eq!(fill.output, expected);
assert!(!fill.exhausted);
}
#[test]
fn depth_output_for_input_exhausts_available_depth() {
let bins = vec![depth_bin("3000000000", "1016949152542372881")];
let fill = depth_output_for_input(
&bins,
&BigUint::from(2_000_000_000_000_000_000u128),
&BigUint::from(3_000_000_000u64),
)
.unwrap();
assert_eq!(fill.output, BigUint::from(3_000_000_000u64));
assert!(fill.exhausted);
}
#[test]
fn depth_output_for_input_walks_multiple_bins() {
let bins = vec![
depth_bin("3000000000", "1000000000000000000"),
depth_bin("6000000000", "2100000000000000000"),
];
let fill = depth_output_for_input(
&bins,
&BigUint::from(1_550_000_000_000_000_000u128),
&BigUint::from(6_000_000_000u64),
)
.unwrap();
assert_eq!(fill.output, BigUint::from(4_500_000_000u64));
assert!(!fill.exhausted);
}
#[test]
fn depth_output_for_input_caps_slice_to_aggregate_inventory() {
let bins = vec![depth_bin("3000000000", "1000000000000000000")];
let fill = depth_output_for_input(
&bins,
&BigUint::from(1_000_000_000_000_000_000u128),
&BigUint::from(1_500_000_000u64),
)
.unwrap();
assert_eq!(fill.output, BigUint::from(1_500_000_000u64));
assert!(fill.exhausted);
}
#[test]
fn depth_output_for_input_rejects_inconsistent_bin() {
let bins = vec![depth_bin("3000000000", "0")];
let err = depth_output_for_input(
&bins,
&BigUint::from(1_000_000_000_000_000_000u128),
&BigUint::from(3_000_000_000u64),
)
.unwrap_err();
assert!(matches!(err, SimulationError::RecoverableError(_)));
}
#[test]
fn depth_output_for_input_rejects_non_monotonic_bins() {
let bins = vec![
depth_bin("3000000000", "1000000000000000000"),
depth_bin("2000000000", "2000000000000000000"),
];
let err = depth_output_for_input(
&bins,
&BigUint::from(2_000_000_000_000_000_000u128),
&BigUint::from(3_000_000_000u64),
)
.unwrap_err();
assert!(matches!(err, SimulationError::RecoverableError(_)));
}
#[tokio::test]
#[ignore = "hits Metric's public API; requires METRIC_API_KEY"]
async fn live_metric_api_state_get_amount_out() {
use crate::pamm::protocols::metric::models::PaginatedMetadataResponse;
let api_key = std::env::var("METRIC_API_KEY").expect("METRIC_API_KEY not set");
let weth = base_weth();
let usdc = base_usdc();
let base_url = "https://api.metric.xyz";
let http_client = reqwest::Client::new();
let metadata: PaginatedMetadataResponse = http_client
.get(format!("{base_url}/public/v1/evm/8453/metadata"))
.header("accept", "application/json")
.query(&[("count", "500")])
.bearer_auth(&api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let mut selected = None;
for pool in metadata
.data
.into_iter()
.filter(|pool| pool.token0 == weth.address && pool.token1 == usdc.address)
{
let checksummed =
alloy::primitives::Address::from_slice(&pool.pool_address).to_checksum(None);
let bid_ask: MetricBidAskResponse = http_client
.get(format!("{base_url}/public/v1/evm/8453/{checksummed}/bid_ask"))
.header("accept", "application/json")
.bearer_auth(&api_key)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
let has_enough_quote_liquidity = bid_ask
.total_token1_available()
.map(|available| available > BigUint::from(10u8))
.unwrap_or(false);
if bid_ask.is_quotable() && has_enough_quote_liquidity {
selected = Some((pool, bid_ask));
break;
}
}
let Some((metadata, bid_ask)) = selected else {
eprintln!("Metric live API returned no liquid Base WETH/USDC pool; skipping");
return;
};
let state = MetricState { base_token: weth, quote_token: usdc, metadata, bid_ask };
assert!(state.bid_ask.is_quotable());
let amount_in = BigUint::from(1_000_000_000u64);
let quote = state
.get_amount_out(amount_in, &state.base_token, &state.quote_token)
.unwrap();
assert!(quote.amount > BigUint::from(0u8));
}
#[test]
fn rejects_a_zero_amount() {
let state = state();
let result = state.get_amount_out(BigUint::ZERO, &weth(), &usdc());
assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))), "{result:?}");
}
}