feat: use clap for cli and resolve pr comments

This commit is contained in:
royvardhan
2025-02-06 18:30:52 +05:30
parent b93856073c
commit a5166f282d
7 changed files with 181 additions and 69 deletions

45
src/bin/lib/cli.rs Normal file
View File

@@ -0,0 +1,45 @@
pub use clap::Parser;
pub const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206a0E7";
#[derive(Parser)]
/// Encode swap transactions for the Tycho router
///
/// Reads a JSON object from stdin with the following structure:
/// ```json
/// {
/// "sender": "0x...",
/// "receiver": "0x...",
/// "given_token": "0x...",
/// "given_amount": "123...",
/// "checked_token": "0x...",
/// "exact_out": false,
/// "slippage": 0.01,
/// "expected_amount": "123...",
/// "check_amount": "123...",
/// "swaps": [{
/// "component": {
/// "id": "...",
/// "protocol_system": "...",
/// "protocol_type_name": "...",
/// "chain": "ethereum",
/// "tokens": ["0x..."],
/// "contract_ids": ["0x..."],
/// "static_attributes": {"key": "0x..."}
/// },
/// "token_in": "0x...",
/// "token_out": "0x...",
/// "split": 1.0
/// }],
/// "router_address": "0x...",
/// "direct_execution": false
/// }
/// ```
pub struct Cli {
/// Router contract address to use for encoding transactions
#[arg(default_value = DEFAULT_ROUTER_ADDRESS)]
pub router_address: String,
/// Private key for signing approvals (required when direct_execution is false)
#[arg(short, long)]
pub private_key: Option<String>,
}

View File

@@ -1,37 +0,0 @@
pub const HELP_TEXT: &str = "\
USAGE:
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:
{
\"sender\": \"0x...\",
\"receiver\": \"0x...\",
\"given_token\": \"0x...\",
\"given_amount\": \"123...\",
\"checked_token\": \"0x...\",
\"exact_out\": false,
\"slippage\": 0.01,
\"expected_amount\": \"123...\",
\"check_amount\": \"123...\",
\"swaps\": [{
\"component\": {
\"id\": \"...\",
\"protocol_system\": \"...\",
\"protocol_type_name\": \"...\",
\"chain\": \"ethereum\",
\"tokens\": [\"0x...\"],
\"contract_ids\": [\"0x...\"],
\"static_attributes\": {\"key\": \"0x...\"}
},
\"token_in\": \"0x...\",
\"token_out\": \"0x...\",
\"split\": 1.0
}],
\"router_address\": \"0x...\",
\"direct_execution\": false
}";

View File

@@ -1,5 +1,6 @@
use std::io::{self, Read};
use clap::Parser;
use serde_json::Value;
use tycho_core::dto::Chain;
use tycho_execution::encoding::{
@@ -13,32 +14,15 @@ use tycho_execution::encoding::{
};
mod lib {
pub mod help;
pub mod cli;
}
const DEFAULT_ROUTER_ADDRESS: &str = "0xaa820C29648D5EA543d712cC928377Bd7206a0E7";
use lib::cli::Cli;
const DEFAULT_EXECUTORS_FILE_PATH: &str = "src/encoding/config/executor_addresses.json";
const DEFAULT_PRIVATE_KEY: &str =
"0x938f4da9d3a947a4a6c53cfd8fcdd876641d6a4519243820b648af0bc3e67f7c";
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().collect();
// Show help text if requested
if args.len() > 1 && (args[1] == "-h" || args[1] == "--help") {
println!("{}", lib::help::HELP_TEXT);
return Ok(());
}
let router_address = args
.get(1)
.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()));
let cli = Cli::parse();
// Read from stdin until EOF
let mut buffer = String::new();
@@ -47,13 +31,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map_err(|e| format!("Failed to read from stdin: {}", e))?;
if buffer.trim().is_empty() {
eprintln!("Error: No input provided");
eprintln!("{}", lib::help::HELP_TEXT);
std::process::exit(1);
return Err("No input provided. Expected JSON input on stdin.".into());
}
// Encode the solution
let encoded = encode_swaps(&buffer, router_address, private_key)?;
let encoded = encode_swaps(&buffer, &cli.router_address, cli.private_key)?;
// Output the encoded result as JSON to stdout
println!(