Merge branch 'main' into router/tnl/ENG-4409-pancake-v3-callback

This commit is contained in:
Tamara
2025-03-31 17:44:27 +02:00
committed by GitHub
31 changed files with 756 additions and 36 deletions

View File

@@ -19,7 +19,7 @@ impl SwapEncoderRegistry {
/// executors' addresses in the file at the given path.
pub fn new(
executors_file_path: Option<String>,
blockchain: tycho_core::models::Chain,
blockchain: tycho_common::models::Chain,
) -> Result<Self, EncodingError> {
let chain = Chain::from(blockchain);
let config_str = if let Some(ref path) = executors_file_path {

View File

@@ -2,7 +2,7 @@ use std::str::FromStr;
use alloy_primitives::{Address, Bytes as AlloyBytes};
use alloy_sol_types::SolValue;
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -258,13 +258,77 @@ impl SwapEncoder for BalancerV2SwapEncoder {
}
}
/// Encodes a swap on an Ekubo pool through the given executor address.
///
/// # Fields
/// * `executor_address` - The address of the executor contract that will perform the swap.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EkuboSwapEncoder {
executor_address: String,
}
impl SwapEncoder for EkuboSwapEncoder {
fn new(executor_address: String) -> Self {
Self { executor_address }
}
fn encode_swap(
&self,
swap: Swap,
encoding_context: EncodingContext,
) -> Result<Vec<u8>, EncodingError> {
if encoding_context.exact_out {
return Err(EncodingError::InvalidInput("exact out swaps not implemented".to_string()));
}
let fee = u64::from_be_bytes(
get_static_attribute(&swap, "fee")?
.try_into()
.map_err(|_| EncodingError::FatalError("fee should be an u64".to_string()))?,
);
let tick_spacing = u32::from_be_bytes(
get_static_attribute(&swap, "tick_spacing")?
.try_into()
.map_err(|_| {
EncodingError::FatalError("tick_spacing should be an u32".to_string())
})?,
);
let extension: Address = get_static_attribute(&swap, "extension")?
.as_slice()
.try_into()
.map_err(|_| EncodingError::FatalError("extension should be an address".to_string()))?;
let mut encoded = vec![];
if encoding_context.group_token_in == swap.token_in {
encoded.extend(bytes_to_address(&encoding_context.receiver)?);
encoded.extend(bytes_to_address(&swap.token_in)?);
}
encoded.extend(bytes_to_address(&swap.token_out)?);
encoded.extend((extension, fee, tick_spacing).abi_encode_packed());
Ok(encoded)
}
fn executor_address(&self) -> &str {
&self.executor_address
}
fn clone_box(&self) -> Box<dyn SwapEncoder> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use alloy::hex::encode;
use num_bigint::BigInt;
use tycho_core::{models::protocol::ProtocolComponent, Bytes};
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
@@ -636,4 +700,142 @@ mod tests {
))
);
}
mod ekubo {
use super::*;
const RECEIVER: &str = "ca4f73fe97d0b987a0d12b39bbd562c779bab6f6"; // Random address
#[test]
fn test_encode_swap_simple() {
let token_in = Bytes::from(Address::ZERO.as_slice());
let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
let static_attributes = HashMap::from([
("fee".to_string(), Bytes::from(0_u64)),
("tick_spacing".to_string(), Bytes::from(0_u32)),
(
"extension".to_string(),
Bytes::from("0x51d02a5948496a67827242eabc5725531342527c"),
), // Oracle
]);
let component = ProtocolComponent { static_attributes, ..Default::default() };
let swap = Swap {
component,
token_in: token_in.clone(),
token_out: token_out.clone(),
split: 0f64,
};
let encoding_context = EncodingContext {
receiver: RECEIVER.into(),
group_token_in: token_in.clone(),
group_token_out: token_out.clone(),
exact_out: false,
router_address: Bytes::default(),
};
let encoder = EkuboSwapEncoder::new(String::default());
let encoded_swap = encoder
.encode_swap(swap, encoding_context)
.unwrap();
let hex_swap = encode(&encoded_swap);
assert_eq!(
hex_swap,
RECEIVER.to_string() +
concat!(
// group token in
"0000000000000000000000000000000000000000",
// token out 1st swap
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
// pool config 1st swap
"51d02a5948496a67827242eabc5725531342527c000000000000000000000000",
),
);
}
#[test]
fn test_encode_swap_multi() {
let group_token_in = Bytes::from(Address::ZERO.as_slice());
let group_token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT
let intermediary_token = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
let encoder = EkuboSwapEncoder::new(String::default());
let encoding_context = EncodingContext {
receiver: RECEIVER.into(),
group_token_in: group_token_in.clone(),
group_token_out: group_token_out.clone(),
exact_out: false,
router_address: Bytes::default(),
};
let first_swap = Swap {
component: ProtocolComponent {
static_attributes: HashMap::from([
("fee".to_string(), Bytes::from(0_u64)),
("tick_spacing".to_string(), Bytes::from(0_u32)),
(
"extension".to_string(),
Bytes::from("0x51d02a5948496a67827242eabc5725531342527c"),
), // Oracle
]),
..Default::default()
},
token_in: group_token_in.clone(),
token_out: intermediary_token.clone(),
split: 0f64,
};
let second_swap = Swap {
component: ProtocolComponent {
// 0.0025% fee & 0.005% base pool
static_attributes: HashMap::from([
("fee".to_string(), Bytes::from(461168601842738_u64)),
("tick_spacing".to_string(), Bytes::from(50_u32)),
("extension".to_string(), Bytes::zero(20)),
]),
..Default::default()
},
token_in: intermediary_token.clone(),
token_out: group_token_out.clone(),
split: 0f64,
};
let first_encoded_swap = encoder
.encode_swap(first_swap, encoding_context.clone())
.unwrap();
let second_encoded_swap = encoder
.encode_swap(second_swap, encoding_context)
.unwrap();
let combined_hex =
format!("{}{}", encode(first_encoded_swap), encode(second_encoded_swap));
println!("{}", combined_hex);
assert_eq!(
combined_hex,
RECEIVER.to_string() +
concat!(
// group token in
"0000000000000000000000000000000000000000",
// token out 1st swap
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
// pool config 1st swap
"51d02a5948496a67827242eabc5725531342527c000000000000000000000000",
// token out 2nd swap
"dac17f958d2ee523a2206206994597c13d831ec7",
// pool config 2nd swap
"00000000000000000000000000000000000000000001a36e2eb1c43200000032",
),
);
}
}
}