use alloy_primitives::{B256, U256, U512, hex, ruint::UintTryFrom};
use super::checks::details::{self, differs};
use crate::{deployments, error::VerificationError, types::Bps, types::VenueId};
pub const FEE_ON_PAYMENT: [u8; 6] = hex!("de65f326692b");
pub const FEE_ON_DELIVERY: [u8; 6] = hex!("cc909c08081a");
pub const WALLET_FEE_ON_PAYMENT: [u8; 6] = hex!("1b4c4374729e");
pub const WALLET_FEE_ON_DELIVERY: [u8; 6] = hex!("fcb0d9ddb285");
pub const MAX_FEE_BPS: u16 = 100;
#[cfg(test)]
pub(crate) const TAGS_FOR_PINS: [(&str, [u8; 6]); 4] = [
("fee_on_payment", FEE_ON_PAYMENT),
("fee_on_delivery", FEE_ON_DELIVERY),
("wallet_fee_on_payment", WALLET_FEE_ON_PAYMENT),
("wallet_fee_on_delivery", WALLET_FEE_ON_DELIVERY),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct Fee {
pub bps: Bps,
pub on_delivery: bool,
}
pub fn take_fee(from: &VenueId, pair_bps: Bps, max_fee_bps: Option<Bps>) -> Fee {
match max_fee_bps {
None => Fee::default(),
Some(_) => Fee { bps: pair_bps, on_delivery: deployments::is_venue_settled(from) },
}
}
pub fn fee_on(amount: U256, bps: Bps) -> U256 {
let scale = U256::from(10_000u16);
let bps = U256::from(bps.get());
amount / scale * bps + amount % scale * bps / scale
}
pub fn price_within(budget: U256, bps: Bps) -> Option<U256> {
let scale = U512::from(10_000u16);
let numerator = (wide(budget) + U512::from(1u8)) * scale - U512::from(1u8);
narrow(numerator / (scale + U512::from(bps.get())))
}
pub(crate) fn mul_div(a: U256, b: U256, c: U256) -> Option<U256> {
let product: U512 = a.widening_mul(b);
product.checked_div(wide(c)).and_then(narrow)
}
pub(crate) fn mul_div_ceil(a: U256, b: U256, c: U256) -> Option<U256> {
let product: U512 = a.widening_mul(b);
let divisor = wide(c);
let quotient = product.checked_div(divisor)?;
let rounded = if (product % divisor).is_zero() { quotient } else { quotient + U512::from(1u8) };
narrow(rounded)
}
pub(crate) fn product_lt(a: U256, b: U256, c: U256, d: U256) -> bool {
a.widening_mul::<256, 4, 512, 8>(b) < c.widening_mul::<256, 4, 512, 8>(d)
}
fn wide(value: U256) -> U512 {
value.widening_mul(U256::from(1u8))
}
fn narrow(value: U512) -> Option<U256> {
U256::uint_try_from(value).ok()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct SwapFee {
pub fee: U256,
pub on_delivery: bool,
pub wallet: bool,
}
pub fn swap_fee(swap_id: &B256) -> SwapFee {
let (tag, rest) = swap_id.split_at(6);
let (on_delivery, wallet) = match <[u8; 6]>::try_from(tag) {
Ok(FEE_ON_PAYMENT) => (false, false),
Ok(FEE_ON_DELIVERY) => (true, false),
Ok(WALLET_FEE_ON_PAYMENT) => (false, true),
Ok(WALLET_FEE_ON_DELIVERY) => (true, true),
_ => return SwapFee { fee: U256::ZERO, on_delivery: false, wallet: false },
};
SwapFee { fee: U256::from_be_slice(&rest[..10]), on_delivery, wallet }
}
pub fn check_fee(
swap_id: &B256,
taker_fee: U256,
amount_in: U256,
amount_out: U256,
on_delivery: bool,
max_fee_bps: Option<Bps>,
) -> Result<(), VerificationError> {
let carried = swap_fee(swap_id);
if carried.fee != taker_fee {
return Err(differs(details::FILLS_TAKER_FEE));
}
if carried.fee.is_zero() {
return Ok(());
}
if carried.on_delivery != on_delivery {
return Err(differs(details::FILLS_SWAP_ID));
}
let charged = if on_delivery {
amount_out.checked_add(carried.fee)
} else {
amount_in.checked_sub(carried.fee)
}
.ok_or(differs(details::FILLS_TAKER_FEE))?;
let accepted = max_fee_bps.is_some_and(|max| {
!product_lt(U256::from(max.get()), charged, carried.fee, U256::from(10_000u16))
});
if accepted { Ok(()) } else { Err(differs(details::FEE_ABOVE_ACCEPTED)) }
}
pub fn minimum_received(to: &VenueId, amount_out: U256) -> Option<U256> {
match to {
VenueId::Polymarket => mul_div_ceil(
amount_out,
U256::from(deployments::WALLET_SWAP_MIN_BPS),
U256::from(10_000u16),
),
_ => Some(amount_out),
}
}
#[cfg(test)]
mod tests;