From 8cd7d9f76e0b68bbf71f61bc56ab60a5e5693327 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Tue, 4 Feb 2025 16:40:32 -0500 Subject: [PATCH 1/4] feat: Get native/wrapped addresses from chain - These were being hardcoded to ETH and WETH, which may not always be the case for EVM-compatible chains. --- examples/quickstart/main.rs | 2 +- src/encoding/evm/constants.rs | 22 +++-- .../evm/strategy_encoder/strategy_encoders.rs | 36 +++++--- src/encoding/evm/tycho_encoder.rs | 88 ++++++++++++------- 4 files changed, 98 insertions(+), 50 deletions(-) diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index ee89104..f01b8e9 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -25,7 +25,7 @@ fn main() { let strategy_encoder_registry = EVMStrategyEncoderRegistry::new(Chain::Ethereum, executors_file_path, signer_pk.clone()) .expect("Failed to create strategy encoder registry"); - let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address) + let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address, Chain::Ethereum) .expect("Failed to create encoder"); // ------------------- Encode a simple swap ------------------- diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs index f0fa47d..af1c5d9 100644 --- a/src/encoding/evm/constants.rs +++ b/src/encoding/evm/constants.rs @@ -1,10 +1,18 @@ use alloy_primitives::hex; -use lazy_static::lazy_static; -use tycho_core::Bytes; +use tycho_core::{models::Chain, Bytes}; -lazy_static! { - pub static ref NATIVE_ADDRESS: Bytes = - Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()); - pub static ref WETH_ADDRESS: Bytes = - Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec()); +pub fn native_address(chain: Chain) -> Bytes { + match chain { + Chain::Ethereum => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), + // Placeholder values for other chains; update with real addresses + _ => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), + } +} + +pub fn wrapped_address(chain: Chain) -> Bytes { + match chain { + Chain::Ethereum => Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec()), + // Placeholder values for other chains; update with real addresses + _ => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), + } } diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 62a873e..a6f37f6 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -9,7 +9,7 @@ use crate::encoding::{ errors::EncodingError, evm::{ approvals::permit2::Permit2, - constants::WETH_ADDRESS, + constants::wrapped_address, swap_encoder::swap_encoder_registry::SwapEncoderRegistry, utils::{biguint_to_u256, bytes_to_address, encode_input, percentage_to_uint24}, }, @@ -58,6 +58,7 @@ pub struct SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, permit2: Permit2, selector: String, + wrapped_address: Bytes, } impl SplitSwapStrategyEncoder { @@ -67,7 +68,13 @@ impl SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, ) -> Result { let selector = "swap(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string(); - Ok(Self { permit2: Permit2::new(signer_pk, chain)?, selector, swap_encoder_registry }) + let wrapped_address = wrapped_address(chain); + Ok(Self { + permit2: Permit2::new(signer_pk, chain)?, + selector, + swap_encoder_registry, + wrapped_address, + }) } } impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} @@ -126,14 +133,14 @@ impl StrategyEncoder for SplitSwapStrategyEncoder { let mut tokens = Vec::with_capacity(2 + intermediary_tokens.len()); if wrap { - tokens.push(WETH_ADDRESS.clone()); + tokens.push(self.wrapped_address.clone()); } else { tokens.push(solution.given_token.clone()); } tokens.extend(intermediary_tokens); if unwrap { - tokens.push(WETH_ADDRESS.clone()); + tokens.push(self.wrapped_address.clone()); } else { tokens.push(solution.checked_token.clone()); } @@ -278,9 +285,16 @@ mod tests { use super::*; use crate::encoding::{ - evm::constants::{NATIVE_ADDRESS, WETH_ADDRESS}, + evm::constants::{native_address, wrapped_address}, models::Swap, }; + fn eth() -> Bytes { + native_address(Chain::Ethereum) + } + + fn weth() -> Bytes { + wrapped_address(Chain::Ethereum) + } fn get_swap_encoder_registry() -> SwapEncoderRegistry { SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", Chain::Ethereum) @@ -292,7 +306,7 @@ mod tests { let swap_encoder_registry = get_swap_encoder_registry(); let encoder = ExecutorStrategyEncoder::new(swap_encoder_registry); - let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); + let token_in = weth(); let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"); let swap = Swap { @@ -494,7 +508,7 @@ mod tests { protocol_system: "uniswap_v2".to_string(), ..Default::default() }, - token_in: WETH_ADDRESS.clone(), + token_in: weth(), token_out: dai.clone(), split: 0f64, }; @@ -504,7 +518,7 @@ mod tests { .unwrap(); let solution = Solution { exact_out: false, - given_token: NATIVE_ADDRESS.clone(), + given_token: eth(), given_amount: BigUint::from_str("1_000000000000000000").unwrap(), checked_token: dai, expected_amount: Some(BigUint::from_str("3_000_000000000000000000").unwrap()), @@ -544,7 +558,7 @@ mod tests { ..Default::default() }, token_in: dai.clone(), - token_out: WETH_ADDRESS.clone(), + token_out: weth(), split: 0f64, }; let swap_encoder_registry = get_swap_encoder_registry(); @@ -555,7 +569,7 @@ mod tests { exact_out: false, given_token: dai, given_amount: BigUint::from_str("3_000_000000000000000000").unwrap(), - checked_token: NATIVE_ADDRESS.clone(), + checked_token: eth(), expected_amount: Some(BigUint::from_str("1_000000000000000000").unwrap()), check_amount: None, sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(), @@ -590,7 +604,7 @@ mod tests { let private_key = "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string(); - let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap(); + let weth = weth(); let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap(); let wbtc = Bytes::from_str("0x2260fac5e5542a773aa44fbcfedf7c193bc2c599").unwrap(); let usdc = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index 1a93400..e03576b 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -1,11 +1,11 @@ use std::str::FromStr; use num_bigint::BigUint; -use tycho_core::Bytes; +use tycho_core::{models::Chain, Bytes}; use crate::encoding::{ errors::EncodingError, - evm::constants::{NATIVE_ADDRESS, WETH_ADDRESS}, + evm::constants::{native_address, wrapped_address}, models::{NativeAction, Solution, Transaction}, strategy_encoder::StrategyEncoderRegistry, tycho_encoder::TychoEncoder, @@ -14,13 +14,26 @@ use crate::encoding::{ pub struct EVMTychoEncoder { strategy_selector: S, router_address: Bytes, + native_address: Bytes, + wrapped_address: Bytes, } impl EVMTychoEncoder { - pub fn new(strategy_selector: S, router_address: String) -> Result { + pub fn new( + strategy_selector: S, + router_address: String, + chain: Chain, + ) -> Result { let router_address = Bytes::from_str(&router_address) .map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?; - Ok(EVMTychoEncoder { strategy_selector, router_address }) + let native_address = native_address(chain); + let wrapped_address = wrapped_address(chain); + if chain != Chain::Ethereum { + return Err(EncodingError::InvalidInput( + "Currently only Ethereum is supported".to_string(), + )); + } + Ok(EVMTychoEncoder { strategy_selector, router_address, native_address, wrapped_address }) } } @@ -36,28 +49,30 @@ impl EVMTychoEncoder { } if let Some(native_action) = solution.clone().native_action { if native_action == NativeAction::Wrap { - if solution.given_token != *NATIVE_ADDRESS { + if solution.given_token != self.native_address { return Err(EncodingError::FatalError( - "ETH must be the input token in order to wrap".to_string(), + "Native token must be the input token in order to wrap".to_string(), )); } if let Some(first_swap) = solution.swaps.first() { - if first_swap.token_in != *WETH_ADDRESS { + if first_swap.token_in != self.wrapped_address { return Err(EncodingError::FatalError( - "WETH must be the first swap's input in order to wrap".to_string(), + "Wrapped token must be the first swap's input in order to wrap" + .to_string(), )); } } } else if native_action == NativeAction::Unwrap { - if solution.checked_token != *NATIVE_ADDRESS { + if solution.checked_token != self.native_address { return Err(EncodingError::FatalError( - "ETH must be the output token in order to unwrap".to_string(), + "Native token must be the output token in order to unwrap".to_string(), )); } if let Some(last_swap) = solution.swaps.last() { - if last_swap.token_out != *WETH_ADDRESS { + if last_swap.token_out != self.wrapped_address { return Err(EncodingError::FatalError( - "WETH must be the last swap's output in order to unwrap".to_string(), + "Wrapped token must be the last swap's output in order to unwrap" + .to_string(), )); } } @@ -119,6 +134,14 @@ mod tests { Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap() } + fn eth() -> Bytes { + native_address(Chain::Ethereum) + } + + fn weth() -> Bytes { + wrapped_address(Chain::Ethereum) + } + impl StrategyEncoderRegistry for MockStrategyRegistry { fn new( _chain: Chain, @@ -162,6 +185,7 @@ mod tests { EVMTychoEncoder::new( strategy_selector, "0x1234567890abcdef1234567890abcdef12345678".to_string(), + Chain::Ethereum, ) .unwrap() } @@ -176,7 +200,7 @@ mod tests { protocol_system: "uniswap_v2".to_string(), ..Default::default() }, - token_in: WETH_ADDRESS.clone(), + token_in: weth(), token_out: dai(), split: 0f64, }; @@ -184,7 +208,7 @@ mod tests { let solution = Solution { exact_out: false, given_amount: eth_amount_in.clone(), - given_token: NATIVE_ADDRESS.clone(), + given_token: eth(), router_address: None, swaps: vec![swap], native_action: Some(NativeAction::Wrap), @@ -228,14 +252,14 @@ mod tests { protocol_system: "uniswap_v2".to_string(), ..Default::default() }, - token_in: WETH_ADDRESS.clone(), + token_in: weth(), token_out: dai(), split: 0f64, }; let solution = Solution { exact_out: false, - given_token: NATIVE_ADDRESS.clone(), + given_token: eth(), checked_token: dai(), check_amount: None, swaps: vec![swap], @@ -257,14 +281,14 @@ mod tests { protocol_system: "uniswap_v2".to_string(), ..Default::default() }, - token_in: WETH_ADDRESS.clone(), + token_in: weth(), token_out: dai(), split: 0f64, }; let solution = Solution { exact_out: false, - given_token: WETH_ADDRESS.clone(), + given_token: weth(), swaps: vec![swap], native_action: Some(NativeAction::Wrap), ..Default::default() @@ -275,7 +299,9 @@ mod tests { assert!(result.is_err()); assert_eq!( result.err().unwrap(), - EncodingError::FatalError("ETH must be the input token in order to wrap".to_string()) + EncodingError::FatalError( + "Native token must be the input token in order to wrap".to_string() + ) ); } @@ -288,14 +314,14 @@ mod tests { protocol_system: "uniswap_v2".to_string(), ..Default::default() }, - token_in: NATIVE_ADDRESS.clone(), + token_in: eth(), token_out: dai(), split: 0f64, }; let solution = Solution { exact_out: false, - given_token: NATIVE_ADDRESS.clone(), + given_token: eth(), swaps: vec![swap], native_action: Some(NativeAction::Wrap), ..Default::default() @@ -307,7 +333,7 @@ mod tests { assert_eq!( result.err().unwrap(), EncodingError::FatalError( - "WETH must be the first swap's input in order to wrap".to_string() + "Wrapped token must be the first swap's input in order to wrap".to_string() ) ); } @@ -317,7 +343,7 @@ mod tests { let encoder = get_mocked_tycho_encoder(); let solution = Solution { exact_out: false, - given_token: NATIVE_ADDRESS.clone(), + given_token: eth(), swaps: vec![], native_action: Some(NativeAction::Wrap), ..Default::default() @@ -342,13 +368,13 @@ mod tests { ..Default::default() }, token_in: dai(), - token_out: WETH_ADDRESS.clone(), + token_out: weth(), split: 0f64, }; let solution = Solution { exact_out: false, - checked_token: NATIVE_ADDRESS.clone(), + checked_token: eth(), check_amount: None, swaps: vec![swap], native_action: Some(NativeAction::Unwrap), @@ -370,14 +396,14 @@ mod tests { ..Default::default() }, token_in: dai(), - token_out: WETH_ADDRESS.clone(), + token_out: weth(), split: 0f64, }; let solution = Solution { exact_out: false, given_token: dai(), - checked_token: WETH_ADDRESS.clone(), + checked_token: weth(), swaps: vec![swap], native_action: Some(NativeAction::Unwrap), ..Default::default() @@ -389,7 +415,7 @@ mod tests { assert_eq!( result.err().unwrap(), EncodingError::FatalError( - "ETH must be the output token in order to unwrap".to_string() + "Native token must be the output token in order to unwrap".to_string() ) ); } @@ -404,13 +430,13 @@ mod tests { ..Default::default() }, token_in: dai(), - token_out: NATIVE_ADDRESS.clone(), + token_out: eth(), split: 0f64, }; let solution = Solution { exact_out: false, - checked_token: NATIVE_ADDRESS.clone(), + checked_token: eth(), swaps: vec![swap], native_action: Some(NativeAction::Unwrap), ..Default::default() @@ -422,7 +448,7 @@ mod tests { assert_eq!( result.err().unwrap(), EncodingError::FatalError( - "WETH must be the last swap's output in order to unwrap".to_string() + "Wrapped token must be the last swap's output in order to unwrap".to_string() ) ); } From e83b8d9aef130839dd88355e110172e1377bad80 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Wed, 5 Feb 2025 15:33:20 -0500 Subject: [PATCH 2/4] feat: Take Chain object containing native/wrapped addresses - This way this chain object contains everything we need, we don't need to worry about doing any transformation or calling any supplementary functions inside any of the encoders - Needed to move our new Chain object to a higher level since this is used in the higher-level encoder traits. This required some weird default values in the constants in order to avoid using alloy's hex literal. I could have instead opted to make Bytes parse a string I think, though this would mean possibly returning an error at the constants level, which is not nice either. Question: - Do we want the user to be in charge of passing the native and wrapped token every single time? This may be a bit annoying for the user. For now, I have defaulted to those in constants.rs, this would take 5 mins to remove though if you don't like it, and it would get rid of this complicated bytes initialization. --- Cargo.lock | 1 + Cargo.toml | 1 + examples/quickstart/main.rs | 10 +-- src/encoding/constants.rs | 19 +++++ src/encoding/evm/approvals/permit2.rs | 30 ++++++-- src/encoding/evm/constants.rs | 18 ----- src/encoding/evm/mod.rs | 2 - src/encoding/evm/models.rs | 20 ------ .../strategy_encoder_registry.rs | 6 +- .../evm/strategy_encoder/strategy_encoders.rs | 58 ++++++++------- .../evm/swap_encoder/swap_encoder_registry.rs | 8 +-- src/encoding/evm/tycho_encoder.rs | 39 ++++++---- src/encoding/mod.rs | 1 + src/encoding/models.rs | 72 ++++++++++++++++++- src/encoding/strategy_encoder.rs | 8 ++- 15 files changed, 190 insertions(+), 103 deletions(-) create mode 100644 src/encoding/constants.rs delete mode 100644 src/encoding/evm/constants.rs delete mode 100644 src/encoding/evm/models.rs diff --git a/Cargo.lock b/Cargo.lock index 21b6199..c4b3b90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4242,6 +4242,7 @@ dependencies = [ "lazy_static", "num-bigint", "num-traits", + "once_cell", "rstest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 8a363ac..c97d285 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ alloy = { version = "0.9.2", features = ["providers", "rpc-types-eth", "eip712", alloy-sol-types = { version = "0.8.14", optional = true } alloy-primitives = { version = "0.8.9", optional = true } tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.46.0" } +once_cell = "1.20.2" [dev-dependencies] rstest = "0.24.0" diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index a86be5b..db22606 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use num_bigint::BigUint; use tycho_core::{ - dto::{Chain, ProtocolComponent}, + dto::{Chain as TychoCoreChain, ProtocolComponent}, Bytes, }; use tycho_execution::encoding::{ @@ -10,7 +10,7 @@ use tycho_execution::encoding::{ strategy_encoder::strategy_encoder_registry::EVMStrategyEncoderRegistry, tycho_encoder::EVMTychoEncoder, }, - models::{Solution, Swap}, + models::{Chain, Solution, Swap}, strategy_encoder::StrategyEncoderRegistry, tycho_encoder::TychoEncoder, }; @@ -24,11 +24,13 @@ fn main() { .expect("Failed to create user address"); let executors_file_path = "src/encoding/config/executor_addresses.json"; + let eth_chain = Chain::from_tycho_core_chain(TychoCoreChain::Ethereum, None, None) + .expect("Failed to create chain."); // Initialize the encoder let strategy_encoder_registry = - EVMStrategyEncoderRegistry::new(Chain::Ethereum, executors_file_path, signer_pk.clone()) + EVMStrategyEncoderRegistry::new(eth_chain.clone(), executors_file_path, signer_pk.clone()) .expect("Failed to create strategy encoder registry"); - let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address, Chain::Ethereum) + let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address, eth_chain) .expect("Failed to create encoder"); // ------------------- Encode a simple swap ------------------- diff --git a/src/encoding/constants.rs b/src/encoding/constants.rs new file mode 100644 index 0000000..f7f9c13 --- /dev/null +++ b/src/encoding/constants.rs @@ -0,0 +1,19 @@ +use std::collections::HashMap; + +use once_cell::sync::Lazy; +use tycho_core::{dto::Chain, Bytes}; +const ETH_NATIVE: &[u8] = &[ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +]; + +const ETH_WRAPPED: &[u8] = &[ + 0xc0, 0x2a, 0xaa, 0x39, 0xb2, 0x23, 0xfe, 0x8d, 0x0a, 0x0e, 0x5c, 0x4f, 0x27, 0xea, 0xd9, 0x08, + 0x3c, 0x75, 0x6c, 0xc2, +]; + +pub static NATIVE_ADDRESSES: Lazy> = + Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_NATIVE))])); + +pub static WRAPPED_ADDRESSES: Lazy> = + Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_WRAPPED))])); diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index b189d08..7538e18 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -12,15 +12,15 @@ use alloy_sol_types::{eip712_domain, sol, SolStruct, SolValue}; use chrono::Utc; use num_bigint::BigUint; use tokio::runtime::Runtime; -use tycho_core::{dto::Chain, Bytes}; +use tycho_core::Bytes; use crate::encoding::{ errors::EncodingError, evm::{ approvals::protocol_approvals_manager::get_client, - models::ChainId, utils::{biguint_to_u256, bytes_to_address, encode_input}, }, + models::{Chain, ChainId}, }; /// Struct for managing Permit2 operations, including encoding approvals and fetching allowance @@ -60,7 +60,6 @@ sol! { impl Permit2 { pub fn new(signer_pk: String, chain: Chain) -> Result { - let chain_id = ChainId::from(chain); let runtime = Runtime::new() .map_err(|_| EncodingError::FatalError("Failed to create runtime".to_string()))?; let client = runtime.block_on(get_client())?; @@ -76,7 +75,7 @@ impl Permit2 { client, runtime, signer, - chain_id, + chain_id: chain.id, }) } @@ -199,11 +198,28 @@ mod tests { } } + fn eth() -> Bytes { + Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap() + } + + fn weth() -> Bytes { + Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap() + } + + fn eth_chain() -> Chain { + Chain { + id: ChainId(1), + name: String::from("ethereum"), + native_token: eth(), + wrapped_token: weth(), + } + } + #[test] fn test_get_existing_allowance() { let signer_pk = "4c0883a69102937d6231471b5dbb6204fe512961708279feb1be6ae5538da033".to_string(); - let manager = Permit2::new(signer_pk, Chain::Ethereum).unwrap(); + let manager = Permit2::new(signer_pk, eth_chain()).unwrap(); let token = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); let owner = Bytes::from_str("0x2c6a3cd97c6283b95ac8c5a4459ebb0d5fd404f4").unwrap(); @@ -223,7 +239,7 @@ mod tests { // Set up a mock private key for signing let private_key = "4c0883a69102937d6231471b5dbb6204fe512961708279feb1be6ae5538da033".to_string(); - let permit2 = Permit2::new(private_key, Chain::Ethereum).expect("Failed to create Permit2"); + let permit2 = Permit2::new(private_key, eth_chain()).expect("Failed to create Permit2"); let owner = Bytes::from_str("0x2c6a3cd97c6283b95ac8c5a4459ebb0d5fd404f4").unwrap(); let spender = Bytes::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8").unwrap(); @@ -264,7 +280,7 @@ mod tests { "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string(); let permit2 = - Permit2::new(anvil_private_key, Chain::Ethereum).expect("Failed to create Permit2"); + Permit2::new(anvil_private_key, eth_chain()).expect("Failed to create Permit2"); let token = Bytes::from_str("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48").unwrap(); let amount = BigUint::from(1000u64); diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs deleted file mode 100644 index f73f6a5..0000000 --- a/src/encoding/evm/constants.rs +++ /dev/null @@ -1,18 +0,0 @@ -use alloy_primitives::hex; -use tycho_core::{dto::Chain, Bytes}; - -pub fn native_address(chain: Chain) -> Bytes { - match chain { - Chain::Ethereum => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), - // Placeholder values for other chains; update with real addresses - _ => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), - } -} - -pub fn wrapped_address(chain: Chain) -> Bytes { - match chain { - Chain::Ethereum => Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec()), - // Placeholder values for other chains; update with real addresses - _ => Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()), - } -} diff --git a/src/encoding/evm/mod.rs b/src/encoding/evm/mod.rs index 543ed21..9c736bb 100644 --- a/src/encoding/evm/mod.rs +++ b/src/encoding/evm/mod.rs @@ -1,6 +1,4 @@ pub mod approvals; -mod constants; -mod models; pub mod strategy_encoder; mod swap_encoder; pub mod tycho_encoder; diff --git a/src/encoding/evm/models.rs b/src/encoding/evm/models.rs deleted file mode 100644 index 706bc46..0000000 --- a/src/encoding/evm/models.rs +++ /dev/null @@ -1,20 +0,0 @@ -use tycho_core::dto::Chain; - -pub struct ChainId(u64); - -impl ChainId { - pub fn id(&self) -> u64 { - self.0 - } -} - -impl From for ChainId { - fn from(chain: Chain) -> Self { - match chain { - Chain::Ethereum => ChainId(1), - Chain::ZkSync => ChainId(324), - Chain::Arbitrum => ChainId(42161), - Chain::Starknet => ChainId(0), - } - } -} diff --git a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs index 94a8253..ffdbf15 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs @@ -1,14 +1,12 @@ use std::collections::HashMap; -use tycho_core::dto::Chain; - use crate::encoding::{ errors::EncodingError, evm::{ strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder}, swap_encoder::swap_encoder_registry::SwapEncoderRegistry, }, - models::Solution, + models::{Chain, Solution}, strategy_encoder::{StrategyEncoder, StrategyEncoderRegistry}, }; @@ -22,7 +20,7 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry { executors_file_path: &str, signer_pk: Option, ) -> Result { - let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, Chain::Ethereum)?; + let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?; let mut strategies: HashMap> = HashMap::new(); strategies.insert( diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index cfc3f71..dc54e19 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -7,17 +7,16 @@ use std::{ use alloy_primitives::{aliases::U24, FixedBytes, U256, U8}; use alloy_sol_types::SolValue; use num_bigint::BigUint; -use tycho_core::{dto::Chain, keccak256, Bytes}; +use tycho_core::{keccak256, Bytes}; use crate::encoding::{ errors::EncodingError, evm::{ approvals::permit2::Permit2, - constants::{native_address, wrapped_address}, swap_encoder::swap_encoder_registry::SwapEncoderRegistry, utils::{biguint_to_u256, bytes_to_address, encode_input, percentage_to_uint24}, }, - models::{EncodingContext, NativeAction, Solution, Swap}, + models::{Chain, EncodingContext, NativeAction, Solution, Swap}, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder, }; @@ -62,8 +61,8 @@ pub struct SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, permit2: Permit2, selector: String, - wrapped_address: Bytes, native_address: Bytes, + wrapped_address: Bytes, } impl SplitSwapStrategyEncoder { @@ -73,14 +72,12 @@ impl SplitSwapStrategyEncoder { swap_encoder_registry: SwapEncoderRegistry, ) -> Result { let selector = "swap(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string(); - let wrapped_address = wrapped_address(chain); - let native_address = native_address(chain); Ok(Self { - permit2: Permit2::new(signer_pk, chain)?, + permit2: Permit2::new(signer_pk, chain.clone())?, selector, swap_encoder_registry, - wrapped_address, - native_address, + native_address: chain.native_token.clone(), + wrapped_address: chain.wrapped_token.clone(), }) } @@ -434,26 +431,37 @@ mod tests { use std::str::FromStr; use alloy::hex::encode; + use alloy_primitives::hex; use num_bigint::BigUint; use rstest::rstest; - use tycho_core::{dto::ProtocolComponent, Bytes}; + use tycho_core::{ + dto::{Chain as TychoCoreChain, ProtocolComponent}, + Bytes, + }; use super::*; - use crate::encoding::{ - evm::constants::{native_address, wrapped_address}, - models::Swap, - }; + use crate::encoding::models::{ChainId, Swap}; + + fn eth_chain() -> Chain { + Chain { + id: ChainId(1), + name: TychoCoreChain::Ethereum.to_string(), + native_token: eth(), + wrapped_token: weth(), + } + } + fn eth() -> Bytes { - native_address(Chain::Ethereum) + Bytes::from(hex!("0000000000000000000000000000000000000000").to_vec()) } fn weth() -> Bytes { - wrapped_address(Chain::Ethereum) + Bytes::from(hex!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").to_vec()) } fn get_swap_encoder_registry() -> SwapEncoderRegistry { - SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", Chain::Ethereum) - .unwrap() + let eth_chain = eth_chain(); + SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", eth_chain).unwrap() } #[test] @@ -567,8 +575,7 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry) - .unwrap(); + SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); let solution = Solution { exact_out: false, given_token: weth, @@ -669,8 +676,7 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry) - .unwrap(); + SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); let solution = Solution { exact_out: false, given_token: eth(), @@ -718,8 +724,7 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry) - .unwrap(); + SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); let solution = Solution { exact_out: false, given_token: dai, @@ -808,8 +813,7 @@ mod tests { }; let swap_encoder_registry = get_swap_encoder_registry(); let encoder = - SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry) - .unwrap(); + SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap(); let solution = Solution { exact_out: false, given_token: weth, @@ -836,7 +840,7 @@ mod tests { let private_key = "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string(); let swap_encoder_registry = get_swap_encoder_registry(); - SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry).unwrap() + SplitSwapStrategyEncoder::new(private_key, eth_chain(), swap_encoder_registry).unwrap() } #[test] diff --git a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs index b04e580..49c40b0 100644 --- a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs +++ b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs @@ -1,9 +1,7 @@ use std::{collections::HashMap, fs}; -use tycho_core::dto::Chain; - use crate::encoding::{ - errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder, + errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder, models::Chain, swap_encoder::SwapEncoder, }; @@ -15,10 +13,10 @@ pub struct SwapEncoderRegistry { impl SwapEncoderRegistry { pub fn new(executors_file_path: &str, blockchain: Chain) -> Result { let config_str = fs::read_to_string(executors_file_path)?; - let config: HashMap> = serde_json::from_str(&config_str)?; + let config: HashMap> = serde_json::from_str(&config_str)?; let mut encoders = HashMap::new(); let executors = config - .get(&blockchain) + .get(&blockchain.name) .ok_or(EncodingError::FatalError("No executors found for blockchain".to_string()))?; for (protocol, executor_address) in executors { let builder = SwapEncoderBuilder::new(protocol, executor_address); diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index a29779a..f5ea521 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -1,12 +1,11 @@ use std::str::FromStr; use num_bigint::BigUint; -use tycho_core::{dto::Chain, Bytes}; +use tycho_core::Bytes; use crate::encoding::{ errors::EncodingError, - evm::constants::{native_address, wrapped_address}, - models::{NativeAction, Solution, Transaction}, + models::{Chain, NativeAction, Solution, Transaction}, strategy_encoder::StrategyEncoderRegistry, tycho_encoder::TychoEncoder, }; @@ -26,14 +25,17 @@ impl EVMTychoEncoder { ) -> Result { let router_address = Bytes::from_str(&router_address) .map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?; - let native_address = native_address(chain); - let wrapped_address = wrapped_address(chain); - if chain != Chain::Ethereum { + if chain.name != *"ethereum" { return Err(EncodingError::InvalidInput( "Currently only Ethereum is supported".to_string(), )); } - Ok(EVMTychoEncoder { strategy_selector, router_address, native_address, wrapped_address }) + Ok(EVMTychoEncoder { + strategy_selector, + router_address, + native_address: chain.native_token, + wrapped_address: chain.wrapped_token, + }) } } @@ -119,27 +121,38 @@ impl TychoEncoder for EVMTychoEncoder { #[cfg(test)] mod tests { - use tycho_core::dto::{Chain, ProtocolComponent}; + use tycho_core::dto::{Chain as TychoCoreChain, ProtocolComponent}; use super::*; use crate::encoding::{ - models::Swap, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder, + models::{ChainId, Swap}, + strategy_encoder::StrategyEncoder, + swap_encoder::SwapEncoder, }; struct MockStrategyRegistry { strategy: Box, } + fn eth_chain() -> Chain { + Chain { + id: ChainId(1), + name: TychoCoreChain::Ethereum.to_string(), + native_token: eth(), + wrapped_token: weth(), + } + } + fn dai() -> Bytes { Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap() } fn eth() -> Bytes { - native_address(Chain::Ethereum) + Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap() } fn weth() -> Bytes { - wrapped_address(Chain::Ethereum) + Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap() } impl StrategyEncoderRegistry for MockStrategyRegistry { @@ -181,11 +194,11 @@ mod tests { } fn get_mocked_tycho_encoder() -> EVMTychoEncoder { - let strategy_selector = MockStrategyRegistry::new(Chain::Ethereum, "", None).unwrap(); + let strategy_selector = MockStrategyRegistry::new(eth_chain(), "", None).unwrap(); EVMTychoEncoder::new( strategy_selector, "0x1234567890abcdef1234567890abcdef12345678".to_string(), - Chain::Ethereum, + eth_chain(), ) .unwrap() } diff --git a/src/encoding/mod.rs b/src/encoding/mod.rs index 502e336..d998904 100644 --- a/src/encoding/mod.rs +++ b/src/encoding/mod.rs @@ -1,3 +1,4 @@ +pub mod constants; mod errors; #[cfg(feature = "evm")] pub mod evm; diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 24c7c9d..c6250a2 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,5 +1,13 @@ use num_bigint::BigUint; -use tycho_core::{dto::ProtocolComponent, Bytes}; +use tycho_core::{ + dto::{Chain as TychoCoreChain, ProtocolComponent}, + Bytes, +}; + +use crate::encoding::{ + constants::{NATIVE_ADDRESSES, WRAPPED_ADDRESSES}, + errors::EncodingError, +}; #[derive(Clone, Default, Debug)] pub struct Solution { @@ -68,3 +76,65 @@ pub struct EncodingContext { pub exact_out: bool, pub router_address: Bytes, } + +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct ChainId(pub u64); + +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct Chain { + pub id: ChainId, + pub name: String, + pub native_token: Bytes, + pub wrapped_token: Bytes, +} + +impl ChainId { + pub fn id(&self) -> u64 { + self.0 + } +} + +impl From for ChainId { + fn from(chain: TychoCoreChain) -> Self { + match chain { + TychoCoreChain::Ethereum => ChainId(1), + TychoCoreChain::ZkSync => ChainId(324), + TychoCoreChain::Arbitrum => ChainId(42161), + TychoCoreChain::Starknet => ChainId(0), + } + } +} + +impl Chain { + pub fn from_tycho_core_chain( + chain: TychoCoreChain, + native_token: Option, + wrapped_token: Option, + ) -> Result { + let native_token_address = match native_token { + Some(token) => token, + None => NATIVE_ADDRESSES.get(&chain) + .cloned() + .ok_or_else(|| EncodingError::InvalidInput(format!( + "Native token does not have a default address for chain {:?}. Please pass the native token address", + chain + )))?, + }; + + let wrapped_token_address = match wrapped_token { + Some(token) => token, + None => WRAPPED_ADDRESSES.get(&chain) + .cloned() + .ok_or_else(|| EncodingError::InvalidInput(format!( + "Wrapped token does not have a default address for chain {:?}. Please pass the wrapped token address", + chain + )))?, + }; + Ok(Chain { + id: chain.into(), + name: chain.to_string(), + native_token: native_token_address, + wrapped_token: wrapped_token_address, + }) + } +} diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index 1b5013b..64b8e9e 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,6 +1,10 @@ -use tycho_core::{dto::Chain, Bytes}; +use tycho_core::Bytes; -use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder}; +use crate::encoding::{ + errors::EncodingError, + models::{Chain, Solution}, + swap_encoder::SwapEncoder, +}; pub trait StrategyEncoder { fn encode_strategy( From 1a07c7dc61ff7f86739ba7fbde2e7a42ebdf284f Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Thu, 6 Feb 2025 11:08:06 -0500 Subject: [PATCH 3/4] Fix: Do not let user specify the native/wrapped token This puts too much burden on the user. Instead, specify native and wrapped tokens as methods which return hardcoded values. --- examples/quickstart/main.rs | 3 +- src/encoding/constants.rs | 19 ------ src/encoding/evm/approvals/permit2.rs | 16 +---- .../evm/strategy_encoder/strategy_encoders.rs | 13 ++-- src/encoding/evm/tycho_encoder.rs | 15 ++--- src/encoding/mod.rs | 1 - src/encoding/models.rs | 67 +++++++++---------- 7 files changed, 41 insertions(+), 93 deletions(-) delete mode 100644 src/encoding/constants.rs diff --git a/examples/quickstart/main.rs b/examples/quickstart/main.rs index db22606..f63cd19 100644 --- a/examples/quickstart/main.rs +++ b/examples/quickstart/main.rs @@ -24,8 +24,7 @@ fn main() { .expect("Failed to create user address"); let executors_file_path = "src/encoding/config/executor_addresses.json"; - let eth_chain = Chain::from_tycho_core_chain(TychoCoreChain::Ethereum, None, None) - .expect("Failed to create chain."); + let eth_chain = Chain::from(TychoCoreChain::Ethereum); // Initialize the encoder let strategy_encoder_registry = EVMStrategyEncoderRegistry::new(eth_chain.clone(), executors_file_path, signer_pk.clone()) diff --git a/src/encoding/constants.rs b/src/encoding/constants.rs deleted file mode 100644 index f7f9c13..0000000 --- a/src/encoding/constants.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::collections::HashMap; - -use once_cell::sync::Lazy; -use tycho_core::{dto::Chain, Bytes}; -const ETH_NATIVE: &[u8] = &[ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, -]; - -const ETH_WRAPPED: &[u8] = &[ - 0xc0, 0x2a, 0xaa, 0x39, 0xb2, 0x23, 0xfe, 0x8d, 0x0a, 0x0e, 0x5c, 0x4f, 0x27, 0xea, 0xd9, 0x08, - 0x3c, 0x75, 0x6c, 0xc2, -]; - -pub static NATIVE_ADDRESSES: Lazy> = - Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_NATIVE))])); - -pub static WRAPPED_ADDRESSES: Lazy> = - Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_WRAPPED))])); diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index 7538e18..c118358 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -164,6 +164,7 @@ mod tests { use alloy_primitives::Uint; use num_bigint::BigUint; + use tycho_core::dto::Chain as TychoCoreChain; use super::*; @@ -198,21 +199,8 @@ mod tests { } } - fn eth() -> Bytes { - Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap() - } - - fn weth() -> Bytes { - Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap() - } - fn eth_chain() -> Chain { - Chain { - id: ChainId(1), - name: String::from("ethereum"), - native_token: eth(), - wrapped_token: weth(), - } + TychoCoreChain::Ethereum.into() } #[test] diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index dc54e19..21aeee1 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -76,8 +76,8 @@ impl SplitSwapStrategyEncoder { permit2: Permit2::new(signer_pk, chain.clone())?, selector, swap_encoder_registry, - native_address: chain.native_token.clone(), - wrapped_address: chain.wrapped_token.clone(), + native_address: chain.native_token()?, + wrapped_address: chain.wrapped_token()?, }) } @@ -440,15 +440,10 @@ mod tests { }; use super::*; - use crate::encoding::models::{ChainId, Swap}; + use crate::encoding::models::Swap; fn eth_chain() -> Chain { - Chain { - id: ChainId(1), - name: TychoCoreChain::Ethereum.to_string(), - native_token: eth(), - wrapped_token: weth(), - } + TychoCoreChain::Ethereum.into() } fn eth() -> Bytes { diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index f5ea521..54c0f7a 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -33,8 +33,8 @@ impl EVMTychoEncoder { Ok(EVMTychoEncoder { strategy_selector, router_address, - native_address: chain.native_token, - wrapped_address: chain.wrapped_token, + native_address: chain.native_token()?, + wrapped_address: chain.wrapped_token()?, }) } } @@ -125,9 +125,7 @@ mod tests { use super::*; use crate::encoding::{ - models::{ChainId, Swap}, - strategy_encoder::StrategyEncoder, - swap_encoder::SwapEncoder, + models::Swap, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder, }; struct MockStrategyRegistry { @@ -135,12 +133,7 @@ mod tests { } fn eth_chain() -> Chain { - Chain { - id: ChainId(1), - name: TychoCoreChain::Ethereum.to_string(), - native_token: eth(), - wrapped_token: weth(), - } + TychoCoreChain::Ethereum.into() } fn dai() -> Bytes { diff --git a/src/encoding/mod.rs b/src/encoding/mod.rs index d998904..502e336 100644 --- a/src/encoding/mod.rs +++ b/src/encoding/mod.rs @@ -1,4 +1,3 @@ -pub mod constants; mod errors; #[cfg(feature = "evm")] pub mod evm; diff --git a/src/encoding/models.rs b/src/encoding/models.rs index c6250a2..77e115a 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,13 +1,11 @@ +use hex; use num_bigint::BigUint; use tycho_core::{ dto::{Chain as TychoCoreChain, ProtocolComponent}, Bytes, }; -use crate::encoding::{ - constants::{NATIVE_ADDRESSES, WRAPPED_ADDRESSES}, - errors::EncodingError, -}; +use crate::encoding::errors::EncodingError; #[derive(Clone, Default, Debug)] pub struct Solution { @@ -84,8 +82,6 @@ pub struct ChainId(pub u64); pub struct Chain { pub id: ChainId, pub name: String, - pub native_token: Bytes, - pub wrapped_token: Bytes, } impl ChainId { @@ -105,36 +101,33 @@ impl From for ChainId { } } -impl Chain { - pub fn from_tycho_core_chain( - chain: TychoCoreChain, - native_token: Option, - wrapped_token: Option, - ) -> Result { - let native_token_address = match native_token { - Some(token) => token, - None => NATIVE_ADDRESSES.get(&chain) - .cloned() - .ok_or_else(|| EncodingError::InvalidInput(format!( - "Native token does not have a default address for chain {:?}. Please pass the native token address", - chain - )))?, - }; - - let wrapped_token_address = match wrapped_token { - Some(token) => token, - None => WRAPPED_ADDRESSES.get(&chain) - .cloned() - .ok_or_else(|| EncodingError::InvalidInput(format!( - "Wrapped token does not have a default address for chain {:?}. Please pass the wrapped token address", - chain - )))?, - }; - Ok(Chain { - id: chain.into(), - name: chain.to_string(), - native_token: native_token_address, - wrapped_token: wrapped_token_address, - }) +impl From for Chain { + fn from(chain: TychoCoreChain) -> Self { + Chain { id: chain.into(), name: chain.to_string() } + } +} + +impl Chain { + pub fn native_token(&self) -> Result { + match self.id.id() { + 1 => Ok(Bytes::from(hex::decode("0000000000000000000000000000000000000000").map_err( + |_| EncodingError::FatalError("Failed to decode native token".to_string()), + )?)), + _ => Err(EncodingError::InvalidInput(format!( + "Native token not set for chain {:?}. Double check the chain is supported.", + self.name + ))), + } + } + pub fn wrapped_token(&self) -> Result { + match self.id.id() { + 1 => Ok(Bytes::from(hex::decode("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").map_err( + |_| EncodingError::FatalError("Failed to decode wrapped token".to_string()), + )?)), + _ => Err(EncodingError::InvalidInput(format!( + "Wrapped token not set for chain {:?}. Double check the chain is supported.", + self.name + ))), + } } } From 54ef671683008bee7c85bcae656718d41a80efc2 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 6 Feb 2025 16:43:48 +0000 Subject: [PATCH 4/4] chore(release): 0.33.0 [skip ci] ## [0.33.0](https://github.com/propeller-heads/tycho-execution/compare/0.32.0...0.33.0) (2025-02-06) ### Features * Get native/wrapped addresses from chain ([8cd7d9f](https://github.com/propeller-heads/tycho-execution/commit/8cd7d9f76e0b68bbf71f61bc56ab60a5e5693327)) * Take Chain object containing native/wrapped addresses ([e83b8d9](https://github.com/propeller-heads/tycho-execution/commit/e83b8d9aef130839dd88355e110172e1377bad80)) ### Bug Fixes * Do not let user specify the native/wrapped token ([1a07c7d](https://github.com/propeller-heads/tycho-execution/commit/1a07c7dc61ff7f86739ba7fbde2e7a42ebdf284f)) --- CHANGELOG.md | 13 +++++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23e9154..874b976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## [0.33.0](https://github.com/propeller-heads/tycho-execution/compare/0.32.0...0.33.0) (2025-02-06) + + +### Features + +* Get native/wrapped addresses from chain ([8cd7d9f](https://github.com/propeller-heads/tycho-execution/commit/8cd7d9f76e0b68bbf71f61bc56ab60a5e5693327)) +* Take Chain object containing native/wrapped addresses ([e83b8d9](https://github.com/propeller-heads/tycho-execution/commit/e83b8d9aef130839dd88355e110172e1377bad80)) + + +### Bug Fixes + +* Do not let user specify the native/wrapped token ([1a07c7d](https://github.com/propeller-heads/tycho-execution/commit/1a07c7dc61ff7f86739ba7fbde2e7a42ebdf284f)) + ## [0.32.0](https://github.com/propeller-heads/tycho-execution/compare/0.31.0...0.32.0) (2025-02-06) diff --git a/Cargo.lock b/Cargo.lock index 510642d..829efb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4231,7 +4231,7 @@ dependencies = [ [[package]] name = "tycho-execution" -version = "0.32.0" +version = "0.33.0" dependencies = [ "alloy", "alloy-primitives", diff --git a/Cargo.toml b/Cargo.toml index 9b5ab1f..9db5661 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tycho-execution" -version = "0.32.0" +version = "0.33.0" edition = "2021" [dependencies]