From 38b8bb0e782d25a4d88fb250c6d1f0e050b76313 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Fri, 17 Jan 2025 16:21:26 +0000 Subject: [PATCH] feat: Simplify StrategyEncoders and RouterEncoder No more SingleSwapStrategyEncoder or SequentialStrategyEncoder No more batch routing (so no need for batch permits as well). If multiple solutions are found, return multiple Transactions --- don't change below this line --- ENG-4076 <#DTT#> --- src/encoding/evm/approvals/permit2.rs | 6 +- src/encoding/evm/router_encoder.rs | 35 ++--- src/encoding/evm/strategy_encoder/encoder.rs | 136 +----------------- src/encoding/evm/strategy_encoder/selector.rs | 13 +- src/encoding/evm/utils.rs | 3 + src/encoding/models.rs | 9 -- src/encoding/router_encoder.rs | 4 +- src/encoding/strategy_encoder.rs | 4 +- src/encoding/user_approvals_manager.rs | 2 +- 9 files changed, 26 insertions(+), 186 deletions(-) diff --git a/src/encoding/evm/approvals/permit2.rs b/src/encoding/evm/approvals/permit2.rs index eba7179..3bf98f0 100644 --- a/src/encoding/evm/approvals/permit2.rs +++ b/src/encoding/evm/approvals/permit2.rs @@ -30,11 +30,11 @@ impl Permit2 { } } impl UserApprovalsManager for Permit2 { - fn encode_approvals(&self, _approvals: Vec) -> Vec { + fn encode_approvals(&self, _approvals: Vec) -> Vec> { // calls get_allowance_data to get nonce // checks if we are not permitted already - // puts data into a permitSingle struct if there is only 1 PermitDetails, if there are - // several, use PermitBatch adds the nonce and the expiration (uniswap recommends + // puts data into a list of PermitSingles + // adds the nonce and the expiration (uniswap recommends // 30 days for expiration) signs data // returns encoded data todo!() diff --git a/src/encoding/evm/router_encoder.rs b/src/encoding/evm/router_encoder.rs index ff81be1..30b49fc 100644 --- a/src/encoding/evm/router_encoder.rs +++ b/src/encoding/evm/router_encoder.rs @@ -1,9 +1,8 @@ -use alloy_sol_types::SolValue; use anyhow::Error; use num_bigint::BigUint; use crate::encoding::{ - evm::utils::{encode_input, ple_encode}, + evm::utils::encode_input, models::{NativeAction, Solution, Transaction, PROPELLER_ROUTER_ADDRESS}, router_encoder::RouterEncoder, strategy_encoder::StrategySelector, @@ -23,11 +22,9 @@ impl EVMRouterEncoder { } } impl RouterEncoder for EVMRouterEncoder { - fn encode_router_calldata(&self, solutions: Vec) -> Result { + fn encode_router_calldata(&self, solutions: Vec) -> Result, Error> { let _approvals_calldata = self.handle_approvals(&solutions)?; // TODO: where should we append this? - let mut calldata_list: Vec> = Vec::new(); - let encode_for_batch_execute = solutions.len() > 1; - let mut value = BigUint::ZERO; + let mut transactions: Vec = Vec::new(); for solution in solutions.iter() { let exact_out = solution.exact_out; let straight_to_pool = solution.straight_to_pool; @@ -37,31 +34,23 @@ impl RouterEncoder for EVMRo .select_strategy(solution); let method_calldata = strategy.encode_strategy((*solution).clone())?; - let contract_interaction = if encode_for_batch_execute { - let args = (strategy.action_type(exact_out) as u16, method_calldata); - args.abi_encode() - } else if straight_to_pool { + let contract_interaction = if straight_to_pool { method_calldata } else { encode_input(strategy.selector(exact_out), method_calldata) }; - calldata_list.push(contract_interaction); - if solution.native_action.clone().unwrap() == NativeAction::Wrap { - value += solution.given_amount.clone(); - } + let value = if solution.native_action.clone().unwrap() == NativeAction::Wrap { + solution.given_amount.clone() + } else { + BigUint::ZERO + }; + transactions.push(Transaction { value, data: contract_interaction }); } - let data = if encode_for_batch_execute { - let args = (false, ple_encode(calldata_list)); - encode_input("batchExecute(bytes)", args.abi_encode()) - } else { - calldata_list[0].clone() - }; - - Ok(Transaction { data, value }) + Ok(transactions) } - fn handle_approvals(&self, solutions: &[Solution]) -> Result, Error> { + fn handle_approvals(&self, solutions: &[Solution]) -> Result>, Error> { let mut approvals = Vec::new(); for solution in solutions.iter() { approvals.push(Approval { diff --git a/src/encoding/evm/strategy_encoder/encoder.rs b/src/encoding/evm/strategy_encoder/encoder.rs index 57750c7..8240147 100644 --- a/src/encoding/evm/strategy_encoder/encoder.rs +++ b/src/encoding/evm/strategy_encoder/encoder.rs @@ -1,17 +1,10 @@ -use std::{cmp::min, str::FromStr}; - use alloy_primitives::Address; use alloy_sol_types::SolValue; use anyhow::Error; -use num_bigint::BigUint; -use num_traits::Zero; use crate::encoding::{ - evm::{ - swap_encoder::SWAP_ENCODER_REGISTRY, - utils::{biguint_to_u256, ple_encode}, - }, - models::{ActionType, EncodingContext, NativeAction, Solution, PROPELLER_ROUTER_ADDRESS}, + evm::swap_encoder::SWAP_ENCODER_REGISTRY, + models::{EncodingContext, Solution}, strategy_encoder::StrategyEncoder, }; @@ -31,132 +24,14 @@ pub trait EVMStrategyEncoder: StrategyEncoder { } } -pub struct SingleSwapStrategyEncoder {} -impl EVMStrategyEncoder for SingleSwapStrategyEncoder {} - -impl StrategyEncoder for SingleSwapStrategyEncoder { - fn encode_strategy(&self, _solution: Solution) -> Result, Error> { - todo!() - } - - fn action_type(&self, exact_out: bool) -> ActionType { - if exact_out { - ActionType::SingleExactOut - } else { - ActionType::SingleExactIn - } - } - - fn selector(&self, exact_out: bool) -> &str { - if exact_out { - "singleExactOut(uint256, bytes)" - } else { - "singleExactIn(uint256, bytes)" - } - } -} - -pub struct SequentialStrategyEncoder {} -impl EVMStrategyEncoder for SequentialStrategyEncoder {} - -impl StrategyEncoder for SequentialStrategyEncoder { - fn encode_strategy(&self, solution: Solution) -> Result, Error> { - let check_amount = if solution.check_amount.is_some() { - let mut check_amount = solution.check_amount.clone().unwrap(); - if solution.slippage.is_some() { - let one_hundred = BigUint::from(100u32); - let slippage_percent = BigUint::from((solution.slippage.unwrap() * 100.0) as u32); - let multiplier = &one_hundred - slippage_percent; - let expected_amount_with_slippage = - (&solution.expected_amount * multiplier) / one_hundred; - check_amount = min(check_amount, expected_amount_with_slippage); - } - check_amount - } else { - BigUint::ZERO - }; - - let mut swaps = vec![]; - for (index, swap) in solution.swaps.iter().enumerate() { - let is_last = index == solution.swaps.len() - 1; - let registry = SWAP_ENCODER_REGISTRY.read().unwrap(); - let swap_encoder = registry - .get_encoder(&swap.component.protocol_system) - .expect("Swap encoder not found"); - let router_address = if solution.router_address.is_some() { - solution.router_address.clone().unwrap() - } else { - PROPELLER_ROUTER_ADDRESS.clone() - }; - let receiver = if is_last { solution.receiver.clone() } else { router_address.clone() }; - - let encoding_context = EncodingContext { - receiver, - exact_out: solution.exact_out, - address_for_approvals: router_address, - }; - let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?; - let executor_address = swap_encoder.executor_address(); - let swap_data = self.encode_protocol_header( - protocol_data, - Address::from_str(executor_address).expect("Couldn't convert executor address"), - 0, - 0, - 0, - ); - swaps.push(swap_data); - } - - let encoded_swaps = ple_encode(swaps); - - let (mut unwrap, mut wrap) = (false, false); - if solution.native_action.is_some() { - match solution.native_action.unwrap() { - NativeAction::Wrap => wrap = true, - NativeAction::Unwrap => unwrap = true, - } - } - let method_calldata = ( - wrap, - unwrap, - biguint_to_u256(&solution.given_amount), - !check_amount.is_zero(), /* if check_amount is zero, then we don't need to check */ - biguint_to_u256(&check_amount), - encoded_swaps, - ) - .abi_encode(); - Ok(method_calldata) - } - - fn action_type(&self, exact_out: bool) -> ActionType { - if exact_out { - ActionType::SequentialExactOut - } else { - ActionType::SequentialExactIn - } - } - - fn selector(&self, exact_out: bool) -> &str { - if exact_out { - "sequentialExactOut(uint256, uint256, bytes[])" - } else { - "sequentialExactIn(uint256, uint256, bytes[])" - } - } -} - pub struct SplitSwapStrategyEncoder {} impl EVMStrategyEncoder for SplitSwapStrategyEncoder {} impl StrategyEncoder for SplitSwapStrategyEncoder { fn encode_strategy(&self, _solution: Solution) -> Result, Error> { todo!() } - fn action_type(&self, _exact_out: bool) -> ActionType { - ActionType::SplitIn - } - fn selector(&self, _exact_out: bool) -> &str { - "splitExactIn(uint256, address, uint256, bytes[])" + "swap(uint256, address, uint256, bytes[])" } } @@ -187,11 +62,6 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder { // TODO: here we need to pass also the address of the executor to be used Ok(protocol_data) } - - fn action_type(&self, _exact_out: bool) -> ActionType { - unimplemented!(); - } - fn selector(&self, _exact_out: bool) -> &str { unimplemented!(); } diff --git a/src/encoding/evm/strategy_encoder/selector.rs b/src/encoding/evm/strategy_encoder/selector.rs index a98432e..37e397c 100644 --- a/src/encoding/evm/strategy_encoder/selector.rs +++ b/src/encoding/evm/strategy_encoder/selector.rs @@ -1,8 +1,5 @@ use crate::encoding::{ - evm::strategy_encoder::encoder::{ - SequentialStrategyEncoder, SingleSwapStrategyEncoder, SplitSwapStrategyEncoder, - StraightToPoolStrategyEncoder, - }, + evm::strategy_encoder::encoder::{SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder}, models::Solution, strategy_encoder::{StrategyEncoder, StrategySelector}, }; @@ -13,14 +10,6 @@ impl StrategySelector for EVMStrategySelector { fn select_strategy(&self, solution: &Solution) -> Box { if solution.straight_to_pool { Box::new(StraightToPoolStrategyEncoder {}) - } else if solution.swaps.len() == 1 { - Box::new(SingleSwapStrategyEncoder {}) - } else if solution - .swaps - .iter() - .all(|s| s.split == 0.0) - { - Box::new(SequentialStrategyEncoder {}) } else { Box::new(SplitSwapStrategyEncoder {}) } diff --git a/src/encoding/evm/utils.rs b/src/encoding/evm/utils.rs index d510fa7..75ef285 100644 --- a/src/encoding/evm/utils.rs +++ b/src/encoding/evm/utils.rs @@ -15,11 +15,14 @@ pub fn bytes_to_address(address: &Bytes) -> Result { Err(anyhow::format_err!("Invalid ERC20 token address: {:?}", address)) } } + +#[allow(dead_code)] pub fn biguint_to_u256(value: &BigUint) -> U256 { let bytes = value.to_bytes_be(); U256::from_be_slice(&bytes) } +#[allow(dead_code)] pub fn ple_encode(action_data_array: Vec>) -> Vec { let mut encoded_action_data: Vec = Vec::new(); diff --git a/src/encoding/models.rs b/src/encoding/models.rs index 9009d57..0b81dbe 100644 --- a/src/encoding/models.rs +++ b/src/encoding/models.rs @@ -78,12 +78,3 @@ pub struct EncodingContext { pub exact_out: bool, pub address_for_approvals: Bytes, } - -#[allow(dead_code)] -pub enum ActionType { - SingleExactIn = 1, - SingleExactOut = 2, - SequentialExactIn = 3, - SequentialExactOut = 4, - SplitIn = 5, -} diff --git a/src/encoding/router_encoder.rs b/src/encoding/router_encoder.rs index 834531f..e653d02 100644 --- a/src/encoding/router_encoder.rs +++ b/src/encoding/router_encoder.rs @@ -8,6 +8,6 @@ use crate::encoding::{ #[allow(dead_code)] pub trait RouterEncoder { - fn encode_router_calldata(&self, solutions: Vec) -> Result; - fn handle_approvals(&self, solutions: &[Solution]) -> Result, Error>; + fn encode_router_calldata(&self, solutions: Vec) -> Result, Error>; + fn handle_approvals(&self, solutions: &[Solution]) -> Result>, Error>; } diff --git a/src/encoding/strategy_encoder.rs b/src/encoding/strategy_encoder.rs index a18cb41..844311b 100644 --- a/src/encoding/strategy_encoder.rs +++ b/src/encoding/strategy_encoder.rs @@ -1,12 +1,10 @@ use anyhow::Error; -use crate::encoding::models::{ActionType, Solution}; +use crate::encoding::models::Solution; #[allow(dead_code)] pub trait StrategyEncoder { fn encode_strategy(&self, to_encode: Solution) -> Result, Error>; - - fn action_type(&self, exact_out: bool) -> ActionType; fn selector(&self, exact_out: bool) -> &str; } diff --git a/src/encoding/user_approvals_manager.rs b/src/encoding/user_approvals_manager.rs index 45ca74e..0656a37 100644 --- a/src/encoding/user_approvals_manager.rs +++ b/src/encoding/user_approvals_manager.rs @@ -11,5 +11,5 @@ pub struct Approval { pub trait UserApprovalsManager { #[allow(dead_code)] - fn encode_approvals(&self, approvals: Vec) -> Vec; + fn encode_approvals(&self, approvals: Vec) -> Vec>; }