From d3ad0ba5bfd50cc3db50b8d341fca2c1c5fcdad3 Mon Sep 17 00:00:00 2001 From: royvardhan Date: Wed, 5 Feb 2025 20:47:51 +0530 Subject: [PATCH] feat: add default private key --- src/bin/lib/help.rs | 3 ++- src/bin/tycho-encode.rs | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/bin/lib/help.rs b/src/bin/lib/help.rs index 250e5aa..a34bd99 100644 --- a/src/bin/lib/help.rs +++ b/src/bin/lib/help.rs @@ -1,9 +1,10 @@ pub const HELP_TEXT: &str = "\ USAGE: - tycho-encode [ROUTER_ADDRESS] + tycho-encode [ROUTER_ADDRESS] [PRIVATE_KEY] ARGS: ROUTER_ADDRESS The address of the router contract [default: 0x1234567890123456789012345678901234567890] + PRIVATE_KEY The private key for signing Permit2 approvals (required when direct_execution is false) The program reads a JSON object from stdin containing the swap details and outputs the encoded transaction. The JSON object should have the following structure: diff --git a/src/bin/tycho-encode.rs b/src/bin/tycho-encode.rs index 13a9630..60d2a51 100644 --- a/src/bin/tycho-encode.rs +++ b/src/bin/tycho-encode.rs @@ -18,9 +18,10 @@ mod lib { const DEFAULT_ROUTER_ADDRESS: &str = "0x1234567890123456789012345678901234567890"; const DEFAULT_EXECUTORS_FILE_PATH: &str = "src/encoding/config/executor_addresses.json"; +const DEFAULT_PRIVATE_KEY: &str = + "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234"; -#[tokio::main] -async fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); // Show help text if requested @@ -34,6 +35,11 @@ async fn main() -> Result<(), Box> { .map(|s| s.as_str()) .unwrap_or(DEFAULT_ROUTER_ADDRESS); + let private_key = args + .get(2) + .map(|s| s.to_string()) + .or_else(|| Some(DEFAULT_PRIVATE_KEY.to_string())); + // Read from stdin until EOF let mut buffer = String::new(); io::stdin() @@ -47,11 +53,25 @@ async fn main() -> Result<(), Box> { } // Parse the JSON input to verify it's valid - serde_json::from_str::(&buffer) - .map_err(|e| format!("Failed to parse JSON input: {}", e))?; + let input_json: Value = + serde_json::from_str(&buffer).map_err(|e| format!("Failed to parse JSON input: {}", e))?; + + // Check if direct_execution is false and private_key is missing + if let Some(obj) = input_json.as_object() { + if let Some(direct_execution) = obj + .get("direct_execution") + .and_then(|v| v.as_bool()) + { + if !direct_execution && private_key.is_none() { + eprintln!("Error: Private key is required when direct_execution is false"); + eprintln!("{}", lib::help::HELP_TEXT); + std::process::exit(1); + } + } + } // Encode the solution - let encoded = encode_swaps(&buffer, router_address)?; + let encoded = encode_swaps(&buffer, router_address, private_key)?; // Output the encoded result as JSON to stdout println!( @@ -63,12 +83,16 @@ async fn main() -> Result<(), Box> { Ok(()) } -fn encode_swaps(input: &str, router_address: &str) -> Result> { +fn encode_swaps( + input: &str, + router_address: &str, + private_key: Option, +) -> Result> { let input_json: Value = serde_json::from_str(input)?; let solution = lib::parse::parse_solution(input_json)?; let strategy_selector = - EVMStrategyEncoderRegistry::new(Chain::Ethereum, DEFAULT_EXECUTORS_FILE_PATH, None)?; + EVMStrategyEncoderRegistry::new(Chain::Ethereum, DEFAULT_EXECUTORS_FILE_PATH, private_key)?; let encoder = EVMTychoEncoder::new(strategy_selector, router_address.to_string())?; let transactions = encoder.encode_router_calldata(vec![solution])?;