fix: Pass proper group tokens in EncodingContext...

Also:
- Remove amount_out_min. Our reasoning is that we perform high-level final amount checks anyway (and expect users to do the same). Lower-level USV4 min amount checks are redundant.
This commit is contained in:
TAMARA LIPOWSKI
2025-02-19 16:27:56 -05:00
parent 2d655b27fe
commit 81c8a04cbb
3 changed files with 17 additions and 29 deletions

View File

@@ -2,7 +2,6 @@ use std::{collections::HashSet, str::FromStr};
use alloy_primitives::{aliases::U24, FixedBytes, U256, U8};
use alloy_sol_types::SolValue;
use num_bigint::BigUint;
use tycho_core::{keccak256, Bytes};
use crate::encoding::{
@@ -187,16 +186,15 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
))
})?;
let encoding_context = EncodingContext {
receiver: solution.router_address.clone(),
exact_out: solution.exact_out,
router_address: solution.router_address.clone(),
group_token_in: tokens.first().unwrap().clone(),
group_token_out: tokens.last().unwrap().clone(),
amount_out_min: min_amount_out.clone(),
};
let mut grouped_protocol_data: Vec<u8> = vec![];
for swap in grouped_swap.swaps.iter() {
let encoding_context = EncodingContext {
receiver: solution.router_address.clone(),
exact_out: solution.exact_out,
router_address: solution.router_address.clone(),
group_token_in: grouped_swap.input_token.clone(),
group_token_out: grouped_swap.output_token.clone(),
};
let protocol_data =
swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
grouped_protocol_data.extend(protocol_data);
@@ -297,9 +295,8 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
receiver: receiver.clone(),
exact_out: solution.exact_out,
router_address: router_address.clone(),
group_token_in: swap.token_in.clone(),
group_token_out: swap.token_out.clone(),
amount_out_min: BigUint::from(1u128),
group_token_in: grouped_swap.input_token.clone(),
group_token_out: grouped_swap.output_token.clone(),
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
grouped_protocol_data.extend(protocol_data);

View File

@@ -1,6 +1,6 @@
use std::str::FromStr;
use alloy_primitives::{Address, Bytes as AlloyBytes};
use alloy_primitives::{Address, Bytes as AlloyBytes, U256};
use alloy_sol_types::SolValue;
use crate::encoding::{
@@ -8,8 +8,7 @@ use crate::encoding::{
evm::{
approvals::protocol_approvals_manager::ProtocolApprovalsManager,
utils::{
biguint_to_u256, bytes_to_address, encode_function_selector, get_static_attribute,
pad_to_fixed_size,
bytes_to_address, encode_function_selector, get_static_attribute, pad_to_fixed_size,
},
},
models::{EncodingContext, Swap},
@@ -192,7 +191,7 @@ impl SwapEncoder for UniswapV4SwapEncoder {
let group_token_in_address = bytes_to_address(&encoding_context.group_token_in)?;
let group_token_out_address = bytes_to_address(&encoding_context.group_token_out)?;
let amount_out_min = biguint_to_u256(&encoding_context.amount_out_min);
let amount_out_min = U256::from(0);
let zero_to_one = Self::get_zero_to_one(token_in_address, token_out_address);
let callback_executor = bytes_to_address(&encoding_context.router_address)?;
@@ -289,7 +288,7 @@ mod tests {
use std::collections::HashMap;
use alloy::hex::encode;
use num_bigint::{BigInt, BigUint};
use num_bigint::BigInt;
use tycho_core::{dto::ProtocolComponent, Bytes};
use super::*;
@@ -315,7 +314,6 @@ mod tests {
router_address: Bytes::zero(20),
group_token_in: token_in.clone(),
group_token_out: token_out.clone(),
amount_out_min: BigUint::from(0u128),
};
let encoder =
UniswapV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
@@ -363,7 +361,6 @@ mod tests {
router_address: Bytes::zero(20),
group_token_in: token_in.clone(),
group_token_out: token_out.clone(),
amount_out_min: BigUint::from(0u128),
};
let encoder =
UniswapV3SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
@@ -412,7 +409,6 @@ mod tests {
router_address: Bytes::zero(20),
group_token_in: token_in.clone(),
group_token_out: token_out.clone(),
amount_out_min: BigUint::from(0u128),
};
let encoder =
BalancerV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
@@ -474,7 +470,6 @@ mod tests {
group_token_in: token_in.clone(),
group_token_out: token_out.clone(),
amount_out_min: BigUint::from(1u128),
};
let encoder =
UniswapV4SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
@@ -490,8 +485,8 @@ mod tests {
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
// group token out
"dac17f958d2ee523a2206206994597c13d831ec7",
// amount out min (0 as u128)
"0000000000000000000000000000000000000000000000000000000000000001",
// amount out min
"0000000000000000000000000000000000000000000000000000000000000000",
// zero for one
"01",
// router address
@@ -543,7 +538,6 @@ mod tests {
group_token_in: group_token_in.clone(),
// Token out is the same as the group token out
group_token_out: token_out.clone(),
amount_out_min: BigUint::from(1u128),
};
let encoder =
@@ -582,7 +576,6 @@ mod tests {
router_address: router_address.clone(),
group_token_in: usde_address.clone(),
group_token_out: wbtc_address.clone(),
amount_out_min: BigUint::from(1u128),
};
// Setup - First sequence: USDE -> USDT
@@ -656,8 +649,8 @@ mod tests {
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
// group_token out
"2260fac5e5542a773aa44fbcfedf7c193bc2c599",
// amount out min (1 as u128)
"0000000000000000000000000000000000000000000000000000000000000001",
// amount out min
"0000000000000000000000000000000000000000000000000000000000000000",
// zero for one
"01",
// router address

View File

@@ -108,7 +108,6 @@ pub struct Transaction {
/// * `router_address`: Address of the router contract to be used for the swaps.
/// * `group_token_in`: Token to be used as the input for the group swap.
/// * `group_token_out`: Token to be used as the output for the group swap.
/// * `amount_out_min`: Minimum amount of the output token to be received.
#[derive(Clone, Debug)]
pub struct EncodingContext {
pub receiver: Bytes,
@@ -116,7 +115,6 @@ pub struct EncodingContext {
pub router_address: Bytes,
pub group_token_in: Bytes,
pub group_token_out: Bytes,
pub amount_out_min: BigUint,
}
#[derive(Clone, PartialEq, Eq, Hash)]