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:
@@ -24,8 +24,7 @@ fn main() {
|
|||||||
.expect("Failed to create user address");
|
.expect("Failed to create user address");
|
||||||
let executors_file_path = "src/encoding/config/executor_addresses.json";
|
let executors_file_path = "src/encoding/config/executor_addresses.json";
|
||||||
|
|
||||||
let eth_chain = Chain::from_tycho_core_chain(TychoCoreChain::Ethereum, None, None)
|
let eth_chain = Chain::from(TychoCoreChain::Ethereum);
|
||||||
.expect("Failed to create chain.");
|
|
||||||
// Initialize the encoder
|
// Initialize the encoder
|
||||||
let strategy_encoder_registry =
|
let strategy_encoder_registry =
|
||||||
EVMStrategyEncoderRegistry::new(eth_chain.clone(), executors_file_path, signer_pk.clone())
|
EVMStrategyEncoderRegistry::new(eth_chain.clone(), executors_file_path, signer_pk.clone())
|
||||||
|
|||||||
@@ -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))]));
|
|
||||||
@@ -164,6 +164,7 @@ mod tests {
|
|||||||
|
|
||||||
use alloy_primitives::Uint;
|
use alloy_primitives::Uint;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
|
use tycho_core::dto::Chain as TychoCoreChain;
|
||||||
|
|
||||||
use super::*;
|
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 {
|
fn eth_chain() -> Chain {
|
||||||
Chain {
|
TychoCoreChain::Ethereum.into()
|
||||||
id: ChainId(1),
|
|
||||||
name: String::from("ethereum"),
|
|
||||||
native_token: eth(),
|
|
||||||
wrapped_token: weth(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -76,8 +76,8 @@ impl SplitSwapStrategyEncoder {
|
|||||||
permit2: Permit2::new(signer_pk, chain.clone())?,
|
permit2: Permit2::new(signer_pk, chain.clone())?,
|
||||||
selector,
|
selector,
|
||||||
swap_encoder_registry,
|
swap_encoder_registry,
|
||||||
native_address: chain.native_token.clone(),
|
native_address: chain.native_token()?,
|
||||||
wrapped_address: chain.wrapped_token.clone(),
|
wrapped_address: chain.wrapped_token()?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,15 +440,10 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::encoding::models::{ChainId, Swap};
|
use crate::encoding::models::Swap;
|
||||||
|
|
||||||
fn eth_chain() -> Chain {
|
fn eth_chain() -> Chain {
|
||||||
Chain {
|
TychoCoreChain::Ethereum.into()
|
||||||
id: ChainId(1),
|
|
||||||
name: TychoCoreChain::Ethereum.to_string(),
|
|
||||||
native_token: eth(),
|
|
||||||
wrapped_token: weth(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eth() -> Bytes {
|
fn eth() -> Bytes {
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
|
|||||||
Ok(EVMTychoEncoder {
|
Ok(EVMTychoEncoder {
|
||||||
strategy_selector,
|
strategy_selector,
|
||||||
router_address,
|
router_address,
|
||||||
native_address: chain.native_token,
|
native_address: chain.native_token()?,
|
||||||
wrapped_address: chain.wrapped_token,
|
wrapped_address: chain.wrapped_token()?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,9 +125,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::encoding::{
|
use crate::encoding::{
|
||||||
models::{ChainId, Swap},
|
models::Swap, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder,
|
||||||
strategy_encoder::StrategyEncoder,
|
|
||||||
swap_encoder::SwapEncoder,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MockStrategyRegistry {
|
struct MockStrategyRegistry {
|
||||||
@@ -135,12 +133,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn eth_chain() -> Chain {
|
fn eth_chain() -> Chain {
|
||||||
Chain {
|
TychoCoreChain::Ethereum.into()
|
||||||
id: ChainId(1),
|
|
||||||
name: TychoCoreChain::Ethereum.to_string(),
|
|
||||||
native_token: eth(),
|
|
||||||
wrapped_token: weth(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dai() -> Bytes {
|
fn dai() -> Bytes {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
pub mod constants;
|
|
||||||
mod errors;
|
mod errors;
|
||||||
#[cfg(feature = "evm")]
|
#[cfg(feature = "evm")]
|
||||||
pub mod evm;
|
pub mod evm;
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
|
use hex;
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
use tycho_core::{
|
use tycho_core::{
|
||||||
dto::{Chain as TychoCoreChain, ProtocolComponent},
|
dto::{Chain as TychoCoreChain, ProtocolComponent},
|
||||||
Bytes,
|
Bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::encoding::{
|
use crate::encoding::errors::EncodingError;
|
||||||
constants::{NATIVE_ADDRESSES, WRAPPED_ADDRESSES},
|
|
||||||
errors::EncodingError,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, Default, Debug)]
|
#[derive(Clone, Default, Debug)]
|
||||||
pub struct Solution {
|
pub struct Solution {
|
||||||
@@ -84,8 +82,6 @@ pub struct ChainId(pub u64);
|
|||||||
pub struct Chain {
|
pub struct Chain {
|
||||||
pub id: ChainId,
|
pub id: ChainId,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub native_token: Bytes,
|
|
||||||
pub wrapped_token: Bytes,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChainId {
|
impl ChainId {
|
||||||
@@ -105,36 +101,33 @@ impl From<TychoCoreChain> for ChainId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Chain {
|
impl From<TychoCoreChain> for Chain {
|
||||||
pub fn from_tycho_core_chain(
|
fn from(chain: TychoCoreChain) -> Self {
|
||||||
chain: TychoCoreChain,
|
Chain { id: chain.into(), name: chain.to_string() }
|
||||||
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 {
|
impl Chain {
|
||||||
Some(token) => token,
|
pub fn native_token(&self) -> Result<Bytes, EncodingError> {
|
||||||
None => WRAPPED_ADDRESSES.get(&chain)
|
match self.id.id() {
|
||||||
.cloned()
|
1 => Ok(Bytes::from(hex::decode("0000000000000000000000000000000000000000").map_err(
|
||||||
.ok_or_else(|| EncodingError::InvalidInput(format!(
|
|_| EncodingError::FatalError("Failed to decode native token".to_string()),
|
||||||
"Wrapped token does not have a default address for chain {:?}. Please pass the wrapped token address",
|
)?)),
|
||||||
chain
|
_ => Err(EncodingError::InvalidInput(format!(
|
||||||
)))?,
|
"Native token not set for chain {:?}. Double check the chain is supported.",
|
||||||
};
|
self.name
|
||||||
Ok(Chain {
|
))),
|
||||||
id: chain.into(),
|
}
|
||||||
name: chain.to_string(),
|
}
|
||||||
native_token: native_token_address,
|
pub fn wrapped_token(&self) -> Result<Bytes, EncodingError> {
|
||||||
wrapped_token: wrapped_token_address,
|
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
|
||||||
|
))),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user