chore: merge main
Took 3 minutes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::io::{self, Read};
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use tycho_core::models::Chain;
|
||||
use tycho_common::{hex_bytes::Bytes, models::Chain};
|
||||
use tycho_execution::encoding::{
|
||||
evm::encoder_builder::EVMEncoderBuilder, models::Solution, tycho_encoder::TychoEncoder,
|
||||
};
|
||||
@@ -35,7 +35,6 @@ use tycho_execution::encoding::{
|
||||
/// "token_out": "0x...",
|
||||
/// "split": 0.0
|
||||
/// }],
|
||||
/// "router_address": "0x..."
|
||||
/// }
|
||||
/// ```
|
||||
#[command(author, version, about, long_about = None)]
|
||||
@@ -44,6 +43,8 @@ pub struct Cli {
|
||||
pub command: Commands,
|
||||
#[arg(short, long)]
|
||||
executors_file_path: Option<String>,
|
||||
#[arg(short, long)]
|
||||
router_address: Option<Bytes>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -79,6 +80,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(config_path) = cli.executors_file_path {
|
||||
builder = builder.executors_file_path(config_path);
|
||||
}
|
||||
if let Some(router_address) = cli.router_address {
|
||||
builder = builder.router_address(router_address);
|
||||
}
|
||||
|
||||
builder = match cli.command {
|
||||
Commands::TychoRouter => builder.initialize_tycho_router()?,
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::{collections::HashSet, sync::LazyLock};
|
||||
|
||||
pub const DEFAULT_EXECUTORS_JSON: &str = include_str!("../../../config/executor_addresses.json");
|
||||
pub const DEFAULT_ROUTERS_JSON: &str = include_str!("../../../config/router_addresses.json");
|
||||
|
||||
/// These protocols support the optimization of grouping swaps.
|
||||
///
|
||||
@@ -11,5 +12,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_v2");
|
||||
set
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use tycho_core::models::Chain;
|
||||
use tycho_common::{models::Chain, Bytes};
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
@@ -17,6 +17,7 @@ pub struct EVMEncoderBuilder {
|
||||
strategy: Option<Box<dyn StrategyEncoder>>,
|
||||
chain: Option<Chain>,
|
||||
executors_file_path: Option<String>,
|
||||
router_address: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Default for EVMEncoderBuilder {
|
||||
@@ -27,7 +28,12 @@ impl Default for EVMEncoderBuilder {
|
||||
|
||||
impl EVMEncoderBuilder {
|
||||
pub fn new() -> Self {
|
||||
EVMEncoderBuilder { chain: None, strategy: None, executors_file_path: None }
|
||||
EVMEncoderBuilder {
|
||||
chain: None,
|
||||
strategy: None,
|
||||
executors_file_path: None,
|
||||
router_address: None,
|
||||
}
|
||||
}
|
||||
pub fn chain(mut self, chain: Chain) -> Self {
|
||||
self.chain = Some(chain);
|
||||
@@ -41,6 +47,13 @@ impl EVMEncoderBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `router_address` manually.
|
||||
/// If it's not set, the default router address will be used (config/router_addresses.json)
|
||||
pub fn router_address(mut self, router_address: Bytes) -> Self {
|
||||
self.router_address = Some(router_address);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the `strategy_encoder` manually.
|
||||
///
|
||||
/// **Note**: This method should not be used in combination with `tycho_router` or
|
||||
@@ -56,12 +69,17 @@ impl EVMEncoderBuilder {
|
||||
if let Some(chain) = self.chain {
|
||||
let swap_encoder_registry =
|
||||
SwapEncoderRegistry::new(self.executors_file_path.clone(), chain)?;
|
||||
let strategy =
|
||||
Box::new(SplitSwapStrategyEncoder::new(chain, swap_encoder_registry, None)?);
|
||||
let strategy = Box::new(SplitSwapStrategyEncoder::new(
|
||||
chain,
|
||||
swap_encoder_registry,
|
||||
None,
|
||||
self.router_address.clone(),
|
||||
)?);
|
||||
Ok(EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
@@ -83,11 +101,13 @@ impl EVMEncoderBuilder {
|
||||
chain,
|
||||
swap_encoder_registry,
|
||||
Some(swapper_pk),
|
||||
self.router_address.clone(),
|
||||
)?);
|
||||
Ok(EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
@@ -107,6 +127,7 @@ impl EVMEncoderBuilder {
|
||||
chain: Some(chain),
|
||||
strategy: Some(strategy),
|
||||
executors_file_path: self.executors_file_path,
|
||||
router_address: self.router_address,
|
||||
})
|
||||
} else {
|
||||
Err(EncodingError::FatalError(
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
use std::{collections::HashSet, str::FromStr};
|
||||
use std::{
|
||||
collections::{HashMap, 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,
|
||||
evm::{
|
||||
approvals::permit2::Permit2,
|
||||
constants::DEFAULT_ROUTERS_JSON,
|
||||
strategy_encoder::{group_swaps::group_swaps, strategy_validators::SplitSwapValidator},
|
||||
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
|
||||
utils::{
|
||||
@@ -68,6 +72,7 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
|
||||
/// * `wrapped_address`: Address of the chain's wrapped token
|
||||
/// * `split_swap_validator`: SplitSwapValidator, responsible for checking validity of split swap
|
||||
/// solutions
|
||||
/// * `router_address`: Address of the router to be used to execute swaps
|
||||
#[derive(Clone)]
|
||||
pub struct SplitSwapStrategyEncoder {
|
||||
swap_encoder_registry: SwapEncoderRegistry,
|
||||
@@ -76,13 +81,15 @@ pub struct SplitSwapStrategyEncoder {
|
||||
native_address: Bytes,
|
||||
wrapped_address: Bytes,
|
||||
split_swap_validator: SplitSwapValidator,
|
||||
router_address: Bytes,
|
||||
}
|
||||
|
||||
impl SplitSwapStrategyEncoder {
|
||||
pub fn new(
|
||||
blockchain: tycho_core::models::Chain,
|
||||
blockchain: tycho_common::models::Chain,
|
||||
swap_encoder_registry: SwapEncoderRegistry,
|
||||
swapper_pk: Option<String>,
|
||||
router_address: Option<Bytes>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let chain = Chain::from(blockchain);
|
||||
let (permit2, selector) = if let Some(swapper_pk) = swapper_pk {
|
||||
@@ -94,6 +101,21 @@ impl SplitSwapStrategyEncoder {
|
||||
.to_string(),
|
||||
)
|
||||
};
|
||||
|
||||
let tycho_router_address;
|
||||
if let Some(address) = router_address {
|
||||
tycho_router_address = address;
|
||||
} else {
|
||||
let default_routers: HashMap<String, Bytes> =
|
||||
serde_json::from_str(DEFAULT_ROUTERS_JSON)?;
|
||||
tycho_router_address = default_routers
|
||||
.get(&chain.name)
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"No default router address found for chain".to_string(),
|
||||
))?
|
||||
.to_owned();
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
permit2,
|
||||
selector,
|
||||
@@ -101,6 +123,7 @@ impl SplitSwapStrategyEncoder {
|
||||
native_address: chain.native_token()?,
|
||||
wrapped_address: chain.wrapped_token()?,
|
||||
split_swap_validator: SplitSwapValidator,
|
||||
router_address: tycho_router_address,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -183,9 +206,9 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
let mut grouped_protocol_data: Vec<u8> = vec![];
|
||||
for swap in grouped_swap.swaps.iter() {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: solution.router_address.clone(),
|
||||
receiver: self.router_address.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: solution.router_address.clone(),
|
||||
router_address: Some(self.router_address.clone()),
|
||||
group_token_in: grouped_swap.input_token.clone(),
|
||||
group_token_out: grouped_swap.output_token.clone(),
|
||||
};
|
||||
@@ -214,7 +237,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
};
|
||||
let method_calldata = if let Some(permit2) = self.permit2.clone() {
|
||||
let (permit, signature) = permit2.get_permit(
|
||||
&solution.router_address,
|
||||
&self.router_address,
|
||||
&solution.sender,
|
||||
&solution.given_token,
|
||||
&solution.given_amount,
|
||||
@@ -249,7 +272,7 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
|
||||
};
|
||||
|
||||
let contract_interaction = encode_input(&self.selector, method_calldata);
|
||||
Ok((contract_interaction, solution.router_address))
|
||||
Ok((contract_interaction, self.router_address.clone()))
|
||||
}
|
||||
|
||||
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||
@@ -294,7 +317,6 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||
.ok_or_else(|| EncodingError::FatalError("Swap grouping failed".to_string()))?;
|
||||
|
||||
let receiver = solution.receiver;
|
||||
let router_address = solution.router_address;
|
||||
|
||||
let swap_encoder = self
|
||||
.get_swap_encoder(&grouped_swap.protocol_system)
|
||||
@@ -310,7 +332,7 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: receiver.clone(),
|
||||
exact_out: solution.exact_out,
|
||||
router_address: router_address.clone(),
|
||||
router_address: None,
|
||||
group_token_in: grouped_swap.input_token.clone(),
|
||||
group_token_out: grouped_swap.output_token.clone(),
|
||||
};
|
||||
@@ -339,10 +361,10 @@ mod tests {
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use alloy::hex::encode;
|
||||
use alloy_primitives::hex;
|
||||
use alloy_primitives::{hex, Address};
|
||||
use num_bigint::{BigInt, BigUint};
|
||||
use rstest::rstest;
|
||||
use tycho_core::{
|
||||
use tycho_common::{
|
||||
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
|
||||
Bytes,
|
||||
};
|
||||
@@ -364,7 +386,8 @@ mod tests {
|
||||
|
||||
fn get_swap_encoder_registry() -> SwapEncoderRegistry {
|
||||
let eth_chain = eth_chain();
|
||||
SwapEncoderRegistry::new(None, eth_chain).unwrap()
|
||||
SwapEncoderRegistry::new(Some("config/test_executor_addresses.json".to_string()), eth_chain)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -397,7 +420,6 @@ mod tests {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap],
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
};
|
||||
@@ -408,7 +430,7 @@ mod tests {
|
||||
let hex_protocol_data = encode(&protocol_data);
|
||||
assert_eq!(
|
||||
executor_address,
|
||||
Bytes::from_str("0xf6c5be66FFf9DC69962d73da0A617a827c382329").unwrap()
|
||||
Bytes::from_str("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
hex_protocol_data,
|
||||
@@ -453,7 +475,6 @@ mod tests {
|
||||
sender: Bytes::from_str("0x0000000000000000000000000000000000000000").unwrap(),
|
||||
receiver: Bytes::from_str("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e").unwrap(),
|
||||
swaps: vec![swap.clone(), swap],
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
slippage: None,
|
||||
native_action: None,
|
||||
};
|
||||
@@ -522,7 +543,6 @@ mod tests {
|
||||
slippage: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_usdc_eth, swap_eth_pepe],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -533,7 +553,7 @@ mod tests {
|
||||
let hex_protocol_data = encode(&protocol_data);
|
||||
assert_eq!(
|
||||
executor_address,
|
||||
Bytes::from_str("0x042C0ebBEAb9d9987c2f64Ee05f2B3aeB86eAf70").unwrap()
|
||||
Bytes::from_str("0xf62849f9a0b5bf2913b396098f7c7019b51a820a").unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
hex_protocol_data,
|
||||
@@ -545,7 +565,7 @@ mod tests {
|
||||
// zero for one
|
||||
"00",
|
||||
// executor address
|
||||
"042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70",
|
||||
"f62849f9a0b5bf2913b396098f7c7019b51a820a",
|
||||
// first pool intermediary token (ETH)
|
||||
"0000000000000000000000000000000000000000",
|
||||
// fee
|
||||
@@ -607,9 +627,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: weth,
|
||||
@@ -620,7 +644,6 @@ mod tests {
|
||||
checked_amount,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -671,7 +694,7 @@ mod tests {
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
// Swap data
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address
|
||||
"5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
@@ -708,9 +731,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: eth(),
|
||||
@@ -720,7 +747,6 @@ mod tests {
|
||||
checked_amount: Some(BigUint::from_str("2659881924818443699787").unwrap()),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
@@ -757,9 +783,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: dai,
|
||||
@@ -769,7 +799,6 @@ mod tests {
|
||||
checked_amount: Some(BigUint::from_str("1_000000000000000000").unwrap()),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Unwrap),
|
||||
..Default::default()
|
||||
@@ -847,9 +876,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: weth,
|
||||
@@ -859,7 +892,6 @@ mod tests {
|
||||
checked_amount: Some(BigUint::from_str("26173932").unwrap()),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_weth_dai, swap_weth_wbtc, swap_dai_usdc, swap_wbtc_usdc],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -928,9 +960,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: usdc,
|
||||
@@ -941,7 +977,6 @@ mod tests {
|
||||
slippage: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_usdc_eth, swap_eth_pepe],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -992,12 +1027,12 @@ mod tests {
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
// Swap data header
|
||||
"042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70", // executor address
|
||||
"f62849f9a0b5bf2913b396098f7c7019b51a820a", // executor address
|
||||
// Protocol data
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // group token in
|
||||
"6982508145454ce325ddbe47a25d4ec3d2311933", // group token in
|
||||
"00", // zero2one
|
||||
"042c0ebbeab9d9987c2f64ee05f2b3aeb86eaf70", // executor address
|
||||
"f62849f9a0b5bf2913b396098f7c7019b51a820a", // executor address
|
||||
// First pool params
|
||||
"0000000000000000000000000000000000000000", // intermediary token (ETH)
|
||||
"000bb8", // fee
|
||||
@@ -1013,6 +1048,67 @@ mod tests {
|
||||
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_encoding_strategy_ekubo() {
|
||||
// ETH ──(EKUBO)──> USDC
|
||||
|
||||
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 {
|
||||
// All Ekubo swaps go through the core contract - not necessary to specify pool id
|
||||
// for test
|
||||
protocol_system: "ekubo_v2".to_string(),
|
||||
static_attributes,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let swap = Swap {
|
||||
component,
|
||||
token_in: token_in.clone(),
|
||||
token_out: token_out.clone(),
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
None,
|
||||
Some(Bytes::from_str("0x1d1499e622D69689cdf9004d05Ec547d650Ff211").unwrap()),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: token_in,
|
||||
given_amount: BigUint::from_str("1_000000000000000000").unwrap(),
|
||||
checked_token: token_out,
|
||||
expected_amount: None,
|
||||
checked_amount: Some(BigUint::from_str("1").unwrap()),
|
||||
slippage: None,
|
||||
// Alice
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
swaps: vec![swap],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (calldata, _) = encoder
|
||||
.encode_strategy(solution)
|
||||
.unwrap();
|
||||
|
||||
let hex_calldata = encode(&calldata);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1039,8 +1135,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, None).unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
None,
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_token: weth,
|
||||
@@ -1051,7 +1152,6 @@ mod tests {
|
||||
checked_amount,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -1078,7 +1178,7 @@ mod tests {
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
// Swap data
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address
|
||||
"5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a478c2975ab1ea89e8196811f51a7b7ade33eb11", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // receiver
|
||||
@@ -1128,9 +1228,13 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
@@ -1142,7 +1246,6 @@ mod tests {
|
||||
slippage: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_eth_pepe],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -1192,9 +1295,13 @@ mod tests {
|
||||
};
|
||||
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
@@ -1206,7 +1313,6 @@ mod tests {
|
||||
slippage: None,
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
swaps: vec![swap_usdc_eth],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -1276,9 +1382,13 @@ mod tests {
|
||||
};
|
||||
|
||||
let swap_encoder_registry = get_swap_encoder_registry();
|
||||
let encoder =
|
||||
SplitSwapStrategyEncoder::new(eth_chain(), swap_encoder_registry, Some(private_key))
|
||||
.unwrap();
|
||||
let encoder = SplitSwapStrategyEncoder::new(
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
@@ -1290,7 +1400,6 @@ mod tests {
|
||||
* test */
|
||||
slippage: None,
|
||||
swaps: vec![swap_usdc_weth, swap_weth_usdc],
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
..Default::default()
|
||||
@@ -1319,7 +1428,7 @@ mod tests {
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"0001f4", // pool fee
|
||||
@@ -1329,7 +1438,7 @@ mod tests {
|
||||
"006d", // ple encoded swaps
|
||||
"01", // token in index
|
||||
"00000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"000bb8", // pool fee
|
||||
@@ -1341,6 +1450,7 @@ mod tests {
|
||||
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1428,6 +1538,7 @@ mod tests {
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key.clone()),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -1439,7 +1550,6 @@ mod tests {
|
||||
expected_amount: None,
|
||||
checked_amount: Some(BigUint::from_str("99574171").unwrap()), /* Expected output from
|
||||
* test */
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
slippage: None,
|
||||
@@ -1470,7 +1580,7 @@ mod tests {
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"999999", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"0001f4", // pool fee
|
||||
@@ -1481,7 +1591,7 @@ mod tests {
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token out
|
||||
"000bb8", // pool fee
|
||||
@@ -1492,7 +1602,7 @@ mod tests {
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"000000", // split
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address,
|
||||
"5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address,
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id,
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
@@ -1502,6 +1612,7 @@ mod tests {
|
||||
.join("");
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1585,6 +1696,7 @@ mod tests {
|
||||
eth_chain(),
|
||||
swap_encoder_registry,
|
||||
Some(private_key.clone()),
|
||||
Some(Bytes::from("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -1596,7 +1708,6 @@ mod tests {
|
||||
expected_amount: None,
|
||||
checked_amount: Some(BigUint::from_str("99525908").unwrap()), /* Expected output from
|
||||
* test */
|
||||
router_address: Bytes::from_str("0x3Ede3eCa2a72B3aeCC820E955B36f38437D01395").unwrap(),
|
||||
sender: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
receiver: Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2").unwrap(),
|
||||
slippage: None,
|
||||
@@ -1628,7 +1739,7 @@ mod tests {
|
||||
"00", // token in index
|
||||
"01", // token out index
|
||||
"000000", // split
|
||||
"f6c5be66fff9dc69962d73da0a617a827c382329", // executor address
|
||||
"5615deb798bb3e4dfa0139dfa1b3d433cc23b72f", // executor address
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token in
|
||||
"b4e16d0168e52d35cacd2c6185b44281ec28c9dc", // component id
|
||||
"3ede3eca2a72b3aecc820e955b36f38437d01395", // router address
|
||||
@@ -1637,7 +1748,7 @@ mod tests {
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"999999", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"0001f4", // pool fee
|
||||
@@ -1648,7 +1759,7 @@ mod tests {
|
||||
"01", // token in index
|
||||
"00", // token out index
|
||||
"000000", // split
|
||||
"dd8559c917393fc8dd2b4dd289c52ff445fde1b0", // executor address
|
||||
"2e234dae75c793f67a35089c9d99245e1c58470b", // executor address
|
||||
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", // token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // token out
|
||||
"000bb8", // pool fee
|
||||
@@ -1661,5 +1772,6 @@ mod tests {
|
||||
|
||||
assert_eq!(hex_calldata[..520], expected_input);
|
||||
assert_eq!(hex_calldata[1288..], expected_swaps);
|
||||
println!("{}", hex_calldata);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::swap_encoder::swap_encoders::{
|
||||
BalancerV2SwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder, UniswapV4SwapEncoder,
|
||||
BalancerV2SwapEncoder, EkuboSwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder,
|
||||
UniswapV4SwapEncoder,
|
||||
},
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
@@ -23,9 +24,13 @@ impl SwapEncoderBuilder {
|
||||
pub fn build(self) -> Result<Box<dyn SwapEncoder>, EncodingError> {
|
||||
match self.protocol_system.as_str() {
|
||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"sushiswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"pancakeswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
|
||||
"uniswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(self.executor_address))),
|
||||
"pancakeswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(self.executor_address))),
|
||||
"uniswap_v4" => Ok(Box::new(UniswapV4SwapEncoder::new(self.executor_address))),
|
||||
"ekubo_v2" => Ok(Box::new(EkuboSwapEncoder::new(self.executor_address))),
|
||||
_ => Err(EncodingError::FatalError(format!(
|
||||
"Unknown protocol system: {}",
|
||||
self.protocol_system
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
@@ -229,13 +229,19 @@ impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
) -> Result<Vec<u8>, EncodingError> {
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new()?;
|
||||
let token = bytes_to_address(&swap.token_in)?;
|
||||
let router_address = bytes_to_address(&encoding_context.router_address)?;
|
||||
let approval_needed = token_approvals_manager.approval_needed(
|
||||
token,
|
||||
router_address,
|
||||
Address::from_str(&self.vault_address)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid vault address".to_string()))?,
|
||||
)?;
|
||||
let approval_needed: bool;
|
||||
|
||||
if let Some(router_address) = encoding_context.router_address {
|
||||
let tycho_router_address = bytes_to_address(&router_address)?;
|
||||
approval_needed = token_approvals_manager.approval_needed(
|
||||
token,
|
||||
tycho_router_address,
|
||||
Address::from_str(&self.vault_address)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid vault address".to_string()))?,
|
||||
)?;
|
||||
} else {
|
||||
approval_needed = true;
|
||||
}
|
||||
|
||||
let component_id = AlloyBytes::from_str(&swap.component.id)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid component ID".to_string()))?;
|
||||
@@ -258,13 +264,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::*;
|
||||
|
||||
@@ -286,7 +356,7 @@ mod tests {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
|
||||
exact_out: false,
|
||||
router_address: Bytes::zero(20),
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
@@ -333,7 +403,7 @@ mod tests {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
|
||||
exact_out: false,
|
||||
router_address: Bytes::zero(20),
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
@@ -381,7 +451,7 @@ mod tests {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
|
||||
exact_out: false,
|
||||
router_address: Bytes::zero(20),
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
@@ -439,7 +509,7 @@ mod tests {
|
||||
receiver: Bytes::from("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f"),
|
||||
exact_out: false,
|
||||
// Same as the executor address
|
||||
router_address: Bytes::from("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f"),
|
||||
router_address: Some(Bytes::from("0x5615deb798bb3e4dfa0139dfa1b3d433cc23b72f")),
|
||||
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
@@ -503,7 +573,7 @@ mod tests {
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
|
||||
exact_out: false,
|
||||
router_address: Bytes::zero(20),
|
||||
router_address: Some(Bytes::zero(20)),
|
||||
group_token_in: group_token_in.clone(),
|
||||
// Token out is the same as the group token out
|
||||
group_token_out: token_out.clone(),
|
||||
@@ -542,7 +612,7 @@ mod tests {
|
||||
let context = EncodingContext {
|
||||
receiver: receiver_address.clone(),
|
||||
exact_out: false,
|
||||
router_address: router_address.clone(),
|
||||
router_address: Some(router_address.clone()),
|
||||
group_token_in: usde_address.clone(),
|
||||
group_token_out: wbtc_address.clone(),
|
||||
};
|
||||
@@ -636,4 +706,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: Some(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: Some(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",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::{
|
||||
@@ -247,7 +249,6 @@ mod tests {
|
||||
exact_out: false,
|
||||
given_amount: eth_amount_in.clone(),
|
||||
given_token: eth(),
|
||||
router_address: Bytes::from_str("0x1234567890abcdef1234567890abcdef12345678").unwrap(),
|
||||
swaps: vec![swap],
|
||||
native_action: Some(NativeAction::Wrap),
|
||||
..Default::default()
|
||||
|
||||
@@ -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,
|
||||
@@ -74,10 +74,10 @@ pub fn get_min_amount_for_solution(solution: Solution) -> BigUint {
|
||||
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;
|
||||
let bps = BigUint::from(10_000u32);
|
||||
let slippage_percent = BigUint::from((slippage * 10000.0) as u32);
|
||||
let multiplier = &bps - slippage_percent;
|
||||
let expected_amount_with_slippage = (expected_amount * &multiplier) / &bps;
|
||||
min_amount_out = max(min_amount_out, expected_amount_with_slippage);
|
||||
}
|
||||
min_amount_out
|
||||
@@ -133,3 +133,27 @@ pub fn get_runtime() -> Result<(Handle, Option<Arc<Runtime>>), EncodingError> {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use num_bigint::BigUint;
|
||||
|
||||
use super::*;
|
||||
use crate::encoding::models::Solution;
|
||||
|
||||
#[test]
|
||||
fn test_min_amount_out_small_slippage() {
|
||||
// Tests that the calculation's precision is high enough to support a slippage of 0.1%.
|
||||
|
||||
let solution = Solution {
|
||||
exact_out: false,
|
||||
given_amount: BigUint::from(1000000000000000000u64),
|
||||
checked_amount: None,
|
||||
slippage: Some(0.001f64),
|
||||
expected_amount: Some(BigUint::from(1000000000000000000u64)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let min_amount_out = get_min_amount_for_solution(solution);
|
||||
assert_eq!(min_amount_out, BigUint::from(999000000000000000u64));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -41,8 +41,6 @@ pub struct Solution {
|
||||
pub checked_amount: Option<BigUint>,
|
||||
/// List of swaps to fulfill the solution.
|
||||
pub swaps: Vec<Swap>,
|
||||
/// Address of the router contract to be used for the swaps.
|
||||
pub router_address: Bytes,
|
||||
/// If set, the corresponding native action will be executed.
|
||||
pub native_action: Option<NativeAction>,
|
||||
}
|
||||
@@ -104,14 +102,15 @@ pub struct Transaction {
|
||||
///
|
||||
/// * `receiver`: Address of the receiver of the out token after the swaps are completed.
|
||||
/// * `exact_out`: true if the solution is a buy order, false if it is a sell order.
|
||||
/// * `router_address`: Address of the router contract to be used for the swaps.
|
||||
/// * `router_address`: Address of the router contract to be used for the swaps. Zero address if
|
||||
/// solution does not require router address.
|
||||
/// * `group_token_in`: Token to be used as the input for the group swap.
|
||||
/// * `group_token_out`: Token to be used as the output for the group swap.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct EncodingContext {
|
||||
pub receiver: Bytes,
|
||||
pub exact_out: bool,
|
||||
pub router_address: Bytes,
|
||||
pub router_address: Option<Bytes>,
|
||||
pub group_token_in: Bytes,
|
||||
pub group_token_out: Bytes,
|
||||
}
|
||||
@@ -130,6 +129,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() },
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,6 +148,7 @@ impl Chain {
|
||||
self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg)
|
||||
}
|
||||
324 => self.decode_hex("000000000000000000000000000000000000800A", decode_err_msg),
|
||||
130 => self.decode_hex("0000000000000000000000000000000000000000", decode_err_msg),
|
||||
_ => Err(EncodingError::InvalidInput(format!(
|
||||
"Native token not set for chain {:?}. Double check the chain is supported.",
|
||||
self.name
|
||||
@@ -162,6 +163,7 @@ impl Chain {
|
||||
8453 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
|
||||
324 => self.decode_hex("5AEa5775959fBC2557Cc8789bC1bf90A239D9a91", decode_err_msg),
|
||||
42161 => self.decode_hex("82aF49447D8a07e3bd95BD0d56f35241523fBab1", decode_err_msg),
|
||||
130 => self.decode_hex("4200000000000000000000000000000000000006", decode_err_msg),
|
||||
_ => Err(EncodingError::InvalidInput(format!(
|
||||
"Wrapped token not set for chain {:?}. Double check the chain is supported.",
|
||||
self.name
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use tycho_core::Bytes;
|
||||
use tycho_common::Bytes;
|
||||
|
||||
use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user