rrelayer_core 0.7.0

Core types and functionality for rrelayer - a powerful blockchain transaction relay service
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use std::{
    collections::HashMap,
    sync::Arc,
    time::{Duration, SystemTime},
};
use tokio::{
    sync::RwLock,
    time::{interval, Interval},
};
use tracing::{debug, error, info};
use uuid::Uuid;

use super::{
    payload::{WebhookPayload, WebhookSigningPayload},
    sender::WebhookSender,
    types::{WebhookDelivery, WebhookDeliveryConfig, WebhookEventType, WebhookFilter},
};
use crate::relayer::RelayerId;
use crate::{
    network::ChainId, postgres::PostgresClient, transaction::types::Transaction,
    yaml::WebhookConfig, SetupConfig,
};

pub struct WebhookManager {
    pub pending_deliveries: Arc<RwLock<HashMap<Uuid, WebhookDelivery>>>,
    pub sender: WebhookSender,
    webhook_configs: Vec<WebhookConfig>,
    network_names: Arc<RwLock<HashMap<ChainId, String>>>,
    // TODO: REVIEW
    #[allow(dead_code)]
    cleanup_interval: Interval,
    // TODO: REVIEW
    #[allow(dead_code)]
    retry_interval: Interval,
}

impl WebhookManager {
    pub fn new(
        db: Arc<PostgresClient>,
        config: &SetupConfig,
        delivery_config: Option<WebhookDeliveryConfig>,
    ) -> Result<Self, reqwest::Error> {
        let webhook_configs = config.webhooks.as_ref().cloned().unwrap_or_default();
        info!("WebhookManager::new - Found {} webhook configs", webhook_configs.len());
        for (i, config) in webhook_configs.iter().enumerate() {
            info!("  {}. Endpoint: {}, Networks: {:?}", i + 1, config.endpoint, config.networks);
        }

        let delivery_config = delivery_config.unwrap_or_default();
        let sender = WebhookSender::new(delivery_config, db)?;

        let mut network_names = HashMap::new();
        for network in &config.networks {
            if let Ok(chain_id) = network.name.parse::<u64>() {
                network_names.insert(ChainId::new(chain_id), network.name.clone());
            }
        }

        Ok(Self {
            pending_deliveries: Arc::new(RwLock::new(HashMap::new())),
            sender,
            webhook_configs,
            network_names: Arc::new(RwLock::new(network_names)),
            cleanup_interval: interval(Duration::from_secs(300)),
            retry_interval: interval(Duration::from_secs(30)),
        })
    }

    pub async fn update_network_names(&self, networks: &[(ChainId, String)]) {
        let mut names = self.network_names.write().await;
        for (chain_id, name) in networks {
            names.insert(*chain_id, name.clone());
        }
        info!("Updated webhook network names for {} networks", networks.len());
    }

    pub async fn queue_webhook_with_payload(
        &self,
        transaction: &Transaction,
        payload: WebhookPayload,
    ) {
        if self.webhook_configs.is_empty() {
            info!("No webhooks configured, skipping webhook for transaction {}", transaction.id);
            return;
        }

        let network_names = self.network_names.read().await;
        let chain_name = network_names
            .get(&transaction.chain_id)
            .cloned()
            .unwrap_or_else(|| transaction.chain_id.to_string());

        let payload_json = match payload.to_json_value() {
            Ok(json) => json,
            Err(e) => {
                error!(
                    "Failed to serialize webhook payload for transaction {}: {}",
                    transaction.id, e
                );
                return;
            }
        };

        let mut deliveries_to_queue = Vec::new();

        for webhook_config in &self.webhook_configs {
            if WebhookFilter::should_send_webhook(webhook_config, transaction, &chain_name) {
                let delivery = WebhookDelivery::new(
                    webhook_config.clone(),
                    payload.event_type.clone(),
                    payload_json.clone(),
                );
                deliveries_to_queue.push(delivery);
            }
        }

        if deliveries_to_queue.is_empty() {
            debug!(
                "No webhooks matched filters for transaction {} on chain {}",
                transaction.id, chain_name
            );
            return;
        }

        info!(
            "Queuing {} webhooks for transaction {} event {} on chain {}",
            deliveries_to_queue.len(),
            transaction.id,
            serde_json::to_string(&payload.event_type).unwrap_or_default(),
            chain_name
        );

        let mut pending = self.pending_deliveries.write().await;
        for delivery in deliveries_to_queue {
            pending.insert(delivery.id, delivery);
        }

        tokio::spawn({
            let manager = self.clone();
            async move {
                manager.process_ready_deliveries().await;
            }
        });
    }

