feat: Simplify router encoder

Don't make selector() a member of the StrategyEncoder trait. It is needed only for certain strategies. The strategy should manage it itself.

--- don't change below this line ---
ENG-4081 Took 17 minutes
This commit is contained in:
Diana Carvalho
2025-01-30 15:19:09 +00:00
parent 089e7d2e0f
commit 6e8d2ede59
3 changed files with 19 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ use tycho_core::{models::Chain, Bytes};
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::utils::encode_input,
models::{NativeAction, Solution, Transaction}, models::{NativeAction, Solution, Transaction},
router_encoder::RouterEncoder, router_encoder::RouterEncoder,
strategy_encoder::StrategySelector, strategy_encoder::StrategySelector,
@@ -37,8 +36,12 @@ impl<S: StrategySelector> RouterEncoder<S> for EVMRouterEncoder<S> {
) -> Result<Vec<Transaction>, EncodingError> { ) -> Result<Vec<Transaction>, EncodingError> {
let mut transactions: Vec<Transaction> = Vec::new(); let mut transactions: Vec<Transaction> = Vec::new();
for solution in solutions.iter() { for solution in solutions.iter() {
let exact_out = solution.exact_out; if solution.exact_out {
let straight_to_pool = solution.straight_to_pool; return Err(EncodingError::FatalError(
"Currently only exact input solutions are supported".to_string(),
));
}
let router_address = solution let router_address = solution
.router_address .router_address
.clone() .clone()
@@ -50,13 +53,9 @@ impl<S: StrategySelector> RouterEncoder<S> for EVMRouterEncoder<S> {
self.signer.clone(), self.signer.clone(),
self.chain, self.chain,
)?; )?;
let method_calldata = strategy.encode_strategy(solution.clone(), router_address)?;
let contract_interaction = if straight_to_pool { let contract_interaction =
method_calldata strategy.encode_strategy(solution.clone(), router_address)?;
} else {
encode_input(strategy.selector(exact_out), method_calldata)
};
let value = if solution.native_action.clone().unwrap() == NativeAction::Wrap { let value = if solution.native_action.clone().unwrap() == NativeAction::Wrap {
solution.given_amount.clone() solution.given_amount.clone()

View File

@@ -10,7 +10,9 @@ use crate::encoding::{
evm::{ evm::{
approvals::permit2::Permit2, approvals::permit2::Permit2,
swap_encoder::SWAP_ENCODER_REGISTRY, swap_encoder::SWAP_ENCODER_REGISTRY,
utils::{biguint_to_u256, bytes_to_address, percentage_to_uint24, ple_encode}, utils::{
biguint_to_u256, bytes_to_address, encode_input, percentage_to_uint24, ple_encode,
},
}, },
models::{EncodingContext, NativeAction, Solution}, models::{EncodingContext, NativeAction, Solution},
strategy_encoder::StrategyEncoder, strategy_encoder::StrategyEncoder,
@@ -36,11 +38,13 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
pub struct SplitSwapStrategyEncoder { pub struct SplitSwapStrategyEncoder {
permit2: Permit2, permit2: Permit2,
selector: String,
} }
impl SplitSwapStrategyEncoder { impl SplitSwapStrategyEncoder {
pub fn new(signer_pk: String, chain: Chain) -> Result<Self, EncodingError> { pub fn new(signer_pk: String, chain: Chain) -> Result<Self, EncodingError> {
Ok(Self { permit2: Permit2::new(signer_pk, chain)? }) let selector = "swap(uint256, address, address, uint256, bool, bool, uint256, address, ((address,uint160,uint48,uint48),address,uint256),bytes, bytes)".to_string();
Ok(Self { permit2: Permit2::new(signer_pk, chain)?, selector })
} }
} }
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
@@ -145,11 +149,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
encoded_swaps, encoded_swaps,
) )
.abi_encode(); .abi_encode();
Ok(method_calldata)
}
fn selector(&self, _exact_out: bool) -> &str { let contract_interaction = encode_input(&self.selector, method_calldata);
"swap(uint256, address, uint256, bytes[])" Ok(contract_interaction)
} }
} }
@@ -193,9 +195,6 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder {
// TODO: here we need to pass also the address of the executor to be used // TODO: here we need to pass also the address of the executor to be used
Ok(protocol_data) Ok(protocol_data)
} }
fn selector(&self, _exact_out: bool) -> &str {
unimplemented!();
}
} }
#[cfg(test)] #[cfg(test)]
@@ -248,7 +247,7 @@ mod tests {
.unwrap(); .unwrap();
let expected_input = String::from(concat!( let expected_input = String::from(concat!(
"0000000000000000000000000000000000000000000000000000000000000020", // offset "e73e3baa", // selector
"0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out
"000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
"0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out "0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out
@@ -298,7 +297,7 @@ mod tests {
"0000000000", // padding "0000000000", // padding
)); ));
let hex_calldata = encode(&calldata); let hex_calldata = encode(&calldata);
assert_eq!(hex_calldata[..576], expected_input); assert_eq!(hex_calldata[..520], expected_input);
assert_eq!(hex_calldata[1283..], expected_swaps); assert_eq!(hex_calldata[1227..], expected_swaps);
} }
} }

View File

@@ -9,7 +9,6 @@ pub trait StrategyEncoder {
to_encode: Solution, to_encode: Solution,
router_address: Bytes, router_address: Bytes,
) -> Result<Vec<u8>, EncodingError>; ) -> Result<Vec<u8>, EncodingError>;
fn selector(&self, exact_out: bool) -> &str;
} }
pub trait StrategySelector { pub trait StrategySelector {