feat: Use Chain from tycho-core and remove current implementation

--- don't change below this line ---
ENG-4705 Took 29 minutes
This commit is contained in:
Diana Carvalho
2025-07-10 13:11:46 +01:00
parent cb6042ea79
commit 2c25b5a18f
17 changed files with 110 additions and 169 deletions

2
Cargo.lock generated
View File

@@ -4635,7 +4635,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]] [[package]]
name = "tycho-common" name = "tycho-common"
version = "0.75.0" version = "0.75.0"
source = "git+https://github.com/propeller-heads/tycho-indexer.git?rev=a30c046ac7b022f7a816545486af81e724863f07#a30c046ac7b022f7a816545486af81e724863f07" source = "git+https://github.com/propeller-heads/tycho-indexer.git?rev=9c79c8f7a3501e1edd2b86cff43460729b62991f#9c79c8f7a3501e1edd2b86cff43460729b62991f"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

View File

@@ -37,7 +37,7 @@ tokio = { version = "1.38.0", features = ["full"] }
chrono = "0.4.39" chrono = "0.4.39"
clap = { version = "4.5.3", features = ["derive"] } clap = { version = "4.5.3", features = ["derive"] }
once_cell = "1.20.2" once_cell = "1.20.2"
tycho-common = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-common", rev = "a30c046ac7b022f7a816545486af81e724863f07" } tycho-common = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-common", rev = "9c79c8f7a3501e1edd2b86cff43460729b62991f" }
alloy = { version = "1.0.6", features = ["providers", "rpc-types-eth", "eip712", "signer-local", "node-bindings"], optional = true } alloy = { version = "1.0.6", features = ["providers", "rpc-types-eth", "eip712", "signer-local", "node-bindings"], optional = true }

View File

