feat: remove direct execution hardcode
This commit is contained in:
@@ -8,7 +8,7 @@ use hex;
|
||||
use num_bigint::BigUint;
|
||||
use serde_json::Value;
|
||||
use tycho_core::{
|
||||
dto::{Chain as DtoChain, ProtocolComponent},
|
||||
dto::{Chain, ProtocolComponent},
|
||||
Bytes,
|
||||
};
|
||||
use tycho_execution::encoding::{
|
||||
@@ -22,6 +22,7 @@ use tycho_execution::encoding::{
|
||||
};
|
||||
|
||||
const DEFAULT_ROUTER_ADDRESS: &str = "0x1234567890123456789012345678901234567890";
|
||||
const DEFAULT_EXECUTORS_FILE_PATH: &str = "src/encoding/config/executor_addresses.json";
|
||||
const HELP_TEXT: &str = "\
|
||||
USAGE:
|
||||
tycho-encode [ROUTER_ADDRESS]
|
||||
@@ -232,13 +233,13 @@ fn parse_protocol_component(obj: &Value) -> Result<ProtocolComponent, Box<dyn st
|
||||
.get("chain")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| match s.to_lowercase().as_str() {
|
||||
"ethereum" => DtoChain::Ethereum,
|
||||
"starknet" => DtoChain::Starknet,
|
||||
"zksync" => DtoChain::ZkSync,
|
||||
"arbitrum" => DtoChain::Arbitrum,
|
||||
_ => DtoChain::Ethereum, // Default to Ethereum
|
||||
"ethereum" => Chain::Ethereum,
|
||||
"starknet" => Chain::Starknet,
|
||||
"zksync" => Chain::ZkSync,
|
||||
"arbitrum" => Chain::Arbitrum,
|
||||
_ => Chain::Ethereum, // Default to Ethereum
|
||||
})
|
||||
.unwrap_or(DtoChain::Ethereum),
|
||||
.unwrap_or(Chain::Ethereum),
|
||||
tokens: obj
|
||||
.get("tokens")
|
||||
.and_then(|v| v.as_array())
|
||||
@@ -306,41 +307,17 @@ fn parse_protocol_component(obj: &Value) -> Result<ProtocolComponent, Box<dyn st
|
||||
fn encode_swaps(input: &str, router_address: &str) -> Result<Value, Box<dyn std::error::Error>> {
|
||||
// Parse the input JSON
|
||||
let input_json: Value = serde_json::from_str(input)?;
|
||||
let solution = parse_solution(input_json)?;
|
||||
|
||||
// Extract the chain from the input JSON
|
||||
let chain = input_json
|
||||
.get("chain")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| match s.to_lowercase().as_str() {
|
||||
"ethereum" => DtoChain::Ethereum,
|
||||
"starknet" => DtoChain::Starknet,
|
||||
"zksync" => DtoChain::ZkSync,
|
||||
"arbitrum" => DtoChain::Arbitrum,
|
||||
_ => DtoChain::Ethereum, // Default to Ethereum
|
||||
})
|
||||
.unwrap_or(DtoChain::Ethereum);
|
||||
// Create encoder and encode the solution
|
||||
let strategy_selector =
|
||||
EVMStrategyEncoderRegistry::new(Chain::Ethereum, DEFAULT_EXECUTORS_FILE_PATH, None)?;
|
||||
let encoder = EVMTychoEncoder::new(strategy_selector, router_address.to_string())?;
|
||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||
|
||||
// Parse the solution from the input JSON
|
||||
let mut solution = parse_solution(input_json)?;
|
||||
solution.direct_execution = true;
|
||||
|
||||
// Create the strategy encoder based on the chain
|
||||
let strategy_encoder: Value = match chain {
|
||||
DtoChain::Ethereum => {
|
||||
// Create encoder and encode the solution with empty executors file path
|
||||
let strategy_selector = EVMStrategyEncoderRegistry::new(DtoChain::Ethereum, "", None)?;
|
||||
let encoder = EVMTychoEncoder::new(strategy_selector, router_address.to_string())?;
|
||||
let transactions = encoder.encode_router_calldata(vec![solution])?;
|
||||
|
||||
let result: Result<Value, Box<dyn std::error::Error>> = Ok(serde_json::json!({
|
||||
"to": format!("0x{}", hex::encode(&transactions[0].to)),
|
||||
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),
|
||||
"data": format!("0x{}", hex::encode(&transactions[0].data)),
|
||||
}));
|
||||
result
|
||||
}
|
||||
_ => Err(format!("Unsupported chain: {:?}", chain).into()),
|
||||
}?;
|
||||
|
||||
Ok(strategy_encoder)
|
||||
Ok(serde_json::json!({
|
||||
"to": format!("0x{}", hex::encode(&transactions[0].to)),
|
||||
"value": format!("0x{}", hex::encode(transactions[0].value.to_bytes_be())),
|
||||
"data": format!("0x{}", hex::encode(&transactions[0].data)),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -22,11 +22,7 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
|
||||
executors_file_path: &str,
|
||||
signer_pk: Option<String>,
|
||||
) -> Result<Self, EncodingError> {
|
||||
let swap_encoder_registry = if executors_file_path.is_empty() {
|
||||
SwapEncoderRegistry::new_direct_execution()
|
||||
} else {
|
||||
SwapEncoderRegistry::new(executors_file_path, Chain::Ethereum)?
|
||||
};
|
||||
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, Chain::Ethereum)?;
|
||||
|
||||
let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new();
|
||||
strategies.insert(
|
||||
|
||||
@@ -29,25 +29,6 @@ impl SwapEncoderRegistry {
|
||||
Ok(Self { encoders })
|
||||
}
|
||||
|
||||
pub fn new_direct_execution() -> Self {
|
||||
let mut encoders = HashMap::new();
|
||||
|
||||
// Add default encoders with their respective executor addresses
|
||||
let default_encoders = [
|
||||
("uniswap_v2", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"),
|
||||
("vm:balancer_v2", "0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
|
||||
];
|
||||
|
||||
for (protocol, executor_address) in default_encoders {
|
||||
let builder = SwapEncoderBuilder::new(protocol, executor_address);
|
||||
if let Ok(encoder) = builder.build() {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user