From 83f1955094693420cd6bf94ff47f145d25ff6624 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 20 Feb 2025 18:24:46 +0530 Subject: [PATCH 1/4] feat: add native and weth addresses for supported networks --- src/encoding/evm/tycho_encoder.rs | 13 +++---------- src/encoding/models.rs | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/encoding/evm/tycho_encoder.rs b/src/encoding/evm/tycho_encoder.rs index 6ffa5ba..e9cfd36 100644 --- a/src/encoding/evm/tycho_encoder.rs +++ b/src/encoding/evm/tycho_encoder.rs @@ -26,16 +26,9 @@ impl EVMTychoEncoder { strategy_encoder: Box, ) -> Result { let chain: Chain = Chain::from(chain); - if chain.name != *"ethereum" { - return Err(EncodingError::InvalidInput( - "Currently only Ethereum is supported".to_string(), - )); - } - Ok(EVMTychoEncoder { - strategy_encoder, - native_address: chain.native_token()?, - wrapped_address: chain.wrapped_token()?, - }) + let native_address = chain.native_token()?; + let wrapped_address = chain.wrapped_token()?; + Ok(EVMTychoEncoder { strategy_encoder, native_address, wrapped_address }) } } diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 6b24030..ed85cca 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -153,22 +153,33 @@ impl From for Chain { } impl Chain { + fn decode_hex(&self, hex_str: &str, err_msg: &str) -> Result { + Ok(Bytes::from( + hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?, + )) + } + pub fn native_token(&self) -> Result { + let decode_err_msg = "Failed to decode native token"; match self.id.id() { - 1 => Ok(Bytes::from(hex::decode("0000000000000000000000000000000000000000").map_err( - |_| EncodingError::FatalError("Failed to decode native token".to_string()), - )?)), + 1 | 8453 | 42161 => { + self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg) + } + 324 => self.decode_hex("000000000000000000000000000000000000800A", 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 { + let decode_err_msg = "Failed to decode wrapped token"; match self.id.id() { - 1 => Ok(Bytes::from(hex::decode("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").map_err( - |_| EncodingError::FatalError("Failed to decode wrapped token".to_string()), - )?)), + 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), _ => Err(EncodingError::InvalidInput(format!( "Wrapped token not set for chain {:?}. Double check the chain is supported.", self.name From dc38f108c3b1c22ffe29221a3d0f8a7a73fe0f20 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 20 Feb 2025 21:46:27 +0530 Subject: [PATCH 2/4] refactor: simplify chain object and move decode_hex to utils --- src/encoding/evm/approvals/permit2.rs | 6 +-- src/encoding/evm/utils.rs | 7 ++++ src/encoding/models.rs | 55 +++++++++------------------ 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index f3b8085..cb1d79d 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -23,7 +23,7 @@ use crate::encoding::{ approvals::protocol_approvals_manager::get_client, utils::{biguint_to_u256, bytes_to_address, encode_input}, }, - models::{Chain, ChainId}, + models::Chain, }; /// Struct for managing Permit2 operations, including encoding approvals and fetching allowance @@ -33,7 +33,7 @@ pub struct Permit2 { address: Address, client: Arc>, signer: PrivateKeySigner, - chain_id: ChainId, + chain_id: u64, runtime_handle: Handle, // Store the runtime to prevent it from being dropped before use. // This is required since tycho-execution does not have a pre-existing runtime. @@ -160,7 +160,7 @@ impl Permit2 { let domain = eip712_domain! { name: "Permit2", - chain_id: self.chain_id.id(), + chain_id: self.chain_id, verifying_contract: self.address, }; let hash = permit_single.eip712_signing_hash(&domain); diff --git a/src/encoding/evm/utils.rs b/src/encoding/evm/utils.rs index a59abff..6a88964 100644 --- a/src/encoding/evm/utils.rs +++ b/src/encoding/evm/utils.rs @@ -120,3 +120,10 @@ pub fn get_static_attribute(swap: &Swap, attribute_name: &str) -> Result })? .to_vec()) } + +/// Decodes a hex string to a `Bytes` object. +pub fn decode_hex(hex_str: &str, err_msg: &str) -> Result { + Ok(Bytes::from( + hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?, + )) +} diff --git a/src/encoding/models.rs b/src/encoding/models.rs index ed85cca..4e1eb23 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,4 +1,3 @@ -use hex; use num_bigint::BigUint; use serde::{Deserialize, Serialize}; use tycho_core::{ @@ -6,6 +5,7 @@ use tycho_core::{ Bytes, }; +use super::evm::utils::decode_hex; use crate::encoding::{ errors::EncodingError, serde_primitives::{biguint_string, biguint_string_option}, @@ -119,53 +119,32 @@ pub struct EncodingContext { pub amount_out_min: BigUint, } -#[derive(Clone, PartialEq, Eq, Hash)] -pub struct ChainId(pub u64); - #[derive(Clone, PartialEq, Eq, Hash)] pub struct Chain { - pub id: ChainId, + pub id: u64, pub name: String, } -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), - TychoCoreChain::Base => ChainId(8453), - } - } -} - impl From for Chain { fn from(chain: TychoCoreChain) -> Self { - Chain { id: chain.into(), name: chain.to_string() } + match chain { + TychoCoreChain::Ethereum => Chain { id: 1, name: chain.to_string() }, + TychoCoreChain::ZkSync => Chain { id: 324, name: chain.to_string() }, + TychoCoreChain::Arbitrum => Chain { id: 42161, name: chain.to_string() }, + TychoCoreChain::Starknet => Chain { id: 0, name: chain.to_string() }, + TychoCoreChain::Base => Chain { id: 8453, name: chain.to_string() }, + } } } impl Chain { - fn decode_hex(&self, hex_str: &str, err_msg: &str) -> Result { - Ok(Bytes::from( - hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?, - )) - } - pub fn native_token(&self) -> Result { let decode_err_msg = "Failed to decode native token"; - match self.id.id() { + match self.id { 1 | 8453 | 42161 => { - self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg) + decode_hex("0000000000000000000000000000000000000000", decode_err_msg) } - 324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg), + 324 => decode_hex("000000000000000000000000000000000000800A", decode_err_msg), _ => Err(EncodingError::InvalidInput(format!( "Native token not set for chain {:?}. Double check the chain is supported.", self.name @@ -175,11 +154,11 @@ impl Chain { pub fn wrapped_token(&self) -> Result { let decode_err_msg = "Failed to decode wrapped token"; - match self.id.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), + match self.id { + 1 => decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg), + 8453 => decode_hex("4200000000000000000000000000000000000006", decode_err_msg), + 324 => decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg), + 42161 => decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg), _ => Err(EncodingError::InvalidInput(format!( "Wrapped token not set for chain {:?}. Double check the chain is supported.", self.name From 7dd59dbe3450ad15b4dc7eb4478b0422c18a6575 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Thu, 20 Feb 2025 21:52:46 +0530 Subject: [PATCH 3/4] fix: add decode_hex to models --- src/encoding/evm/utils.rs | 7 ------- src/encoding/models.rs | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/encoding/evm/utils.rs b/src/encoding/evm/utils.rs index 6a88964..a59abff 100644 --- a/src/encoding/evm/utils.rs +++ b/src/encoding/evm/utils.rs @@ -120,10 +120,3 @@ pub fn get_static_attribute(swap: &Swap, attribute_name: &str) -> Result })? .to_vec()) } - -/// Decodes a hex string to a `Bytes` object. -pub fn decode_hex(hex_str: &str, err_msg: &str) -> Result { - Ok(Bytes::from( - hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?, - )) -} diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 4e1eb23..957cd8f 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -1,3 +1,4 @@ +use hex; use num_bigint::BigUint; use serde::{Deserialize, Serialize}; use tycho_core::{ @@ -5,7 +6,6 @@ use tycho_core::{ Bytes, }; -use super::evm::utils::decode_hex; use crate::encoding::{ errors::EncodingError, serde_primitives::{biguint_string, biguint_string_option}, @@ -138,13 +138,19 @@ impl From for Chain { } impl Chain { + fn decode_hex(&self, hex_str: &str, err_msg: &str) -> Result { + Ok(Bytes::from( + hex::decode(hex_str).map_err(|_| EncodingError::FatalError(err_msg.to_string()))?, + )) + } + pub fn native_token(&self) -> Result { let decode_err_msg = "Failed to decode native token"; match self.id { 1 | 8453 | 42161 => { - decode_hex("0000000000000000000000000000000000000000", decode_err_msg) + self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg) } - 324 => decode_hex("000000000000000000000000000000000000800A", decode_err_msg), + 324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg), _ => Err(EncodingError::InvalidInput(format!( "Native token not set for chain {:?}. Double check the chain is supported.", self.name @@ -155,10 +161,10 @@ impl Chain { pub fn wrapped_token(&self) -> Result { let decode_err_msg = "Failed to decode wrapped token"; match self.id { - 1 => decode_hex("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decode_err_msg), - 8453 => decode_hex("4200000000000000000000000000000000000006", decode_err_msg), - 324 => decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg), - 42161 => decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg), + 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), _ => Err(EncodingError::InvalidInput(format!( "Wrapped token not set for chain {:?}. Double check the chain is supported.", self.name From 9693856dcad428d43ef01b7dc33f6af2febee676 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Thu, 20 Feb 2025 16:40:55 +0000 Subject: [PATCH 4/4] chore(release): 0.46.0 [skip ci] ## [0.46.0](https://github.com/propeller-heads/tycho-execution/compare/0.45.0...0.46.0) (2025-02-20) ### Features * add native and weth addresses for supported networks ([83f1955](https://github.com/propeller-heads/tycho-execution/commit/83f1955094693420cd6bf94ff47f145d25ff6624)) ### Bug Fixes * add decode_hex to models ([7dd59db](https://github.com/propeller-heads/tycho-execution/commit/7dd59dbe3450ad15b4dc7eb4478b0422c18a6575)) --- CHANGELOG.md | 12 ++++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aab6ef..5b07399 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +## [0.46.0](https://github.com/propeller-heads/tycho-execution/compare/0.45.0...0.46.0) (2025-02-20) + + +### Features + +* add native and weth addresses for supported networks ([83f1955](https://github.com/propeller-heads/tycho-execution/commit/83f1955094693420cd6bf94ff47f145d25ff6624)) + + +### Bug Fixes + +* add decode_hex to models ([7dd59db](https://github.com/propeller-heads/tycho-execution/commit/7dd59dbe3450ad15b4dc7eb4478b0422c18a6575)) + ## [0.45.0](https://github.com/propeller-heads/tycho-execution/compare/0.44.0...0.45.0) (2025-02-20) diff --git a/Cargo.lock b/Cargo.lock index 97c94ec..d5a3200 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4340,7 +4340,7 @@ dependencies = [ [[package]] name = "tycho-execution" -version = "0.45.0" +version = "0.46.0" dependencies = [ "alloy", "alloy-primitives", diff --git a/Cargo.toml b/Cargo.toml index a1468f7..18c5fe8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tycho-execution" -version = "0.45.0" +version = "0.46.0" edition = "2021" [[bin]]