feat: Optimize transfer to first pool
TODO: Update integration tests on solidity side, since a few were now updated on the encoding side to use a transferFrom.
This commit is contained in:
committed by
Diana Carvalho
parent
ee7495551e
commit
59a80dc392
@@ -1,2 +1,4 @@
|
||||
pub mod strategy_encoders;
|
||||
mod strategy_validators;
|
||||
|
||||
mod transfer_optimizations;
|
||||
|
||||
@@ -9,8 +9,9 @@ use crate::encoding::{
|
||||
evm::{
|
||||
approvals::permit2::Permit2,
|
||||
group_swaps::group_swaps,
|
||||
strategy_encoder::strategy_validators::{
|
||||
SequentialSwapValidator, SplitSwapValidator, SwapValidator,
|
||||
strategy_encoder::{
|
||||
strategy_validators::{SequentialSwapValidator, SplitSwapValidator, SwapValidator},
|
||||
transfer_optimizations::TransferOptimization,
|
||||
},
|
||||
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
|
||||
utils::{
|
||||
@@ -69,6 +70,8 @@ impl SingleSwapStrategyEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
impl TransferOptimization for SingleSwapStrategyEncoder {}
|
||||
|
||||
impl StrategyEncoder for SingleSwapStrategyEncoder {
|
||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
let grouped_swaps = group_swaps(solution.clone().swaps);
|
||||
@@ -111,12 +114,19 @@ impl StrategyEncoder for SingleSwapStrategyEncoder {
|
||||
|
||||
let mut grouped_protocol_data: Vec<u8> = vec![];
|
||||
for swap in grouped_swap.swaps.iter() {
|
||||
let transfer_type = self.get_transfer_method(
|
||||
swap.clone(),
|
||||
solution.given_token.clone(),
|
||||
self.permit2.clone().is_some(),
|
||||
);
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: self.router_address.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: Some(self.router_address.clone()),
|
||||
group_token_in: grouped_swap.input_token.clone(),
|
||||
group_token_out: grouped_swap.output_token.clone(),
|
||||
transfer_type: transfer_type.clone(),
|
||||
};
|
||||
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
||||
grouped_protocol_data.extend(protocol_data);
|
||||
@@ -199,6 +209,8 @@ pub struct SequentialSwapStrategyEncoder {
|
||||
sequential_swap_validator: SequentialSwapValidator,
|
||||
}
|
||||
|
||||
impl TransferOptimization for SequentialSwapStrategyEncoder {}
|
||||
|
||||
impl SequentialSwapStrategyEncoder {
|
||||
pub fn new(
|
||||
chain: Chain,
|
||||
@@ -274,12 +286,19 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder {
|
||||
|
||||
let mut grouped_protocol_data: Vec<u8> = vec![];
|
||||
for swap in grouped_swap.swaps.iter() {
|
||||
let transfer_type = self.get_transfer_method(
|
||||
swap.clone(),
|
||||
solution.given_token.clone(),
|
||||
self.permit2.clone().is_some(),
|
||||
);
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: self.router_address.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: Some(self.router_address.clone()),
|
||||
group_token_in: grouped_swap.input_token.clone(),
|
||||
group_token_out: grouped_swap.output_token.clone(),
|
||||
transfer_type: transfer_type.clone(),
|
||||
};
|
||||
let protocol_data =
|
||||
swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
||||
@@ -415,6 +434,8 @@ impl SplitSwapStrategyEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
impl TransferOptimization for SplitSwapStrategyEncoder {}
|
||||
|
||||
impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
self.split_swap_validator
|
||||
@@ -491,12 +512,19 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
|
||||
let mut grouped_protocol_data: Vec<u8> = vec![];
|
||||
for swap in grouped_swap.swaps.iter() {
|
||||
let transfer_type = self.get_transfer_method(
|
||||
swap.clone(),
|
||||
solution.given_token.clone(),
|
||||
self.permit2.clone().is_some(),
|
||||
);
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: self.router_address.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: Some(self.router_address.clone()),
|
||||
group_token_in: grouped_swap.input_token.clone(),
|
||||
group_token_out: grouped_swap.output_token.clone(),
|
||||
transfer_type: transfer_type.clone(),
|
||||
};
|
||||
let protocol_data =
|
||||
swap_encoder.encode_swap(swap.clone(), encoding_context.clone())?;
|
||||
@@ -722,7 +750,7 @@ mod tests {
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
"00", // zero2one
|
||||
"00", // transfer type
|
||||
"02", // transfer type
|
||||
"00000000000000", // padding
|
||||
));
|
||||
let hex_calldata = encode(&calldata);
|
||||
@@ -825,9 +853,8 @@ mod tests {
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
"00", // zero2one
|
||||
"00", // exact out
|
||||
"00", // transfer type
|
||||
"00000000000000000000000000", // padding
|
||||
"02", // transfer type
|
||||
"0000000000000000000000000000", // padding
|
||||
));
|
||||
let hex_calldata = encode(&calldata);
|
||||
|
||||
@@ -1484,9 +1511,8 @@ mod tests {
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
"00", // zero2one
|
||||
"00", // exact out
|
||||
"00", // transfer type
|
||||
"00000000000000000000000000", // padding
|
||||
"01", // transfer type
|
||||
"0000000000000000000000000000", // padding
|
||||
]
|
||||
.join("");
|
||||
|
||||
@@ -1568,9 +1594,8 @@ mod tests {
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
"00", // zero2one
|
||||
"00", // exact out
|
||||
"00", // transfer type
|
||||
"000000000000", // padding
|
||||
"01", // transfer type
|
||||
"00000000000000", // padding
|
||||
]
|
||||
.join("");
|
||||
|
||||
@@ -1821,7 +1846,7 @@ mod tests {
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
|
||||
"01", // zero2one
|
||||
"00", // transfer type
|
||||
"02", // transfer type
|
||||
"006e", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00000000", // split
|
||||
@@ -1975,7 +2000,7 @@ mod tests {
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
|
||||
"01", // zero2one
|
||||
"00", // transfer type
|
||||
"02", // transfer type
|
||||
"006e", // ple encoded swaps
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
@@ -1987,7 +2012,7 @@ mod tests {
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"8ad599c3a0ff1de082011efddc58f1908eb6e6d8", // component id
|
||||
"01", // zero2one
|
||||
"00", // transfer type
|
||||
"02", // transfer type
|
||||
"0057", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
@@ -2135,7 +2160,7 @@ mod tests {
|
||||
"b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
"01", // zero2one
|
||||
"00", // transfer type
|
||||
"02", // transfer type
|
||||
"006e", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
|
||||
95
src/encoding/evm/strategy_encoder/transfer_optimizations.rs
Normal file
95
src/encoding/evm/strategy_encoder/transfer_optimizations.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use tycho_common::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
evm::constants::IN_TRANSFER_OPTIMIZABLE_PROTOCOLS,
|
||||
models::{Swap, TransferType},
|
||||
};
|
||||
|
||||
/// A trait that defines how the tokens will be transferred into the given pool given the solution.
|
||||
pub trait TransferOptimization {
|
||||
/// Returns the transfer method that should be used for the given swap and solution.
|
||||
///
|
||||
/// If the swap is for the in token of the solution and the protocol supports transferring
|
||||
/// straight from the user, it will return `TransferType::Permit2Transfer` or
|
||||
/// `TransferType::TransferFrom`.
|
||||
fn get_transfer_method(&self, swap: Swap, given_token: Bytes, permit2: bool) -> TransferType {
|
||||
let optimize_in_transfer =
|
||||
IN_TRANSFER_OPTIMIZABLE_PROTOCOLS.contains(&swap.component.protocol_system.as_str());
|
||||
if (swap.token_in == given_token) && optimize_in_transfer {
|
||||
if permit2 {
|
||||
TransferType::Permit2Transfer
|
||||
} else {
|
||||
TransferType::TransferFrom
|
||||
}
|
||||
} else {
|
||||
TransferType::Transfer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use alloy_primitives::hex;
|
||||
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
|
||||
|
||||
use super::*;
|
||||
|
||||
struct MockStrategy {}
|
||||
impl TransferOptimization for MockStrategy {}
|
||||
|
||||
fn weth() -> Bytes {
|
||||
Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec())
|
||||
}
|
||||
|
||||
fn dai() -> Bytes {
|
||||
Bytes::from(hex!("6b175474e89094c44da98b954eedeac495271d0f").to_vec())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_swap_transfer_from_permit2() {
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), true);
|
||||
assert_eq!(transfer_method, TransferType::Permit2Transfer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_swap_transfer_from() {
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), weth(), false);
|
||||
assert_eq!(transfer_method, TransferType::TransferFrom);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_first_swap_transfer() {
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: weth(),
|
||||
token_out: dai(),
|
||||
split: 0f64,
|
||||
};
|
||||
let strategy = MockStrategy {};
|
||||
let transfer_method = strategy.get_transfer_method(swap.clone(), dai(), false);
|
||||
assert_eq!(transfer_method, TransferType::Transfer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user