|
| 1 | +export type CCIPEvent = { |
| 2 | + chain: string; |
| 3 | + tx_hash: string; |
| 4 | + ts: number; |
| 5 | + tx_from: string; |
| 6 | + tx_to: string; |
| 7 | + token: string; |
| 8 | + amount: string; |
| 9 | + is_deposit: boolean; |
| 10 | + is_usd_volume: boolean; // Per requirements, always false for CCIP |
| 11 | +}; |
| 12 | + |
| 13 | +interface CCIPTransaction { |
| 14 | + sourceChain: string; |
| 15 | + destChain: string; |
| 16 | + sourceTxHash: string; |
| 17 | + destTxHash: string; |
| 18 | + blockTimestamp: number; // In seconds from API |
| 19 | + messageID: string; |
| 20 | + tokenTransferFrom: string; |
| 21 | + tokenTransferTo: string; |
| 22 | + tokenAddressSource: string; |
| 23 | + tokenAddressDest: string; |
| 24 | + tokenAmount: number; // Number from API, converted to string for CCIPEvent |
| 25 | +} |
| 26 | + |
| 27 | +interface ApiResponse { |
| 28 | + transactions: CCIPTransaction[]; |
| 29 | +} |
| 30 | + |
| 31 | +// --- Constants --- |
| 32 | +const API_BASE_URL = "https://dsa-metrics-api-dev-81875351881.europe-west2.run.app/v1/ccip_transactions"; |
| 33 | +const API_KEY = ""; // API Key, currently an empty string |
| 34 | + |
| 35 | +// --- Main function --- |
| 36 | +export async function fetchCCIPEvents(dateString: string): Promise<CCIPEvent[]> { |
| 37 | + console.log(`[fetchCCIPEvents] Starting for date: ${dateString}`); |
| 38 | + |
| 39 | + // Basic validation for dateString format (optional but good practice) |
| 40 | + if (!/^\d{4}-\d{2}-\d{2}$/.test(dateString)) { |
| 41 | + const errorMsg = `[fetchCCIPEvents] Invalid date format: "${dateString}". Expected YYYY-MM-DD.`; |
| 42 | + console.error(errorMsg); |
| 43 | + throw new Error(errorMsg); |
| 44 | + } |
| 45 | + |
| 46 | + const apiUrlWithDate = `${API_BASE_URL}?date=${dateString}`; |
| 47 | + console.log(`[fetchCCIPEvents] Constructed API URL: ${apiUrlWithDate}`); |
| 48 | + |
| 49 | + const allEvents: CCIPEvent[] = []; |
| 50 | + |
| 51 | + try { |
| 52 | + console.log(`[fetchCCIPEvents] Making API request to ${apiUrlWithDate}...`); |
| 53 | + const response = await fetch(apiUrlWithDate, { |
| 54 | + method: 'GET', |
| 55 | + headers: { |
| 56 | + 'X-API-Key': API_KEY, |
| 57 | + 'Accept': 'application/json', |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + console.log(`[fetchCCIPEvents] API response status: ${response.status}`); |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + const errorBody = await response.text(); |
| 65 | + const errorMsg = `[fetchCCIPEvents] API Error ${response.status}: ${response.statusText}. URL: ${apiUrlWithDate}. Body: ${errorBody}`; |
| 66 | + console.error(errorMsg); |
| 67 | + throw new Error(`Failed to fetch CCIP events: ${response.status} ${response.statusText}`); |
| 68 | + } |
| 69 | + |
| 70 | + const apiData: ApiResponse = await response.json(); |
| 71 | + console.log("[fetchCCIPEvents] Successfully parsed API response."); |
| 72 | + |
| 73 | + const rawTransactions = apiData?.transactions; |
| 74 | + if (!Array.isArray(rawTransactions)) { |
| 75 | + const errorMsg = `[fetchCCIPEvents] Invalid data format: 'transactions' array not found or not an array. URL: ${apiUrlWithDate}. Response: ${JSON.stringify(apiData)}`; |
| 76 | + console.error(errorMsg); |
| 77 | + throw new Error(errorMsg); |
| 78 | + } |
| 79 | + console.log(`[fetchCCIPEvents] Received ${rawTransactions.length} raw transactions from API.`); |
| 80 | + |
| 81 | + let processedCount = 0; |
| 82 | + for (const transaction of rawTransactions) { |
| 83 | + console.log(`[fetchCCIPEvents] Processing raw transaction with messageID: ${transaction.messageID}`); |
| 84 | + if ( |
| 85 | + !transaction.sourceChain || !transaction.destChain || |
| 86 | + !transaction.sourceTxHash || !transaction.destTxHash || |
| 87 | + typeof transaction.blockTimestamp !== 'number' || |
| 88 | + !transaction.tokenTransferFrom || !transaction.tokenTransferTo || |
| 89 | + !transaction.tokenAddressSource || !transaction.tokenAddressDest || |
| 90 | + typeof transaction.tokenAmount !== 'number' |
| 91 | + ) { |
| 92 | + console.warn("[fetchCCIPEvents] Skipping incomplete raw transaction data:", transaction); |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const timestampMs = transaction.blockTimestamp * 1000; |
| 97 | + const amountStr = String(transaction.tokenAmount); |
| 98 | + |
| 99 | + const withdrawalEvent: CCIPEvent = { |
| 100 | + chain: transaction.sourceChain, |
| 101 | + tx_hash: transaction.sourceTxHash, |
| 102 | + ts: timestampMs, |
| 103 | + tx_from: transaction.tokenTransferFrom, |
| 104 | + tx_to: transaction.tokenTransferTo, |
| 105 | + token: transaction.tokenAddressSource, |
| 106 | + amount: amountStr, |
| 107 | + is_deposit: false, |
| 108 | + is_usd_volume: false, |
| 109 | + }; |
| 110 | + allEvents.push(withdrawalEvent); |
| 111 | + |
| 112 | + const depositEvent: CCIPEvent = { |
| 113 | + chain: transaction.destChain, |
| 114 | + tx_hash: transaction.destTxHash, |
| 115 | + ts: timestampMs, |
| 116 | + tx_from: transaction.tokenTransferFrom, |
| 117 | + tx_to: transaction.tokenTransferTo, |
| 118 | + token: transaction.tokenAddressDest, |
| 119 | + amount: amountStr, |
| 120 | + is_deposit: true, |
| 121 | + is_usd_volume: false, |
| 122 | + }; |
| 123 | + allEvents.push(depositEvent); |
| 124 | + processedCount++; |
| 125 | + } |
| 126 | + console.log(`[fetchCCIPEvents] Successfully processed ${processedCount} raw transactions, created ${allEvents.length} CCIPEvent objects.`); |
| 127 | + |
| 128 | + } catch (error) { |
| 129 | + // Ensure error is an instance of Error for consistent message access |
| 130 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 131 | + console.error(`[fetchCCIPEvents] Error during processing for date ${dateString}: ${errorMessage}`, error); |
| 132 | + throw error; // Re-throw the original error or a new error encapsulating it |
| 133 | + } |
| 134 | + |
| 135 | + console.log(`[fetchCCIPEvents] Finished for date: ${dateString}. Returning ${allEvents.length} events.`); |
| 136 | + return allEvents; |
| 137 | +} |
0 commit comments