feat: Make check amount optional
This commit is contained in:
@@ -24,7 +24,8 @@ pub struct Solution {
|
|||||||
/// Expected amount of the bought token (exact in) or sold token (exact out).
|
/// Expected amount of the bought token (exact in) or sold token (exact out).
|
||||||
pub expected_amount: BigUint,
|
pub expected_amount: BigUint,
|
||||||
/// Minimum amount to be checked for the solution to be valid.
|
/// 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<BigUint>,
|
||||||
/// Address of the sender.
|
/// Address of the sender.
|
||||||
pub sender: Bytes,
|
pub sender: Bytes,
|
||||||
/// Address of the receiver.
|
/// Address of the receiver.
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use crate::encoding::utils::{encode_input, ple_encode};
|
|||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use std::cmp::PartialEq;
|
|
||||||
|
|
||||||
struct RouterEncoder<S: StrategySelector, A: UserApprovalsManager> {
|
struct RouterEncoder<S: StrategySelector, A: UserApprovalsManager> {
|
||||||
strategy_selector: S,
|
strategy_selector: S,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
|
use num_traits::Zero;
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
|
||||||
use crate::encoding::models::{
|
use crate::encoding::models::{
|
||||||
@@ -58,7 +59,8 @@ pub struct SequentialStrategyEncoder {}
|
|||||||
|
|
||||||
impl StrategyEncoder for SequentialStrategyEncoder {
|
impl StrategyEncoder for SequentialStrategyEncoder {
|
||||||
fn encode_strategy(&self, solution: Solution) -> Result<Vec<u8>, Error> {
|
fn encode_strategy(&self, solution: Solution) -> Result<Vec<u8>, Error> {
|
||||||
let mut check_amount = solution.check_amount.clone();
|
let check_amount = if solution.check_amount.is_some() {
|
||||||
|
let mut check_amount = solution.check_amount.clone().unwrap();
|
||||||
if solution.slippage.is_some() {
|
if solution.slippage.is_some() {
|
||||||
let one_hundred = BigUint::from(100u32);
|
let one_hundred = BigUint::from(100u32);
|
||||||
let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32);
|
let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32);
|
||||||
@@ -67,6 +69,11 @@ impl StrategyEncoder for SequentialStrategyEncoder {
|
|||||||
(&solution.expected_amount * multiplier) / one_hundred;
|
(&solution.expected_amount * multiplier) / one_hundred;
|
||||||
check_amount = min(check_amount, expected_amount_with_slippage);
|
check_amount = min(check_amount, expected_amount_with_slippage);
|
||||||
}
|
}
|
||||||
|
check_amount
|
||||||
|
} else {
|
||||||
|
BigUint::ZERO
|
||||||
|
};
|
||||||
|
|
||||||
let mut swaps = vec![];
|
let mut swaps = vec![];
|
||||||
for (index, swap) in solution.swaps.iter().enumerate() {
|
for (index, swap) in solution.swaps.iter().enumerate() {
|
||||||
let is_last = index == solution.swaps.len() - 1;
|
let is_last = index == solution.swaps.len() - 1;
|
||||||
@@ -106,6 +113,7 @@ impl StrategyEncoder for SequentialStrategyEncoder {
|
|||||||
wrap,
|
wrap,
|
||||||
unwrap,
|
unwrap,
|
||||||
biguint_to_u256(&solution.given_amount),
|
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),
|
biguint_to_u256(&check_amount),
|
||||||
encoded_swaps,
|
encoded_swaps,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user