feat: Remove router_address from TychoEncoder
Make the router address mandatory for the Solution attribute instead --- don't change below this line --- ENG-4088 Took 30 minutes
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
pub use clap::Parser;
|
||||
pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206a0E7";
|
||||
|
||||
#[derive(Parser)]
|
||||
/// Encode swap transactions for the Tycho router
|
||||
@@ -35,10 +34,6 @@ pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206
|
||||
/// }
|
||||
/// ```
|
||||
pub struct Cli {
|
||||
/// Router contract address to use for encoding transactions
|
||||
#[arg(default_value = DEFAULT_ROUTER_ADDRESS)]
|
||||
pub router_address: String,
|
||||
|
||||
/// Private key for signing approvals (required when direct_execution is false)
|
||||
#[arg(short)]
|
||||
pub private_key: Option<String>,
|
||||
|
||||
@@ -33,7 +33,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
// Encode the solution
|
||||
let encoded = encode_swaps(&buffer, &cli.router_address, cli.config_path, cli.private_key)?;
|
||||
let encoded = encode_swaps(&buffer, cli.config_path, cli.private_key)?;
|
||||
|
||||
// Output the encoded result as JSON to stdout
|
||||
println!(
|
||||
@@ -47,7 +47,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
fn encode_swaps(
|
||||
input: &str,
|
||||
router_address: &str,
|
||||
config_path: Option<String>,
|
||||
private_key: Option<String>,
|
||||
) -> Result<Value, Box<dyn std::error::Error>> {
|
||||
@@ -55,7 +54,7 @@ fn encode_swaps(
|
||||
let chain = Chain::Ethereum;
|
||||
|
||||
let strategy_selector = EVMStrategyEncoderRegistry::new(chain, config_path, private_key)?;
|
||||
let encoder = EVMTychoEncoder::new(strategy_selector, router_address.to_string(), chain)?;
|
||||
let encoder = EVMTychoEncoder::new(strategy_selector, chain)?;
|
||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||
|
||||
Ok(serde_json::json!({
|
||||
|
||||
@@ -261,11 +261,7 @@ impl SplitSwapStrategyEncoder {
|
||||
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
|
||||
|
||||
impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
fn encode_strategy(
|
||||
&self,
|
||||
solution: Solution,
|
||||
router_address: Bytes,
|
||||
) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
self.validate_split_percentages(&solution.swaps)?;
|
||||
self.validate_swap_path(
|
||||
&solution.swaps,
|
||||
@@ -274,7 +270,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
&solution.native_action,
|
||||
)?;
|
||||
let (permit, signature) = self.permit2.get_permit(
|
||||
&router_address,
|
||||
&solution.router_address,
|
||||
&solution.sender,
|
||||
&solution.given_token,
|
||||
&solution.given_amount,
|
||||
@@ -346,9 +342,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
})?;
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: router_address.clone(),
|
||||
receiver: solution.router_address.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: router_address.clone(),
|
||||
router_address: solution.router_address.clone(),
|
||||
};
|
||||
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
|
||||
let swap_data = self.encode_swap_header(
|
||||
@@ -399,7 +395,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
.abi_encode();
|
||||
|
||||
let contract_interaction = encode_input(&self.selector, method_calldata);
|
||||
Ok((contract_interaction, router_address))
|
||||
Ok((contract_interaction, solution.router_address))
|
||||
}
|
||||
|
||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||
@@ -429,17 +425,7 @@ impl ExecutorStrategyEncoder {
|
||||
}
|
||||
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
|
||||
impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||
fn encode_strategy(
|
||||
&self,
|
||||
solution: Solution,
|
||||
_router_address: Bytes,
|
||||
) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
let router_address = solution.router_address.ok_or_else(|| {
|
||||
EncodingError::InvalidInput(
|
||||
"Router address is required for straight-to-executor solutions".to_string(),
|
||||
)
|
||||
})?;
|
||||
|
||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
let swap = solution
|
||||
.swaps
|
||||
.first()
|
||||
@@ -457,7 +443,7 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: solution.receiver,
|
||||
exact_out: solution.exact_out,
|
||||
router_address,
|
||||
router_address: solution.router_address,
|
||||
};
|
||||
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
|
||||
|
||||
@@ -540,13 +526,13 @@ mod tests {
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap],
|
||||
direct_execution: true,
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
};
|
||||
|
||||
let (protocol_data, executor_address) = encoder
|
||||
.encode_strategy(solution, Bytes::zero(20))
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
let hex_protocol_data = encode(&protocol_data);
|
||||
assert_eq!(
|
||||
@@ -631,13 +617,13 @@ mod tests {
|
||||
check_amount,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
..Default::default()
|
||||
};
|
||||
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution, router_address)
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
|
||||
let expected_input = [
|
||||
@@ -731,14 +717,14 @@ mod tests {
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
};
|
||||
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution, router_address)
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let hex_calldata = encode(&calldata);
|
||||
@@ -779,14 +765,14 @@ mod tests {
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Unwrap),
|
||||
..Default::default()
|
||||
};
|
||||
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution, router_address)
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let hex_calldata = encode(&calldata);
|
||||
@@ -868,13 +854,13 @@ mod tests {
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc],
|
||||
..Default::default()
|
||||
};
|
||||
let router_address = Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap();
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution, router_address)
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let _hex_calldata = encode(&calldata);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
@@ -10,31 +8,22 @@ use crate::encoding::{
|
||||
tycho_encoder::TychoEncoder,
|
||||
};
|
||||
|
||||
/// Represents an encoder for a swap through the given router address using any strategy supported
|
||||
/// by the strategy registry.
|
||||
/// Represents an encoder for a swap using any strategy supported by the strategy registry.
|
||||
///
|
||||
/// # Fields
|
||||
/// * `strategy_registry`: S, the strategy registry to use to select the best strategy to encode a
|
||||
/// solution, based on its supported strategies and the solution attributes.
|
||||
/// * `router_address`: Bytes, the address of the router to use to execute the swaps.
|
||||
/// * `native_address`: Address of the chain's native token
|
||||
/// * `wrapped_address`: Address of the chain's wrapped native token
|
||||
#[derive(Clone)]
|
||||
pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
|
||||
strategy_registry: S,
|
||||
router_address: Bytes,
|
||||
native_address: Bytes,
|
||||
wrapped_address: Bytes,
|
||||
}
|
||||
|
||||
impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
|
||||
pub fn new(
|
||||
strategy_registry: S,
|
||||
router_address: String,
|
||||
chain: tycho_core::dto::Chain,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let router_address = Bytes::from_str(&router_address)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?;
|
||||
pub fn new(strategy_registry: S, chain: tycho_core::dto::Chain) -> Result<Self, EncodingError> {
|
||||
let chain: Chain = Chain::from(chain);
|
||||
if chain.name != *"ethereum" {
|
||||
return Err(EncodingError::InvalidInput(
|
||||
@@ -43,7 +32,6 @@ impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
|
||||
}
|
||||
Ok(EVMTychoEncoder {
|
||||
strategy_registry,
|
||||
router_address,
|
||||
native_address: chain.native_token()?,
|
||||
wrapped_address: chain.wrapped_token()?,
|
||||
})
|
||||
@@ -113,16 +101,11 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
|
||||
for solution in solutions.iter() {
|
||||
self.validate_solution(solution)?;
|
||||
|
||||
let router_address = solution
|
||||
.router_address
|
||||
.clone()
|
||||
.unwrap_or(self.router_address.clone());
|
||||
|
||||
let strategy = self
|
||||
.strategy_registry
|
||||
.get_encoder(solution)?;
|
||||
let (contract_interaction, target_address) =
|
||||
strategy.encode_strategy(solution.clone(), router_address)?;
|
||||
strategy.encode_strategy(solution.clone())?;
|
||||
|
||||
let value = match solution.native_action.as_ref() {
|
||||
Some(NativeAction::Wrap) => solution.given_amount.clone(),
|
||||
@@ -141,6 +124,8 @@ impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
|
||||
use tycho_core::dto::{Chain as TychoCoreChain, ProtocolComponent};
|
||||
|
||||
use super::*;
|
||||
@@ -185,11 +170,7 @@ mod tests {
|
||||
struct MockStrategy;
|
||||
|
||||
impl StrategyEncoder for MockStrategy {
|
||||
fn encode_strategy(
|
||||
&self,
|
||||
_solution: Solution,
|
||||
_router_address: Bytes,
|
||||
) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
Ok((
|
||||
Bytes::from_str("0x1234")
|
||||
.unwrap()
|
||||
@@ -209,12 +190,7 @@ mod tests {
|
||||
fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategyRegistry> {
|
||||
let strategy_registry =
|
||||
MockStrategyRegistry::new(TychoCoreChain::Ethereum, None, None).unwrap();
|
||||
EVMTychoEncoder::new(
|
||||
strategy_registry,
|
||||
"0x1234567890abcdef1234567890abcdef12345678".to_string(),
|
||||
TychoCoreChain::Ethereum,
|
||||
)
|
||||
.unwrap()
|
||||
EVMTychoEncoder::new(strategy_registry, TychoCoreChain::Ethereum).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -236,7 +212,7 @@ mod tests {
|
||||
exact_out: false,
|
||||
given_amount: eth_amount_in.clone(),
|
||||
given_token: eth(),
|
||||
router_address: None,
|
||||
router_address: Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
|
||||
@@ -41,8 +41,8 @@ pub struct Solution {
|
||||
pub check_amount: Option<BigUint>,
|
||||
/// List of swaps to fulfill the solution.
|
||||
pub swaps: Vec<Swap>,
|
||||
/// If not set, then the Tycho Router will be used
|
||||
pub router_address: Option<Bytes>,
|
||||
/// Address of the router contract to be used for the swaps.
|
||||
pub router_address: Bytes,
|
||||
/// If set, the corresponding native action will be executed.
|
||||
pub native_action: Option<NativeAction>,
|
||||
/// If set to true, the solution will be encoded to be sent directly to the Executor and
|
||||
|
||||
@@ -4,11 +4,7 @@ use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::Swa
|
||||
|
||||
/// Encodes a solution using a specific strategy.
|
||||
pub trait StrategyEncoder {
|
||||
fn encode_strategy(
|
||||
&self,
|
||||
to_encode: Solution,
|
||||
router_address: Bytes,
|
||||
) -> Result<(Vec<u8>, Bytes), EncodingError>;
|
||||
fn encode_strategy(&self, to_encode: Solution) -> Result<(Vec<u8>, Bytes), EncodingError>;
|
||||
|
||||
#[allow(clippy::borrowed_box)]
|
||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
|
||||
|
||||
Reference in New Issue
Block a user