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
}";