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

@@ -1,7 +1,7 @@
use std::io::{self, Read};
use clap::{Parser, Subcommand};
use tycho_core::models::Chain;
use tycho_common::models::Chain;
use tycho_execution::encoding::{
evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder,
};

View File

@@ -15,7 +15,7 @@ use tokio::{
runtime::{Handle, Runtime},
task::block_in_place,
};
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -175,7 +175,7 @@ mod tests {
use alloy_primitives::Uint;
use num_bigint::BigUint;
use tycho_core::models::Chain as TychoCoreChain;
use tycho_common::models::Chain as TychoCoreChain;
use super::*;

View File

@@ -11,5 +11,6 @@ pub static GROUPABLE_PROTOCOLS: LazyLock<HashSet<&'static str>> = LazyLock::new(
let mut set = HashSet::new();
set.insert("uniswap_v4");
set.insert("balancer_v3");
set.insert("ekubo");
set
});

View File

@@ -1,4 +1,4 @@
use tycho_core::models::Chain;
use tycho_common::models::Chain;
use crate::encoding::{
errors::EncodingError,

View File

@@ -1,4 +1,4 @@
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{evm::constants::GROUPABLE_PROTOCOLS, models::Swap};
@@ -74,7 +74,7 @@ mod tests {
use std::str::FromStr;
use alloy_primitives::hex;
use tycho_core::{models::protocol::ProtocolComponent, Bytes};
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
use crate::encoding::models::Swap;

View File

@@ -2,7 +2,7 @@ use std::{collections::HashSet, str::FromStr};
use alloy_primitives::{aliases::U24, U256, U8};
use alloy_sol_types::SolValue;
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -80,7 +80,7 @@ pub struct SplitSwapStrategyEncoder {
impl SplitSwapStrategyEncoder {
pub fn new(
blockchain: tycho_core::models::Chain,
blockchain: tycho_common::models::Chain,
swap_encoder_registry: SwapEncoderRegistry,
swapper_pk: Option<String>,
) -> Result<Self, EncodingError> {
@@ -341,7 +341,7 @@ mod tests {
use alloy_primitives::hex;
use num_bigint::{BigInt, BigUint};
use rstest::rstest;
use tycho_core::{
use tycho_common::{
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
Bytes,
};

View File

@@ -1,6 +1,6 @@
use std::collections::{HashMap, HashSet, VecDeque};
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -203,7 +203,7 @@ mod tests {
use num_bigint::BigUint;
use rstest::rstest;
use tycho_core::{models::protocol::ProtocolComponent, Bytes};
use tycho_common::{models::protocol::ProtocolComponent, Bytes};
use super::*;
use crate::encoding::models::Swap;

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",
),
);
}
}
}

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
use num_bigint::BigUint;
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,
@@ -34,7 +34,7 @@ impl Clone for EVMTychoEncoder {
impl EVMTychoEncoder {
pub fn new(
chain: tycho_core::models::Chain,
chain: tycho_common::models::Chain,
strategy_encoder: Box<dyn StrategyEncoder>,
) -> Result<Self, EncodingError> {
let chain: Chain = Chain::from(chain);
@@ -54,6 +54,8 @@ impl EVMTychoEncoder {
/// swap's input is the chain's wrapped token.
/// * If the solution is unwrapping, the checked token is the chain's native token and the last
/// swap's output is the chain's wrapped token.
/// * The token cannot appear more than once in the solution unless it is the first and last
/// token (i.e. a true cyclical swap).
fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
if solution.exact_out {
return Err(EncodingError::FatalError(
@@ -175,7 +177,7 @@ impl TychoEncoder for EVMTychoEncoder {
mod tests {
use std::str::FromStr;
use tycho_core::models::{protocol::ProtocolComponent, Chain as TychoCoreChain};
use tycho_common::models::{protocol::ProtocolComponent, Chain as TychoCoreChain};
use super::*;
use crate::encoding::{

View File

@@ -3,7 +3,7 @@ use std::{cmp::max, sync::Arc};
use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8};
use num_bigint::BigUint;
use tokio::runtime::{Handle, Runtime};
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{
errors::EncodingError,

View File

@@ -1,7 +1,7 @@
use hex;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use tycho_core::{
use tycho_common::{
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
Bytes,
};
@@ -130,6 +130,7 @@ impl From<TychoCoreChain> for Chain {
TychoCoreChain::Arbitrum => Chain { id: 42161, name: chain.to_string() },
TychoCoreChain::Starknet => Chain { id: 0, name: chain.to_string() },
TychoCoreChain::Base => Chain { id: 8453, name: chain.to_string() },
TychoCoreChain::Unichain => Chain { id: 130, name: chain.to_string() },
}
}
}

View File

@@ -1,4 +1,4 @@
use tycho_core::Bytes;
use tycho_common::Bytes;
use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder};