feat: Get native/wrapped addresses from chain

- These were being hardcoded to ETH and WETH, which may not always be the case for EVM-compatible chains.
This commit is contained in:
TAMARA LIPOWSKI
2025-02-04 16:40:32 -05:00
parent 6557ede64b
commit 8cd7d9f76e
4 changed files with 98 additions and 50 deletions

View File

@@ -1,11 +1,11 @@
use std::str::FromStr;
use num_bigint::BigUint;
use tycho_core::Bytes;
use tycho_core::{models::Chain, Bytes};
use crate::encoding::{
errors::EncodingError,
evm::constants::{NATIVE_ADDRESS, WETH_ADDRESS},
evm::constants::{native_address, wrapped_address},
models::{NativeAction, Solution, Transaction},
strategy_encoder::StrategyEncoderRegistry,
tycho_encoder::TychoEncoder,
@@ -14,13 +14,26 @@ use crate::encoding::{
pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
strategy_selector: S,
router_address: Bytes,
native_address: Bytes,
wrapped_address: Bytes,
}
impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
pub fn new(strategy_selector: S, router_address: String) -> Result<Self, EncodingError> {
pub fn new(
strategy_selector: S,
router_address: String,
chain: Chain,
) -> Result<Self, EncodingError> {
let router_address = Bytes::from_str(&router_address)
.map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?;
Ok(EVMTychoEncoder { strategy_selector, router_address })
let native_address = native_address(chain);
let wrapped_address = wrapped_address(chain);
if chain != Chain::Ethereum {
return Err(EncodingError::InvalidInput(
"Currently only Ethereum is supported".to_string(),
));
}
Ok(EVMTychoEncoder { strategy_selector, router_address, native_address, wrapped_address })
}
}
@@ -36,28 +49,30 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
}
if let Some(native_action) = solution.clone().native_action {
if native_action == NativeAction::Wrap {
if solution.given_token != *NATIVE_ADDRESS {
if solution.given_token != self.native_address {
return Err(EncodingError::FatalError(
"ETH must be the input token in order to wrap".to_string(),
"Native token must be the input token in order to wrap".to_string(),
));
}
if let Some(first_swap) = solution.swaps.first() {
if first_swap.token_in != *WETH_ADDRESS {
if first_swap.token_in != self.wrapped_address {
return Err(EncodingError::FatalError(
"WETH must be the first swap's input in order to wrap".to_string(),
"Wrapped token must be the first swap's input in order to wrap"
.to_string(),
));
}
}
} else if native_action == NativeAction::Unwrap {
if solution.checked_token != *NATIVE_ADDRESS {
if solution.checked_token != self.native_address {
return Err(EncodingError::FatalError(
"ETH must be the output token in order to unwrap".to_string(),
"Native token must be the output token in order to unwrap".to_string(),
));
}
if let Some(last_swap) = solution.swaps.last() {
if last_swap.token_out != *WETH_ADDRESS {
if last_swap.token_out != self.wrapped_address {
return Err(EncodingError::FatalError(
"WETH must be the last swap's output in order to unwrap".to_string(),
"Wrapped token must be the last swap's output in order to unwrap"
.to_string(),
));
}
}
@@ -119,6 +134,14 @@ mod tests {
Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
}
fn eth() -> Bytes {
native_address(Chain::Ethereum)
}
fn weth() -> Bytes {
wrapped_address(Chain::Ethereum)
}
impl StrategyEncoderRegistry for MockStrategyRegistry {
fn new(
_chain: Chain,
@@ -162,6 +185,7 @@ mod tests {
EVMTychoEncoder::new(
strategy_selector,
"0x1234567890abcdef1234567890abcdef12345678".to_string(),
Chain::Ethereum,
)
.unwrap()
}
@@ -176,7 +200,7 @@ mod tests {
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: WETH_ADDRESS.clone(),
token_in: weth(),
token_out: dai(),
split: 0f64,
};
@@ -184,7 +208,7 @@ mod tests {
let solution = Solution {
exact_out: false,
given_amount: eth_amount_in.clone(),
given_token: NATIVE_ADDRESS.clone(),
given_token: eth(),
router_address: None,
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
@@ -228,14 +252,14 @@ mod tests {
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: WETH_ADDRESS.clone(),
token_in: weth(),
token_out: dai(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
given_token: NATIVE_ADDRESS.clone(),
given_token: eth(),
checked_token: dai(),
check_amount: None,
swaps: vec![swap],
@@ -257,14 +281,14 @@ mod tests {
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: WETH_ADDRESS.clone(),
token_in: weth(),
token_out: dai(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
given_token: WETH_ADDRESS.clone(),
given_token: weth(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()
@@ -275,7 +299,9 @@ mod tests {
assert!(result.is_err());
assert_eq!(
result.err().unwrap(),
EncodingError::FatalError("ETH must be the input token in order to wrap".to_string())
EncodingError::FatalError(
"Native token must be the input token in order to wrap".to_string()
)
);
}
@@ -288,14 +314,14 @@ mod tests {
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: NATIVE_ADDRESS.clone(),
token_in: eth(),
token_out: dai(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
given_token: NATIVE_ADDRESS.clone(),
given_token: eth(),
swaps: vec![swap],
native_action: Some(NativeAction::Wrap),
..Default::default()
@@ -307,7 +333,7 @@ mod tests {
assert_eq!(
result.err().unwrap(),
EncodingError::FatalError(
"WETH must be the first swap's input in order to wrap".to_string()
"Wrapped token must be the first swap's input in order to wrap".to_string()
)
);
}
@@ -317,7 +343,7 @@ mod tests {
let encoder = get_mocked_tycho_encoder();
let solution = Solution {
exact_out: false,
given_token: NATIVE_ADDRESS.clone(),
given_token: eth(),
swaps: vec![],
native_action: Some(NativeAction::Wrap),
..Default::default()
@@ -342,13 +368,13 @@ mod tests {
..Default::default()
},
token_in: dai(),
token_out: WETH_ADDRESS.clone(),
token_out: weth(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
checked_token: NATIVE_ADDRESS.clone(),
checked_token: eth(),
check_amount: None,
swaps: vec![swap],
native_action: Some(NativeAction::Unwrap),
@@ -370,14 +396,14 @@ mod tests {
..Default::default()
},
token_in: dai(),
token_out: WETH_ADDRESS.clone(),
token_out: weth(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
given_token: dai(),
checked_token: WETH_ADDRESS.clone(),
checked_token: weth(),
swaps: vec![swap],
native_action: Some(NativeAction::Unwrap),
..Default::default()
@@ -389,7 +415,7 @@ mod tests {
assert_eq!(
result.err().unwrap(),
EncodingError::FatalError(
"ETH must be the output token in order to unwrap".to_string()
"Native token must be the output token in order to unwrap".to_string()
)
);
}
@@ -404,13 +430,13 @@ mod tests {
..Default::default()
},
token_in: dai(),
token_out: NATIVE_ADDRESS.clone(),
token_out: eth(),
split: 0f64,
};
let solution = Solution {
exact_out: false,
checked_token: NATIVE_ADDRESS.clone(),
checked_token: eth(),
swaps: vec![swap],
native_action: Some(NativeAction::Unwrap),
..Default::default()
@@ -422,7 +448,7 @@ mod tests {
assert_eq!(
result.err().unwrap(),
EncodingError::FatalError(
"WETH must be the last swap's output in order to unwrap".to_string()
"Wrapped token must be the last swap's output in order to unwrap".to_string()
)
);
}