use std::fmt::Debug;
use alloy::primitives::{Address, Uint};
use itertools::{iproduct, Itertools};
pub use proptest;
use proptest::prelude::*;
use tycho_common::{
models::{token::Token, Chain},
simulation::protocol_sim::ProtocolSim,
Bytes,
};
use super::CheckableState;
pub trait StateCases: ProtocolSim + CheckableState + Clone {
type Tokens: AsRef<[Token]> + Clone + Debug + 'static;
fn cases() -> impl Domain<Value = Case<Self, Self::Tokens>>;
}
#[derive(Debug, Clone)]
pub struct Case<S, T> {
pub state: S,
pub tokens: T,
}
pub trait Domain {
type Value: Clone + Debug + 'static;
fn corners(&self) -> Vec<Self::Value>;
fn random(&self) -> impl Strategy<Value = Self::Value>;
}
pub trait DomainExt: Domain {
fn corner_or_random(&self) -> impl Strategy<Value = Self::Value> {
prop_oneof![
1 => prop::sample::select(self.corners()),
9 => self.random(),
]
}
fn map<F, U>(self, f: F) -> Map<Self, F>
where
Self: Sized,
F: Fn(Self::Value) -> U,
U: Clone + Debug + 'static,
{
Map { domain: self, f }
}
}
impl<D: Domain + ?Sized> DomainExt for D {}
#[derive(Debug, Clone)]
pub struct Map<D, F> {
domain: D,
f: F,
}
impl<D, F, U> Domain for Map<D, F>
where
D: Domain,
F: Fn(D::Value) -> U,
U: Clone + Debug + 'static,
{
type Value = U;
fn corners(&self) -> Vec<U> {
self.domain
.corners()
.into_iter()
.map(&self.f)
.collect()
}
fn random(&self) -> impl Strategy<Value = U> {
self.domain.random().prop_map(&self.f)
}
}
macro_rules! tuple_domain {
($($name:ident: $index:tt),+) => {
impl<$($name: Domain),+> Domain for ($($name,)+) {
type Value = ($($name::Value,)+);
fn corners(&self) -> Vec<Self::Value> {
iproduct!($(self.$index.corners()),+).collect()
}
fn random(&self) -> impl Strategy<Value = Self::Value> {
($(self.$index.corner_or_random(),)+)
}
}
};
}
tuple_domain!(A: 0, B: 1);
tuple_domain!(A: 0, B: 1, C: 2);
tuple_domain!(A: 0, B: 1, C: 2, D: 3);
#[derive(Debug, Clone, Copy)]
pub struct AnyUint<const BITS: usize, const LIMBS: usize>;
impl<const BITS: usize, const LIMBS: usize> Domain for AnyUint<BITS, LIMBS> {
type Value = Uint<BITS, LIMBS>;
fn corners(&self) -> Vec<Self::Value> {
vec![Uint::ZERO, Uint::ONE, Uint::MAX - Uint::ONE, Uint::MAX]
}
fn random(&self) -> impl Strategy<Value = Self::Value> {
(0..=BITS, any::<Uint<BITS, LIMBS>>()).prop_map(|(bits, value)| {
if bits == 0 {
Uint::ZERO
} else {
(value >> (BITS - bits)) | (Uint::ONE << (bits - 1))
}
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct SetOf<D>(
pub D,
);
impl<D: Domain> Domain for SetOf<D> {
type Value = Vec<D::Value>;
fn corners(&self) -> Vec<Self::Value> {
vec![self.0.corners()]
}
fn random(&self) -> impl Strategy<Value = Self::Value> {
prop::collection::vec(self.0.corner_or_random(), 0..=self.0.corners().len())
}
}
#[derive(Debug, Clone, Copy)]
pub struct AnyTokens<const N: usize>;
impl<const N: usize> Domain for AnyTokens<N> {
type Value = [Token; N];
fn corners(&self) -> Vec<[Token; N]> {
let addresses: [Address; N] = std::array::from_fn(|i| {
Address::with_last_byte(u8::try_from(i + 1).expect("at most 255 tokens"))
});
(0..N)
.map(|_| Decimals.corners())
.multi_cartesian_product()
.map(|decimals| std::array::from_fn(|i| token(i, addresses[i], decimals[i])))
.collect()
}
fn random(&self) -> impl Strategy<Value = [Token; N]> {
(
prop::collection::btree_set(any::<[u8; 20]>().prop_map(Address::from), N),
prop::array::uniform::<_, N>(Decimals.corner_or_random()),
)
.prop_map(|(addresses, decimals)| {
let addresses = Vec::from_iter(addresses);
std::array::from_fn(|i| token(i, addresses[i], decimals[i]))
})
}
}
fn token(index: usize, address: Address, decimals: u32) -> Token {
Token::new(
&Bytes::from(address.as_slice()),
&format!("T{index}"),
decimals,
0,
&[Some(100_000)],
Chain::Ethereum,
100,
)
}
#[derive(Debug, Clone, Copy)]
struct Decimals;
impl Domain for Decimals {
type Value = u32;
fn corners(&self) -> Vec<u32> {
vec![0, 18, 39, 78, 255]
}
fn random(&self) -> impl Strategy<Value = u32> {
prop_oneof![prop::sample::select(vec![6u32, 8, 18]), 0u32..=255,]
}
}