diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 7c3870b..94cbff9 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -24,7 +24,8 @@ pub struct Solution { /// Expected amount of the bought token (exact in) or sold token (exact out). pub expected_amount: BigUint, /// Minimum amount to be checked for the solution to be valid. - pub check_amount: BigUint, + /// If not set, the check will not be performed. + pub check_amount: Option, /// Address of the sender. pub sender: Bytes, /// Address of the receiver. diff --git a/src/encoding/router_encoder.rs b/src/encoding/router_encoder.rs index a655f8d..a57a7da 100644 --- a/src/encoding/router_encoder.rs +++ b/src/encoding/router_encoder.rs @@ -5,7 +5,6 @@ use crate::encoding::utils::{encode_input, ple_encode}; use alloy_sol_types::SolValue; use anyhow::Error; use num_bigint::BigUint; -use std::cmp::PartialEq; struct RouterEncoder { strategy_selector: S, diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index 849ecd2..735156d 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,6 +1,7 @@ use alloy_sol_types::SolValue; use anyhow::Error; use num_bigint::BigUint; +use num_traits::Zero; use std::cmp::min; use crate::encoding::models::{ @@ -58,15 +59,21 @@ pub struct SequentialStrategyEncoder {} impl StrategyEncoder for SequentialStrategyEncoder { fn encode_strategy(&self, solution: Solution) -> Result, Error> { - let mut check_amount = solution.check_amount.clone(); - if solution.slippage.is_some() { - let one_hundred = BigUint::from(100u32); - let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32); - let multiplier = &one_hundred - slippage_percent; - let expected_amount_with_slippage = - (&solution.expected_amount * multiplier) / one_hundred; - check_amount = min(check_amount, expected_amount_with_slippage); - } + let check_amount = if solution.check_amount.is_some() { + let mut check_amount = solution.check_amount.clone().unwrap(); + if solution.slippage.is_some() { + let one_hundred = BigUint::from(100u32); + let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32); + let multiplier = &one_hundred - slippage_percent; + let expected_amount_with_slippage = + (&solution.expected_amount * multiplier) / one_hundred; + check_amount = min(check_amount, expected_amount_with_slippage); + } + check_amount + } else { + BigUint::ZERO + }; + let mut swaps = vec![]; for (index, swap) in solution.swaps.iter().enumerate() { let is_last = index == solution.swaps.len() - 1; @@ -106,6 +113,7 @@ impl StrategyEncoder for SequentialStrategyEncoder { wrap, unwrap, biguint_to_u256(&solution.given_amount), + if check_amount.is_zero() { false } else { true }, // if check_amount is zero, then we don't need to check biguint_to_u256(&check_amount), encoded_swaps, )