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 + ))), + } } }