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.
This commit is contained in:
TAMARA LIPOWSKI
2025-02-06 11:08:06 -05:00
parent e83b8d9aef
commit 1a07c7dc61
7 changed files with 41 additions and 93 deletions

View File

@@ -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())

View File

@@ -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<HashMap<Chain, Bytes>> =
Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_NATIVE))]));
pub static WRAPPED_ADDRESSES: Lazy<HashMap<Chain, Bytes>> =
Lazy::new(|| HashMap::from([(Chain::Ethereum, Bytes::from(ETH_WRAPPED))]));

View File

@@ -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]

View File

@@ -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 {

View File

@@ -33,8 +33,8 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
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 {

View File

@@ -1,4 +1,3 @@
pub mod constants;
mod errors;
#[cfg(feature = "evm")]
pub mod evm;

View File

@@ -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<TychoCoreChain> for ChainId {
}
}
impl Chain {
pub fn from_tycho_core_chain(
chain: TychoCoreChain,
native_token: Option<Bytes>,
wrapped_token: Option<Bytes>,
) -> Result<Self, EncodingError> {
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<TychoCoreChain> for Chain {
fn from(chain: TychoCoreChain) -> Self {
Chain { id: chain.into(), name: chain.to_string() }
}
}
impl Chain {
pub fn native_token(&self) -> Result<Bytes, EncodingError> {
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<Bytes, EncodingError> {
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
))),
}
}
}