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.
This commit is contained in:
TAMARA LIPOWSKI
2025-02-05 15:33:20 -05:00
parent f8b3baff55
commit e83b8d9aef
15 changed files with 190 additions and 103 deletions

View File

@@ -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<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
) -> Result<Self, EncodingError> {
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<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
#[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<dyn StrategyEncoder>,
}
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<MockStrategyRegistry> {
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()
}