    pub async fn queue_signing_webhook(
        &self,
        relayer_id: &RelayerId,
        chain_id: ChainId,
        payload: WebhookSigningPayload,
    ) {
        if self.webhook_configs.is_empty() {
            info!("No webhooks configured, skipping signing webhook for relayer {}", relayer_id);
            return;
        }

        let network_names = self.network_names.read().await;
        let chain_name =
            network_names.get(&chain_id).cloned().unwrap_or_else(|| chain_id.to_string());

        let payload_json = match payload.to_json_value() {
            Ok(json) => json,
            Err(e) => {
                error!(
                    "Failed to serialize signing webhook payload for relayer {}: {}",
                    relayer_id, e
                );
                return;
            }
        };

        let mut deliveries_to_queue = Vec::new();

        for webhook_config in &self.webhook_configs {
            if webhook_config.networks.is_empty() || webhook_config.networks.contains(&chain_name) {
                let delivery = WebhookDelivery::new(
                    webhook_config.clone(),
                    payload.event_type.clone(),
                    payload_json.clone(),
                );
                deliveries_to_queue.push(delivery);
            }
        }

        if deliveries_to_queue.is_empty() {
            debug!(
                "No webhooks matched filters for signing operation relayer {} on chain {}",
                relayer_id, chain_name
            );
            return;
        }

        info!(
            "Queuing {} signing webhooks for relayer {} event {} on chain {}",
            deliveries_to_queue.len(),
            relayer_id,
            serde_json::to_string(&payload.event_type).unwrap_or_default(),
            chain_name
        );

        let mut pending = self.pending_deliveries.write().await;
        for delivery in deliveries_to_queue {
            pending.insert(delivery.id, delivery);
        }

        tokio::spawn({
            let manager = self.clone();
            async move {
                manager.process_ready_deliveries().await;
            }
        });
    }

    pub async fn queue_webhook(&self, transaction: &Transaction, event_type: WebhookEventType) {
        info!(
            "queue_webhook called for transaction {} with event {:?}",
            transaction.id, event_type
        );

        if self.webhook_configs.is_empty() {
            info!("No webhooks configured, skipping webhook for transaction {}", transaction.id);
            return;
        }

        info!(
            "Found {} webhook configs for transaction {}",
            self.webhook_configs.len(),
            transaction.id
        );

        let network_names = self.network_names.read().await;
        let chain_name = network_names
            .get(&transaction.chain_id)
            .cloned()
            .unwrap_or_else(|| transaction.chain_id.to_string());

        let payload = WebhookPayload::new(transaction, event_type.clone());
        let payload_json = match payload.to_json_value() {
            Ok(json) => json,
            Err(e) => {
                error!(
                    "Failed to serialize webhook payload for transaction {}: {}",
                    transaction.id, e
                );
                return;
            }
        };

        let mut deliveries_to_queue = Vec::new();

        for webhook_config in &self.webhook_configs {
            if WebhookFilter::should_send_webhook(webhook_config, transaction, &chain_name) {
                let delivery = WebhookDelivery::new(
                    webhook_config.clone(),
                    event_type.clone(),
                    payload_json.clone(),
                );
                deliveries_to_queue.push(delivery);
            }
        }

        if deliveries_to_queue.is_empty() {
            debug!(
                "No webhooks matched filters for transaction {} on chain {}",
                transaction.id, chain_name
            );
            return;
        }

        info!(
            "Queuing {} webhooks for transaction {} event {} on chain {}",
            deliveries_to_queue.len(),
            transaction.id,
            serde_json::to_string(&event_type).unwrap_or_default(),
            chain_name
        );

        let mut pending = self.pending_deliveries.write().await;
        for delivery in deliveries_to_queue {
            pending.insert(delivery.id, delivery);
        }

        tokio::spawn({
            let manager = self.clone();
            async move {
                manager.process_ready_deliveries().await;
            }
        });
    }

