feat: Implement SplitSwapStrategyEncoder
The strategy works as follows: - Manage approvals needed - Compute min amount (if check amount is any): - if slippage is defined, apply slippage on the expected amount and take the min value between that and the check amount - if not, it's just the check amount - Iterate through the swaps - call the corresponding swap encoder to encode the swap - add swap header (tokens indexes and split) - ple encode the swaps - Add extra inputs (amounts, token addresses, min amount, (un)wrap, number of tokens and receiver) Misc: - Move executor address and selector encoding inside the SwapEncoder - Add default executor_selector to SwapEncoder - Pass router address inside the SplitSwapStrategyEncoder - Move Permit2 inside the SplitSwapStrategyEncoder. It is a responsibility and a specificity of the strategy to need permit2 approvals --- don't change below this line --- ENG-4081 Took 1 hour 21 minutes
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use alloy_primitives::{Address, Keccak256, U256};
|
||||
use alloy_primitives::{aliases::U24, Address, Keccak256, U256};
|
||||
use alloy_sol_types::SolValue;
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::Bytes;
|
||||
@@ -13,7 +13,7 @@ pub fn bytes_to_address(address: &Bytes) -> Result<Address, EncodingError> {
|
||||
if address.len() == 20 {
|
||||
Ok(Address::from_slice(address))
|
||||
} else {
|
||||
Err(EncodingError::InvalidInput(format!("Invalid ERC20 token address: {:?}", address)))
|
||||
Err(EncodingError::InvalidInput(format!("Invalid address: {:?}", address)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,3 +56,12 @@ pub fn encode_input(selector: &str, mut encoded_args: Vec<u8>) -> Vec<u8> {
|
||||
call_data.extend(encoded_args);
|
||||
call_data
|
||||
}
|
||||
|
||||
/// Converts a percentage to a `U24` value. The percentage is a `f64` value between 0 and 100.
|
||||
/// MAX_UINT24 corresponds to 100%.
|
||||
pub fn percentage_to_uint24(percentage: f64) -> U24 {
|
||||
const MAX_UINT24: u32 = 16_777_215; // 2^24 - 1
|
||||
|
||||
let scaled = (percentage / 100.0) * (MAX_UINT24 as f64);
|
||||
U24::from(scaled.round())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user