Merge branch 'main' into encoding/tnl/fix-wrapping-indices
This commit is contained in:
@@ -1,155 +0,0 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use alloy_primitives::Address;
|
||||
use alloy_sol_types::SolValue;
|
||||
use tycho_core::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::swap_encoder::SWAP_ENCODER_REGISTRY,
|
||||
models::{EncodingContext, Solution},
|
||||
strategy_encoder::StrategyEncoder,
|
||||
};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub trait EVMStrategyEncoder: StrategyEncoder {
|
||||
fn encode_protocol_header(
|
||||
&self,
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SplitSwapStrategyEncoder {}
|
||||
impl EVMStrategyEncoder for SplitSwapStrategyEncoder {}
|
||||
impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
fn encode_strategy(&self, _solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
todo!()
|
||||
}
|
||||
fn selector(&self, _exact_out: bool) -> &str {
|
||||
"swap(uint256, address, uint256, bytes[])"
|
||||
}
|
||||
}
|
||||
|
||||
/// This strategy encoder is used for solutions that are sent directly to the pool.
|
||||
/// Only 1 solution with 1 swap is supported.
|
||||
pub struct ExecutorStrategyEncoder {}
|
||||
impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
|
||||
impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||
fn encode_strategy(&self, solution: Solution) -> Result<(Vec<u8>, Bytes), EncodingError> {
|
||||
if solution.router_address.is_none() {
|
||||
return Err(EncodingError::InvalidInput(
|
||||
"Router address is required for straight to pool solutions".to_string(),
|
||||
));
|
||||
}
|
||||
let swap = solution.swaps.first().unwrap();
|
||||
let registry = SWAP_ENCODER_REGISTRY
|
||||
.read()
|
||||
.map_err(|_| {
|
||||
EncodingError::FatalError("Failed to read the swap encoder registry".to_string())
|
||||
})?;
|
||||
let swap_encoder = registry
|
||||
.get_encoder(&swap.component.protocol_system)
|
||||
.ok_or_else(|| {
|
||||
EncodingError::InvalidInput(format!(
|
||||
"Swap encoder not found for protocol: {}",
|
||||
swap.component.protocol_system
|
||||
))
|
||||
})?;
|
||||
let router_address = solution.router_address.unwrap();
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: solution.receiver,
|
||||
exact_out: solution.exact_out,
|
||||
router_address,
|
||||
};
|
||||
let protocol_data = swap_encoder.encode_swap(swap.clone(), encoding_context)?;
|
||||
let executor_address = Bytes::from_str(swap_encoder.executor_address())
|
||||
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
|
||||
Ok((protocol_data, executor_address))
|
||||
}
|
||||
fn selector(&self, _exact_out: bool) -> &str {
|
||||
"swap(uint256, bytes)"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use alloy::hex::encode;
|
||||
use num_bigint::BigUint;
|
||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||
|
||||
use super::*;
|
||||
use crate::encoding::models::Swap;
|
||||
|
||||
#[test]
|
||||
fn test_executor_strategy_encode() {
|
||||
let encoder = ExecutorStrategyEncoder {};
|
||||
|
||||
let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
|
||||
let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f");
|
||||
|
||||
let swap = Swap {
|
||||
component: ProtocolComponent {
|
||||
id: "0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11".to_string(),
|
||||
protocol_system: "uniswap_v2".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
token_in: token_in.clone(),
|
||||
token_out: token_out.clone(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: token_in,
|
||||
given_amount: BigUint::from(1000000000000000000u64),
|
||||
expected_amount: BigUint::from(1000000000000000000u64),
|
||||
checked_token: token_out,
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap],
|
||||
direct_execution: true,
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
};
|
||||
|
||||
let (protocol_data, executor_address) = encoder
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
let hex_protocol_data = encode(&protocol_data);
|
||||
assert_eq!(
|
||||
executor_address,
|
||||
Bytes::from_str("0x5c2f5a71f67c01775180adc06909288b4c329308").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
hex_protocol_data,
|
||||
String::from(concat!(
|
||||
// in token
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
|
||||
// component id
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11",
|
||||
// receiver
|
||||
"1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e",
|
||||
// zero for one
|
||||
"00",
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_selector() {
|
||||
let encoder = ExecutorStrategyEncoder {};
|
||||
assert_eq!(encoder.selector(false), "swap(uint256, bytes)");
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
mod strategy_encoders;
|
||||
mod strategy_selector;
|
||||
pub mod strategy_selector;
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::encoding::{
|
||||
strategy_encoder::StrategyEncoder,
|
||||
};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub trait EVMStrategyEncoder: StrategyEncoder {
|
||||
fn encode_swap_header(
|
||||
&self,
|
||||
@@ -78,16 +77,18 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
&solution.given_token,
|
||||
&solution.given_amount,
|
||||
)?;
|
||||
let mut min_amount_out = BigUint::ZERO;
|
||||
if let Some(user_specified_min_amount) = solution.check_amount {
|
||||
if let Some(slippage) = solution.slippage {
|
||||
let one_hundred = BigUint::from(100u32);
|
||||
let slippage_percent = BigUint::from((slippage * 100.0) as u32);
|
||||
let multiplier = &one_hundred - slippage_percent;
|
||||
let expected_amount_with_slippage =
|
||||
(&solution.expected_amount * multiplier) / one_hundred;
|
||||
min_amount_out = max(user_specified_min_amount, expected_amount_with_slippage);
|
||||
}
|
||||
let mut min_amount_out = solution
|
||||
.check_amount
|
||||
.unwrap_or(BigUint::ZERO);
|
||||
|
||||
if let (Some(expected_amount), Some(slippage)) =
|
||||
(solution.expected_amount.as_ref(), solution.slippage)
|
||||
{
|
||||
let one_hundred = BigUint::from(100u32);
|
||||
let slippage_percent = BigUint::from((slippage * 100.0) as u32);
|
||||
let multiplier = &one_hundred - slippage_percent;
|
||||
let expected_amount_with_slippage = (expected_amount * &multiplier) / &one_hundred;
|
||||
min_amount_out = max(min_amount_out, expected_amount_with_slippage);
|
||||
}
|
||||
// The tokens array is composed of the given token, the checked token and all the
|
||||
// intermediary tokens in between. The contract expects the tokens to be in this order.
|
||||
@@ -255,6 +256,7 @@ mod tests {
|
||||
|
||||
use alloy::hex::encode;
|
||||
use num_bigint::BigUint;
|
||||
use rstest::rstest;
|
||||
use tycho_core::{dto::ProtocolComponent, Bytes};
|
||||
|
||||
use super::*;
|
||||
@@ -285,7 +287,7 @@ mod tests {
|
||||
exact_out: false,
|
||||
given_token: token_in,
|
||||
given_amount: BigUint::from(1000000000000000000u64),
|
||||
expected_amount: BigUint::from(1000000000000000000u64),
|
||||
expected_amount: Some(BigUint::from(1000000000000000000u64)),
|
||||
checked_token: token_out,
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
|
||||
@@ -321,8 +323,37 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_swap_strategy_encoder_simple_route() {
|
||||
#[rstest]
|
||||
#[case::no_check_no_slippage(
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
U256::from_str("0").unwrap(),
|
||||
)]
|
||||
#[case::with_check_no_slippage(
|
||||
None,
|
||||
None,
|
||||
Some(BigUint::from_str("3_000_000000000000000000").unwrap()),
|
||||
U256::from_str("3_000_000000000000000000").unwrap(),
|
||||
)]
|
||||
#[case::no_check_with_slippage(
|
||||
Some(BigUint::from_str("3_000_000000000000000000").unwrap()),
|
||||
Some(0.01f64),
|
||||
None,
|
||||
U256::from_str("2_970_000000000000000000").unwrap(),
|
||||
)]
|
||||
#[case::with_check_and_slippage(
|
||||
Some(BigUint::from_str("3_000_000000000000000000").unwrap()),
|
||||
Some(0.01f64),
|
||||
Some(BigUint::from_str("2_999_000000000000000000").unwrap()),
|
||||
U256::from_str("2_999_000000000000000000").unwrap(),
|
||||
)]
|
||||
fn test_split_swap_strategy_encoder_simple_route(
|
||||
#[case] expected_amount: Option<BigUint>,
|
||||
#[case] slippage: Option<f64>,
|
||||
#[case] check_amount: Option<BigUint>,
|
||||
#[case] expected_min_amount: U256,
|
||||
) {
|
||||
// Performs a single swap from WETH to DAI on a USV2 pool
|
||||
|
||||
// Set up a mock private key for signing
|
||||
@@ -349,8 +380,9 @@ mod tests {
|
||||
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,
|
||||
expected_amount,
|
||||
slippage,
|
||||
check_amount,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
swaps: vec![swap],
|
||||
@@ -361,18 +393,20 @@ mod tests {
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution, router_address)
|
||||
.unwrap();
|
||||
let expected_min_amount_encoded = hex::encode(U256::abi_encode(&expected_min_amount));
|
||||
let expected_input = [
|
||||
"4860f9ed", // Function selector
|
||||
"0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out
|
||||
"000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out
|
||||
&expected_min_amount_encoded, // min amount out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // wrap
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // receiver
|
||||
]
|
||||
.join("");
|
||||
|
||||
let expected_input = String::from(concat!(
|
||||
"4860f9ed",
|
||||
"0000000000000000000000000000000000000000000000000de0b6b3a7640000", // amount out
|
||||
"000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f", // token out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // min amount out
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // wrap
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // unwrap
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // tokens length
|
||||
"000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2", // 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
|
||||
@@ -581,7 +615,7 @@ mod tests {
|
||||
given_token: weth,
|
||||
given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
|
||||
checked_token: usdc,
|
||||
expected_amount: BigUint::from_str("3_000_000000").unwrap(),
|
||||
expected_amount: Some(BigUint::from_str("3_000_000000").unwrap()),
|
||||
check_amount: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
|
||||
Reference in New Issue
Block a user