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:
@@ -2,7 +2,6 @@ use std::{collections::HashSet, str::FromStr};
|
|||||||
|
|
||||||
use alloy_primitives::{aliases::U24, FixedBytes, U256, U8};
|
use alloy_primitives::{aliases::U24, FixedBytes, U256, U8};
|
||||||
use alloy_sol_types::SolValue;
|
use alloy_sol_types::SolValue;
|
||||||
use num_bigint::BigUint;
|
|
||||||
use tycho_core::{keccak256, Bytes};
|
use tycho_core::{keccak256, Bytes};
|
||||||
|
|
||||||
use crate::encoding::{
|
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![];
|
let mut grouped_protocol_data: Vec<u8> = vec![];
|
||||||
for swap in grouped_swap.swaps.iter() {
|
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 =
|
let protocol_data =
|
||||||
swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
||||||
grouped_protocol_data.extend(protocol_data);
|
grouped_protocol_data.extend(protocol_data);
|
||||||
@@ -297,9 +295,8 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
|
|||||||
receiver: receiver.clone(),
|
receiver: receiver.clone(),
|
||||||
exact_out: solution.exact_out,
|
exact_out: solution.exact_out,
|
||||||
router_address: router_address.clone(),
|
router_address: router_address.clone(),
|
||||||
group_token_in: swap.token_in.clone(),
|
group_token_in: grouped_swap.input_token.clone(),
|
||||||
group_token_out: swap.token_out.clone(),
|
group_token_out: grouped_swap.output_token.clone(),
|
||||||
amount_out_min: BigUint::from(1u128),
|
|
||||||
};
|
};
|
||||||
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
||||||
grouped_protocol_data.extend(protocol_data);
|
grouped_protocol_data.extend(protocol_data);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::str::FromStr;
|
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 alloy_sol_types::SolValue;
|
||||||
|
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
@@ -8,8 +8,7 @@ use crate::encoding::{
|
|||||||
evm::{
|
evm::{
|
||||||
approvals::protocol_approvals_manager::ProtocolApprovalsManager,
|
approvals::protocol_approvals_manager::ProtocolApprovalsManager,
|
||||||
utils::{
|
utils::{
|
||||||
biguint_to_u256, bytes_to_address, encode_function_selector, get_static_attribute,
|
bytes_to_address, encode_function_selector, get_static_attribute, pad_to_fixed_size,
|
||||||
pad_to_fixed_size,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
models::{EncodingContext, Swap},
|
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_in_address = bytes_to_address(&encoding_context.group_token_in)?;
|
||||||
let group_token_out_address = bytes_to_address(&encoding_context.group_token_out)?;
|
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 zero_to_one = Self::get_zero_to_one(token_in_address, token_out_address);
|
||||||
let callback_executor = bytes_to_address(&encoding_context.router_address)?;
|
let callback_executor = bytes_to_address(&encoding_context.router_address)?;
|
||||||
|
|
||||||
@@ -289,7 +288,7 @@ mod tests {
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use alloy::hex::encode;
|
use alloy::hex::encode;
|
||||||
use num_bigint::{BigInt, BigUint};
|
use num_bigint::BigInt;
|
||||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -315,7 +314,6 @@ mod tests {
|
|||||||
router_address: Bytes::zero(20),
|
router_address: Bytes::zero(20),
|
||||||
group_token_in: token_in.clone(),
|
group_token_in: token_in.clone(),
|
||||||
group_token_out: token_out.clone(),
|
group_token_out: token_out.clone(),
|
||||||
amount_out_min: BigUint::from(0u128),
|
|
||||||
};
|
};
|
||||||
let encoder =
|
let encoder =
|
||||||
UniswapV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
UniswapV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||||
@@ -363,7 +361,6 @@ mod tests {
|
|||||||
router_address: Bytes::zero(20),
|
router_address: Bytes::zero(20),
|
||||||
group_token_in: token_in.clone(),
|
group_token_in: token_in.clone(),
|
||||||
group_token_out: token_out.clone(),
|
group_token_out: token_out.clone(),
|
||||||
amount_out_min: BigUint::from(0u128),
|
|
||||||
};
|
};
|
||||||
let encoder =
|
let encoder =
|
||||||
UniswapV3SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
UniswapV3SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||||
@@ -412,7 +409,6 @@ mod tests {
|
|||||||
router_address: Bytes::zero(20),
|
router_address: Bytes::zero(20),
|
||||||
group_token_in: token_in.clone(),
|
group_token_in: token_in.clone(),
|
||||||
group_token_out: token_out.clone(),
|
group_token_out: token_out.clone(),
|
||||||
amount_out_min: BigUint::from(0u128),
|
|
||||||
};
|
};
|
||||||
let encoder =
|
let encoder =
|
||||||
BalancerV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
BalancerV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||||
@@ -474,7 +470,6 @@ mod tests {
|
|||||||
|
|
||||||
group_token_in: token_in.clone(),
|
group_token_in: token_in.clone(),
|
||||||
group_token_out: token_out.clone(),
|
group_token_out: token_out.clone(),
|
||||||
amount_out_min: BigUint::from(1u128),
|
|
||||||
};
|
};
|
||||||
let encoder =
|
let encoder =
|
||||||
UniswapV4SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
UniswapV4SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||||
@@ -490,8 +485,8 @@ mod tests {
|
|||||||
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
|
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
|
||||||
// group token out
|
// group token out
|
||||||
"dac17f958d2ee523a2206206994597c13d831ec7",
|
"dac17f958d2ee523a2206206994597c13d831ec7",
|
||||||
// amount out min (0 as u128)
|
// amount out min
|
||||||
"0000000000000000000000000000000000000000000000000000000000000001",
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
// zero for one
|
// zero for one
|
||||||
"01",
|
"01",
|
||||||
// router address
|
// router address
|
||||||
@@ -543,7 +538,6 @@ mod tests {
|
|||||||
group_token_in: group_token_in.clone(),
|
group_token_in: group_token_in.clone(),
|
||||||
// Token out is the same as the group token out
|
// Token out is the same as the group token out
|
||||||
group_token_out: token_out.clone(),
|
group_token_out: token_out.clone(),
|
||||||
amount_out_min: BigUint::from(1u128),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let encoder =
|
let encoder =
|
||||||
@@ -582,7 +576,6 @@ mod tests {
|
|||||||
router_address: router_address.clone(),
|
router_address: router_address.clone(),
|
||||||
group_token_in: usde_address.clone(),
|
group_token_in: usde_address.clone(),
|
||||||
group_token_out: wbtc_address.clone(),
|
group_token_out: wbtc_address.clone(),
|
||||||
amount_out_min: BigUint::from(1u128),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Setup - First sequence: USDE -> USDT
|
// Setup - First sequence: USDE -> USDT
|
||||||
@@ -656,8 +649,8 @@ mod tests {
|
|||||||
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
|
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
|
||||||
// group_token out
|
// group_token out
|
||||||
"2260fac5e5542a773aa44fbcfedf7c193bc2c599",
|
"2260fac5e5542a773aa44fbcfedf7c193bc2c599",
|
||||||
// amount out min (1 as u128)
|
// amount out min
|
||||||
"0000000000000000000000000000000000000000000000000000000000000001",
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
// zero for one
|
// zero for one
|
||||||
"01",
|
"01",
|
||||||
// router address
|
// router address
|
||||||
|
|||||||
@@ -108,7 +108,6 @@ pub struct Transaction {
|
|||||||
/// * `router_address`: Address of the router contract to be used for the swaps.
|
/// * `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_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.
|
/// * `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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct EncodingContext {
|
pub struct EncodingContext {
|
||||||
pub receiver: Bytes,
|
pub receiver: Bytes,
|
||||||
@@ -116,7 +115,6 @@ pub struct EncodingContext {
|
|||||||
pub router_address: Bytes,
|
pub router_address: Bytes,
|
||||||
pub group_token_in: Bytes,
|
pub group_token_in: Bytes,
|
||||||
pub group_token_out: Bytes,
|
pub group_token_out: Bytes,
|
||||||
pub amount_out_min: BigUint,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||||
|
|||||||
Reference in New Issue
Block a user