feat: Remove router_address from TychoEncoder

Make the router address mandatory for the Solution attribute instead

--- don't change below this line ---
ENG-4088 Took 30 minutes
This commit is contained in:
Diana Carvalho
2025-02-07 14:58:09 +00:00
parent d7f20aa74f
commit a234ff701f
8 changed files with 42 additions and 93 deletions

View File

@@ -261,11 +261,7 @@ impl SplitSwapStrategyEncoder {
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
impl StrategyEncoder for SplitSwapStrategyEncoder {
fn encode_strategy(
&self,
solution: Solution,
router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
self.validate_split_percentages(&solution.swaps)?;
self.validate_swap_path(
&solution.swaps,
@@ -274,7 +270,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
&solution.native_action,
)?;
let (permit, signature) = self.permit2.get_permit(
&router_address,
&solution.router_address,
&solution.sender,
&solution.given_token,
&solution.given_amount,
@@ -346,9 +342,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
})?;
let encoding_context = EncodingContext {
receiver: router_address.clone(),
receiver: solution.router_address.clone(),
exact_out: solution.exact_out,
router_address: router_address.clone(),
router_address: solution.router_address.clone(),
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
let swap_data = self.encode_swap_header(
@@ -399,7 +395,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
.abi_encode();
let contract_interaction = encode_input(&self.selector, method_calldata);
Ok((contract_interaction, router_address))
Ok((contract_interaction, solution.router_address))
}
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
@@ -429,17 +425,7 @@ impl ExecutorStrategyEncoder {
}
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
impl StrategyEncoder for ExecutorStrategyEncoder {
fn encode_strategy(
&self,
solution: Solution,
_router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
let router_address = solution.router_address.ok_or_else(|| {
EncodingError::InvalidInput(
"Router address is required for straight-to-executor solutions".to_string(),
)
})?;
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
let swap = solution
.swaps
.first()
@@ -457,7 +443,7 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
let encoding_context = EncodingContext {
receiver: solution.receiver,
exact_out: solution.exact_out,
router_address,
router_address: solution.router_address,
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
@@ -540,13 +526,13 @@ mod tests {
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
swaps: vec![swap],
direct_execution: true,
router_address: Some(Bytes::zero(20)),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
slippage: None,
native_action: None,
};
let (protocol_data, executor_address) = encoder
.encode_strategy(solution, Bytes::zero(20))
.encode_strategy(solution)
.unwrap();
let hex_protocol_data = encode(&protocol_data);
assert_eq!(
@@ -631,13 +617,13 @@ mod tests {
check_amount,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
let expected_input = [
@@ -731,14 +717,14 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let hex_calldata = encode(&calldata);
@@ -779,14 +765,14 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Unwrap),
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let hex_calldata = encode(&calldata);
@@ -868,13 +854,13 @@ mod tests {
check_amount: None,
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc],
..Default::default()
};
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
let (calldata, _) = encoder
.encode_strategy(solution, router_address)
.encode_strategy(solution)
.unwrap();
let _hex_calldata = encode(&calldata);

View File

@@ -1,5 +1,3 @@
use std::str::FromStr;
use num_bigint::BigUint;
use tycho_core::Bytes;
@@ -10,31 +8,22 @@ use crate::encoding::{
tycho_encoder::TychoEncoder,
};
/// Represents an encoder for a swap through the given router address using any strategy supported
/// by the strategy registry.
/// Represents an encoder for a swap using any strategy supported by the strategy registry.
///
/// # Fields
/// * `strategy_registry`: S, the strategy registry to use to select the best strategy to encode a
/// solution, based on its supported strategies and the solution attributes.
/// * `router_address`: Bytes, the address of the router to use to execute the swaps.
/// * `native_address`: Address of the chain's native token
/// * `wrapped_address`: Address of the chain's wrapped native token
#[derive(Clone)]
pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
strategy_registry: S,
router_address: Bytes,
native_address: Bytes,
wrapped_address: Bytes,
}
impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
pub fn new(
strategy_registry: S,
router_address: String,
chain: tycho_core::dto::Chain,
) -> Result<Self, EncodingError> {
let router_address = Bytes::from_str(&router_address)
.map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?;
pub fn new(strategy_registry: S, chain: tycho_core::dto::Chain) -> Result<Self, EncodingError> {
let chain: Chain = Chain::from(chain);
if chain.name != *"ethereum" {
return Err(EncodingError::InvalidInput(
@@ -43,7 +32,6 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
}
Ok(EVMTychoEncoder {
strategy_registry,
router_address,
native_address: chain.native_token()?,
wrapped_address: chain.wrapped_token()?,
})
@@ -113,16 +101,11 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
for solution in solutions.iter() {
self.validate_solution(solution)?;
let router_address = solution
.router_address
.clone()
.unwrap_or(self.router_address.clone());
let strategy = self
.strategy_registry
.get_encoder(solution)?;
let (contract_interaction, target_address) =
strategy.encode_strategy(solution.clone(), router_address)?;
strategy.encode_strategy(solution.clone())?;
let value = match solution.native_action.as_ref() {
Some(NativeAction::Wrap) => solution.given_amount.clone(),
@@ -141,6 +124,8 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
#[cfg(test)]
mod tests {
use std::str::FromStr;
use tycho_core::dto::{Chain as TychoCoreChain, ProtocolComponent};
use super::*;
@@ -185,11 +170,7 @@ mod tests {
struct MockStrategy;
impl StrategyEncoder for MockStrategy {
fn encode_strategy(
&self,
_solution: Solution,
_router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError> {
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
Ok((
Bytes::from_str("0x1234")
.unwrap()
@@ -209,12 +190,7 @@ mod tests {
fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategyRegistry> {
let strategy_registry =
MockStrategyRegistry::new(TychoCoreChain::Ethereum, None, None).unwrap();
EVMTychoEncoder::new(
strategy_registry,
"0x1234567890abcdef1234567890abcdef12345678".to_string(),
TychoCoreChain::Ethereum,
)
.unwrap()
EVMTychoEncoder::new(strategy_registry, TychoCoreChain::Ethereum).unwrap()
}
#[test]
@@ -236,7 +212,7 @@ mod tests {
exact_out: false,
given_amount: eth_amount_in.clone(),
given_token: eth(),
router_address: None,
router_address: Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()

View File

@@ -41,8 +41,8 @@ pub struct Solution {
pub check_amount: Option<BigUint>,
/// List of swaps to fulfill the solution.
pub swaps: Vec<Swap>,
/// If not set, then the Tycho Router will be used
pub router_address: Option<Bytes>,
/// Address of the router contract to be used for the swaps.
pub router_address: Bytes,
/// If set, the corresponding native action will be executed.
pub native_action: Option<NativeAction>,
/// If set to true, the solution will be encoded to be sent directly to the Executor and

View File

@@ -4,11 +4,7 @@ use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::Swa
/// Encodes a solution using a specific strategy.
pub trait StrategyEncoder {
fn encode_strategy(
&self,
to_encode: Solution,
router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError>;
fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec<u8>, Bytes), EncodingError>;
#[allow(clippy::borrowed_box)]
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;