refactor: use Bytes instead of String for addresses in encoders

This commit is contained in:
adrian
2025-10-08 12:23:12 +02:00
committed by Adrian Benavides
parent 026315c283
commit ed61a01e8a
6 changed files with 117 additions and 112 deletions

View File

@@ -1,4 +1,4 @@
use std::{collections::HashSet, str::FromStr}; use std::collections::HashSet;
use alloy::primitives::{aliases::U24, U8}; use alloy::primitives::{aliases::U24, U8};
use tycho_common::{models::Chain, Bytes}; use tycho_common::{models::Chain, Bytes};
@@ -149,11 +149,8 @@ impl StrategyEncoder for SingleSwapStrategyEncoder {
} }
} }
let swap_data = self.encode_swap_header( let swap_data =
Bytes::from_str(swap_encoder.executor_address()) self.encode_swap_header(swap_encoder.executor_address().clone(), initial_protocol_data);
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?,
initial_protocol_data,
);
Ok(EncodedSolution { Ok(EncodedSolution {
function_signature: self.function_signature.clone(), function_signature: self.function_signature.clone(),
interacting_with: self.router_address.clone(), interacting_with: self.router_address.clone(),
@@ -321,12 +318,8 @@ impl StrategyEncoder for SequentialSwapStrategyEncoder {
} }
} }
let swap_data = self.encode_swap_header( let swap_data = self
Bytes::from_str(swap_encoder.executor_address()).map_err(|_| { .encode_swap_header(swap_encoder.executor_address().clone(), initial_protocol_data);
EncodingError::FatalError("Invalid executor address".to_string())
})?,
initial_protocol_data,
);
swaps.push(swap_data); swaps.push(swap_data);
} }
@@ -537,9 +530,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
get_token_position(&tokens, &grouped_swap.token_in)?, get_token_position(&tokens, &grouped_swap.token_in)?,
get_token_position(&tokens, &grouped_swap.token_out)?, get_token_position(&tokens, &grouped_swap.token_out)?,
percentage_to_uint24(grouped_swap.split), percentage_to_uint24(grouped_swap.split),
Bytes::from_str(swap_encoder.executor_address()).map_err(|_| { swap_encoder.executor_address().clone(),
EncodingError::FatalError("Invalid executor address".to_string())
})?,
initial_protocol_data, initial_protocol_data,
); );
swaps.push(swap_data); swaps.push(swap_data);

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use tycho_common::models::Chain; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -15,7 +15,7 @@ use crate::encoding::{
/// Builds a `SwapEncoder` for the given protocol system and executor address. /// Builds a `SwapEncoder` for the given protocol system and executor address.
pub struct SwapEncoderBuilder { pub struct SwapEncoderBuilder {
protocol_system: String, protocol_system: String,
executor_address: String, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
} }
@@ -23,13 +23,13 @@ pub struct SwapEncoderBuilder {
impl SwapEncoderBuilder { impl SwapEncoderBuilder {
pub fn new( pub fn new(
protocol_system: &str, protocol_system: &str,
executor_address: &str, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Self { ) -> Self {
SwapEncoderBuilder { SwapEncoderBuilder {
protocol_system: protocol_system.to_string(), protocol_system: protocol_system.to_string(),
executor_address: executor_address.to_string(), executor_address,
chain, chain,
config, config,
} }

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::{collections::HashMap, str::FromStr};
use tycho_common::models::Chain; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -43,7 +43,12 @@ impl SwapEncoderRegistry {
for (protocol, executor_address) in executors { for (protocol, executor_address) in executors {
let builder = SwapEncoderBuilder::new( let builder = SwapEncoderBuilder::new(
protocol, protocol,
executor_address, Bytes::from_str(executor_address).map_err(|_| {
EncodingError::FatalError(format!(
"Invalid executor address for protocol {}",
protocol
))
})?,
chain, chain,
protocol_specific_config protocol_specific_config
.get(protocol) .get(protocol)

View File

@@ -32,7 +32,7 @@ use crate::encoding::{
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct UniswapV2SwapEncoder { pub struct UniswapV2SwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl UniswapV2SwapEncoder { impl UniswapV2SwapEncoder {
@@ -43,7 +43,7 @@ impl UniswapV2SwapEncoder {
impl SwapEncoder for UniswapV2SwapEncoder { impl SwapEncoder for UniswapV2SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -75,7 +75,7 @@ impl SwapEncoder for UniswapV2SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -90,7 +90,7 @@ impl SwapEncoder for UniswapV2SwapEncoder {
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct UniswapV3SwapEncoder { pub struct UniswapV3SwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl UniswapV3SwapEncoder { impl UniswapV3SwapEncoder {
@@ -101,7 +101,7 @@ impl UniswapV3SwapEncoder {
impl SwapEncoder for UniswapV3SwapEncoder { impl SwapEncoder for UniswapV3SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -137,7 +137,7 @@ impl SwapEncoder for UniswapV3SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> { fn clone_box(&self) -> Box<dyn SwapEncoder> {
@@ -151,7 +151,7 @@ impl SwapEncoder for UniswapV3SwapEncoder {
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct UniswapV4SwapEncoder { pub struct UniswapV4SwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl UniswapV4SwapEncoder { impl UniswapV4SwapEncoder {
@@ -162,7 +162,7 @@ impl UniswapV4SwapEncoder {
impl SwapEncoder for UniswapV4SwapEncoder { impl SwapEncoder for UniswapV4SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -235,7 +235,7 @@ impl SwapEncoder for UniswapV4SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -251,13 +251,13 @@ impl SwapEncoder for UniswapV4SwapEncoder {
/// * `vault_address` - The address of the vault contract that will perform the swap. /// * `vault_address` - The address of the vault contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct BalancerV2SwapEncoder { pub struct BalancerV2SwapEncoder {
executor_address: String, executor_address: Bytes,
vault_address: String, vault_address: Bytes,
} }
impl SwapEncoder for BalancerV2SwapEncoder { impl SwapEncoder for BalancerV2SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -266,10 +266,15 @@ impl SwapEncoder for BalancerV2SwapEncoder {
))?; ))?;
let vault_address = config let vault_address = config
.get("vault_address") .get("vault_address")
.map(|s| {
Bytes::from_str(s).map_err(|_| {
EncodingError::FatalError("Invalid balancer v2 vault address".to_string())
})
})
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"Missing balancer v2 vault address in config".to_string(), "Missing balancer v2 vault address in config".to_string(),
))? ))
.to_string(); .flatten()?;
Ok(Self { executor_address, vault_address }) Ok(Self { executor_address, vault_address })
} }
@@ -288,9 +293,7 @@ impl SwapEncoder for BalancerV2SwapEncoder {
approval_needed = token_approvals_manager.approval_needed( approval_needed = token_approvals_manager.approval_needed(
token, token,
tycho_router_address, tycho_router_address,
Address::from_str(&self.vault_address).map_err(|_| { Address::from_slice(&self.vault_address),
EncodingError::FatalError("Invalid vault address".to_string())
})?,
)?; )?;
} }
}; };
@@ -309,7 +312,7 @@ impl SwapEncoder for BalancerV2SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> { fn clone_box(&self) -> Box<dyn SwapEncoder> {
@@ -323,12 +326,12 @@ impl SwapEncoder for BalancerV2SwapEncoder {
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct EkuboSwapEncoder { pub struct EkuboSwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl SwapEncoder for EkuboSwapEncoder { impl SwapEncoder for EkuboSwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -377,7 +380,7 @@ impl SwapEncoder for EkuboSwapEncoder {
Ok(encoded) Ok(encoded)
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -396,8 +399,8 @@ impl SwapEncoder for EkuboSwapEncoder {
/// * `native_token_address` - The address of the native token. /// * `native_token_address` - The address of the native token.
#[derive(Clone)] #[derive(Clone)]
pub struct CurveSwapEncoder { pub struct CurveSwapEncoder {
executor_address: String, executor_address: Bytes,
native_token_curve_address: String, native_token_curve_address: Bytes,
native_token_address: Bytes, native_token_address: Bytes,
wrapped_native_token_address: Bytes, wrapped_native_token_address: Bytes,
} }
@@ -438,10 +441,7 @@ impl CurveSwapEncoder {
// Some curve pools support both ETH and WETH as tokens. // Some curve pools support both ETH and WETH as tokens.
// They do the wrapping/unwrapping inside the pool // They do the wrapping/unwrapping inside the pool
fn normalize_token(&self, token: Address, coins: &[Address]) -> Result<Address, EncodingError> { fn normalize_token(&self, token: Address, coins: &[Address]) -> Result<Address, EncodingError> {
let native_token_address = let native_token_address = Address::from_slice(&self.native_token_curve_address);
Address::from_str(&self.native_token_curve_address).map_err(|_| {
EncodingError::FatalError("Invalid native token curve address".to_string())
})?;
let wrapped_native_token_address = bytes_to_address(&self.wrapped_native_token_address)?; let wrapped_native_token_address = bytes_to_address(&self.wrapped_native_token_address)?;
if token == native_token_address && !coins.contains(&token) { if token == native_token_address && !coins.contains(&token) {
Ok(wrapped_native_token_address) Ok(wrapped_native_token_address)
@@ -482,7 +482,7 @@ impl CurveSwapEncoder {
impl SwapEncoder for CurveSwapEncoder { impl SwapEncoder for CurveSwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -491,10 +491,15 @@ impl SwapEncoder for CurveSwapEncoder {
))?; ))?;
let native_token_curve_address = config let native_token_curve_address = config
.get("native_token_address") .get("native_token_address")
.map(|s| {
Bytes::from_str(s).map_err(|_| {
EncodingError::FatalError("Invalid native token curve address".to_string())
})
})
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"Missing native token curve address in config".to_string(), "Missing native token curve address in config".to_string(),
))? ))
.to_string(); .flatten()?;
Ok(Self { Ok(Self {
executor_address, executor_address,
native_token_address: chain.native_token().address, native_token_address: chain.native_token().address,
@@ -509,10 +514,7 @@ impl SwapEncoder for CurveSwapEncoder {
encoding_context: &EncodingContext, encoding_context: &EncodingContext,
) -> Result<Vec<u8>, EncodingError> { ) -> Result<Vec<u8>, EncodingError> {
let token_approvals_manager = ProtocolApprovalsManager::new()?; let token_approvals_manager = ProtocolApprovalsManager::new()?;
let native_token_curve_address = Address::from_str(&self.native_token_curve_address) let native_token_curve_address = Address::from_slice(&self.native_token_curve_address);
.map_err(|_| {
EncodingError::FatalError("Invalid Curve native token curve address".to_string())
})?;
let token_in = if swap.token_in == self.native_token_address { let token_in = if swap.token_in == self.native_token_address {
native_token_curve_address native_token_curve_address
} else { } else {
@@ -574,7 +576,7 @@ impl SwapEncoder for CurveSwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> { fn clone_box(&self) -> Box<dyn SwapEncoder> {
@@ -588,12 +590,12 @@ impl SwapEncoder for CurveSwapEncoder {
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct MaverickV2SwapEncoder { pub struct MaverickV2SwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl SwapEncoder for MaverickV2SwapEncoder { impl SwapEncoder for MaverickV2SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -617,7 +619,7 @@ impl SwapEncoder for MaverickV2SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> { fn clone_box(&self) -> Box<dyn SwapEncoder> {
@@ -631,12 +633,12 @@ impl SwapEncoder for MaverickV2SwapEncoder {
/// * `executor_address` - The address of the executor contract that will perform the swap. /// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone)] #[derive(Clone)]
pub struct BalancerV3SwapEncoder { pub struct BalancerV3SwapEncoder {
executor_address: String, executor_address: Bytes,
} }
impl SwapEncoder for BalancerV3SwapEncoder { impl SwapEncoder for BalancerV3SwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
_chain: Chain, _chain: Chain,
_config: Option<HashMap<String, String>>, _config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -662,7 +664,7 @@ impl SwapEncoder for BalancerV3SwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -681,8 +683,8 @@ impl SwapEncoder for BalancerV3SwapEncoder {
/// * `settlement_address` - The address of the Bebop settlement contract. /// * `settlement_address` - The address of the Bebop settlement contract.
#[derive(Clone)] #[derive(Clone)]
pub struct BebopSwapEncoder { pub struct BebopSwapEncoder {
executor_address: String, executor_address: Bytes,
settlement_address: String, settlement_address: Bytes,
native_token_bebop_address: Bytes, native_token_bebop_address: Bytes,
native_token_address: Bytes, native_token_address: Bytes,
runtime_handle: Handle, runtime_handle: Handle,
@@ -692,7 +694,7 @@ pub struct BebopSwapEncoder {
impl SwapEncoder for BebopSwapEncoder { impl SwapEncoder for BebopSwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -701,20 +703,26 @@ impl SwapEncoder for BebopSwapEncoder {
))?; ))?;
let settlement_address = config let settlement_address = config
.get("bebop_settlement_address") .get("bebop_settlement_address")
.map(|s| {
Bytes::from_str(s).map_err(|_| {
EncodingError::FatalError("Invalid bebop settlement address".to_string())
})
})
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"Missing bebop settlement address in config".to_string(), "Missing bebop settlement address in config".to_string(),
))? ))
.to_string(); .flatten()?;
let native_token_bebop_address = config let native_token_bebop_address = config
.get("native_token_address") .get("native_token_address")
.map(|s| {
Bytes::from_str(s).map_err(|_| {
EncodingError::FatalError("Invalid native token bebop address".to_string())
})
})
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"Missing native token bebop address in config".to_string(), "Missing native token bebop address in config".to_string(),
))? ))
.to_string(); .flatten()?;
let native_token_bebop_address =
Bytes::from_str(&native_token_bebop_address).map_err(|_| {
EncodingError::FatalError("Invalid Bebop native token address".to_string())
})?;
let (runtime_handle, runtime) = get_runtime()?; let (runtime_handle, runtime) = get_runtime()?;
Ok(Self { Ok(Self {
executor_address, executor_address,
@@ -743,8 +751,10 @@ impl SwapEncoder for BebopSwapEncoder {
false false
} else { } else {
let tycho_router_address = bytes_to_address(&sender)?; let tycho_router_address = bytes_to_address(&sender)?;
let settlement_address = Address::from_str(&self.settlement_address) let settlement_address = Address::from_str(&self.settlement_address.to_string())
.map_err(|_| EncodingError::FatalError("Invalid settlement address".to_string()))?; .map_err(|_| {
EncodingError::FatalError("Invalid bebop settlement address".to_string())
})?;
ProtocolApprovalsManager::new()?.approval_needed( ProtocolApprovalsManager::new()?.approval_needed(
token_in, token_in,
tycho_router_address, tycho_router_address,
@@ -839,7 +849,7 @@ impl SwapEncoder for BebopSwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -850,8 +860,8 @@ impl SwapEncoder for BebopSwapEncoder {
#[derive(Clone)] #[derive(Clone)]
pub struct HashflowSwapEncoder { pub struct HashflowSwapEncoder {
executor_address: String, executor_address: Bytes,
hashflow_router_address: String, hashflow_router_address: Bytes,
native_token_address: Bytes, native_token_address: Bytes,
runtime_handle: Handle, runtime_handle: Handle,
#[allow(dead_code)] #[allow(dead_code)]
@@ -860,7 +870,7 @@ pub struct HashflowSwapEncoder {
impl SwapEncoder for HashflowSwapEncoder { impl SwapEncoder for HashflowSwapEncoder {
fn new( fn new(
executor_address: String, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
@@ -869,10 +879,15 @@ impl SwapEncoder for HashflowSwapEncoder {
))?; ))?;
let hashflow_router_address = config let hashflow_router_address = config
.get("hashflow_router_address") .get("hashflow_router_address")
.map(|s| {
Bytes::from_str(s).map_err(|_| {
EncodingError::FatalError("Invalid hashflow router address".to_string())
})
})
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"Missing hashflow router address in config".to_string(), "Missing hashflow router address in config".to_string(),
))? ))
.to_string(); .flatten()?;
let native_token_address = chain.native_token().address; let native_token_address = chain.native_token().address;
let (runtime_handle, runtime) = get_runtime()?; let (runtime_handle, runtime) = get_runtime()?;
Ok(Self { Ok(Self {
@@ -902,10 +917,7 @@ impl SwapEncoder for HashflowSwapEncoder {
false false
} else { } else {
let tycho_router_address = bytes_to_address(&sender)?; let tycho_router_address = bytes_to_address(&sender)?;
let hashflow_router_address = Address::from_str(&self.hashflow_router_address) let hashflow_router_address = Address::from_slice(&self.hashflow_router_address);
.map_err(|_| {
EncodingError::FatalError("Invalid hashflow router address address".to_string())
})?;
ProtocolApprovalsManager::new()?.approval_needed( ProtocolApprovalsManager::new()?.approval_needed(
bytes_to_address(&swap.token_in)?, bytes_to_address(&swap.token_in)?,
tycho_router_address, tycho_router_address,
@@ -981,7 +993,7 @@ impl SwapEncoder for HashflowSwapEncoder {
Ok(args.abi_encode_packed()) Ok(args.abi_encode_packed())
} }
fn executor_address(&self) -> &str { fn executor_address(&self) -> &Bytes {
&self.executor_address &self.executor_address
} }
@@ -1030,7 +1042,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = UniswapV2SwapEncoder::new( let encoder = UniswapV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1086,7 +1098,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = UniswapV3SwapEncoder::new( let encoder = UniswapV3SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1144,7 +1156,7 @@ mod tests {
historical_trade: true, historical_trade: true,
}; };
let encoder = BalancerV2SwapEncoder::new( let encoder = BalancerV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
Some(HashMap::from([( Some(HashMap::from([(
"vault_address".to_string(), "vault_address".to_string(),
@@ -1214,7 +1226,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"), Bytes::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1284,7 +1296,7 @@ mod tests {
}; };
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1371,7 +1383,7 @@ mod tests {
.build(); .build();
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"), Bytes::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1458,7 +1470,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = EkuboSwapEncoder::new(String::default(), Chain::Ethereum, None).unwrap(); let encoder = EkuboSwapEncoder::new(Bytes::default(), Chain::Ethereum, None).unwrap();
let encoded_swap = encoder let encoded_swap = encoder
.encode_swap(&swap, &encoding_context) .encode_swap(&swap, &encoding_context)
@@ -1489,7 +1501,7 @@ mod tests {
let group_token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT let group_token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT
let intermediary_token = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC let intermediary_token = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
let encoder = EkuboSwapEncoder::new(String::default(), Chain::Ethereum, None).unwrap(); let encoder = EkuboSwapEncoder::new(Bytes::default(), Chain::Ethereum, None).unwrap();
let encoding_context = EncodingContext { let encoding_context = EncodingContext {
receiver: RECEIVER.into(), receiver: RECEIVER.into(),
@@ -1653,7 +1665,7 @@ mod tests {
.build(); .build();
let encoder = let encoder =
CurveSwapEncoder::new(String::default(), Chain::Ethereum, curve_config()).unwrap(); CurveSwapEncoder::new(Bytes::default(), Chain::Ethereum, curve_config()).unwrap();
let (i, j) = encoder let (i, j) = encoder
.get_coin_indexes( .get_coin_indexes(
&swap, &swap,
@@ -1699,7 +1711,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), Bytes::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
Chain::Ethereum, Chain::Ethereum,
curve_config(), curve_config(),
) )
@@ -1766,7 +1778,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), Bytes::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
Chain::Ethereum, Chain::Ethereum,
curve_config(), curve_config(),
) )
@@ -1834,7 +1846,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), Bytes::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
Chain::Ethereum, Chain::Ethereum,
Some(HashMap::from([ Some(HashMap::from([
( (
@@ -1903,7 +1915,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = BalancerV3SwapEncoder::new( let encoder = BalancerV3SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -1956,7 +1968,7 @@ mod tests {
historical_trade: false, historical_trade: false,
}; };
let encoder = MaverickV2SwapEncoder::new( let encoder = MaverickV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
None, None,
) )
@@ -2051,7 +2063,7 @@ mod tests {
}; };
let encoder = BebopSwapEncoder::new( let encoder = BebopSwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
Some(bebop_config()), Some(bebop_config()),
) )
@@ -2126,7 +2138,7 @@ mod tests {
}; };
let encoder = HashflowSwapEncoder::new( let encoder = HashflowSwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
hashflow_config(), hashflow_config(),
) )
@@ -2220,7 +2232,7 @@ mod tests {
}; };
let encoder = HashflowSwapEncoder::new( let encoder = HashflowSwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), Bytes::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
Chain::Ethereum, Chain::Ethereum,
hashflow_config(), hashflow_config(),
) )

View File

@@ -1,4 +1,4 @@
use std::{collections::HashSet, str::FromStr}; use std::collections::HashSet;
use alloy::signers::local::PrivateKeySigner; use alloy::signers::local::PrivateKeySigner;
use tycho_common::{models::Chain, Bytes}; use tycho_common::{models::Chain, Bytes};
@@ -352,12 +352,9 @@ impl TychoExecutorEncoder {
initial_protocol_data.extend(ple_encode(grouped_protocol_data)); initial_protocol_data.extend(ple_encode(grouped_protocol_data));
} }
let executor_address = Bytes::from_str(swap_encoder.executor_address())
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
Ok(EncodedSolution { Ok(EncodedSolution {
swaps: initial_protocol_data, swaps: initial_protocol_data,
interacting_with: executor_address, interacting_with: swap_encoder.executor_address().clone(),
permit: None, permit: None,
function_signature: "".to_string(), function_signature: "".to_string(),
n_tokens: 0, n_tokens: 0,

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use tycho_common::models::Chain; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -18,7 +18,7 @@ pub trait SwapEncoder: Sync + Send {
/// * `config` - Additional configuration parameters for the encoder, like vault or registry /// * `config` - Additional configuration parameters for the encoder, like vault or registry
/// address /// address
fn new( fn new(
executor_address: String, executor_address: Bytes,
chain: Chain, chain: Chain,
config: Option<HashMap<String, String>>, config: Option<HashMap<String, String>>,
) -> Result<Self, EncodingError> ) -> Result<Self, EncodingError>
@@ -41,7 +41,7 @@ pub trait SwapEncoder: Sync + Send {
) -> Result<Vec<u8>, EncodingError>; ) -> Result<Vec<u8>, EncodingError>;
/// Returns the address of the protocol-specific executor contract. /// Returns the address of the protocol-specific executor contract.
fn executor_address(&self) -> &str; fn executor_address(&self) -> &Bytes;
/// Creates a cloned instance of the swap encoder. /// Creates a cloned instance of the swap encoder.
/// ///