    async fn process_ready_deliveries(&self) {
        let now = SystemTime::now();
        let ready_deliveries = {
            let pending = self.pending_deliveries.read().await;
            pending.values().filter(|d| d.is_ready_for_retry(now)).cloned().collect::<Vec<_>>()
        };

        if ready_deliveries.is_empty() {
            debug!("No webhook deliveries ready for processing");
            return;
        }

        info!("Processing {} ready webhook deliveries", ready_deliveries.len());

        let updated_deliveries = self.sender.send_multiple_webhooks(ready_deliveries).await;

        let mut pending = self.pending_deliveries.write().await;
        for delivery in updated_deliveries {
            pending.insert(delivery.id, delivery);
        }
    }

    // TODO: REVIEW
    #[allow(dead_code)]
    async fn cleanup_deliveries(&self) {
        let mut pending = self.pending_deliveries.write().await;
        let initial_count = pending.len();

        pending.retain(|_, delivery| !delivery.completed && !delivery.failed);

        let removed_count = initial_count - pending.len();
        if removed_count > 0 {
            info!("Cleaned up {} completed/failed webhook deliveries", removed_count);
        }
    }

    pub async fn pending_count(&self) -> usize {
        self.pending_deliveries.read().await.len()
    }
}

impl Clone for WebhookManager {
    fn clone(&self) -> Self {
        Self {
            pending_deliveries: self.pending_deliveries.clone(),
            sender: self.sender.clone(),
            webhook_configs: self.webhook_configs.clone(),
            network_names: self.network_names.clone(),
            cleanup_interval: interval(Duration::from_secs(300)),
            retry_interval: interval(Duration::from_secs(30)),
        }
    }
}

impl WebhookManager {
    pub async fn on_transaction_queued(&self, transaction: &Transaction) {
        self.queue_webhook(transaction, WebhookEventType::TransactionQueued).await;
    }

    pub async fn on_transaction_sent(&self, transaction: &Transaction) {
        self.queue_webhook(transaction, WebhookEventType::TransactionSent).await;
    }

    pub async fn on_transaction_mined(
        &self,
        transaction: &Transaction,
        receipt: &alloy::network::AnyTransactionReceipt,
    ) {
        let payload = WebhookPayload::transaction_mined_with_receipt(transaction, receipt);
        self.queue_webhook_with_payload(transaction, payload).await;
    }

    pub async fn on_transaction_confirmed(
        &self,
        transaction: &Transaction,
        receipt: &alloy::network::AnyTransactionReceipt,
    ) {
        let payload = WebhookPayload::transaction_confirmed_with_receipt(transaction, receipt);
        self.queue_webhook_with_payload(transaction, payload).await;
    }

    pub async fn on_transaction_failed(&self, transaction: &Transaction) {
        self.queue_webhook(transaction, WebhookEventType::TransactionFailed).await;
    }

    pub async fn on_transaction_expired(&self, transaction: &Transaction) {
        self.queue_webhook(transaction, WebhookEventType::TransactionExpired).await;
    }

    pub async fn on_transaction_cancelled(&self, transaction: &Transaction) {
        self.queue_webhook(transaction, WebhookEventType::TransactionCancelled).await;
    }

