|
| 1 | +use alloy::primitives::Address; |
| 2 | +use clap::Args; |
| 3 | + |
| 4 | +use defi_core::error::{DefiError, Result}; |
| 5 | +use defi_core::registry::{ChainConfig, ProtocolCategory, Registry}; |
| 6 | +use defi_core::types::PriceData; |
| 7 | + |
| 8 | +use crate::output::OutputMode; |
| 9 | + |
| 10 | +#[derive(Args)] |
| 11 | +pub struct PriceArgs { |
| 12 | + /// Asset symbol (e.g. WHYPE) or address |
| 13 | + #[arg(long)] |
| 14 | + pub asset: String, |
| 15 | + |
| 16 | + /// Price source filter: all, oracle, dex |
| 17 | + #[arg(long, default_value = "all")] |
| 18 | + pub source: String, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(serde::Serialize)] |
| 22 | +struct PriceReport { |
| 23 | + asset: String, |
| 24 | + asset_address: String, |
| 25 | + prices: Vec<PriceEntry>, |
| 26 | + max_spread_pct: f64, |
| 27 | + oracle_vs_dex_spread_pct: f64, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(serde::Serialize)] |
| 31 | +struct PriceEntry { |
| 32 | + source: String, |
| 33 | + source_type: String, |
| 34 | + price: f64, |
| 35 | +} |
| 36 | + |
| 37 | +fn resolve_asset(registry: &Registry, chain: &str, asset: &str) -> Result<(Address, String, u8)> { |
| 38 | + if let Ok(addr) = asset.parse::<Address>() { |
| 39 | + return Ok((addr, asset.to_string(), 18)); |
| 40 | + } |
| 41 | + let token = registry.resolve_token(chain, asset)?; |
| 42 | + Ok((token.address, token.symbol.clone(), token.decimals)) |
| 43 | +} |
| 44 | + |
| 45 | +pub async fn run( |
| 46 | + args: PriceArgs, |
| 47 | + registry: &Registry, |
| 48 | + chain: &ChainConfig, |
| 49 | + output: &OutputMode, |
| 50 | +) -> Result<()> { |
| 51 | + let chain_key = chain.name.to_lowercase(); |
| 52 | + let rpc_url = chain.effective_rpc_url(); |
| 53 | + let (asset_addr, asset_symbol, _asset_decimals) = |
| 54 | + resolve_asset(registry, &chain_key, &args.asset)?; |
| 55 | + |
| 56 | + let fetch_oracle = args.source == "all" || args.source == "oracle"; |
| 57 | + let fetch_dex = args.source == "all" || args.source == "dex"; |
| 58 | + |
| 59 | + let mut all_prices: Vec<PriceData> = Vec::new(); |
| 60 | + |
| 61 | + // === Oracle prices from lending protocols (Aave V3 forks) === |
| 62 | + if fetch_oracle { |
| 63 | + let lending_protocols = registry.get_protocols_by_category(ProtocolCategory::Lending); |
| 64 | + for entry in &lending_protocols { |
| 65 | + match defi_protocols::factory::create_oracle_from_lending(entry, &rpc_url) { |
| 66 | + Ok(oracle) => match oracle.get_price(asset_addr).await { |
| 67 | + Ok(price) => all_prices.push(price), |
| 68 | + Err(e) => { |
| 69 | + eprintln!("[{}] oracle price failed: {e}", entry.name); |
| 70 | + } |
| 71 | + }, |
| 72 | + Err(_) => continue, // Interface doesn't support oracle |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // === Oracle prices from CDP protocols (Felix) === |
| 77 | + // Felix PriceFeed only returns WHYPE collateral price. |
| 78 | + // Only query it when the asset is WHYPE or a WHYPE-related token. |
| 79 | + let whype_addr: Address = "0x5555555555555555555555555555555555555555" |
| 80 | + .parse() |
| 81 | + .unwrap(); |
| 82 | + let is_whype = asset_addr == whype_addr |
| 83 | + || asset_symbol.eq_ignore_ascii_case("WHYPE") |
| 84 | + || asset_symbol.eq_ignore_ascii_case("HYPE"); |
| 85 | + |
| 86 | + if is_whype { |
| 87 | + let cdp_protocols = registry.get_protocols_by_category(ProtocolCategory::Cdp); |
| 88 | + for entry in &cdp_protocols { |
| 89 | + match defi_protocols::factory::create_oracle_from_cdp(entry, asset_addr, &rpc_url) { |
| 90 | + Ok(oracle) => match oracle.get_price(asset_addr).await { |
| 91 | + Ok(price) => all_prices.push(price), |
| 92 | + Err(e) => { |
| 93 | + eprintln!("[{}] oracle price failed: {e}", entry.name); |
| 94 | + } |
| 95 | + }, |
| 96 | + Err(_) => continue, |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // === DEX spot prices === |
| 103 | + if fetch_dex { |
| 104 | + // Resolve USDC as the quote token |
| 105 | + let usdc = registry.resolve_token(&chain_key, "USDC"); |
| 106 | + if let Ok(usdc_token) = usdc { |
| 107 | + let usdc_addr = usdc_token.address; |
| 108 | + let usdc_decimals = usdc_token.decimals; |
| 109 | + |
| 110 | + let dex_protocols = registry.get_protocols_by_category(ProtocolCategory::Dex); |
| 111 | + for entry in &dex_protocols { |
| 112 | + match defi_protocols::factory::create_dex_with_rpc(entry, Some(&rpc_url)) { |
| 113 | + Ok(dex) => { |
| 114 | + match defi_protocols::dex::DexSpotPrice::get_price( |
| 115 | + dex.as_ref(), |
| 116 | + asset_addr, |
| 117 | + _asset_decimals, |
| 118 | + usdc_addr, |
| 119 | + usdc_decimals, |
| 120 | + ) |
| 121 | + .await |
| 122 | + { |
| 123 | + Ok(price) => all_prices.push(price), |
| 124 | + Err(e) => { |
| 125 | + eprintln!("[{}] dex price failed: {e}", entry.name); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + Err(_) => continue, |
| 130 | + } |
| 131 | + } |
| 132 | + } else { |
| 133 | + eprintln!("USDC token not found in registry — skipping DEX prices"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if all_prices.is_empty() { |
| 138 | + return Err(DefiError::Internal( |
| 139 | + "No prices could be fetched from any source".to_string(), |
| 140 | + )); |
| 141 | + } |
| 142 | + |
| 143 | + // Compute spreads |
| 144 | + let prices_f64: Vec<f64> = all_prices.iter().map(|p| p.price_f64).collect(); |
| 145 | + let max_price = prices_f64.iter().cloned().fold(f64::NEG_INFINITY, f64::max); |
| 146 | + let min_price = prices_f64.iter().cloned().fold(f64::INFINITY, f64::min); |
| 147 | + let max_spread_pct = if min_price > 0.0 { |
| 148 | + (max_price - min_price) / min_price * 100.0 |
| 149 | + } else { |
| 150 | + 0.0 |
| 151 | + }; |
| 152 | + |
| 153 | + // Oracle vs DEX spread |
| 154 | + let oracle_prices: Vec<f64> = all_prices |
| 155 | + .iter() |
| 156 | + .filter(|p| p.source_type == "oracle") |
| 157 | + .map(|p| p.price_f64) |
| 158 | + .collect(); |
| 159 | + let dex_prices: Vec<f64> = all_prices |
| 160 | + .iter() |
| 161 | + .filter(|p| p.source_type == "dex_spot") |
| 162 | + .map(|p| p.price_f64) |
| 163 | + .collect(); |
| 164 | + |
| 165 | + let oracle_vs_dex_spread_pct = if !oracle_prices.is_empty() && !dex_prices.is_empty() { |
| 166 | + let avg_oracle = oracle_prices.iter().sum::<f64>() / oracle_prices.len() as f64; |
| 167 | + let avg_dex = dex_prices.iter().sum::<f64>() / dex_prices.len() as f64; |
| 168 | + let min_avg = avg_oracle.min(avg_dex); |
| 169 | + if min_avg > 0.0 { |
| 170 | + (avg_oracle - avg_dex).abs() / min_avg * 100.0 |
| 171 | + } else { |
| 172 | + 0.0 |
| 173 | + } |
| 174 | + } else { |
| 175 | + 0.0 |
| 176 | + }; |
| 177 | + |
| 178 | + let report = PriceReport { |
| 179 | + asset: asset_symbol, |
| 180 | + asset_address: asset_addr.to_string(), |
| 181 | + prices: all_prices |
| 182 | + .iter() |
| 183 | + .map(|p| PriceEntry { |
| 184 | + source: p.source.clone(), |
| 185 | + source_type: p.source_type.clone(), |
| 186 | + price: (p.price_f64 * 100.0).round() / 100.0, |
| 187 | + }) |
| 188 | + .collect(), |
| 189 | + max_spread_pct: (max_spread_pct * 100.0).round() / 100.0, |
| 190 | + oracle_vs_dex_spread_pct: (oracle_vs_dex_spread_pct * 100.0).round() / 100.0, |
| 191 | + }; |
| 192 | + |
| 193 | + output.print(&report)?; |
| 194 | + Ok(()) |
| 195 | +} |
0 commit comments