|
| 1 | +use axum::{extract::State, http::StatusCode, Json}; |
| 2 | +use dymension_kaspa::dym_kas_core::api::client::Deposit; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | + |
| 5 | +use hyperlane_base::server::utils::{ |
| 6 | + ServerErrorBody, ServerErrorResponse, ServerResult, ServerSuccessResponse, |
| 7 | +}; |
| 8 | + |
| 9 | +use super::ServerState; |
| 10 | + |
| 11 | +#[derive(Clone, Debug, Deserialize)] |
| 12 | +pub struct RequestBody { |
| 13 | + /// The Kaspa transaction ID to recover |
| 14 | + pub kaspa_tx: String, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Clone, Debug, Serialize)] |
| 18 | +pub struct ResponseBody { |
| 19 | + pub message: String, |
| 20 | + pub deposit_id: String, |
| 21 | +} |
| 22 | + |
| 23 | +/// Recover a Kaspa deposit by fetching it from the Kaspa REST API and submitting |
| 24 | +/// it for processing. This is useful for deposits that fell outside the normal |
| 25 | +/// lookback window due to relayer DB being wiped or other issues. |
| 26 | +/// |
| 27 | +/// POST /kaspa/deposit/recover |
| 28 | +/// Body: { "kaspa_tx": "242b5987..." } |
| 29 | +pub async fn handler( |
| 30 | + State(state): State<ServerState>, |
| 31 | + Json(body): Json<RequestBody>, |
| 32 | +) -> ServerResult<ServerSuccessResponse<ResponseBody>> { |
| 33 | + let RequestBody { kaspa_tx } = body; |
| 34 | + tracing::info!(%kaspa_tx, "Received deposit recovery request"); |
| 35 | + |
| 36 | + // Check if recovery is enabled |
| 37 | + let recovery = state.recovery.as_ref().ok_or_else(|| { |
| 38 | + ServerErrorResponse::new( |
| 39 | + StatusCode::SERVICE_UNAVAILABLE, |
| 40 | + ServerErrorBody { |
| 41 | + message: "Deposit recovery is not enabled on this relayer".to_string(), |
| 42 | + }, |
| 43 | + ) |
| 44 | + })?; |
| 45 | + |
| 46 | + // Fetch the transaction from Kaspa REST API |
| 47 | + let tx = recovery |
| 48 | + .http_client |
| 49 | + .get_tx_by_id(&kaspa_tx) |
| 50 | + .await |
| 51 | + .map_err(|e| { |
| 52 | + tracing::error!(%kaspa_tx, error = ?e, "Failed to fetch transaction from Kaspa API"); |
| 53 | + ServerErrorResponse::new( |
| 54 | + StatusCode::NOT_FOUND, |
| 55 | + ServerErrorBody { |
| 56 | + message: format!("Transaction not found or API error: {}", e), |
| 57 | + }, |
| 58 | + ) |
| 59 | + })?; |
| 60 | + |
| 61 | + // Validate it's a valid escrow transfer |
| 62 | + if !is_valid_escrow_transfer(&tx, &recovery.escrow_address) { |
| 63 | + return Err(ServerErrorResponse::new( |
| 64 | + StatusCode::BAD_REQUEST, |
| 65 | + ServerErrorBody { |
| 66 | + message: format!( |
| 67 | + "Transaction {} is not a valid deposit to escrow {}", |
| 68 | + kaspa_tx, recovery.escrow_address |
| 69 | + ), |
| 70 | + }, |
| 71 | + )); |
| 72 | + } |
| 73 | + |
| 74 | + // Convert to Deposit |
| 75 | + let deposit: Deposit = tx.try_into().map_err(|e: eyre::Error| { |
| 76 | + tracing::error!(%kaspa_tx, error = ?e, "Failed to convert transaction to deposit"); |
| 77 | + ServerErrorResponse::new( |
| 78 | + StatusCode::BAD_REQUEST, |
| 79 | + ServerErrorBody { |
| 80 | + message: format!("Invalid deposit transaction: {}", e), |
| 81 | + }, |
| 82 | + ) |
| 83 | + })?; |
| 84 | + |
| 85 | + let deposit_id = deposit.id.to_string(); |
| 86 | + |
| 87 | + // Send to recovery channel |
| 88 | + recovery.sender.send(deposit).await.map_err(|e| { |
| 89 | + tracing::error!(%kaspa_tx, error = ?e, "Failed to send deposit to recovery channel"); |
| 90 | + ServerErrorResponse::new( |
| 91 | + StatusCode::INTERNAL_SERVER_ERROR, |
| 92 | + ServerErrorBody { |
| 93 | + message: "Failed to queue deposit for recovery".to_string(), |
| 94 | + }, |
| 95 | + ) |
| 96 | + })?; |
| 97 | + |
| 98 | + tracing::info!(%kaspa_tx, %deposit_id, "Deposit queued for recovery"); |
| 99 | + |
| 100 | + Ok(ServerSuccessResponse::new(ResponseBody { |
| 101 | + message: "Deposit queued for recovery processing".to_string(), |
| 102 | + deposit_id, |
| 103 | + })) |
| 104 | +} |
| 105 | + |
| 106 | +fn is_valid_escrow_transfer( |
| 107 | + tx: &dymension_kaspa::dym_kas_api::models::TxModel, |
| 108 | + escrow_address: &str, |
| 109 | +) -> bool { |
| 110 | + tx.outputs.as_ref().map_or(false, |outputs| { |
| 111 | + outputs.iter().any(|utxo| { |
| 112 | + utxo.script_public_key_address |
| 113 | + .as_ref() |
| 114 | + .map_or(false, |dest| dest == escrow_address) |
| 115 | + }) |
| 116 | + }) |
| 117 | +} |
0 commit comments