    pub async fn on_transaction_replaced(
        &self,
        new_transaction: &Transaction,
        original_transaction: &Transaction,
    ) {
        let payload = WebhookPayload::transaction_replaced(new_transaction, original_transaction);
        self.queue_webhook_with_payload(new_transaction, payload).await;
    }

    pub async fn on_text_signed(
        &self,
        relayer_id: &RelayerId,
        chain_id: ChainId,
        message: String,
        signature: alloy::primitives::Signature,
    ) {
        let payload = WebhookSigningPayload::text_signed(*relayer_id, chain_id, message, signature);
        self.queue_signing_webhook(relayer_id, chain_id, payload).await;
    }

    pub async fn on_typed_data_signed(
        &self,
        relayer_id: &RelayerId,
        chain_id: ChainId,
        domain_data: serde_json::Value,
        message_data: serde_json::Value,
        primary_type: String,
        signature: alloy::primitives::Signature,
    ) {
        let payload = WebhookSigningPayload::typed_data_signed(
            *relayer_id,
            chain_id,
            domain_data,
            message_data,
            primary_type,
            signature,
        );
        self.queue_signing_webhook(relayer_id, chain_id, payload).await;
    }

    /// Get webhook configurations that should receive low balance alerts for a specific chain
    pub fn get_webhook_configs_for_chain(&self, chain_id: &ChainId) -> Vec<&WebhookConfig> {
        self.webhook_configs
            .iter()
            .filter(|config| {
                config.alert_on_low_balances.unwrap_or(false)
                    && (config.networks.is_empty()
                        || config.networks.contains(&chain_id.to_string()))
            })
            .collect()
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn queue_low_balance_webhook(
        &self,
        relayer_id: &str,
        address: &crate::common_types::EvmAddress,
        chain_id: ChainId,
        current_balance: u128,
        minimum_balance: u128,
        current_balance_formatted: String,
        minimum_balance_formatted: String,
    ) {
        use crate::webhooks::WebhookLowBalancePayload;

        let payload = WebhookLowBalancePayload::new(
            relayer_id.to_string(),
            *address,
            chain_id,
            current_balance,
            minimum_balance,
            current_balance_formatted,
            minimum_balance_formatted,
        );

        if let Ok(payload_json) = payload.to_json_value() {
            self.queue_low_balance_webhook_internal(chain_id, payload.event_type, payload_json)
                .await;
        } else {
            error!("Failed to serialize low balance webhook payload");
        }
    }

    async fn queue_low_balance_webhook_internal(
        &self,
        chain_id: ChainId,
        event_type: crate::webhooks::types::WebhookEventType,
        payload_json: serde_json::Value,
    ) {
        if self.webhook_configs.is_empty() {
            info!("No webhooks configured, skipping low balance webhook for chain {}", chain_id);
            return;
        }

        let network_names = self.network_names.read().await;
        let chain_name =
            network_names.get(&chain_id).cloned().unwrap_or_else(|| chain_id.to_string());

        let mut deliveries_to_queue = Vec::new();

        for webhook_config in &self.webhook_configs {
            if webhook_config.alert_on_low_balances.unwrap_or(false)
                && (webhook_config.networks.is_empty()
                    || webhook_config.networks.contains(&chain_name))
            {
                use crate::webhooks::types::WebhookDelivery;
                let delivery = WebhookDelivery::new(
                    webhook_config.clone(),
                    event_type.clone(),
                    payload_json.clone(),
                );
                deliveries_to_queue.push(delivery);
            }
        }

        if deliveries_to_queue.is_empty() {
            debug!("No webhooks configured for low balance alerts on chain {}", chain_name);
            return;
        }

        info!(
            "Queuing {} low balance webhooks for chain {}",
            deliveries_to_queue.len(),
            chain_name
        );

        let mut pending = self.pending_deliveries.write().await;
        for delivery in deliveries_to_queue {
            pending.insert(delivery.id, delivery);
        }

        tokio::spawn({
            let manager = self.clone();
            async move {
                manager.process_ready_deliveries().await;
            }
        });
    }
}