@@ -187,10 +187,10 @@ mod tests {
signers::local::PrivateKeySigner, signers::local::PrivateKeySigner,
}; };
use num_bigint::BigUint; use num_bigint::BigUint;
use tycho_common::models::Chain as TychoCommonChain; use tycho_common::models::Chain;
use super::*; use super::*;
use crate::encoding::{evm::encoding_utils::sign_permit, models::Chain}; use crate::encoding::evm::encoding_utils::sign_permit;
// These two implementations are to avoid comparing the expiration and sig_deadline fields // These two implementations are to avoid comparing the expiration and sig_deadline fields
// because they are timestamps // because they are timestamps
@@ -224,7 +224,7 @@ mod tests {
} }
fn eth_chain() -> Chain { fn eth_chain() -> Chain {
TychoCommonChain::Ethereum.into() Chain::Ethereum
} }
#[test] #[test]
@@ -334,7 +334,7 @@ mod tests {
let sol_permit: PermitSingle = let sol_permit: PermitSingle =
PermitSingle::try_from(&permit).expect("Failed to convert to PermitSingle"); PermitSingle::try_from(&permit).expect("Failed to convert to PermitSingle");
let signature = sign_permit(eth_chain().id, &permit, signer).unwrap(); let signature = sign_permit(eth_chain().id(), &permit, signer).unwrap();
let encoded = let encoded =
(bytes_to_address(&anvil_account).unwrap(), sol_permit, signature.as_bytes().to_vec()) (bytes_to_address(&anvil_account).unwrap(), sol_permit, signature.as_bytes().to_vec())
.abi_encode(); .abi_encode();

View File

@@ -1,7 +1,7 @@
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, str::FromStr};
use alloy::{primitives::B256, signers::local::PrivateKeySigner}; use alloy::{primitives::B256, signers::local::PrivateKeySigner};
use tycho_common::{models::Chain as TychoCommonChain, Bytes}; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -10,7 +10,7 @@ use crate::encoding::{
swap_encoder::swap_encoder_registry::SwapEncoderRegistry, swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
tycho_encoders::{TychoExecutorEncoder, TychoRouterEncoder}, tycho_encoders::{TychoExecutorEncoder, TychoRouterEncoder},
}, },
models::{Chain, UserTransferType}, models::UserTransferType,
tycho_encoder::TychoEncoder, tycho_encoder::TychoEncoder,
}; };
@@ -41,8 +41,8 @@ impl TychoRouterEncoderBuilder {
user_transfer_type: None, user_transfer_type: None,
} }
} }
pub fn chain(mut self, chain: TychoCommonChain) -> Self { pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain.into()); self.chain = Some(chain);
self self
} }
@@ -85,10 +85,10 @@ impl TychoRouterEncoderBuilder {
if let Some(address) = self.router_address { if let Some(address) = self.router_address {
tycho_router_address = address; tycho_router_address = address;
} else { } else {
let default_routers: HashMap<String, Bytes> = let default_routers: HashMap<Chain, Bytes> =
serde_json::from_str(DEFAULT_ROUTERS_JSON)?; serde_json::from_str(DEFAULT_ROUTERS_JSON)?;
tycho_router_address = default_routers tycho_router_address = default_routers
.get(&chain.name) .get(&chain)
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"No default router address found for chain".to_string(), "No default router address found for chain".to_string(),
))? ))?
@@ -96,7 +96,7 @@ impl TychoRouterEncoderBuilder {
} }
let swap_encoder_registry = let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?; SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
let signer = if let Some(pk) = self.swapper_pk { let signer = if let Some(pk) = self.swapper_pk {
let pk = B256::from_str(&pk).map_err(|_| { let pk = B256::from_str(&pk).map_err(|_| {
@@ -141,8 +141,8 @@ impl TychoExecutorEncoderBuilder {
pub fn new() -> Self { pub fn new() -> Self {
TychoExecutorEncoderBuilder { chain: None, executors_file_path: None } TychoExecutorEncoderBuilder { chain: None, executors_file_path: None }
} }
pub fn chain(mut self, chain: TychoCommonChain) -> Self { pub fn chain(mut self, chain: Chain) -> Self {
self.chain = Some(chain.into()); self.chain = Some(chain);
self self
} }
@@ -158,7 +158,7 @@ impl TychoExecutorEncoderBuilder {
pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> { pub fn build(self) -> Result<Box<dyn TychoEncoder>, EncodingError> {
if let Some(chain) = self.chain { if let Some(chain) = self.chain {
let swap_encoder_registry = let swap_encoder_registry =
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain.clone())?; SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?)) Ok(Box::new(TychoExecutorEncoder::new(swap_encoder_registry)?))
} else { } else {
Err(EncodingError::FatalError( Err(EncodingError::FatalError(

View File

@@ -1,7 +1,7 @@
use std::{collections::HashSet, str::FromStr}; use std::{collections::HashSet, str::FromStr};
use alloy::primitives::{aliases::U24, U8}; use alloy::primitives::{aliases::U24, U8};
use tycho_common::Bytes; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -14,7 +14,7 @@ use crate::encoding::{
swap_encoder::swap_encoder_registry::SwapEncoderRegistry, swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
utils::{get_token_position, percentage_to_uint24, ple_encode}, utils::{get_token_position, percentage_to_uint24, ple_encode},
}, },
models::{Chain, EncodedSolution, EncodingContext, NativeAction, Solution, UserTransferType}, models::{EncodedSolution, EncodingContext, NativeAction, Solution, UserTransferType},
strategy_encoder::StrategyEncoder, strategy_encoder::StrategyEncoder,
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };
@@ -52,8 +52,8 @@ impl SingleSwapStrategyEncoder {
swap_encoder_registry, swap_encoder_registry,
router_address: router_address.clone(), router_address: router_address.clone(),
transfer_optimization: TransferOptimization::new( transfer_optimization: TransferOptimization::new(
chain.native_token()?, chain.native_token().address,
chain.wrapped_token()?, chain.wrapped_native_token().address,
user_transfer_type, user_transfer_type,
router_address, router_address,
), ),
@@ -186,16 +186,18 @@ impl SequentialSwapStrategyEncoder {
"sequentialSwap(uint256,address,address,uint256,bool,bool,address,bool,bytes)" "sequentialSwap(uint256,address,address,uint256,bool,bool,address,bool,bytes)"
}.to_string(); }.to_string();
let native_token_address = chain.native_token().address;
let wrapped_token_address = chain.wrapped_native_token().address;
Ok(Self { Ok(Self {
function_signature, function_signature,
swap_encoder_registry, swap_encoder_registry,
router_address: router_address.clone(), router_address: router_address.clone(),
native_address: chain.native_token()?, native_address: native_token_address.clone(),
wrapped_address: chain.wrapped_token()?, wrapped_address: wrapped_token_address.clone(),
sequential_swap_validator: SequentialSwapValidator, sequential_swap_validator: SequentialSwapValidator,
transfer_optimization: TransferOptimization::new( transfer_optimization: TransferOptimization::new(
chain.native_token()?, native_token_address,
chain.wrapped_token()?, wrapped_token_address,
user_transfer_type, user_transfer_type,
router_address, router_address,
), ),
@@ -338,16 +340,18 @@ impl SplitSwapStrategyEncoder {
} else { } else {
"splitSwap(uint256,address,address,uint256,bool,bool,uint256,address,bool,bytes)" "splitSwap(uint256,address,address,uint256,bool,bool,uint256,address,bool,bytes)"
}.to_string(); }.to_string();
let native_token_address = chain.native_token().address;
let wrapped_token_address = chain.wrapped_native_token().address;
Ok(Self { Ok(Self {
function_signature, function_signature,
swap_encoder_registry, swap_encoder_registry,
native_address: chain.native_token()?, native_address: native_token_address.clone(),
wrapped_address: chain.wrapped_token()?, wrapped_address: wrapped_token_address.clone(),
split_swap_validator: SplitSwapValidator, split_swap_validator: SplitSwapValidator,
router_address: router_address.clone(), router_address: router_address.clone(),
transfer_optimization: TransferOptimization::new( transfer_optimization: TransferOptimization::new(
chain.native_token()?, native_token_address,
chain.wrapped_token()?, wrapped_token_address,
user_transfer_type, user_transfer_type,
router_address, router_address,
), ),
@@ -508,7 +512,7 @@ mod tests {
use alloy::{hex::encode, primitives::hex}; use alloy::{hex::encode, primitives::hex};
use num_bigint::{BigInt, BigUint}; use num_bigint::{BigInt, BigUint};
use tycho_common::{ use tycho_common::{
models::{protocol::ProtocolComponent, Chain as TychoCommonChain}, models::{protocol::ProtocolComponent, Chain},
Bytes, Bytes,
}; };
@@ -516,7 +520,7 @@ mod tests {
use crate::encoding::models::Swap; use crate::encoding::models::Swap;
fn eth_chain() -> Chain { fn eth_chain() -> Chain {
TychoCommonChain::Ethereum.into() Chain::Ethereum
} }
fn weth() -> Bytes { fn weth() -> Bytes {

View File

@@ -1,12 +1,13 @@
use std::collections::HashMap; use std::collections::HashMap;
use tycho_common::models::Chain;
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::swap_encoder::swap_encoders::{ evm::swap_encoder::swap_encoders::{
BalancerV2SwapEncoder, BalancerV3SwapEncoder, CurveSwapEncoder, EkuboSwapEncoder, BalancerV2SwapEncoder, BalancerV3SwapEncoder, CurveSwapEncoder, EkuboSwapEncoder,
MaverickV2SwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder, UniswapV4SwapEncoder, MaverickV2SwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder, UniswapV4SwapEncoder,
}, },
models::Chain,
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };

View File

@@ -1,12 +1,13 @@
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs};
use tycho_common::models::Chain;
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::{ evm::{
constants::{DEFAULT_EXECUTORS_JSON, PROTOCOL_SPECIFIC_CONFIG}, constants::{DEFAULT_EXECUTORS_JSON, PROTOCOL_SPECIFIC_CONFIG},
swap_encoder::builder::SwapEncoderBuilder, swap_encoder::builder::SwapEncoderBuilder,
}, },
models::Chain,
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };
@@ -30,15 +31,15 @@ impl SwapEncoderRegistry {
} else { } else {
DEFAULT_EXECUTORS_JSON.to_string() DEFAULT_EXECUTORS_JSON.to_string()
}; };
let config: HashMap<String, HashMap<String, String>> = serde_json::from_str(&config_str)?; let config: HashMap<Chain, HashMap<String, String>> = serde_json::from_str(&config_str)?;
let executors = config let executors = config
.get(&chain.name) .get(&chain)
.ok_or(EncodingError::FatalError("No executors found for chain".to_string()))?; .ok_or(EncodingError::FatalError("No executors found for chain".to_string()))?;
let protocol_specific_config: HashMap<String, HashMap<String, HashMap<String, String>>> = let protocol_specific_config: HashMap<Chain, HashMap<String, HashMap<String, String>>> =
serde_json::from_str(PROTOCOL_SPECIFIC_CONFIG)?; serde_json::from_str(PROTOCOL_SPECIFIC_CONFIG)?;
let protocol_specific_config = protocol_specific_config let protocol_specific_config = protocol_specific_config
.get(&chain.name) .get(&chain)
.ok_or(EncodingError::FatalError( .ok_or(EncodingError::FatalError(
"No protocol specific config found for chain".to_string(), "No protocol specific config found for chain".to_string(),
))?; ))?;
@@ -47,7 +48,7 @@ impl SwapEncoderRegistry {
let builder = SwapEncoderBuilder::new( let builder = SwapEncoderBuilder::new(
protocol, protocol,
executor_address, executor_address,
chain.clone(), chain,
protocol_specific_config protocol_specific_config
.get(protocol) .get(protocol)
.cloned(), .cloned(),

View File

@@ -5,7 +5,7 @@ use alloy::{
sol_types::SolValue, sol_types::SolValue,
}; };
use serde_json::from_str; use serde_json::from_str;
use tycho_common::Bytes; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -13,7 +13,7 @@ use crate::encoding::{
approvals::protocol_approvals_manager::ProtocolApprovalsManager, approvals::protocol_approvals_manager::ProtocolApprovalsManager,
utils::{bytes_to_address, get_static_attribute, pad_to_fixed_size}, utils::{bytes_to_address, get_static_attribute, pad_to_fixed_size},
}, },
models::{Chain, EncodingContext, Swap}, models::{EncodingContext, Swap},
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };
@@ -466,9 +466,9 @@ impl SwapEncoder for CurveSwapEncoder {
.to_string(); .to_string();
Ok(Self { Ok(Self {
executor_address, executor_address,
native_token_address: chain.native_token()?, native_token_address: chain.native_token().address,
native_token_curve_address, native_token_curve_address,
wrapped_native_token_address: chain.wrapped_token()?, wrapped_native_token_address: chain.wrapped_native_token().address,
}) })
} }
@@ -646,7 +646,7 @@ mod tests {
use alloy::hex::encode; use alloy::hex::encode;
use num_bigint::BigInt; use num_bigint::BigInt;
use tycho_common::{ use tycho_common::{
models::{protocol::ProtocolComponent, Chain as TychoCoreChain}, models::{protocol::ProtocolComponent, Chain},
Bytes, Bytes,
}; };
@@ -681,7 +681,7 @@ mod tests {
}; };
let encoder = UniswapV2SwapEncoder::new( let encoder = UniswapV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -741,7 +741,7 @@ mod tests {
}; };
let encoder = UniswapV3SwapEncoder::new( let encoder = UniswapV3SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -803,7 +803,7 @@ mod tests {
}; };
let encoder = BalancerV2SwapEncoder::new( let encoder = BalancerV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
Some(HashMap::from([( Some(HashMap::from([(
"vault_address".to_string(), "vault_address".to_string(),
"0xba12222222228d8ba445958a75a0704d566bf2c8".to_string(), "0xba12222222228d8ba445958a75a0704d566bf2c8".to_string(),
@@ -878,7 +878,7 @@ mod tests {
}; };
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"), String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -951,7 +951,7 @@ mod tests {
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -1046,7 +1046,7 @@ mod tests {
let encoder = UniswapV4SwapEncoder::new( let encoder = UniswapV4SwapEncoder::new(
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"), String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -1130,9 +1130,7 @@ mod tests {
transfer_type: TransferType::Transfer, transfer_type: TransferType::Transfer,
}; };
let encoder = let encoder = EkuboSwapEncoder::new(String::default(), Chain::Ethereum, None).unwrap();
EkuboSwapEncoder::new(String::default(), TychoCoreChain::Ethereum.into(), None)
.unwrap();
let encoded_swap = encoder let encoded_swap = encoder
.encode_swap(&swap, &encoding_context) .encode_swap(&swap, &encoding_context)
@@ -1163,9 +1161,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 = let encoder = EkuboSwapEncoder::new(String::default(), Chain::Ethereum, None).unwrap();
EkuboSwapEncoder::new(String::default(), TychoCoreChain::Ethereum.into(), None)
.unwrap();
let encoding_context = EncodingContext { let encoding_context = EncodingContext {
receiver: RECEIVER.into(), receiver: RECEIVER.into(),
@@ -1329,12 +1325,8 @@ mod tests {
split: 0f64, split: 0f64,
user_data: None, user_data: None,
}; };
let encoder = CurveSwapEncoder::new( let encoder =
String::default(), CurveSwapEncoder::new(String::default(), Chain::Ethereum, curve_config()).unwrap();
TychoCoreChain::Ethereum.into(),
curve_config(),
)
.unwrap();
let (i, j) = encoder let (i, j) = encoder
.get_coin_indexes( .get_coin_indexes(
&swap, &swap,
@@ -1384,7 +1376,7 @@ mod tests {
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
curve_config(), curve_config(),
) )
.unwrap(); .unwrap();
@@ -1456,7 +1448,7 @@ mod tests {
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
curve_config(), curve_config(),
) )
.unwrap(); .unwrap();
@@ -1529,7 +1521,7 @@ mod tests {
}; };
let encoder = CurveSwapEncoder::new( let encoder = CurveSwapEncoder::new(
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"), String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
Some(HashMap::from([ Some(HashMap::from([
( (
"native_token_address".to_string(), "native_token_address".to_string(),
@@ -1603,7 +1595,7 @@ mod tests {
}; };
let encoder = BalancerV3SwapEncoder::new( let encoder = BalancerV3SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();
@@ -1661,7 +1653,7 @@ mod tests {
}; };
let encoder = MaverickV2SwapEncoder::new( let encoder = MaverickV2SwapEncoder::new(
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"), String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
TychoCoreChain::Ethereum.into(), Chain::Ethereum,
None, None,
) )
.unwrap(); .unwrap();

View File

@@ -1,7 +1,7 @@
use std::{collections::HashSet, str::FromStr}; use std::{collections::HashSet, str::FromStr};
use alloy::signers::local::PrivateKeySigner; use alloy::signers::local::PrivateKeySigner;
use tycho_common::Bytes; use tycho_common::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
@@ -16,7 +16,7 @@ use crate::encoding::{
swap_encoder::swap_encoder_registry::SwapEncoderRegistry, swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
}, },
models::{ models::{
Chain, EncodedSolution, EncodingContext, NativeAction, Solution, Transaction, TransferType, EncodedSolution, EncodingContext, NativeAction, Solution, Transaction, TransferType,
UserTransferType, UserTransferType,
}, },
strategy_encoder::StrategyEncoder, strategy_encoder::StrategyEncoder,
@@ -61,19 +61,19 @@ impl TychoRouterEncoder {
}; };
Ok(TychoRouterEncoder { Ok(TychoRouterEncoder {
single_swap_strategy: SingleSwapStrategyEncoder::new( single_swap_strategy: SingleSwapStrategyEncoder::new(
chain.clone(), chain,
swap_encoder_registry.clone(), swap_encoder_registry.clone(),
user_transfer_type.clone(), user_transfer_type.clone(),
router_address.clone(), router_address.clone(),
)?, )?,
sequential_swap_strategy: SequentialSwapStrategyEncoder::new( sequential_swap_strategy: SequentialSwapStrategyEncoder::new(
chain.clone(), chain,
swap_encoder_registry.clone(), swap_encoder_registry.clone(),
user_transfer_type.clone(), user_transfer_type.clone(),
router_address.clone(), router_address.clone(),
)?, )?,
split_swap_strategy: SplitSwapStrategyEncoder::new( split_swap_strategy: SplitSwapStrategyEncoder::new(
chain.clone(), chain,
swap_encoder_registry, swap_encoder_registry,
user_transfer_type.clone(), user_transfer_type.clone(),
router_address.clone(), router_address.clone(),
@@ -153,11 +153,11 @@ impl TychoEncoder for TychoRouterEncoder {
let encoded_solution = self.encode_solution(solution)?; let encoded_solution = self.encode_solution(solution)?;
let transaction = encode_tycho_router_call( let transaction = encode_tycho_router_call(
self.chain.id, self.chain.id(),
encoded_solution, encoded_solution,
solution, solution,
&self.user_transfer_type, &self.user_transfer_type,
&self.chain.native_token()?, &self.chain.native_token().address,
self.signer.clone(), self.signer.clone(),
)?; )?;
@@ -186,8 +186,11 @@ impl TychoEncoder for TychoRouterEncoder {
if solution.swaps.is_empty() { if solution.swaps.is_empty() {
return Err(EncodingError::FatalError("No swaps found in solution".to_string())); return Err(EncodingError::FatalError("No swaps found in solution".to_string()));
} }
let native_address = self.chain.native_token()?; let native_address = self.chain.native_token().address;
let wrapped_address = self.chain.wrapped_token()?; let wrapped_address = self
.chain
.wrapped_native_token()
.address;
if let Some(native_action) = &solution.native_action { if let Some(native_action) = &solution.native_action {
if native_action == &NativeAction::Wrap { if native_action == &NativeAction::Wrap {
if solution.given_token != native_address { if solution.given_token != native_address {
@@ -387,7 +390,7 @@ mod tests {
use std::{collections::HashMap, str::FromStr}; use std::{collections::HashMap, str::FromStr};
use num_bigint::{BigInt, BigUint}; use num_bigint::{BigInt, BigUint};
use tycho_common::models::{protocol::ProtocolComponent, Chain as TychoCommonChain}; use tycho_common::models::{protocol::ProtocolComponent, Chain};
use super::*; use super::*;
use crate::encoding::models::Swap; use crate::encoding::models::Swap;
@@ -466,7 +469,7 @@ mod tests {
} }
fn eth_chain() -> Chain { fn eth_chain() -> Chain {
TychoCommonChain::Ethereum.into() Chain::Ethereum
} }
fn get_swap_encoder_registry() -> SwapEncoderRegistry { fn get_swap_encoder_registry() -> SwapEncoderRegistry {

View File

@@ -1,13 +1,9 @@
use clap::ValueEnum; use clap::ValueEnum;
use hex;
use num_bigint::BigUint; use num_bigint::BigUint;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tycho_common::{ use tycho_common::{models::protocol::ProtocolComponent, Bytes};
models::{protocol::ProtocolComponent, Chain as TychoCommonChain},
Bytes,
};
use crate::encoding::{errors::EncodingError, serde_primitives::biguint_string}; use crate::encoding::serde_primitives::biguint_string;
/// Specifies the method for transferring user funds into Tycho execution. /// Specifies the method for transferring user funds into Tycho execution.
/// ///
@@ -210,63 +206,6 @@ pub enum TransferType {
None = 2, None = 2,
} }
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Chain {
pub id: u64,
pub name: String,
}
impl From<TychoCommonChain> for Chain {
fn from(chain: TychoCommonChain) -> Self {
match chain {
TychoCommonChain::Ethereum => Chain { id: 1, name: chain.to_string() },
TychoCommonChain::ZkSync => Chain { id: 324, name: chain.to_string() },
TychoCommonChain::Arbitrum => Chain { id: 42161, name: chain.to_string() },
TychoCommonChain::Starknet => Chain { id: 0, name: chain.to_string() },
TychoCommonChain::Base => Chain { id: 8453, name: chain.to_string() },
TychoCommonChain::Unichain => Chain { id: 130, name: chain.to_string() },
}
}
}
impl Chain {
fn decode_hex(&self, hex_str: &str, err_msg: &str) -> Result<Bytes, EncodingError> {
Ok(Bytes::from(
hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?,
))
}
pub fn native_token(&self) -> Result<Bytes, EncodingError> {
let decode_err_msg = "Failed to decode native token";
match self.id {
1 | 8453 | 42161 => {
self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg)
}
324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg),
130 => self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg),
_ => Err(EncodingError::InvalidInput(format!(
"Native token not set for chain {:?}. Double check the chain is supported.",
self.name
))),
}
}
pub fn wrapped_token(&self) -> Result<Bytes, EncodingError> {
let decode_err_msg = "Failed to decode wrapped token";
match self.id {
1 => self.decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg),
8453 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
324 => self.decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg),
42161 => self.decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg),
130 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
_ => Err(EncodingError::InvalidInput(format!(
"Wrapped token not set for chain {:?}. Double check the chain is supported.",
self.name
))),
}
}
}
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,8 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use tycho_common::models::Chain;
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
models::{Chain, EncodingContext, Swap}, models::{EncodingContext, Swap},
}; };
/// A trait for protocol-specific swap encoding, where each implementation should handle the /// A trait for protocol-specific swap encoding, where each implementation should handle the

View File

@@ -4,10 +4,9 @@ pub mod encoding;
use std::str::FromStr; use std::str::FromStr;
use alloy::{primitives::B256, signers::local::PrivateKeySigner}; use alloy::{primitives::B256, signers::local::PrivateKeySigner};
use tycho_common::{models::Chain as TychoCommonChain, Bytes}; use tycho_common::{models::Chain, Bytes};
use tycho_execution::encoding::{ use tycho_execution::encoding::{
evm::encoder_builders::TychoRouterEncoderBuilder, evm::encoder_builders::TychoRouterEncoderBuilder, models::UserTransferType,
models::{Chain, UserTransferType},
tycho_encoder::TychoEncoder, tycho_encoder::TychoEncoder,
}; };
@@ -16,7 +15,7 @@ pub fn router_address() -> Bytes {
} }
pub fn eth_chain() -> Chain { pub fn eth_chain() -> Chain {
TychoCommonChain::Ethereum.into() Chain::Ethereum
} }
pub fn eth() -> Bytes { pub fn eth() -> Bytes {

View File

@@ -80,7 +80,7 @@ fn test_uniswap_v3_uniswap_v2() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -167,7 +167,7 @@ fn test_uniswap_v3_uniswap_v3() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -263,7 +263,7 @@ fn test_uniswap_v3_curve() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -334,7 +334,7 @@ fn test_balancer_v2_uniswap_v2() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -490,7 +490,7 @@ fn test_multi_protocol() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -565,7 +565,7 @@ fn test_uniswap_v3_balancer_v3() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,

View File

@@ -63,7 +63,7 @@ fn test_single_encoding_strategy_ekubo() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -115,7 +115,7 @@ fn test_single_encoding_strategy_maverick() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -177,7 +177,7 @@ fn test_single_encoding_strategy_usv4_eth_in() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -244,7 +244,7 @@ fn test_single_encoding_strategy_usv4_eth_out() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -330,7 +330,7 @@ fn test_single_encoding_strategy_usv4_grouped_swap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -440,7 +440,7 @@ fn test_single_encoding_strategy_curve() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -507,7 +507,7 @@ fn test_single_encoding_strategy_curve_st_eth() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -560,7 +560,7 @@ fn test_single_encoding_strategy_balancer_v3() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,

View File

@@ -69,7 +69,7 @@ fn test_sequential_swap_strategy_encoder() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -135,7 +135,7 @@ fn test_sequential_swap_strategy_encoder_no_permit2() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -260,7 +260,7 @@ fn test_sequential_strategy_cyclic_swap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,

View File

@@ -54,7 +54,7 @@ fn test_single_swap_strategy_encoder() {
.unwrap(); .unwrap();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solutions[0].clone(), encoded_solutions[0].clone(),
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -139,7 +139,7 @@ fn test_single_swap_strategy_encoder_no_permit2() {
.unwrap()[0] .unwrap()[0]
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFrom, &UserTransferType::TransferFrom,
@@ -221,7 +221,7 @@ fn test_single_swap_strategy_encoder_no_transfer_in() {
.unwrap()[0] .unwrap()[0]
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::None, &UserTransferType::None,
@@ -305,7 +305,7 @@ fn test_single_swap_strategy_encoder_wrap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -357,7 +357,7 @@ fn test_single_swap_strategy_encoder_unwrap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,

View File

@@ -99,7 +99,7 @@ fn test_split_swap_strategy_encoder() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -212,7 +212,7 @@ fn test_split_input_cyclic_swap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,
@@ -372,7 +372,7 @@ fn test_split_output_cyclic_swap() {
.clone(); .clone();
let calldata = encode_tycho_router_call( let calldata = encode_tycho_router_call(
eth_chain().id, eth_chain().id(),
encoded_solution, encoded_solution,
&solution, &solution,
&UserTransferType::TransferFromPermit2, &UserTransferType::TransferFromPermit2,