use std::{
collections::{HashMap, HashSet},
time::Duration,
};
use alloy::{
primitives::{address, Address, U256},
sol,
sol_types::SolCall,
};
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use tracing::warn;
use tycho_common::{models::Chain, Bytes};
use crate::{
evm::protocol::u256_num::u256_to_biguint, pamm::protocols::elfomofi::state::Ladder,
snapshot_feed::errors::FeedError,
};
sol! {
#[derive(Debug)]
struct CumulativeLevel {
uint256 amountIn;
uint256 amountOut;
}
#[derive(Debug)]
struct PairOrderbook {
address base;
address quote;
CumulativeLevel[] askCumulativeLevels;
CumulativeLevel[] bidCumulativeLevels;
uint256 balanceBase;
uint256 balanceQuote;
}
function getAllOrderbooks() external view
returns (PairOrderbook[] books, uint256 blockNumber, uint256 blockTimestamp);
}
const NATIVE_MARKER: Address = address!("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ElfomofiSupportedChain {
Base,
Bsc,
Robinhood,
}
impl TryFrom<Chain> for ElfomofiSupportedChain {
type Error = FeedError;
fn try_from(chain: Chain) -> Result<Self, Self::Error> {
match chain {
Chain::Base => Ok(ElfomofiSupportedChain::Base),
Chain::Bsc => Ok(ElfomofiSupportedChain::Bsc),
Chain::Robinhood => Ok(ElfomofiSupportedChain::Robinhood),
Chain::Ethereum |
Chain::Starknet |
Chain::ZkSync |
Chain::Arbitrum |
Chain::Unichain |
Chain::Polygon |
Chain::Plasma |
Chain::Arc |
Chain::Elysium |
Chain::Custom(_) |
_ => Err(FeedError::InvalidInput(format!("ElFomoFi is not deployed on {chain}"))),
}
}
}
impl ElfomofiSupportedChain {
pub fn chain(self) -> Chain {
match self {
ElfomofiSupportedChain::Base => Chain::Base,
ElfomofiSupportedChain::Bsc => Chain::Bsc,
ElfomofiSupportedChain::Robinhood => Chain::Robinhood,
}
}
pub(super) fn helper(self) -> Address {
match self {
ElfomofiSupportedChain::Base => address!("c1b13606fc7227f2554067afb3fb12de75c02d81"),
ElfomofiSupportedChain::Bsc => address!("78015e3544d989f0712a0b9986cb05838c3fe06d"),
ElfomofiSupportedChain::Robinhood => {
address!("20d24ee45c9b338ef7acf2eede75183b9e2c9e3c")
}
}
}
pub(super) fn max_read_lag(self) -> Duration {
match self {
ElfomofiSupportedChain::Base |
ElfomofiSupportedChain::Bsc |
ElfomofiSupportedChain::Robinhood => Duration::from_secs(2),
}
}
pub fn max_snapshot_age(self) -> Duration {
match self {
ElfomofiSupportedChain::Base |
ElfomofiSupportedChain::Bsc |
ElfomofiSupportedChain::Robinhood => Duration::from_secs(2),
}
}
pub(super) fn swap_gas(self) -> u64 {
match self {
ElfomofiSupportedChain::Base => 436_000,
ElfomofiSupportedChain::Bsc => 470_000,
ElfomofiSupportedChain::Robinhood => 387_000,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub(super) struct PairBook {
pub base: Bytes,
pub quote: Bytes,
pub asks: Ladder,
pub bids: Ladder,
pub balance_base: BigUint,
pub balance_quote: BigUint,
}
#[derive(Debug, Clone)]
pub(super) struct HelperRead {
pub block_number: u64,
pub block_timestamp: u64,
pub books: Vec<PairBook>,
}
pub(super) fn decode_orderbooks(raw: &[u8]) -> Result<HelperRead, FeedError> {
let answer = getAllOrderbooksCall::abi_decode_returns(raw)
.map_err(|e| FeedError::Parsing(format!("ElFomoFi getAllOrderbooks: {e}")))?;
let block_number = to_u64(answer.blockNumber, "block number")?;
let block_timestamp = to_u64(answer.blockTimestamp, "block timestamp")?;
let received = answer.books.len();
let mut rejected = Vec::new();
let mut checked = Vec::with_capacity(received);
for book in answer.books {
let pair = format!("{}/{}", book.base, book.quote);
match check_book(book) {
Ok(book) => checked.push(book),
Err(reason) => rejected.push(format!("{pair}: {reason}")),
}
}
let books = without_repeated_pairs(checked, &mut rejected);
if let Some(first) = rejected.first() {
warn!(skipped = rejected.len(), received, first_reason = %first, "skipping ElFomoFi books");
}
if received > 0 && books.is_empty() {
return Err(FeedError::Parsing(format!(
"none of the {received} ElFomoFi books passed the checks; first: {}",
rejected
.first()
.map(String::as_str)
.unwrap_or_default()
)));
}
Ok(HelperRead { block_number, block_timestamp, books })
}
fn check_book(book: PairOrderbook) -> Result<PairBook, String> {
for (name, token) in [("base", book.base), ("quote", book.quote)] {
if token == Address::ZERO || token == NATIVE_MARKER {
return Err(format!("{name} token is {token}, not an ERC20"));
}
}
if book.base == book.quote {
return Err("base and quote are the same token".to_string());
}
let asks = ladder(book.askCumulativeLevels).map_err(|e| format!("asks: {e}"))?;
let bids = ladder(book.bidCumulativeLevels).map_err(|e| format!("bids: {e}"))?;
Ok(PairBook {
base: Bytes::from(book.base.to_vec()),
quote: Bytes::from(book.quote.to_vec()),
asks,
bids,
balance_base: u256_to_biguint(book.balanceBase),
balance_quote: u256_to_biguint(book.balanceQuote),
})
}
fn ladder(levels: Vec<CumulativeLevel>) -> Result<Ladder, String> {
Ladder::new(
levels
.into_iter()
.map(|level| (u256_to_biguint(level.amountIn), u256_to_biguint(level.amountOut)))
.collect(),
)
}
fn without_repeated_pairs(books: Vec<PairBook>, rejected: &mut Vec<String>) -> Vec<PairBook> {
let pair_key = |book: &PairBook| {
if book.base < book.quote {
(book.base.clone(), book.quote.clone())
} else {
(book.quote.clone(), book.base.clone())
}
};
let mut counts: HashMap<(Bytes, Bytes), usize> = HashMap::new();
for book in &books {
*counts
.entry(pair_key(book))
.or_default() += 1;
}
let repeated: HashSet<_> = counts
.into_iter()
.filter(|(_, count)| *count > 1)
.map(|(pair, _)| pair)
.collect();
let mut kept = Vec::with_capacity(books.len());
for book in books {
if repeated.contains(&pair_key(&book)) {
rejected.push(format!("{}/{}: the pair appears more than once", book.base, book.quote));
} else {
kept.push(book);
}
}
kept
}
fn to_u64(value: U256, what: &str) -> Result<u64, FeedError> {
value
.try_into()
.map_err(|e| FeedError::Parsing(format!("ElFomoFi {what} {value}: {e}")))
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
use crate::pamm::protocols::elfomofi::test_support::{
big, bytes, helper_answer, ladder, recording, weth_usdc_wire, wire_book, BASE_RECORDING,
BSC_RECORDING, CBBTC, ROBINHOOD_RECORDING, USDC, WETH,
};
#[rstest]
#[case::base(
BASE_RECORDING,
(62_017_050, 1_795_593_785, 12),
address!("b200000000000000000000c2e324d24d7eecd1fb"),
(60_456_809, 17_146_572),
(9_735_857_880, 34_167_438_981),
(134_146_572, 34_167_438_987)
)]
#[case::bsc(
BSC_RECORDING,
(135_619_488, 1_795_593_787, 14),
address!("21caef8a43163eea865baee23b9c2e327696a3bf"),
(253_292_575_996_800_000_000, 72_000),
(24_231_680, 48_505_176_275_990_400_000_000),
(752_320, 59_581_064_238_861_470_876_832)
)]
#[case::robinhood(
ROBINHOOD_RECORDING,
(122_891_612, 1_795_593_786, 11),
address!("39dbed3a2bd333467115de45665cc57f813c4571"),
(165_515_119, 1_890_000_000_000_000_000_000),
(657_542_294_937_822_440_589_292, 29_076_102_682),
(236_903_547_961_879_156_240_201, 29_076_102_685)
)]
fn recordings_decode_to_the_wei(
#[case] hex: &str,
#[case] (block, timestamp, count): (u64, u64, usize),
#[case] base: Address,
#[case] first_ask: (u128, u128),
#[case] last_bid: (u128, u128),
#[case] balances: (u128, u128),
) {
let read = decode_orderbooks(&recording(hex)).unwrap();
assert_eq!(
(read.block_number, read.block_timestamp, read.books.len()),
(block, timestamp, count)
);
let book = read
.books
.iter()
.find(|book| book.base == bytes(base))
.unwrap();
assert_eq!(book.asks.first_rung(), Some((&big(first_ask.0), &big(first_ask.1))));
assert_eq!(book.bids.last_rung(), Some((&big(last_bid.0), &big(last_bid.1))));
assert_eq!((&book.balance_base, &book.balance_quote), (&big(balances.0), &big(balances.1)));
}
#[rstest]
#[case::zero_base(wire_book(Address::ZERO, USDC, &[], &[(1, 1)], (1, 1)))]
#[case::native_quote(wire_book(WETH, NATIVE_MARKER, &[], &[(1, 1)], (1, 1)))]
#[case::same_token(wire_book(WETH, WETH, &[], &[(1, 1)], (1, 1)))]
#[case::falling_asks(wire_book(WETH, USDC, &[(2, 2), (1, 3)], &[], (1, 1)))]
#[case::flat_bids(wire_book(WETH, USDC, &[], &[(1, 2), (2, 2)], (1, 1)))]
fn a_book_failing_a_check_is_skipped(#[case] bad: PairOrderbook) {
let answer = helper_answer(vec![bad, weth_usdc_wire()], 1, 1);
let read = decode_orderbooks(&answer).unwrap();
assert_eq!(read.books.len(), 1);
assert_eq!(read.books[0].base, bytes(WETH));
}
#[rstest]
#[case::same_orientation(WETH, USDC)]
#[case::flipped(USDC, WETH)]
fn every_book_of_a_repeated_pair_is_skipped(#[case] base: Address, #[case] quote: Address) {
let repeated = wire_book(base, quote, &[], &[(1, 1)], (1, 1));
let other = wire_book(CBBTC, USDC, &[], &[(1, 1)], (1, 1));
let answer = helper_answer(vec![weth_usdc_wire(), repeated, other], 1, 1);
let read = decode_orderbooks(&answer).unwrap();
assert_eq!(read.books.len(), 1);
assert_eq!(read.books[0].base, bytes(CBBTC));
}
#[test]
fn an_answer_whose_books_all_fail_fails_the_poll() {
let answer = helper_answer(vec![wire_book(WETH, WETH, &[], &[(1, 1)], (1, 1))], 1, 1);
let error = decode_orderbooks(&answer).unwrap_err();
assert!(matches!(error, FeedError::Parsing(_)), "{error}");
}
#[test]
fn an_answer_without_books_is_an_empty_venue() {
let read = decode_orderbooks(&helper_answer(vec![], 7, 8)).unwrap();
assert!(read.books.is_empty());
assert_eq!((read.block_number, read.block_timestamp), (7, 8));
}
#[test]
fn an_answer_that_does_not_decode_fails_the_poll() {
let error = decode_orderbooks(&[0u8; 31]).unwrap_err();
assert!(matches!(error, FeedError::Parsing(_)), "{error}");
}
#[test]
fn a_book_keeps_its_ladders_and_balances() {
let read = decode_orderbooks(&helper_answer(vec![weth_usdc_wire()], 1, 1)).unwrap();
let book = &read.books[0];
assert_eq!(book.bids, ladder(&crate::pamm::protocols::elfomofi::test_support::BIDS));
assert_eq!(book.asks, ladder(&crate::pamm::protocols::elfomofi::test_support::ASKS));
assert_eq!(book.quote, bytes(USDC));
}
#[rstest]
#[case::base(Chain::Base, ElfomofiSupportedChain::Base)]
#[case::bsc(Chain::Bsc, ElfomofiSupportedChain::Bsc)]
#[case::robinhood(Chain::Robinhood, ElfomofiSupportedChain::Robinhood)]
fn deployed_chains_resolve_both_ways(
#[case] chain: Chain,
#[case] supported: ElfomofiSupportedChain,
) {
assert_eq!(ElfomofiSupportedChain::try_from(chain).unwrap(), supported);
assert_eq!(supported.chain(), chain);
}
#[test]
fn another_chain_is_invalid_input() {
let error = ElfomofiSupportedChain::try_from(Chain::Ethereum).unwrap_err();
assert!(matches!(error, FeedError::InvalidInput(_)), "{error}");
}
}