From f9f83b439f5ecf0ea9186a21a4a1fe3591173c03 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Tue, 14 Jan 2025 17:59:59 +0000 Subject: [PATCH] fix: Add expected_amount to Solution --- src/encoding/models.rs | 4 +++- src/encoding/strategy_encoder.rs | 9 ++++++--- src/encoding/strategy_selector.rs | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/encoding/models.rs b/src/encoding/models.rs index a02bd08..09439f7 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -21,7 +21,9 @@ pub struct Solution { pub given_amount: BigUint, /// The token being bought (exact in) or sold (exact out). checked_token: Bytes, - /// Amount of the checked token. + /// 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, /// Address of the sender. pub sender: Bytes, diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index 9a4e6eb..849ecd2 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 std::cmp::min; use crate::encoding::models::{ ActionType, EncodingContext, NativeAction, Solution, PROPELLER_ROUTER_ADDRESS, @@ -62,7 +63,9 @@ impl StrategyEncoder for SequentialStrategyEncoder { let one_hundred = BigUint::from(100u32); let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32); let multiplier = &one_hundred - slippage_percent; - check_amount = (&solution.check_amount * multiplier) / one_hundred; + let expected_amount_with_slippage = + (&solution.expected_amount * multiplier) / one_hundred; + check_amount = min(check_amount, expected_amount_with_slippage); } let mut swaps = vec![]; for (index, swap) in solution.swaps.iter().enumerate() { @@ -127,9 +130,9 @@ impl StrategyEncoder for SequentialStrategyEncoder { } } -pub struct SlipSwapStrategyEncoder {} +pub struct SplitSwapStrategyEncoder {} -impl StrategyEncoder for SlipSwapStrategyEncoder { +impl StrategyEncoder for SplitSwapStrategyEncoder { fn encode_strategy(&self, solution: Solution) -> Result, Error> { todo!() } diff --git a/src/encoding/strategy_selector.rs b/src/encoding/strategy_selector.rs index 7dcde79..d48e5d2 100644 --- a/src/encoding/strategy_selector.rs +++ b/src/encoding/strategy_selector.rs @@ -1,6 +1,6 @@ use crate::encoding::models::Solution; use crate::encoding::strategy_encoder::{ - SequentialStrategyEncoder, SingleSwapStrategyEncoder, SlipSwapStrategyEncoder, + SequentialStrategyEncoder, SingleSwapStrategyEncoder, SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder, StrategyEncoder, }; @@ -19,7 +19,7 @@ impl StrategySelector for DefaultStrategySelector { } else if solution.swaps.iter().all(|s| s.split == 0.0) { Box::new(SequentialStrategyEncoder {}) } else { - Box::new(SlipSwapStrategyEncoder {}) + Box::new(SplitSwapStrategyEncoder {}) } } }