|
| 1 | +use alloy::primitives::{Address, U256}; |
| 2 | +use clap::{Args, Subcommand}; |
| 3 | + |
| 4 | +use defi_core::error::{DefiError, Result}; |
| 5 | +use defi_core::registry::{ChainConfig, Registry}; |
| 6 | + |
| 7 | +use crate::executor::Executor; |
| 8 | +use crate::output::OutputMode; |
| 9 | + |
| 10 | +#[derive(Args)] |
| 11 | +pub struct GaugeArgs { |
| 12 | + #[command(subcommand)] |
| 13 | + pub command: GaugeCommand, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Subcommand)] |
| 17 | +pub enum GaugeCommand { |
| 18 | + /// Deposit LP tokens into a gauge for farming |
| 19 | + Deposit { |
| 20 | + /// Protocol to use (e.g., ramses-cl, nest-v1) |
| 21 | + #[arg(long)] |
| 22 | + protocol: String, |
| 23 | + /// Gauge contract address |
| 24 | + #[arg(long)] |
| 25 | + gauge: String, |
| 26 | + /// LP token amount (raw U256) |
| 27 | + #[arg(long)] |
| 28 | + amount: String, |
| 29 | + /// veNFT token ID for boosted rewards (optional) |
| 30 | + #[arg(long)] |
| 31 | + ve_nft: Option<String>, |
| 32 | + }, |
| 33 | + /// Withdraw LP tokens from a gauge |
| 34 | + Withdraw { |
| 35 | + /// Protocol to use |
| 36 | + #[arg(long)] |
| 37 | + protocol: String, |
| 38 | + /// Gauge contract address |
| 39 | + #[arg(long)] |
| 40 | + gauge: String, |
| 41 | + /// LP token amount (raw U256) |
| 42 | + #[arg(long)] |
| 43 | + amount: String, |
| 44 | + }, |
| 45 | + /// Claim earned rewards from a gauge |
| 46 | + Claim { |
| 47 | + /// Protocol to use |
| 48 | + #[arg(long)] |
| 49 | + protocol: String, |
| 50 | + /// Gauge contract address |
| 51 | + #[arg(long)] |
| 52 | + gauge: String, |
| 53 | + }, |
| 54 | + /// Create a veNFT lock |
| 55 | + Lock { |
| 56 | + /// Protocol to use |
| 57 | + #[arg(long)] |
| 58 | + protocol: String, |
| 59 | + /// Amount to lock (human-readable, 18 decimals) |
| 60 | + #[arg(long)] |
| 61 | + amount: String, |
| 62 | + /// Lock duration in days |
| 63 | + #[arg(long, default_value = "365")] |
| 64 | + days: u64, |
| 65 | + }, |
| 66 | + /// Vote on gauge emissions with veNFT |
| 67 | + Vote { |
| 68 | + /// Protocol to use |
| 69 | + #[arg(long)] |
| 70 | + protocol: String, |
| 71 | + /// veNFT token ID |
| 72 | + #[arg(long)] |
| 73 | + ve_nft: String, |
| 74 | + /// Pool addresses (comma-separated) |
| 75 | + #[arg(long)] |
| 76 | + pools: String, |
| 77 | + /// Vote weights (comma-separated, same order as pools) |
| 78 | + #[arg(long)] |
| 79 | + weights: String, |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +fn parse_amount_18(amount: &str) -> Result<U256> { |
| 84 | + let parts: Vec<&str> = amount.split('.').collect(); |
| 85 | + let (whole, frac) = match parts.len() { |
| 86 | + 1 => (parts[0], ""), |
| 87 | + 2 => (parts[0], parts[1]), |
| 88 | + _ => return Err(DefiError::InvalidParam("Invalid amount format".to_string())), |
| 89 | + }; |
| 90 | + let whole_val = U256::from( |
| 91 | + whole |
| 92 | + .parse::<u64>() |
| 93 | + .map_err(|e| DefiError::InvalidParam(format!("Invalid amount: {e}")))?, |
| 94 | + ); |
| 95 | + let frac_val = if frac.is_empty() { |
| 96 | + U256::ZERO |
| 97 | + } else { |
| 98 | + let frac_padded = format!("{:0<18}", frac); |
| 99 | + U256::from( |
| 100 | + frac_padded[..18] |
| 101 | + .parse::<u64>() |
| 102 | + .map_err(|e| DefiError::InvalidParam(format!("Invalid fractional amount: {e}")))?, |
| 103 | + ) |
| 104 | + }; |
| 105 | + Ok(whole_val * U256::from(10u64).pow(U256::from(18)) + frac_val) |
| 106 | +} |
| 107 | + |
| 108 | +fn parse_address(s: &str) -> Result<Address> { |
| 109 | + s.parse::<Address>() |
| 110 | + .map_err(|e| DefiError::InvalidParam(format!("Invalid address '{s}': {e}"))) |
| 111 | +} |
| 112 | + |
| 113 | +fn parse_u256(s: &str) -> Result<U256> { |
| 114 | + U256::from_str_radix(s, 10) |
| 115 | + .map_err(|e| DefiError::InvalidParam(format!("Invalid U256 '{s}': {e}"))) |
| 116 | +} |
| 117 | + |
| 118 | +pub async fn run( |
| 119 | + args: GaugeArgs, |
| 120 | + registry: &Registry, |
| 121 | + _chain: &ChainConfig, |
| 122 | + executor: &Executor, |
| 123 | + output: &OutputMode, |
| 124 | +) -> Result<()> { |
| 125 | + match args.command { |
| 126 | + GaugeCommand::Deposit { |
| 127 | + protocol, |
| 128 | + gauge, |
| 129 | + amount, |
| 130 | + ve_nft, |
| 131 | + } => { |
| 132 | + let entry = registry.get_protocol(&protocol)?; |
| 133 | + let gauge_adapter = defi_protocols::factory::create_gauge(entry)?; |
| 134 | + let gauge_addr = parse_address(&gauge)?; |
| 135 | + let amount_val = parse_amount_18(&amount)?; |
| 136 | + let token_id = ve_nft.as_deref().map(parse_u256).transpose()?; |
| 137 | + |
| 138 | + let tx = gauge_adapter |
| 139 | + .build_deposit(gauge_addr, amount_val, token_id) |
| 140 | + .await?; |
| 141 | + let result = executor.execute(tx).await?; |
| 142 | + output.print(&result)?; |
| 143 | + } |
| 144 | + GaugeCommand::Withdraw { |
| 145 | + protocol, |
| 146 | + gauge, |
| 147 | + amount, |
| 148 | + } => { |
| 149 | + let entry = registry.get_protocol(&protocol)?; |
| 150 | + let gauge_adapter = defi_protocols::factory::create_gauge(entry)?; |
| 151 | + let gauge_addr = parse_address(&gauge)?; |
| 152 | + let amount_val = parse_amount_18(&amount)?; |
| 153 | + |
| 154 | + let tx = gauge_adapter.build_withdraw(gauge_addr, amount_val).await?; |
| 155 | + let result = executor.execute(tx).await?; |
| 156 | + output.print(&result)?; |
| 157 | + } |
| 158 | + GaugeCommand::Claim { protocol, gauge } => { |
| 159 | + let entry = registry.get_protocol(&protocol)?; |
| 160 | + let gauge_adapter = defi_protocols::factory::create_gauge(entry)?; |
| 161 | + let gauge_addr = parse_address(&gauge)?; |
| 162 | + |
| 163 | + let tx = gauge_adapter.build_claim_rewards(gauge_addr).await?; |
| 164 | + let result = executor.execute(tx).await?; |
| 165 | + output.print(&result)?; |
| 166 | + } |
| 167 | + GaugeCommand::Lock { |
| 168 | + protocol, |
| 169 | + amount, |
| 170 | + days, |
| 171 | + } => { |
| 172 | + let entry = registry.get_protocol(&protocol)?; |
| 173 | + let system = defi_protocols::factory::create_gauge(entry)?; |
| 174 | + let amount_val = parse_amount_18(&amount)?; |
| 175 | + let lock_duration_secs = days * 86400; |
| 176 | + |
| 177 | + let tx = system |
| 178 | + .build_create_lock(amount_val, lock_duration_secs) |
| 179 | + .await?; |
| 180 | + let result = executor.execute(tx).await?; |
| 181 | + output.print(&result)?; |
| 182 | + } |
| 183 | + GaugeCommand::Vote { |
| 184 | + protocol, |
| 185 | + ve_nft, |
| 186 | + pools, |
| 187 | + weights, |
| 188 | + } => { |
| 189 | + let entry = registry.get_protocol(&protocol)?; |
| 190 | + let system = defi_protocols::factory::create_gauge(entry)?; |
| 191 | + let token_id = parse_u256(&ve_nft)?; |
| 192 | + let pool_addrs: Result<Vec<Address>> = |
| 193 | + pools.split(',').map(|s| parse_address(s.trim())).collect(); |
| 194 | + let weight_vals: Result<Vec<U256>> = |
| 195 | + weights.split(',').map(|s| parse_u256(s.trim())).collect(); |
| 196 | + |
| 197 | + let tx = system |
| 198 | + .build_vote(token_id, pool_addrs?, weight_vals?) |
| 199 | + .await?; |
| 200 | + let result = executor.execute(tx).await?; |
| 201 | + output.print(&result)?; |
| 202 | + } |
| 203 | + } |
| 204 | + Ok(()) |
| 205 | +} |
0 commit comments