Merge branch 'refs/heads/main' into feature/gas-optimization
# Conflicts: # foundry/test/TychoRouter.t.sol # src/encoding/evm/strategy_encoder/strategy_encoders.rs # src/encoding/evm/utils.rs Took 3 minutes Took 35 seconds
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::swap_encoder::swap_encoders::{
|
||||
BalancerV2SwapEncoder, EkuboSwapEncoder, UniswapV2SwapEncoder, UniswapV3SwapEncoder,
|
||||
UniswapV4SwapEncoder,
|
||||
BalancerV2SwapEncoder, CurveSwapEncoder, EkuboSwapEncoder, UniswapV2SwapEncoder,
|
||||
UniswapV3SwapEncoder, UniswapV4SwapEncoder,
|
||||
},
|
||||
models::Chain,
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
@@ -11,26 +14,68 @@ use crate::encoding::{
|
||||
pub struct SwapEncoderBuilder {
|
||||
protocol_system: String,
|
||||
executor_address: String,
|
||||
chain: Chain,
|
||||
config: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
impl SwapEncoderBuilder {
|
||||
pub fn new(protocol_system: &str, executor_address: &str) -> Self {
|
||||
pub fn new(
|
||||
protocol_system: &str,
|
||||
executor_address: &str,
|
||||
chain: Chain,
|
||||
config: Option<HashMap<String, String>>,
|
||||
) -> Self {
|
||||
SwapEncoderBuilder {
|
||||
protocol_system: protocol_system.to_string(),
|
||||
executor_address: executor_address.to_string(),
|
||||
chain,
|
||||
config,
|
||||
}
|
||||
}
|
||||
|
||||
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))),
|
||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"sushiswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"pancakeswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"uniswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"pancakeswap_v3" => Ok(Box::new(UniswapV3SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"uniswap_v4" => Ok(Box::new(UniswapV4SwapEncoder::new(
|
||||
self.executor_address,
|
||||
self.chain,
|
||||
self.config,
|
||||
)?)),
|
||||
"ekubo_v2" => {
|
||||
Ok(Box::new(EkuboSwapEncoder::new(self.executor_address, self.chain, self.config)?))
|
||||
}
|
||||
"vm:curve" => {
|
||||
Ok(Box::new(CurveSwapEncoder::new(self.executor_address, self.chain, self.config)?))
|
||||
}
|
||||
_ => Err(EncodingError::FatalError(format!(
|
||||
"Unknown protocol system: {}",
|
||||
self.protocol_system
|
||||
|
||||
@@ -2,7 +2,10 @@ use std::{collections::HashMap, fs};
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::{constants::DEFAULT_EXECUTORS_JSON, swap_encoder::builder::SwapEncoderBuilder},
|
||||
evm::{
|
||||
constants::{DEFAULT_EXECUTORS_JSON, PROTOCOL_SPECIFIC_CONFIG},
|
||||
swap_encoder::builder::SwapEncoderBuilder,
|
||||
},
|
||||
models::Chain,
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
@@ -29,12 +32,27 @@ impl SwapEncoderRegistry {
|
||||
DEFAULT_EXECUTORS_JSON.to_string()
|
||||
};
|
||||
let config: HashMap<String, HashMap<String, String>> = serde_json::from_str(&config_str)?;
|
||||
let mut encoders = HashMap::new();
|
||||
let executors = config
|
||||
.get(&chain.name)
|
||||
.ok_or(EncodingError::FatalError("No executors found for chain".to_string()))?;
|
||||
|
||||
let protocol_specific_config: HashMap<String, HashMap<String, HashMap<String, String>>> =
|
||||
serde_json::from_str(PROTOCOL_SPECIFIC_CONFIG)?;
|
||||
let protocol_specific_config = protocol_specific_config
|
||||
.get(&chain.name)
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"No protocol specific config found for chain".to_string(),
|
||||
))?;
|
||||
let mut encoders = HashMap::new();
|
||||
for (protocol, executor_address) in executors {
|
||||
let builder = SwapEncoderBuilder::new(protocol, executor_address);
|
||||
let builder = SwapEncoderBuilder::new(
|
||||
protocol,
|
||||
executor_address,
|
||||
chain.clone(),
|
||||
protocol_specific_config
|
||||
.get(protocol)
|
||||
.cloned(),
|
||||
);
|
||||
let encoder = builder.build()?;
|
||||
encoders.insert(protocol.to_string(), encoder);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
use std::str::FromStr;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use alloy_primitives::{Address, Bytes as AlloyBytes};
|
||||
use alloy::{
|
||||
providers::Provider,
|
||||
rpc::types::{TransactionInput, TransactionRequest},
|
||||
};
|
||||
use alloy_primitives::{Address, Bytes as AlloyBytes, TxKind, U256, U8};
|
||||
use alloy_sol_types::SolValue;
|
||||
use tokio::task::block_in_place;
|
||||
use tycho_common::Bytes;
|
||||
|
||||
use crate::encoding::{
|
||||
errors::EncodingError,
|
||||
evm::{
|
||||
approvals::protocol_approvals_manager::ProtocolApprovalsManager,
|
||||
utils::{bytes_to_address, get_static_attribute, pad_to_fixed_size},
|
||||
utils,
|
||||
utils::{
|
||||
bytes_to_address, encode_input, get_runtime, get_static_attribute, pad_to_fixed_size,
|
||||
},
|
||||
},
|
||||
models::{EncodingContext, Swap},
|
||||
models::{Chain, EncodingContext, Swap},
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
@@ -31,8 +39,12 @@ impl UniswapV2SwapEncoder {
|
||||
}
|
||||
|
||||
impl SwapEncoder for UniswapV2SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self { executor_address }
|
||||
fn new(
|
||||
executor_address: String,
|
||||
_chain: Chain,
|
||||
_config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
Ok(Self { executor_address })
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
@@ -85,8 +97,12 @@ impl UniswapV3SwapEncoder {
|
||||
}
|
||||
|
||||
impl SwapEncoder for UniswapV3SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self { executor_address }
|
||||
fn new(
|
||||
executor_address: String,
|
||||
_chain: Chain,
|
||||
_config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
Ok(Self { executor_address })
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
@@ -143,8 +159,12 @@ impl UniswapV4SwapEncoder {
|
||||
}
|
||||
|
||||
impl SwapEncoder for UniswapV4SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self { executor_address }
|
||||
fn new(
|
||||
executor_address: String,
|
||||
_chain: Chain,
|
||||
_config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
Ok(Self { executor_address })
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
@@ -216,12 +236,23 @@ pub struct BalancerV2SwapEncoder {
|
||||
}
|
||||
|
||||
impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self {
|
||||
executor_address,
|
||||
vault_address: "0xba12222222228d8ba445958a75a0704d566bf2c8".to_string(),
|
||||
}
|
||||
fn new(
|
||||
executor_address: String,
|
||||
_chain: Chain,
|
||||
config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let config = config.ok_or(EncodingError::FatalError(
|
||||
"Missing balancer specific addresses in config".to_string(),
|
||||
))?;
|
||||
let vault_address = config
|
||||
.get("vault_address")
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"Missing balancer vault address in config".to_string(),
|
||||
))?
|
||||
.to_string();
|
||||
Ok(Self { executor_address, vault_address })
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
&self,
|
||||
swap: Swap,
|
||||
@@ -274,8 +305,12 @@ pub struct EkuboSwapEncoder {
|
||||
}
|
||||
|
||||
impl SwapEncoder for EkuboSwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self { executor_address }
|
||||
fn new(
|
||||
executor_address: String,
|
||||
_chain: Chain,
|
||||
_config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
Ok(Self { executor_address })
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
@@ -328,13 +363,213 @@ impl SwapEncoder for EkuboSwapEncoder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes a swap on a Curve pool through the given executor address.
|
||||
///
|
||||
/// # Fields
|
||||
/// * `executor_address` - The address of the executor contract that will perform the swap.
|
||||
/// * `meta_registry_address` - The address of the Curve meta registry contract. Used to get coin
|
||||
/// indexes.
|
||||
/// * `native_token_curve_address` - The address used as native token in curve pools.
|
||||
/// * `native_token_address` - The address of the native token.
|
||||
#[derive(Clone)]
|
||||
pub struct CurveSwapEncoder {
|
||||
executor_address: String,
|
||||
meta_registry_address: String,
|
||||
native_token_curve_address: String,
|
||||
native_token_address: Bytes,
|
||||
}
|
||||
|
||||
impl CurveSwapEncoder {
|
||||
fn get_pool_type(&self, pool_id: &str, factory_address: &str) -> Result<U8, EncodingError> {
|
||||
match pool_id {
|
||||
// TriPool
|
||||
"0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7" => Ok(U8::from(1)),
|
||||
// STETHPool
|
||||
"0xDC24316b9AE028F1497c275EB9192a3Ea0f67022" => Ok(U8::from(1)),
|
||||
// TriCryptoPool
|
||||
"0xD51a44d3FaE010294C616388b506AcdA1bfAAE46" => Ok(U8::from(3)),
|
||||
// SUSDPool
|
||||
"0xA5407eAE9Ba41422680e2e00537571bcC53efBfD" => Ok(U8::from(1)),
|
||||
// FRAXUSDCPool
|
||||
"0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2" => Ok(U8::from(1)),
|
||||
_ => match factory_address {
|
||||
// CryptoSwapNG factory
|
||||
"0x6A8cbed756804B16E05E741eDaBd5cB544AE21bf" => Ok(U8::from(1)),
|
||||
// Metapool factory
|
||||
"0xB9fC157394Af804a3578134A6585C0dc9cc990d4" => Ok(U8::from(1)),
|
||||
// CryptoPool factory
|
||||
"0xF18056Bbd320E96A48e3Fbf8bC061322531aac99" => Ok(U8::from(2)),
|
||||
// Tricrypto factory
|
||||
"0x0c0e5f2fF0ff18a3be9b835635039256dC4B4963" => Ok(U8::from(3)),
|
||||
// Twocrypto factory
|
||||
"0x98EE851a00abeE0d95D08cF4CA2BdCE32aeaAF7F" => Ok(U8::from(2)),
|
||||
// StableSwap factory
|
||||
"0x4F8846Ae9380B90d2E71D5e3D042dff3E7ebb40d" => Ok(U8::from(1)),
|
||||
_ => Err(EncodingError::FatalError(format!(
|
||||
"Unsupported curve factory address: {}",
|
||||
factory_address
|
||||
))),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn get_coin_indexes(
|
||||
&self,
|
||||
pool_id: Address,
|
||||
token_in: Address,
|
||||
token_out: Address,
|
||||
) -> Result<(U8, U8), EncodingError> {
|
||||
let (handle, _runtime) = get_runtime()?;
|
||||
let client = block_in_place(|| handle.block_on(utils::get_client()))?;
|
||||
let args = (pool_id, token_in, token_out);
|
||||
let data = encode_input("get_coin_indices(address,address,address)", args.abi_encode());
|
||||
let tx = TransactionRequest {
|
||||
to: Some(TxKind::from(Address::from_str(&self.meta_registry_address).map_err(
|
||||
|_| EncodingError::FatalError("Invalid Curve meta registry address".to_string()),
|
||||
)?)),
|
||||
input: TransactionInput {
|
||||
input: Some(alloy_primitives::Bytes::from(data)),
|
||||
data: None,
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let output = block_in_place(|| handle.block_on(async { client.call(&tx).await }));
|
||||
type ResponseType = (U256, U256, bool);
|
||||
|
||||
match output {
|
||||
Ok(response) => {
|
||||
let (i_256, j_256, _): ResponseType = ResponseType::abi_decode(&response, true)
|
||||
.map_err(|_| {
|
||||
EncodingError::FatalError(
|
||||
"Failed to decode response when getting coin indexes on a curve pool"
|
||||
.to_string(),
|
||||
)
|
||||
})?;
|
||||
let i = U8::from(i_256);
|
||||
let j = U8::from(j_256);
|
||||
Ok((i, j))
|
||||
}
|
||||
Err(err) => Err(EncodingError::RecoverableError(format!(
|
||||
"Curve meta registry call failed with error: {:?}",
|
||||
err
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SwapEncoder for CurveSwapEncoder {
|
||||
fn new(
|
||||
executor_address: String,
|
||||
chain: Chain,
|
||||
config: Option<HashMap<String, String>>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let config = config.ok_or(EncodingError::FatalError(
|
||||
"Missing curve specific addresses in config".to_string(),
|
||||
))?;
|
||||
let native_token_curve_address = config
|
||||
.get("native_token_address")
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"Missing native token curve address in config".to_string(),
|
||||
))?
|
||||
.to_string();
|
||||
let meta_registry_address = config
|
||||
.get("meta_registry_address")
|
||||
.ok_or(EncodingError::FatalError(
|
||||
"Missing meta registry address in config".to_string(),
|
||||
))?
|
||||
.to_string();
|
||||
Ok(Self {
|
||||
executor_address,
|
||||
meta_registry_address,
|
||||
native_token_address: chain.native_token()?,
|
||||
native_token_curve_address,
|
||||
})
|
||||
}
|
||||
|
||||
fn encode_swap(
|
||||
&self,
|
||||
swap: Swap,
|
||||
encoding_context: EncodingContext,
|
||||
) -> Result<Vec<u8>, EncodingError> {
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new()?;
|
||||
let native_token_curve_address = Address::from_str(&self.native_token_curve_address)
|
||||
.map_err(|_| {
|
||||
EncodingError::FatalError("Invalid Curve native token curve address".to_string())
|
||||
})?;
|
||||
let token_in = if swap.token_in == self.native_token_address {
|
||||
native_token_curve_address
|
||||
} else {
|
||||
bytes_to_address(&swap.token_in)?
|
||||
};
|
||||
let token_out = if swap.token_out == self.native_token_address {
|
||||
native_token_curve_address
|
||||
} else {
|
||||
bytes_to_address(&swap.token_out)?
|
||||
};
|
||||
let approval_needed: bool;
|
||||
|
||||
let component_address = Address::from_str(&swap.component.id)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid curve pool address".to_string()))?;
|
||||
if let Some(router_address) = encoding_context.router_address {
|
||||
if token_in != native_token_curve_address {
|
||||
let tycho_router_address = bytes_to_address(&router_address)?;
|
||||
approval_needed = token_approvals_manager.approval_needed(
|
||||
token_in,
|
||||
tycho_router_address,
|
||||
component_address,
|
||||
)?;
|
||||
} else {
|
||||
approval_needed = false;
|
||||
}
|
||||
} else {
|
||||
approval_needed = true;
|
||||
}
|
||||
|
||||
let factory_bytes = get_static_attribute(&swap, "factory")?.to_vec();
|
||||
// the conversion to Address is necessary to checksum the address
|
||||
let factory_address =
|
||||
Address::from_str(std::str::from_utf8(&factory_bytes).map_err(|_| {
|
||||
EncodingError::FatalError(
|
||||
"Failed to convert curve factory address to string".to_string(),
|
||||
)
|
||||
})?)
|
||||
.map_err(|_| EncodingError::FatalError("Invalid curve factory address".to_string()))?;
|
||||
|
||||
let pool_type = self.get_pool_type(&swap.component.id, &factory_address.to_string())?;
|
||||
|
||||
let (i, j) = self.get_coin_indexes(component_address, token_in, token_out)?;
|
||||
|
||||
let args = (
|
||||
token_in,
|
||||
token_out,
|
||||
component_address,
|
||||
pool_type.to_be_bytes::<1>(),
|
||||
i.to_be_bytes::<1>(),
|
||||
j.to_be_bytes::<1>(),
|
||||
approval_needed,
|
||||
);
|
||||
|
||||
Ok(args.abi_encode_packed())
|
||||
}
|
||||
|
||||
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_common::{models::protocol::ProtocolComponent, Bytes};
|
||||
use tycho_common::{
|
||||
models::{protocol::ProtocolComponent, Chain as TychoCoreChain},
|
||||
Bytes,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -360,8 +595,12 @@ mod tests {
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder =
|
||||
UniswapV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||
let encoder = UniswapV2SwapEncoder::new(
|
||||
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
@@ -407,8 +646,12 @@ mod tests {
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder =
|
||||
UniswapV3SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||
let encoder = UniswapV3SwapEncoder::new(
|
||||
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
@@ -455,8 +698,15 @@ mod tests {
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder =
|
||||
BalancerV2SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||
let encoder = BalancerV2SwapEncoder::new(
|
||||
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
Some(HashMap::from([(
|
||||
"vault_address".to_string(),
|
||||
"0xba12222222228d8ba445958a75a0704d566bf2c8".to_string(),
|
||||
)])),
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
@@ -514,8 +764,12 @@ mod tests {
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder =
|
||||
UniswapV4SwapEncoder::new(String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"));
|
||||
let encoder = UniswapV4SwapEncoder::new(
|
||||
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
@@ -579,8 +833,12 @@ mod tests {
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
|
||||
let encoder =
|
||||
UniswapV4SwapEncoder::new(String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"));
|
||||
let encoder = UniswapV4SwapEncoder::new(
|
||||
String::from("0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
@@ -667,8 +925,12 @@ mod tests {
|
||||
split: 0f64,
|
||||
};
|
||||
|
||||
let encoder =
|
||||
UniswapV4SwapEncoder::new(String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"));
|
||||
let encoder = UniswapV4SwapEncoder::new(
|
||||
String::from("0xF62849F9A0B5Bf2913b396098F7c7019b51A820a"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let initial_encoded_swap = encoder
|
||||
.encode_swap(initial_swap, context.clone())
|
||||
.unwrap();
|
||||
@@ -743,7 +1005,9 @@ mod tests {
|
||||
router_address: Some(Bytes::default()),
|
||||
};
|
||||
|
||||
let encoder = EkuboSwapEncoder::new(String::default());
|
||||
let encoder =
|
||||
EkuboSwapEncoder::new(String::default(), TychoCoreChain::Ethereum.into(), None)
|
||||
.unwrap();
|
||||
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
@@ -771,7 +1035,9 @@ mod tests {
|
||||
let group_token_out = Bytes::from("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT
|
||||
let intermediary_token = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
|
||||
|
||||
let encoder = EkuboSwapEncoder::new(String::default());
|
||||
let encoder =
|
||||
EkuboSwapEncoder::new(String::default(), TychoCoreChain::Ethereum.into(), None)
|
||||
.unwrap();
|
||||
|
||||
let encoding_context = EncodingContext {
|
||||
receiver: RECEIVER.into(),
|
||||
@@ -844,4 +1110,288 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
mod curve {
|
||||
use rstest::rstest;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn curve_config() -> Option<HashMap<String, String>> {
|
||||
Some(HashMap::from([
|
||||
(
|
||||
"native_token_address".to_string(),
|
||||
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE".to_string(),
|
||||
),
|
||||
(
|
||||
"meta_registry_address".to_string(),
|
||||
"0xF98B45FA17DE75FB1aD0e7aFD971b0ca00e379fC".to_string(),
|
||||
),
|
||||
]))
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(
|
||||
"0x5500307Bcf134E5851FB4D7D8D1Dc556dCdB84B4",
|
||||
"0xdA16Cf041E2780618c49Dbae5d734B89a6Bac9b3",
|
||||
"0xdAC17F958D2ee523a2206206994597C13D831ec7",
|
||||
1,
|
||||
0
|
||||
)]
|
||||
#[case(
|
||||
"0xef484de8C07B6e2d732A92B5F78e81B38f99f95E",
|
||||
"0x865377367054516e17014CcdED1e7d814EDC9ce4",
|
||||
"0xA5588F7cdf560811710A2D82D3C9c99769DB1Dcb",
|
||||
0,
|
||||
1
|
||||
)]
|
||||
#[case(
|
||||
"0xA5407eAE9Ba41422680e2e00537571bcC53efBfD",
|
||||
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
||||
"0x57Ab1ec28D129707052df4dF418D58a2D46d5f51",
|
||||
1,
|
||||
3
|
||||
)]
|
||||
#[case(
|
||||
"0xD51a44d3FaE010294C616388b506AcdA1bfAAE46",
|
||||
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
||||
"0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
|
||||
2,
|
||||
1
|
||||
)]
|
||||
#[case(
|
||||
"0x7F86Bf177Dd4F3494b841a37e810A34dD56c829B",
|
||||
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
||||
"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
||||
2,
|
||||
0
|
||||
)]
|
||||
fn test_curve_get_coin_indexes(
|
||||
#[case] pool: &str,
|
||||
#[case] token_in: &str,
|
||||
#[case] token_out: &str,
|
||||
#[case] expected_i: u64,
|
||||
#[case] expected_j: u64,
|
||||
) {
|
||||
let encoder = CurveSwapEncoder::new(
|
||||
String::default(),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
curve_config(),
|
||||
)
|
||||
.unwrap();
|
||||
let (i, j) = encoder
|
||||
.get_coin_indexes(
|
||||
Address::from_str(pool).unwrap(),
|
||||
Address::from_str(token_in).unwrap(),
|
||||
Address::from_str(token_out).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(i, U8::from(expected_i));
|
||||
assert_eq!(j, U8::from(expected_j));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_curve_encode_tripool() {
|
||||
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
|
||||
static_attributes.insert(
|
||||
"factory".into(),
|
||||
Bytes::from(
|
||||
"0x0000000000000000000000000000000000000000"
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
),
|
||||
);
|
||||
let curve_tri_pool = ProtocolComponent {
|
||||
id: String::from("0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7"),
|
||||
protocol_system: String::from("vm:curve"),
|
||||
static_attributes,
|
||||
..Default::default()
|
||||
};
|
||||
let token_in = Bytes::from("0x6B175474E89094C44Da98b954EedeAC495271d0F");
|
||||
let token_out = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
|
||||
let swap = Swap {
|
||||
component: curve_tri_pool,
|
||||
token_in: token_in.clone(),
|
||||
token_out: token_out.clone(),
|
||||
split: 0f64,
|
||||
};
|
||||
let encoding_context = EncodingContext {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
|
||||
exact_out: false,
|
||||
router_address: None,
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder = CurveSwapEncoder::new(
|
||||
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
curve_config(),
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
let hex_swap = encode(&encoded_swap);
|
||||
|
||||
assert_eq!(
|
||||
hex_swap,
|
||||
String::from(concat!(
|
||||
// token in
|
||||
"6b175474e89094c44da98b954eedeac495271d0f",
|
||||
// token out
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
||||
// pool address
|
||||
"bebc44782c7db0a1a60cb6fe97d0b483032ff1c7",
|
||||
// pool type 1
|
||||
"01",
|
||||
// i index
|
||||
"00",
|
||||
// j index
|
||||
"01",
|
||||
// approval needed
|
||||
"01",
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_curve_encode_factory() {
|
||||
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
|
||||
static_attributes.insert(
|
||||
"factory".into(),
|
||||
Bytes::from(
|
||||
"0x6A8cbed756804B16E05E741eDaBd5cB544AE21bf"
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
),
|
||||
);
|
||||
let curve_pool = ProtocolComponent {
|
||||
id: String::from("0x02950460E2b9529D0E00284A5fA2d7bDF3fA4d72"),
|
||||
protocol_system: String::from("vm:curve"),
|
||||
static_attributes,
|
||||
..Default::default()
|
||||
};
|
||||
let token_in = Bytes::from("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
|
||||
let token_out = Bytes::from("0x4c9EDD5852cd905f086C759E8383e09bff1E68B3");
|
||||
let swap = Swap {
|
||||
component: curve_pool,
|
||||
token_in: token_in.clone(),
|
||||
token_out: token_out.clone(),
|
||||
split: 0f64,
|
||||
};
|
||||
let encoding_context = EncodingContext {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
|
||||
exact_out: false,
|
||||
router_address: None,
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder = CurveSwapEncoder::new(
|
||||
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
curve_config(),
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
let hex_swap = encode(&encoded_swap);
|
||||
|
||||
assert_eq!(
|
||||
hex_swap,
|
||||
String::from(concat!(
|
||||
// token in
|
||||
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
||||
// token out
|
||||
"4c9edd5852cd905f086c759e8383e09bff1e68b3",
|
||||
// pool address
|
||||
"02950460e2b9529d0e00284a5fa2d7bdf3fa4d72",
|
||||
// pool type 1
|
||||
"01",
|
||||
// i index
|
||||
"01",
|
||||
// j index
|
||||
"00",
|
||||
// approval needed
|
||||
"01",
|
||||
))
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_curve_encode_st_eth() {
|
||||
// This test is for the stETH pool, which is a special case in Curve
|
||||
// where the token in is ETH but not as the zero address.
|
||||
let mut static_attributes: HashMap<String, Bytes> = HashMap::new();
|
||||
static_attributes.insert(
|
||||
"factory".into(),
|
||||
Bytes::from(
|
||||
"0x0000000000000000000000000000000000000000"
|
||||
.as_bytes()
|
||||
.to_vec(),
|
||||
),
|
||||
);
|
||||
let curve_pool = ProtocolComponent {
|
||||
id: String::from("0xDC24316b9AE028F1497c275EB9192a3Ea0f67022"),
|
||||
protocol_system: String::from("vm:curve"),
|
||||
static_attributes,
|
||||
..Default::default()
|
||||
};
|
||||
let token_in = Bytes::from("0x0000000000000000000000000000000000000000");
|
||||
let token_out = Bytes::from("0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84");
|
||||
let swap = Swap {
|
||||
component: curve_pool,
|
||||
token_in: token_in.clone(),
|
||||
token_out: token_out.clone(),
|
||||
split: 0f64,
|
||||
};
|
||||
let encoding_context = EncodingContext {
|
||||
// The receiver was generated with `makeAddr("bob") using forge`
|
||||
receiver: Bytes::from("0x1d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e"),
|
||||
exact_out: false,
|
||||
router_address: None,
|
||||
group_token_in: token_in.clone(),
|
||||
group_token_out: token_out.clone(),
|
||||
};
|
||||
let encoder = CurveSwapEncoder::new(
|
||||
String::from("0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f"),
|
||||
TychoCoreChain::Ethereum.into(),
|
||||
Some(HashMap::from([
|
||||
(
|
||||
"native_token_address".to_string(),
|
||||
"0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE".to_string(),
|
||||
),
|
||||
(
|
||||
"meta_registry_address".to_string(),
|
||||
"0xF98B45FA17DE75FB1aD0e7aFD971b0ca00e379fC".to_string(),
|
||||
),
|
||||
])),
|
||||
)
|
||||
.unwrap();
|
||||
let encoded_swap = encoder
|
||||
.encode_swap(swap, encoding_context)
|
||||
.unwrap();
|
||||
let hex_swap = encode(&encoded_swap);
|
||||
|
||||
assert_eq!(
|
||||
hex_swap,
|
||||
String::from(concat!(
|
||||
// token in
|
||||
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
|
||||
// token out
|
||||
"ae7ab96520de3a18e5e111b5eaab095312d7fe84",
|
||||
// pool address
|
||||
"dc24316b9ae028f1497c275eb9192a3ea0f67022",
|
||||
// pool type 1
|
||||
"01",
|
||||
// i index
|
||||
"00",
|
||||
// j index
|
||||
"01",
|
||||
// approval needed
|
||||
"01",
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user