|
| 1 | +use eyre::{eyre, Result}; |
| 2 | +use hyperlane_core::{HyperlaneMessage, Indexer, H256, H512}; |
| 3 | +use std::{collections::HashMap, sync::Arc}; |
| 4 | +use tracing::{debug, error, warn}; |
| 5 | + |
| 6 | +/// Extract all Hyperlane messages from a transaction hash on a specific chain |
| 7 | +pub async fn extract_messages( |
| 8 | + indexers: &HashMap<String, Arc<dyn Indexer<HyperlaneMessage>>>, |
| 9 | + chain_name: &str, |
| 10 | + tx_hash: &str, |
| 11 | +) -> Result<Vec<ExtractedMessage>> { |
| 12 | + // Get indexer for chain |
| 13 | + let indexer = indexers |
| 14 | + .get(chain_name) |
| 15 | + .ok_or_else(|| eyre!("Chain not found in registry: {}", chain_name))?; |
| 16 | + |
| 17 | + debug!( |
| 18 | + chain = %chain_name, |
| 19 | + tx_hash = %tx_hash, |
| 20 | + "Extracting message from transaction" |
| 21 | + ); |
| 22 | + |
| 23 | + // Parse tx hash using protocol-specific method |
| 24 | + let tx_hash_512 = indexer |
| 25 | + .parse_tx_hash(tx_hash) |
| 26 | + .map_err(|e| eyre!("Invalid tx hash format: {}", e))?; |
| 27 | + |
| 28 | + // Fetch messages from transaction |
| 29 | + let messages_with_meta = indexer |
| 30 | + .fetch_logs_by_tx_hash(tx_hash_512) |
| 31 | + .await |
| 32 | + .map_err(|e| { |
| 33 | + error!( |
| 34 | + chain = %chain_name, |
| 35 | + tx_hash = %tx_hash, |
| 36 | + error = ?e, |
| 37 | + "Failed to fetch logs from transaction" |
| 38 | + ); |
| 39 | + eyre!("Failed to fetch transaction logs: {}", e) |
| 40 | + })?; |
| 41 | + |
| 42 | + // Extract just the messages |
| 43 | + let messages: Vec<HyperlaneMessage> = messages_with_meta |
| 44 | + .into_iter() |
| 45 | + .map(|(indexed_msg, _log_meta)| indexed_msg.inner().clone()) |
| 46 | + .collect(); |
| 47 | + |
| 48 | + if messages.is_empty() { |
| 49 | + error!( |
| 50 | + chain = %chain_name, |
| 51 | + tx_hash = %tx_hash, |
| 52 | + "No Hyperlane Dispatch events found in transaction" |
| 53 | + ); |
| 54 | + return Err(eyre!("No Hyperlane Dispatch events found in transaction")); |
| 55 | + } |
| 56 | + |
| 57 | + debug!( |
| 58 | + chain = %chain_name, |
| 59 | + tx_hash = %tx_hash, |
| 60 | + message_count = messages.len(), |
| 61 | + "Successfully extracted messages from transaction" |
| 62 | + ); |
| 63 | + |
| 64 | + // Check once per tx whether this is a CCTP fast transfer. |
| 65 | + // Errors are treated as false — the relay API will reject the request below. |
| 66 | + let is_cctp_v2 = indexer.is_cctp_v2(tx_hash_512).await.unwrap_or_else(|e| { |
| 67 | + warn!( |
| 68 | + chain = %chain_name, |
| 69 | + tx_hash = %tx_hash, |
| 70 | + error = ?e, |
| 71 | + "Failed to check for CCTP V2 burn event, treating as non-CCTP" |
| 72 | + ); |
| 73 | + false |
| 74 | + }); |
| 75 | + |
| 76 | + debug!( |
| 77 | + chain = %chain_name, |
| 78 | + tx_hash = %tx_hash, |
| 79 | + is_cctp_v2, |
| 80 | + "CCTP V2 burn event check result" |
| 81 | + ); |
| 82 | + |
| 83 | + // Convert all messages to ExtractedMessage structs |
| 84 | + let extracted_messages: Vec<ExtractedMessage> = messages |
| 85 | + .into_iter() |
| 86 | + .map(|message| { |
| 87 | + let origin_domain = message.origin; |
| 88 | + let destination_domain = message.destination; |
| 89 | + let message_id = message.id(); |
| 90 | + |
| 91 | + debug!( |
| 92 | + chain = %chain_name, |
| 93 | + tx_hash = %tx_hash, |
| 94 | + message_id = ?message_id, |
| 95 | + origin_domain = origin_domain, |
| 96 | + destination_domain = destination_domain, |
| 97 | + "Extracted message" |
| 98 | + ); |
| 99 | + |
| 100 | + ExtractedMessage { |
| 101 | + message, |
| 102 | + origin_domain, |
| 103 | + destination_domain, |
| 104 | + message_id, |
| 105 | + is_cctp_v2, |
| 106 | + tx_hash: tx_hash_512, |
| 107 | + } |
| 108 | + }) |
| 109 | + .collect(); |
| 110 | + |
| 111 | + Ok(extracted_messages) |
| 112 | +} |
| 113 | + |
| 114 | +#[derive(Debug, Clone)] |
| 115 | +pub struct ExtractedMessage { |
| 116 | + pub message: HyperlaneMessage, |
| 117 | + pub origin_domain: u32, |
| 118 | + pub destination_domain: u32, |
| 119 | + pub message_id: H256, |
| 120 | + /// True when the transaction contains a Circle CCTP V2 `DepositForBurn` event. |
| 121 | + pub is_cctp_v2: bool, |
| 122 | + /// The origin transaction hash, used by the ccip-server to skip GraphQL lookup. |
| 123 | + pub tx_hash: H512, |
| 124 | +} |
0 commit comments