use std::{any::Any, collections::HashMap, sync::Arc};
use async_trait::async_trait;
use num_bigint::BigUint;
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};
use tycho_common::{
dto::ProtocolStateDelta,
models::{protocol::GetAmountOutParams, token::Token},
simulation::{
errors::{SimulationError, TransitionError},
indicatively_priced::{IndicativelyPriced, SignedQuote},
protocol_sim::{Balances, GetAmountOutResult, ProtocolSim},
},
Bytes,
};
use crate::{
book::{
all_pairs::{all_pairs_state, RefusesQuote, SignedByMakers},
component::PairState,
sim::{self, SwapDirection},
},
rfq::protocols::hashflow::{client::HashflowClient, models::HashflowMarketMakerLevels},
};
const HASHFLOW_SWAP_GAS: u64 = 151_000;
#[derive(Clone, derive_more::Debug, Serialize, Deserialize)]
pub struct HashflowState {
pub(super) base_token: Token,
pub(super) quote_token: Token,
#[debug(skip)]
pub(super) levels: HashflowMarketMakerLevels,
pub(super) market_maker: String,
#[serde(skip)]
#[debug(skip)]
client: Option<Arc<HashflowClient>>,
}
impl HashflowState {
pub(super) fn new(
base_token: Token,
quote_token: Token,
levels: HashflowMarketMakerLevels,
market_maker: String,
client: Arc<HashflowClient>,
) -> Self {
HashflowState { base_token, quote_token, levels, market_maker, client: Some(client) }
}
pub fn min_amount_in(&self) -> BigUint {
let first_level_quantity = self
.levels
.levels
.first()
.map_or(0.0, |level| level.quantity);
let scaled = first_level_quantity * 10f64.powi(self.base_token.decimals as i32);
BigUint::from_f64(scaled).unwrap_or_default()
}
fn valid_direction_guard(
&self,
token_address_in: &Bytes,
token_address_out: &Bytes,
) -> Result<(), SimulationError> {
SwapDirection::require_base_to_quote(
&self.base_token,
&self.quote_token,
token_address_in,
token_address_out,
)
}
}
impl PairState for HashflowState {
fn base_token(&self) -> &Token {
&self.base_token
}
fn quote_token(&self) -> &Token {
&self.quote_token
}
fn directions(&self) -> Vec<(Bytes, Bytes)> {
sim::directions(&self.base_token, &self.quote_token, !self.levels.levels.is_empty(), false)
}
}
#[typetag::serde]
impl ProtocolSim for HashflowState {
fn fee(&self) -> f64 {
todo!()
}
fn spot_price(&self, base: &Token, quote: &Token) -> Result<f64, SimulationError> {
self.valid_direction_guard(&base.address, "e.address)?;
self.levels
.levels
.first()
.ok_or(SimulationError::RecoverableError("No liquidity".into()))
.map(|level| level.price)
}
fn get_amount_out(
&self,
amount_in: BigUint,
token_in: &Token,
token_out: &Token,
) -> Result<GetAmountOutResult, SimulationError> {
sim::require_positive_amount(&amount_in)?;
self.valid_direction_guard(&token_in.address, &token_out.address)?;
let min_amount_in = self.min_amount_in();
if amount_in < min_amount_in {
return Err(SimulationError::RecoverableError(format!(
"Amount below minimum. Input amount: {amount_in}, min amount: {min_amount_in} \
(both in base units)"
)));
}
let amount_in = sim::to_human(&amount_in, token_in.decimals);
let fill = self.levels.levels.fill(amount_in);
sim::fill_result(fill, amount_in, token_out.decimals, HASHFLOW_SWAP_GAS, self)
}
fn get_limits(
&self,
sell_token: Bytes,
buy_token: Bytes,
) -> Result<(BigUint, BigUint), SimulationError> {
self.valid_direction_guard(&sell_token, &buy_token)?;
if self.levels.levels.is_empty() {
return Err(SimulationError::RecoverableError("No liquidity".into()));
}
sim::limits(&self.levels.levels, self.base_token.decimals, self.quote_token.decimals)
}
fn as_indicatively_priced(&self) -> Result<&dyn IndicativelyPriced, SimulationError> {
Ok(self)
}
fn delta_transition(
&mut self,
_delta: ProtocolStateDelta,
_tokens: &HashMap<Bytes, Token>,
_balances: &Balances,
) -> Result<(), TransitionError> {
todo!()
}
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::<HashflowState>()
{
self.base_token == other_state.base_token &&
self.quote_token == other_state.quote_token &&
self.levels == other_state.levels
} else {
false
}
}
}
#[async_trait]
impl IndicativelyPriced for HashflowState {
async fn request_signed_quote(
&self,
params: GetAmountOutParams,
) -> Result<SignedQuote, SimulationError> {
Ok(sim::quoting_client(self.client.as_ref(), "Hashflow")?
.request_binding_quote(¶ms, self.market_maker.clone())
.await?)
}
}
all_pairs_state! {
HashflowAllPairsState(HashflowState, SignedByMakers);
}
impl RefusesQuote for HashflowAllPairsState {
fn check_amount(
&self,
amount_in: &BigUint,
token_in: &Bytes,
token_out: &Bytes,
) -> Result<(), SimulationError> {
match self.min_amount_in(token_in, token_out) {
Some(minimum) if amount_in < &minimum => {
Err(SimulationError::RecoverableError(format!(
"Amount below minimum. Input amount: {amount_in}, min amount: {minimum} \
(both in base units)"
)))
}
_ => Ok(()),
}
}
}
impl HashflowAllPairsState {
pub fn min_amount_in(&self, token_in: &Bytes, token_out: &Bytes) -> Option<BigUint> {
self.0
.books_pricing(token_in, token_out)
.map(|book| book.state.min_amount_in())
.min()
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tokio::time::Duration;
use tycho_common::models::Chain;
use super::*;
use crate::{
book::levels::{Levels, PriceLevel},
rfq::protocols::hashflow::models::HashflowPair,
};
fn wbtc() -> Token {
Token::new(
&hex::decode("2260fac5e5542a773aa44fbcfedf7c193bc2c599")
.unwrap()
.into(),
"WBTC",
8,
0,
&[Some(10_000)],
Chain::Ethereum,
100,
)
}
fn usdc() -> Token {
Token::new(
&hex::decode("a0b86991c6218a76c1d19d4a2e9eb0ce3606eb48")
.unwrap()
.into(),
"USDC",
6,
0,
&[Some(10_000)],
Chain::Ethereum,
100,
)
}
fn weth() -> Token {
Token::new(
&Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(),
"WETH",
18,
0,
&[],
Default::default(),
100,
)
}
fn empty_client() -> Arc<HashflowClient> {
Arc::new(HashflowClient::new(
None,
1,
String::new(),
String::new(),
Duration::from_secs(30),
))
}
fn create_test_hashflow_state() -> HashflowState {
HashflowState {
base_token: weth(),
quote_token: usdc(),
levels: HashflowMarketMakerLevels {
pair: HashflowPair {
base_token: Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")
.unwrap(),
quote_token: Bytes::from_str("0xa0b86991c6218a76c1d19d4a2e9eb0ce3606eb48")
.unwrap(),
},
levels: Levels::new(vec![
PriceLevel { quantity: 0.5, price: 3000.0 },
PriceLevel { quantity: 1.5, price: 3000.0 },
PriceLevel { quantity: 5.0, price: 2999.0 },
])
.unwrap(),
},
market_maker: "test_mm".to_string(),
client: Some(empty_client()),
}
}
#[test]
fn the_minimum_of_a_single_level_book_is_within_its_depth() {
let state = HashflowState {
base_token: usdc(),
quote_token: weth(),
levels: HashflowMarketMakerLevels {
pair: HashflowPair { base_token: usdc().address, quote_token: weth().address },
levels: Levels::new(vec![PriceLevel {
quantity: 4327.8578109263835,
price: 0.000_25,
}])
.unwrap(),
},
market_maker: "test_mm".to_string(),
client: Some(empty_client()),
};
let minimum = state.min_amount_in();
let (limit, _) = state
.get_limits(usdc().address, weth().address)
.unwrap();
assert!(minimum <= limit, "minimum {minimum} is past the book's own limit {limit}");
let result = state.get_amount_out(minimum, &usdc(), &weth());
assert!(result.is_ok(), "{result:?}");
}
#[test]
fn the_advertised_minimum_is_quotable() {
let mut state = create_test_hashflow_state();
state.levels.levels =
Levels::new(vec![PriceLevel { quantity: 31374.751353434924, price: 3000.0 }]).unwrap();
let result = state.get_amount_out(state.min_amount_in(), &weth(), &usdc());
assert!(result.is_ok(), "{result:?}");
}
#[test]
fn rejects_tokens_outside_the_pair_and_the_reverse_direction() {
let state = create_test_hashflow_state();
let amount = BigUint::from_str("1000000000000000000").unwrap();
let results = [
state
.spot_price(&wbtc(), &usdc())
.map(|_| ()),
state
.get_amount_out(amount.clone(), &usdc(), &weth())
.map(|_| ()),
state
.get_amount_out(amount, &wbtc(), &usdc())
.map(|_| ()),
state
.get_limits(wbtc().address.clone(), usdc().address.clone())
.map(|_| ()),
];
for result in results {
assert!(
matches!(&result, Err(SimulationError::InvalidInput(msg, None)) if msg.contains("Invalid token addresses")),
"{result:?}"
);
}
}
#[test]
fn an_empty_ladder_has_nothing_to_offer() {
let mut state = create_test_hashflow_state();
state.levels.levels = Levels::default();
let price = state.spot_price(&weth(), &usdc());
assert!(
matches!(&price, Err(SimulationError::RecoverableError(msg)) if msg == "No liquidity"),
"{price:?}"
);
let swap = state.get_amount_out(
BigUint::from_str("1000000000000000000").unwrap(),
&weth(),
&usdc(),
);
assert!(matches!(swap, Err(SimulationError::InvalidInput(_, None))), "{swap:?}");
assert!(matches!(
state.get_limits(weth().address.clone(), usdc().address.clone()),
Err(SimulationError::RecoverableError(_))
));
}
#[test]
fn spot_price_is_the_first_level() {
let state = create_test_hashflow_state();
let price = state
.spot_price(&state.base_token, &state.quote_token)
.unwrap();
assert_eq!(price, 3000.0);
}
mod get_amount_out {
use super::*;
#[test]
fn wbtc_to_usdc() {
let state = create_test_hashflow_state();
let amount_out_result = state
.get_amount_out(
BigUint::from_str("1500000000000000000").unwrap(), &weth(),
&usdc(),
)
.unwrap();
assert_eq!(amount_out_result.amount, BigUint::from_str("4500000000").unwrap()); assert_eq!(amount_out_result.gas, BigUint::from(151_000u64));
}
#[test]
fn below_minimum() {
let state = create_test_hashflow_state();
let result = state.get_amount_out(
BigUint::from_str("250000000000000000").unwrap(), &weth(),
&usdc(),
);
assert!(result.is_err());
if let Err(SimulationError::RecoverableError(msg)) = result {
assert!(msg.contains("Amount below minimum"));
} else {
panic!("Expected RecoverableError");
}
}
#[test]
fn insufficient_liquidity() {
let state = create_test_hashflow_state();
let result = state.get_amount_out(
BigUint::from_str("8000000000000000000").unwrap(), &weth(),
&usdc(),
);
assert!(result.is_err());
if let Err(SimulationError::InvalidInput(msg, _)) = result {
assert!(msg.contains("Pool has not enough liquidity"));
} else {
panic!("Expected InvalidInput");
}
}
}
#[test]
fn limits_are_the_ladder_totals() {
let state = create_test_hashflow_state();
let (sell_limit, buy_limit) = state
.get_limits(state.base_token.address.clone(), state.quote_token.address.clone())
.unwrap();
assert_eq!(sell_limit, BigUint::from((7.0 * 10f64.powi(18)) as u128));
assert_eq!(buy_limit, BigUint::from((20995.0 * 10f64.powi(6)) as u128));
}
#[test]
fn rejects_a_zero_amount() {
let state = create_test_hashflow_state();
let result = state.get_amount_out(BigUint::ZERO, &weth(), &usdc());
assert!(matches!(result, Err(SimulationError::InvalidInput(_, None))), "{result:?}");
}
}