feat: Remove slippage and expected_amount from Solution

The user is responsible for coming up with a sensible value for this themselves

Took 37 minutes
This commit is contained in:
Diana Carvalho
2025-05-21 16:44:39 +01:00
parent 4e8c6ddc8c
commit 08056c4a6c
7 changed files with 45 additions and 283 deletions

View File

@@ -1,5 +1,4 @@
use std::{
cmp::max,
env,
fs::OpenOptions,
io::{BufRead, BufReader, Write},
@@ -17,10 +16,7 @@ use once_cell::sync::Lazy;
use tokio::runtime::{Handle, Runtime};
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
models::{Solution, Swap},
};
use crate::encoding::{errors::EncodingError, models::Swap};
/// Safely converts a `Bytes` object to an `Address` object.
///
@@ -49,30 +45,6 @@ pub fn percentage_to_uint24(decimal: f64) -> U24 {
U24::from(scaled.round())
}
/// Gets the minimum amount out for a solution to pass when executed on-chain.
///
/// The minimum amount is calculated based on the expected amount and the slippage percentage, if
/// passed. If this information is not passed, the user-passed checked amount will be used.
/// If both the slippage and minimum user-passed checked amount are passed, the maximum of the two
/// will be used.
/// If neither are passed, the minimum amount will be zero.
pub fn get_min_amount_for_solution(solution: Solution) -> BigUint {
let mut min_amount_out = solution
.checked_amount
.unwrap_or(BigUint::ZERO);
if let (Some(expected_amount), Some(slippage)) =
(solution.expected_amount.as_ref(), solution.slippage)
{
let bps = BigUint::from(10_000u32);
let slippage_percent = BigUint::from((slippage * 10000.0) as u32);
let multiplier = &bps - slippage_percent;
let expected_amount_with_slippage = (expected_amount * &multiplier) / &bps;
min_amount_out = max(min_amount_out, expected_amount_with_slippage);
}
min_amount_out
}
/// Gets the position of a token in a list of tokens.
pub fn get_token_position(tokens: Vec<Bytes>, token: Bytes) -> Result<U8, EncodingError> {
let position = U8::from(
@@ -187,28 +159,3 @@ pub fn write_calldata_to_file(test_identifier: &str, hex_calldata: &str) {
writeln!(file, "{line}").expect("Failed to write calldata");
}
}
#[cfg(test)]
mod tests {
use num_bigint::BigUint;
use super::*;
use crate::encoding::models::Solution;
#[test]
fn test_min_amount_out_small_slippage() {
// Tests that the calculation's precision is high enough to support a slippage of 0.1%.
let solution = Solution {
exact_out: false,
given_amount: BigUint::from(1000000000000000000u64),
checked_amount: None,
slippage: Some(0.001f64),
expected_amount: Some(BigUint::from(1000000000000000000u64)),
..Default::default()
};
let min_amount_out = get_min_amount_for_solution(solution);
assert_eq!(min_amount_out, BigUint::from(999000000000000000u64));
}
}