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:
Diana Carvalho
2025-01-30 11:22:30 +00:00
parent 3a69bbf603
commit feb91cc639
10 changed files with 355 additions and 65 deletions

View File

@@ -1,35 +1,150 @@
use alloy_primitives::Address;
use std::cmp::min;
use alloy::signers::local::PrivateKeySigner;
use alloy_primitives::{aliases::U24, map::HashSet, ChainId, U256, U8};
use alloy_sol_types::SolValue;
use num_bigint::BigUint;
use tycho_core::Bytes;
use crate::encoding::{
errors::EncodingError,
evm::swap_encoder::SWAP_ENCODER_REGISTRY,
models::{EncodingContext, Solution},
evm::{
approvals::permit2::Permit2,
swap_encoder::SWAP_ENCODER_REGISTRY,
utils::{biguint_to_u256, bytes_to_address, percentage_to_uint24, ple_encode},
},
models::{EncodingContext, NativeAction, Solution},
strategy_encoder::StrategyEncoder,
};
#[allow(dead_code)]
pub trait EVMStrategyEncoder: StrategyEncoder {
fn encode_protocol_header(
fn encode_swap_header(
&self,
token_in: U8,
token_out: U8,
split: U24,
protocol_data: Vec<u8>,
executor_address: Address,
// Token indices, split, and token inclusion are only used for split swaps
token_in: u16,
token_out: u16,
split: u16, // not sure what should be the type of this :/
) -> Vec<u8> {
let args = (executor_address, token_in, token_out, split, protocol_data);
args.abi_encode()
let mut encoded = Vec::new();
encoded.push(token_in.to_be_bytes_vec()[0]);
encoded.push(token_out.to_be_bytes_vec()[0]);
encoded.extend_from_slice(&split.to_be_bytes_vec());
encoded.extend(protocol_data);
encoded
}
}
pub struct SplitSwapStrategyEncoder {}
pub struct SplitSwapStrategyEncoder {
permit2: Permit2,
}
impl SplitSwapStrategyEncoder {
pub fn new(signer: PrivateKeySigner, chain_id: ChainId) -> Result<Self, EncodingError> {
Ok(Self { permit2: Permit2::new(signer, chain_id)? })
}
}
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
impl StrategyEncoder for SplitSwapStrategyEncoder {
fn encode_strategy(&self, _solution: Solution) -> Result<Vec<u8>, EncodingError> {
todo!()
fn encode_strategy(
&self,
solution: Solution,
router_address: Bytes,
) -> Result<Vec<u8>, EncodingError> {
let (permit, signature) = self.permit2.get_permit(
&router_address,
&solution.sender,
&solution.given_token,
&solution.given_amount,
)?;
let min_amount_out = if solution.check_amount.is_some() {
let mut min_amount_out = 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;
min_amount_out = min(min_amount_out, expected_amount_with_slippage);
}
min_amount_out
} else {
BigUint::ZERO
};
let tokens: Vec<Bytes> = solution
.swaps
.iter()
.flat_map(|swap| vec![swap.token_in.clone(), swap.token_out.clone()])
.collect::<HashSet<Bytes>>()
.into_iter()
.collect();
let mut swaps = vec![];
for swap in solution.swaps.iter() {
let registry = SWAP_ENCODER_REGISTRY.read().unwrap();
let swap_encoder = registry
.get_encoder(&swap.component.protocol_system)
.expect("Swap encoder not found");
let encoding_context = EncodingContext {
receiver: router_address.clone(),
exact_out: solution.exact_out,
router_address: router_address.clone(),
};
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
let swap_data = self.encode_swap_header(
U8::from(
tokens
.iter()
.position(|t| *t == swap.token_in)
.ok_or_else(|| {
EncodingError::InvalidInput(
"Token in not found in tokens array".to_string(),
)
})?,
),
U8::from(
tokens
.iter()
.position(|t| *t == swap.token_out)
.ok_or_else(|| {
EncodingError::InvalidInput(
"Token out not found in tokens array".to_string(),
)
})?,
),
percentage_to_uint24(swap.split),
protocol_data,
);
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 = (
biguint_to_u256(&solution.given_amount),
bytes_to_address(&solution.given_token)?,
bytes_to_address(&solution.checked_token)?,
biguint_to_u256(&min_amount_out),
wrap,
unwrap,
U256::from(tokens.len()),
bytes_to_address(&solution.receiver)?,
permit,
signature.as_bytes().to_vec(),
encoded_swaps,
)
.abi_encode();
Ok(method_calldata)
}
fn selector(&self, _exact_out: bool) -> &str {
"swap(uint256, address, uint256, bytes[])"
}
@@ -40,7 +155,11 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
pub struct StraightToPoolStrategyEncoder {}
impl EVMStrategyEncoder for StraightToPoolStrategyEncoder {}
impl StrategyEncoder for StraightToPoolStrategyEncoder {
fn encode_strategy(&self, solution: Solution) -> Result<Vec<u8>, EncodingError> {
fn encode_strategy(
&self,
solution: Solution,
_router_address: Bytes,
) -> Result<Vec<u8>, EncodingError> {
if solution.router_address.is_none() {
return Err(EncodingError::InvalidInput(
"Router address is required for straight to pool solutions".to_string(),
@@ -75,3 +194,111 @@ impl StrategyEncoder for StraightToPoolStrategyEncoder {
unimplemented!();
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use alloy::hex::encode;
use alloy_primitives::B256;
use tycho_core::dto::ProtocolComponent;
use super::*;
use crate::encoding::models::Swap;
#[test]
fn test_split_swap_strategy_encoder() {
// Set up a mock private key for signing
let private_key =
B256::from_str("4c0883a69102937d6231471b5dbb6204fe512961708279feb1be6ae5538da033")
.expect("Invalid private key");
let signer = PrivateKeySigner::from_bytes(&private_key).expect("Failed to create signer");
let weth = Bytes::from_str("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2").unwrap();
let dai = Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap();
let swap = Swap {
component: ProtocolComponent {
id: "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640".to_string(),
protocol_system: "uniswap_v2".to_string(),
..Default::default()
},
token_in: weth.clone(),
token_out: dai.clone(),
split: 0f64,
};
let encoder = SplitSwapStrategyEncoder::new(signer, 1).unwrap();
let solution = Solution {
exact_out: false,
given_token: weth,
given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
checked_token: dai,
expected_amount: BigUint::from_str("3_000_000000000000000000").unwrap(),
check_amount: None,
sender: Bytes::from_str("0x2c6A3cd97c6283b95Ac8C5A4459eBB0d5Fd404F4").unwrap(),
receiver: Bytes::from_str("0x2c6A3cd97c6283b95Ac8C5A4459eBB0d5Fd404F4").unwrap(),
swaps: vec![swap],
..Default::default()
};
let router_address = Bytes::from_str("0x2c6A3cd97c6283b95Ac8C5A4459eBB0d5Fd404F4").unwrap();
let calldata = encoder
.encode_strategy(solution, router_address)
.unwrap();
let expected_input = String::from(concat!(
"0000000000000000000000000000000000000000000000000000000000000020", // offset
"0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out
"000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
"0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out
"0000000000000000000000000000000000000000000000000000000000000000", // min amount out
"0000000000000000000000000000000000000000000000000000000000000000", // wrap
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
"0000000000000000000000002c6a3cd97c6283b95ac8c5a4459ebb0d5fd404f4", // receiver
));
// after this there is the permit and because of the deadlines (that depend on block time)
// it's hard to assert
// "000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
// "0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount in
// "0000000000000000000000000000000000000000000000000000000067c205fe", // expiration
// "0000000000000000000000000000000000000000000000000000000000000000", // nonce
// "0000000000000000000000002c6a3cd97c6283b95ac8c5a4459ebb0d5fd404f4", // spender
// "00000000000000000000000000000000000000000000000000000000679a8006", // deadline
// offsets???
// "0000000000000000000000000000000000000000000000000000000000000200",
// "0000000000000000000000000000000000000000000000000000000000000280",
// "0000000000000000000000000000000000000000000000000000000000000041",
// signature
// "fc5bac4e27cd5d71c85d232d8c6b31ea924d2e0031091ff9a39579d9e49c214328ea34876961d9200af691373c71a174e166793d02241c76adb93c5f87fe0f381c",
let expected_swaps = String::from(concat!(
// ple encode adds aaalll of this :/ is it correct?
"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000120000",
"0000000000000000000000000000000000000000000000000000000000020000",
"0000000000000000000000000000000000000000000000000000000000060000",
"000000000000000000000000000000000000000000000000000000000005b000",
"0000000000000000000000000000000000000000000000000000000000080000",
"0000000000000000000000000000000000000000000000000000000000000000",
"000000000000000000000000000000000000000000000000000000000005b",
// Swap header
"00", // token in index
"01", // token out index
"000000", // split
// Swap data
"5c2f5a71f67c01775180adc06909288b4c329308", // executor address
"bd0625ab", // selector
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
"88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", // component id
"2c6a3cd97c6283b95ac8c5a4459ebb0d5fd404f4", // receiver
"00", // zero2one
"00", // exact out
"0000000000", // padding
));
let hex_calldata = encode(&calldata);
assert_eq!(hex_calldata[..576], expected_input);
assert_eq!(hex_calldata[1283..], expected_swaps);
}
}

View File

@@ -1,4 +1,8 @@
use alloy::signers::local::PrivateKeySigner;
use alloy_primitives::ChainId;
use crate::encoding::{
errors::EncodingError,
evm::strategy_encoder::encoder::{SplitSwapStrategyEncoder, StraightToPoolStrategyEncoder},
models::Solution,
strategy_encoder::{StrategyEncoder, StrategySelector},
@@ -7,11 +11,21 @@ use crate::encoding::{
pub struct EVMStrategySelector;
impl StrategySelector for EVMStrategySelector {
fn select_strategy(&self, solution: &Solution) -> Box<dyn StrategyEncoder> {
fn select_strategy(
&self,
solution: &Solution,
signer: Option<PrivateKeySigner>,
chain_id: ChainId,
) -> Result<Box<dyn StrategyEncoder>, EncodingError> {
if solution.straight_to_pool {
Box::new(StraightToPoolStrategyEncoder {})
Ok(Box::new(StraightToPoolStrategyEncoder {}))
} else {
Box::new(SplitSwapStrategyEncoder {})
let signer = signer.ok_or_else(|| {
EncodingError::FatalError(
"Signer is required for SplitSwapStrategyEncoder".to_string(),
)
})?;
Ok(Box::new(SplitSwapStrategyEncoder::new(signer, chain_id).unwrap()))
}
}
}