feat: Add evm feature gate
- Move all evm code inside evm directory - StrategyEncoder: - Kept StrategyEncoder trait but created a new one: EVMStrategyEncoder to implement encode_protocol_header (that is evm specific). - All StrategyEncoders implement both traits now - Renamed DefaultStrategySelector -> EVMStrategySelector - RouterEncoder: - Created a RouterEncoder trait and a EVMRouterEncoder that implements it - Moved utils inside evm directory as well - Renamed config.json -> executor_addresses.json and moved it to a higher config directory - Make alloy optional and dependent on the evm feature gate --- don't change below this line --- ENG-4075 <#DTT#>
This commit is contained in:
26
src/encoding/evm/swap_encoder/builder.rs
Normal file
26
src/encoding/evm/swap_encoder/builder.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::encoding::{
|
||||
evm::swap_encoder::encoders::{BalancerV2SwapEncoder, UniswapV2SwapEncoder},
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
pub struct SwapEncoderBuilder {
|
||||
protocol_system: String,
|
||||
executor_address: String,
|
||||
}
|
||||
|
||||
impl SwapEncoderBuilder {
|
||||
pub fn new(protocol_system: &str, executor_address: &str) -> Self {
|
||||
SwapEncoderBuilder {
|
||||
protocol_system: protocol_system.to_string(),
|
||||
executor_address: executor_address.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Box<dyn SwapEncoder>, String> {
|
||||
match self.protocol_system.as_str() {
|
||||
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
|
||||
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
|
||||
_ => Err(format!("Unknown protocol system: {}", self.protocol_system)),
|
||||
}
|
||||
}
|
||||
}
|
||||
87
src/encoding/evm/swap_encoder/encoders.rs
Normal file
87
src/encoding/evm/swap_encoder/encoders.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use alloy_primitives::Address;
|
||||
use alloy_sol_types::SolValue;
|
||||
use anyhow::Error;
|
||||
|
||||
use crate::encoding::{
|
||||
evm::{
|
||||
approvals::protocol_approvals_manager::ProtocolApprovalsManager, utils::bytes_to_address,
|
||||
},
|
||||
models::{EncodingContext, Swap},
|
||||
swap_encoder::SwapEncoder,
|
||||
};
|
||||
|
||||
pub struct UniswapV2SwapEncoder {
|
||||
executor_address: String,
|
||||
}
|
||||
|
||||
impl UniswapV2SwapEncoder {}
|
||||
impl SwapEncoder for UniswapV2SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self { executor_address }
|
||||
}
|
||||
fn encode_swap(
|
||||
&self,
|
||||
_swap: Swap,
|
||||
_encoding_context: EncodingContext,
|
||||
) -> Result<Vec<u8>, Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn executor_address(&self) -> &str {
|
||||
&self.executor_address
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BalancerV2SwapEncoder {
|
||||
executor_address: String,
|
||||
vault_address: Address,
|
||||
}
|
||||
|
||||
impl SwapEncoder for BalancerV2SwapEncoder {
|
||||
fn new(executor_address: String) -> Self {
|
||||
Self {
|
||||
executor_address,
|
||||
vault_address: Address::from_str("0xba12222222228d8ba445958a75a0704d566bf2c8")
|
||||
.expect("Invalid string for balancer vault address"),
|
||||
}
|
||||
}
|
||||
fn encode_swap(&self, swap: Swap, encoding_context: EncodingContext) -> Result<Vec<u8>, Error> {
|
||||
let token_approvals_manager = ProtocolApprovalsManager::new();
|
||||
let runtime = tokio::runtime::Handle::try_current()
|
||||
.is_err()
|
||||
.then(|| tokio::runtime::Runtime::new().unwrap())
|
||||
.unwrap();
|
||||
let token = bytes_to_address(&swap.token_in)?;
|
||||
let router_address = bytes_to_address(&encoding_context.address_for_approvals)?;
|
||||
let approval_needed = runtime.block_on(async {
|
||||
token_approvals_manager
|
||||
.approval_needed(token, self.vault_address, router_address)
|
||||
.await
|
||||
});
|
||||
// should we return gas estimation here too?? if there is an approval needed, gas will be
|
||||
// higher.
|
||||
let args = (
|
||||
bytes_to_address(&swap.token_in)?,
|
||||
bytes_to_address(&swap.token_out)?,
|
||||
swap.component.id,
|
||||
bytes_to_address(&encoding_context.receiver)?,
|
||||
encoding_context.exact_out,
|
||||
approval_needed,
|
||||
);
|
||||
Ok(args.abi_encode())
|
||||
}
|
||||
|
||||
fn executor_address(&self) -> &str {
|
||||
&self.executor_address
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn test_encode_swap() {
|
||||
// Dummy test to make CI pass. Please implement me.
|
||||
}
|
||||
}
|
||||
18
src/encoding/evm/swap_encoder/mod.rs
Normal file
18
src/encoding/evm/swap_encoder/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
mod builder;
|
||||
mod encoders;
|
||||
mod registry;
|
||||
|
||||
use std::sync::RwLock;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use tycho_core::dto::Chain;
|
||||
|
||||
use crate::encoding::evm::swap_encoder::registry::{Config, SwapEncoderRegistry};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SWAP_ENCODER_REGISTRY: RwLock<SwapEncoderRegistry> = {
|
||||
let config = Config::from_file("src/encoding/config/executor_addresses.json")
|
||||
.expect("Failed to load configuration file");
|
||||
RwLock::new(SwapEncoderRegistry::new(config, Chain::Ethereum))
|
||||
};
|
||||
}
|
||||
48
src/encoding/evm/swap_encoder/registry.rs
Normal file
48
src/encoding/evm/swap_encoder/registry.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use serde::Deserialize;
|
||||
use tycho_core::dto::Chain;
|
||||
|
||||
use crate::encoding::{evm::swap_encoder::builder::SwapEncoderBuilder, swap_encoder::SwapEncoder};
|
||||
|
||||
pub struct SwapEncoderRegistry {
|
||||
encoders: HashMap<String, Box<dyn SwapEncoder>>,
|
||||
}
|
||||
|
||||
impl SwapEncoderRegistry {
|
||||
pub fn new(config: Config, blockchain: Chain) -> Self {
|
||||
let mut encoders = HashMap::new();
|
||||
let executors = config
|
||||
.executors
|
||||
.get(&blockchain)
|
||||
.unwrap_or_else(|| panic!("No executors found for blockchain: {}", blockchain));
|
||||
for (protocol, executor_address) in executors {
|
||||
let builder = SwapEncoderBuilder::new(protocol, executor_address);
|
||||
let encoder = builder.build().unwrap_or_else(|_| {
|
||||
panic!("Failed to build swap encoder for protocol: {}", protocol)
|
||||
});
|
||||
encoders.insert(protocol.to_string(), encoder);
|
||||
}
|
||||
|
||||
Self { encoders }
|
||||
}
|
||||
|
||||
#[allow(clippy::borrowed_box)]
|
||||
pub fn get_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
|
||||
self.encoders.get(protocol_system)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub executors: HashMap<Chain, HashMap<String, String>>, /* Blockchain -> {Protocol ->
|
||||
* Executor address} mapping */
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_file(path: &str) -> Result<Self, anyhow::Error> {
|
||||
let config_str = fs::read_to_string(path)?;
|
||||
let config: Config = serde_json::from_str(&config_str)?;
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user