use alloy::primitives::U256;
use tycho_common::simulation::errors::SimulationError;
use super::constants::{PIPS_TO_PRECISION, PRECISION};
use crate::evm::protocol::utils::{
solidity_math::{mul_div, mul_div_rounding_up},
uniswap::FEE_PIPS_DENOMINATOR as PIPS_DENOMINATOR,
};
pub fn get_fee_amount_from(amount_with_fee: u128, fee_pips: u32) -> Result<u128, SimulationError> {
let total_fee = U256::from(fee_pips) * PIPS_TO_PRECISION;
let fee = mul_div_rounding_up(U256::from(amount_with_fee), total_fee, PRECISION)?;
u128::try_from(fee).map_err(|_wider_than_u128| {
SimulationError::InvalidInput(format!("fee overflows u128: {fee}"), None)
})
}
pub fn get_fee_amount(amount: u128, fee_pips: u32) -> Result<u128, SimulationError> {
if fee_pips >= PIPS_DENOMINATOR {
return Err(SimulationError::InvalidInput(
format!("fee out of range: {fee_pips} pips, must be below {PIPS_DENOMINATOR}"),
None,
));
}
let total_fee = U256::from(fee_pips) * PIPS_TO_PRECISION;
let fee = mul_div_rounding_up(U256::from(amount), total_fee, PRECISION - total_fee)?;
u128::try_from(fee).map_err(|_wider_than_u128| {
SimulationError::InvalidInput(format!("fee overflows u128: {fee}"), None)
})
}
pub fn calculate_swap_fee(protocol_fee: u16, lp_fee: u32) -> u32 {
let protocol_fee = u32::from(protocol_fee) & 0xfff;
let lp_fee = lp_fee & 0xff_ffff;
let numerator = u64::from(protocol_fee) * u64::from(lp_fee);
#[expect(
clippy::cast_possible_truncation,
reason = "a 12-bit times a 24-bit value over 1e6 is below 2^17"
)]
let overlap = (numerator / u64::from(PIPS_DENOMINATOR)) as u32;
protocol_fee + lp_fee - overlap
}
pub fn protocol_fee_amount(
fee_amount: u128,
protocol_fee: u16,
swap_fee: u32,
) -> Result<u128, SimulationError> {
let protocol_fee = protocol_fee & 0xfff;
if protocol_fee == 0 || swap_fee == 0 {
return Ok(0);
}
if u32::from(protocol_fee) == swap_fee {
return Ok(fee_amount);
}
let share = mul_div(U256::from(fee_amount), U256::from(protocol_fee), U256::from(swap_fee))?;
u128::try_from(share).map_err(|_wider_than_u128| {
SimulationError::InvalidInput(format!("protocol fee overflows u128: {share}"), None)
})
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[test]
fn test_fee_amount_from_known_value() {
assert_eq!(
get_fee_amount_from(1_000_000_000_000_000_000, 100).unwrap(),
100_000_000_000_000
);
}
#[test]
fn test_swap_fee_charges_protocol_fee_first() {
assert_eq!(calculate_swap_fee(1_000, 3_000), 3_997);
}
#[test]
fn test_protocol_takes_its_share_of_the_swap_fee() {
let swap_fee = calculate_swap_fee(3, 7);
assert_eq!(swap_fee, 10, "protocol fee first, LP fee on the remainder");
assert_eq!(protocol_fee_amount(100, 3, swap_fee).unwrap(), 30);
}
#[rstest]
#[case::no_protocol_fee(100, 0, 10, 0)]
#[case::no_swap_fee(100, 3, 0, 0)]
#[case::protocol_takes_all(100, 10, 10, 100)]
#[case::masked_to_twelve_bits(100, 0x1000, 10, 0)]
fn test_protocol_fee_amount(
#[case] fee_amount: u128,
#[case] protocol_fee: u16,
#[case] swap_fee: u32,
#[case] expected: u128,
) {
assert_eq!(protocol_fee_amount(fee_amount, protocol_fee, swap_fee).unwrap(), expected);
}
}