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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
use std::{
    collections::{HashMap, VecDeque},
    sync::Arc,
};

use alloy::{
    consensus::TypedTransaction,
    transports::{RpcError, TransportErrorKind},
};
use chrono::{DateTime, Utc};
use thiserror::Error;
use tokio::sync::Mutex;
use tracing::{error, info, warn};

/// Error types for transaction queues operations.
#[derive(Error, Debug)]
pub enum TransactionsQueuesError {
    #[error("Wallet or provider error: {0}")]
    WalletOrProvider(#[from] WalletOrProviderError),
    #[error("Database connection error: {0}")]
    DatabaseConnection(#[from] PostgresConnectionError),
}

use super::{
    start::spawn_processing_tasks_for_relayer,
    transactions_queue::TransactionsQueue,
    types::{
        AddTransactionError, CancelTransactionError, CancelTransactionResult, CompetitionType,
        EditableTransactionType, ProcessInmempoolStatus, ProcessInmempoolTransactionError,
        ProcessMinedStatus, ProcessMinedTransactionError, ProcessPendingStatus,
        ProcessPendingTransactionError, ProcessResult, ReplaceTransactionError,
        ReplaceTransactionResult, TransactionRelayerSetup, TransactionToSend,
        TransactionsQueueSetup,
    },
};
use crate::transaction::api::RelayTransactionRequest;
use crate::transaction::queue_system::types::SendTransactionGasPriceError;
use crate::transaction::types::{TransactionBlob, TransactionConversionError, TransactionSpeed};
use crate::{
    gas::{BlobGasOracleCache, BlobGasPriceResult, GasLimit, GasOracleCache, GasPriceResult},
    postgres::{PostgresClient, PostgresConnectionError},
    relayer::RelayerId,
    safe_proxy::SafeProxyManager,
    shared::{cache::Cache, common_types::WalletOrProviderError},
    shutdown::enter_critical_operation,
    transaction::{
        cache::invalidate_transaction_no_state_cache,
        nonce_manager::NonceManager,
        queue_system::types::TransactionQueueSendTransactionError,
        types::{Transaction, TransactionData, TransactionId, TransactionStatus, TransactionValue},
    },
    webhooks::WebhookManager,
};

/// Container for managing multiple transaction queues across different relayers.
///
/// This struct coordinates transaction processing for all active relayers,
/// providing centralized access to individual queue operations and shared resources.
pub struct TransactionsQueues {
    pub queues: HashMap<RelayerId, Arc<Mutex<TransactionsQueue>>>,
    pub relayer_block_times_ms: HashMap<RelayerId, u64>,
    gas_oracle_cache: Arc<Mutex<GasOracleCache>>,
    blob_gas_oracle_cache: Arc<Mutex<BlobGasOracleCache>>,
    db: PostgresClient,
    cache: Arc<Cache>,
    webhook_manager: Option<Arc<Mutex<WebhookManager>>>,
    safe_proxy_manager: Arc<SafeProxyManager>,
}

impl TransactionsQueues {
    pub async fn new(
        setups: Vec<TransactionRelayerSetup>,
        gas_oracle_cache: Arc<Mutex<GasOracleCache>>,
        blob_gas_oracle_cache: Arc<Mutex<BlobGasOracleCache>>,
        cache: Arc<Cache>,
        webhook_manager: Option<Arc<Mutex<WebhookManager>>>,
        safe_proxy_manager: Arc<SafeProxyManager>,
    ) -> Result<Self, TransactionsQueuesError> {
        let mut queues = HashMap::new();
        let mut relayer_block_times_ms = HashMap::new();

        for setup in setups {
            let current_nonce =
                setup.evm_provider.get_nonce(&setup.relayer.wallet_index_type().index()).await?;

            info!(
                "Startup nonce synchronization for relayer {} ({}): synchronizing nonce manager with on-chain nonce {}",
                setup.relayer.name, setup.relayer.id, current_nonce.into_inner()
            );

            relayer_block_times_ms.insert(setup.relayer.id, setup.evm_provider.blocks_every);

            queues.insert(
                setup.relayer.id,
                Arc::new(Mutex::new(TransactionsQueue::new(
                    TransactionsQueueSetup::new(
                        setup.relayer,
                        setup.evm_provider,
                        NonceManager::new(current_nonce),
                        setup.pending_transactions,
                        setup.inmempool_transactions,
                        setup.mined_transactions,
                        safe_proxy_manager.clone(),
                        setup.gas_bump_config,
                        setup.max_gas_price_multiplier,
                    ),
                    gas_oracle_cache.clone(),
                    blob_gas_oracle_cache.clone(),
                ))),
            );
        }

        Ok(Self {
            queues,
            relayer_block_times_ms,
            gas_oracle_cache,
            blob_gas_oracle_cache,
            db: PostgresClient::new().await?,
            cache,
            webhook_manager,
            safe_proxy_manager,
        })
    }

    /// Retrieves a transaction queue for the specified relayer.
    pub fn get_transactions_queue(
        &self,
        relayer_id: &RelayerId,
    ) -> Option<Arc<Mutex<TransactionsQueue>>> {
        self.queues.get(relayer_id).cloned()
    }

    /// Retrieves a transaction queue for the specified relayer.
    pub fn get_transactions_queue_unsafe(
        &self,
        relayer_id: &RelayerId,
    ) -> Result<Arc<Mutex<TransactionsQueue>>, String> {
        self.queues
            .get(relayer_id)
            .cloned()
            .ok_or_else(|| format!("transactions queue does not exist for relayer: {}", relayer_id))
    }

    /// Removes a transaction queue for the specified relayer.
    pub async fn delete_queue(&mut self, relayer_id: &RelayerId) {
        self.queues.remove(relayer_id);
    }

    /// Invalidates the cache entry for a specific transaction.
    async fn invalidate_transaction_cache(&self, id: &TransactionId) {
        invalidate_transaction_no_state_cache(&self.cache, id).await;
    }

    /// Returns the count of pending transactions for a specific relayer.
    pub async fn pending_transactions_count(&self, relayer_id: &RelayerId) -> usize {
        if let Some(queue_arc) = self.get_transactions_queue(relayer_id) {
            let queue = queue_arc.lock().await;
            queue.get_pending_transaction_count().await
        } else {
            0
        }
    }

    /// Returns the count of in-mempool transactions for a specific relayer.
    pub async fn inmempool_transactions_count(&self, relayer_id: &RelayerId) -> usize {
        if let Some(queue_arc) = self.get_transactions_queue(relayer_id) {
            let queue = queue_arc.lock().await;
            queue.get_inmempool_transaction_count().await
        } else {
            0
        }
    }

    /// Adds a new relayer and its transaction queue to the system.
    pub async fn add_new_relayer(
        &mut self,
        setup: TransactionsQueueSetup,
        queues_arc: Arc<Mutex<TransactionsQueues>>,
    ) -> Result<(), WalletOrProviderError> {
        let current_nonce =
            setup.evm_provider.get_nonce(&setup.relayer.wallet_index_type().index()).await?;
        let relayer_id = setup.relayer.id;

        self.queues.insert(
            relayer_id,
            Arc::new(Mutex::new(TransactionsQueue::new(
                TransactionsQueueSetup::new(
                    setup.relayer,
                    setup.evm_provider,
                    NonceManager::new(current_nonce),
                    VecDeque::new(),
                    VecDeque::new(),
                    HashMap::new(),
                    self.safe_proxy_manager.clone(),
                    setup.gas_bump_config,
                    setup.max_gas_price_multiplier,
                ),
                self.gas_oracle_cache.clone(),
                self.blob_gas_oracle_cache.clone(),
            ))),
        );

        spawn_processing_tasks_for_relayer(queues_arc, &relayer_id).await;

        Ok(())
    }

    fn expires_at(&self) -> DateTime<Utc> {
        Utc::now() + chrono::Duration::hours(12)
    }

    /// Checks if a transaction has expired.
    fn has_expired(&self, transaction: &Transaction) -> bool {
        transaction.expires_at < Utc::now()
    }

    /// Converts a transaction to a no-op transaction.
    fn transaction_to_noop(
        &self,
        transactions_queue: &mut TransactionsQueue,
        transaction: &mut Transaction,
    ) {
        transaction.to = transactions_queue.relay_address();
        transaction.value = TransactionValue::zero();
        transaction.data = TransactionData::empty();
        transaction.gas_limit = Some(GasLimit::new(21000_u128));
        transaction.is_noop = true;
        transaction.speed = TransactionSpeed::FAST;
    }

    /// Replaces the content of an existing transaction with new parameters.
    fn transaction_replace(
        &self,
        current_transaction: &mut Transaction,
        replace_with: &RelayTransactionRequest,
    ) {
        current_transaction.to = replace_with.to;
        current_transaction.data = replace_with.data.clone();
        current_transaction.value = replace_with.value;
        current_transaction.is_noop = current_transaction.from == current_transaction.to;

        if let Some(ref blob_strings) = replace_with.blobs {
            current_transaction.blobs = Some(
                blob_strings
                    .iter()
                    .map(|blob_hex| TransactionBlob::from_hex(blob_hex))
                    .collect::<Result<Vec<_>, _>>()
                    .expect("Failed to convert blob hex strings to TransactionBlob"),
            );
        } else {
            current_transaction.blobs = None;
        }
        current_transaction.gas_limit = None;
        current_transaction.external_id = replace_with.external_id.clone();
    }

    /// Computes gas prices for a transaction based on its type.
    async fn compute_transaction_gas_prices(
        transactions_queue: &TransactionsQueue,
        transaction: &Transaction,
        speed: &TransactionSpeed,
    ) -> Result<(GasPriceResult, Option<BlobGasPriceResult>), SendTransactionGasPriceError> {
        let blob_gas_price = if transaction.is_blob_transaction() {
            Some(transactions_queue.compute_blob_gas_price_for_transaction(speed, &None).await?)
        } else {
            None
        };

        let gas_price = transactions_queue.compute_gas_price_for_transaction(speed, None).await?;

        Ok((gas_price, blob_gas_price))
    }

    /// Creates a typed transaction request for gas estimation or sending.
    fn create_typed_transaction(
        transactions_queue: &TransactionsQueue,
        transaction: &Transaction,
        gas_price: &GasPriceResult,
        blob_gas_price: Option<&BlobGasPriceResult>,
        gas_limit: GasLimit,
    ) -> Result<TypedTransaction, TransactionConversionError> {
        if transaction.is_blob_transaction() {
            Ok(transaction.to_blob_typed_transaction_with_gas_limit(
                Some(gas_price),
                blob_gas_price,
                Some(gas_limit),
            )?)
        } else if transactions_queue.is_legacy_transactions() {
            Ok(transaction
                .to_legacy_typed_transaction_with_gas_limit(Some(gas_price), Some(gas_limit))?)
        } else {
            Ok(transaction
                .to_eip1559_typed_transaction_with_gas_limit(Some(gas_price), Some(gas_limit))?)
        }
    }

    /// Estimates gas limit for a transaction and validates it via simulation.
    async fn estimate_and_validate_gas(
        transactions_queue: &mut TransactionsQueue,
        transaction: &Transaction,
        gas_price: &GasPriceResult,
        blob_gas_price: Option<&BlobGasPriceResult>,
    ) -> Result<GasLimit, AddTransactionError> {
        // Use a reasonable temporary limit for gas estimation
        const TEMP_GAS_LIMIT: u128 = 1_000_000;
        let temp_gas_limit = GasLimit::new(TEMP_GAS_LIMIT);

        let current_onchain_nonce = transactions_queue.get_nonce().await.map_err(|e| {
            AddTransactionError::CouldNotGetCurrentOnChainNonce(transaction.relayer_id, e)
        })?;

        let mut estimation_transaction = transaction.clone();
        estimation_transaction.nonce = current_onchain_nonce;

        let temp_transaction_request = Self::create_typed_transaction(
            transactions_queue,
            &estimation_transaction,
            gas_price,
            blob_gas_price,
            temp_gas_limit,
        )?;

        let estimated_gas_limit = transactions_queue
            .estimate_gas(&temp_transaction_request, transaction.is_noop)
            .await
            .map_err(|e| {
                AddTransactionError::TransactionEstimateGasError(transaction.relayer_id, e)
            })?;

        let relayer_balance = transactions_queue.get_balance().await.map_err(|e| {
            AddTransactionError::TransactionEstimateGasError(transaction.relayer_id, e)
        })?;

        let gas_cost = estimated_gas_limit.into_inner() * gas_price.legacy_gas_price().into_u128();
        let total_required =
            transaction.value.into_inner() + alloy::primitives::U256::from(gas_cost);

        if relayer_balance < total_required {
            error!(
                "Insufficient balance for relayer {}: has {}, needs {}",
                transaction.relayer_id, relayer_balance, total_required
            );
            return Err(AddTransactionError::TransactionEstimateGasError(
                transaction.relayer_id,
                RpcError::Transport(TransportErrorKind::Custom(
                    "Insufficient funds for gas * price + value".to_string().into(),
                )),
            ));
        }

        Ok(estimated_gas_limit)
    }

    /// Adds a new transaction to the specified relayer's queue.
    pub async fn add_transaction(
        &mut self,
        relayer_id: &RelayerId,
        transaction_to_send: &TransactionToSend,
    ) -> Result<Transaction, AddTransactionError> {
        let expires_at = self.expires_at();

        let queue_arc = self
            .get_transactions_queue(relayer_id)
            .ok_or(AddTransactionError::RelayerNotFound(*relayer_id))?;

        let mut transactions_queue = queue_arc.lock().await;

        if transactions_queue.is_paused() {
            return Err(AddTransactionError::RelayerIsPaused(*relayer_id));
        }

        // Check if this is a blob transaction and if the wallet manager supports blobs
        if transaction_to_send.blobs.is_some() && !transactions_queue.supports_blobs() {
            return Err(AddTransactionError::UnsupportedTransactionType {
                message: "EIP-4844 blob transactions are not supported by this wallet manager"
                    .to_string(),
            });
        }

        // Sync nonce manager with on-chain nonce to ensure consistency
        let current_onchain_nonce = transactions_queue
            .get_nonce()
            .await
            .map_err(|e| AddTransactionError::CouldNotGetCurrentOnChainNonce(*relayer_id, e))?;

        transactions_queue.nonce_manager.sync_with_onchain_nonce(current_onchain_nonce).await;
        let assigned_nonce = transactions_queue.nonce_manager.get_and_increment().await;

        let mut transaction = Transaction {
            id: transaction_to_send.id,
            relayer_id: *relayer_id,
            to: transaction_to_send.to,
            from: transactions_queue.relay_address(),
            value: transaction_to_send.value,
            data: transaction_to_send.data.clone(),
            nonce: assigned_nonce,
            gas_limit: None,
            status: TransactionStatus::PENDING,
            blobs: transaction_to_send.blobs.clone(),
            chain_id: transactions_queue.chain_id(),
            known_transaction_hash: None,
            queued_at: Utc::now(),
            expires_at,
            sent_at: None,
            mined_at: None,
            mined_at_block_number: None,
            confirmed_at: None,
            speed: transaction_to_send.speed.clone(),
            sent_with_max_priority_fee_per_gas: None,
            sent_with_max_fee_per_gas: None,
            is_noop: false,
            sent_with_gas: None,
            sent_with_blob_gas: None,
            external_id: transaction_to_send.external_id.clone(),
            cancelled_by_transaction_id: None,
        };

        let (gas_price, blob_gas_price) = Self::compute_transaction_gas_prices(
            &transactions_queue,
            &transaction,
            &transaction_to_send.speed,
        )
        .await?;

        let estimated_gas_limit = Self::estimate_and_validate_gas(
            &mut transactions_queue,
            &transaction,
            &gas_price,
            blob_gas_price.as_ref(),
        )
        .await;

        let estimated_gas_limit = match estimated_gas_limit {
            Ok(limit) => limit,
            Err(err) => {
                self.db
                    .transaction_failed_on_send(
                        relayer_id,
                        &transaction,
                        "Failed to send transaction as always failing on gas estimation",
                    )
                    .await
                    .map_err(AddTransactionError::CouldNotSaveTransactionDb)?;

                self.invalidate_transaction_cache(&transaction.id).await;
                return Err(err);
            }
        };

        transaction.gas_limit = Some(estimated_gas_limit);

        let transaction_request = Self::create_typed_transaction(
            &transactions_queue,
            &transaction,
            &gas_price,
            blob_gas_price.as_ref(),
            estimated_gas_limit,
        )?;

        transaction.known_transaction_hash =
            Some(transactions_queue.compute_tx_hash(&transaction_request).await?);

        self.db
            .save_transaction(relayer_id, &transaction)
            .await
            .map_err(AddTransactionError::CouldNotSaveTransactionDb)?;

        transactions_queue.add_pending_transaction(transaction.clone()).await;
        // Nonce already incremented atomically above - no need for separate increase call
        self.invalidate_transaction_cache(&transaction.id).await;

        if let Some(webhook_manager) = &self.webhook_manager {
            let webhook_manager = webhook_manager.clone();
            let transaction_clone = transaction.clone();
            tokio::spawn(async move {
                let webhook_manager = webhook_manager.lock().await;
                webhook_manager.on_transaction_queued(&transaction_clone).await;
            });
        }

        Ok(transaction)
    }

    /// Cancels an existing transaction.
    pub async fn cancel_transaction(
        &mut self,
        transaction: &Transaction,
    ) -> Result<CancelTransactionResult, CancelTransactionError> {
        if let Some(queue_arc) = self.get_transactions_queue(&transaction.relayer_id) {
            let mut transactions_queue = queue_arc.lock().await;
            if transactions_queue.is_paused() {
                return Err(CancelTransactionError::RelayerIsPaused(transaction.relayer_id));
            }

            if let Some(mut result) =
                transactions_queue.get_editable_transaction_by_id(&transaction.id).await
            {
                let _guard = enter_critical_operation().ok_or_else(|| {
                    info!(
                        "cancel_transaction: refusing to start during shutdown for transaction {}",
                        transaction.id
                    );
                    CancelTransactionError::RelayerIsPaused(transaction.relayer_id)
                })?;

                match result.type_name {
                    EditableTransactionType::Pending => {
                        info!("cancel_transaction: removing pending transaction from queue and marking as cancelled");

                        result.transaction.status = TransactionStatus::CANCELLED;
                        transactions_queue.remove_pending_transaction_by_id(&transaction.id).await;

                        self.db
                            .transaction_update(&result.transaction)
                            .await
                            .map_err(CancelTransactionError::CouldNotUpdateTransactionDb)?;

                        self.invalidate_transaction_cache(&transaction.id).await;

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let original_transaction = result.transaction.clone();
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager
                                    .on_transaction_cancelled(&original_transaction)
                                    .await;
                            });
                        }

                        Ok(CancelTransactionResult { success: true, cancel_transaction_id: None })
                    }
                    EditableTransactionType::Inmempool => {
                        let cancel_transaction_id = TransactionId::new();
                        let expires_at = self.expires_at();

                        let original_gas_limit = result.transaction.gas_limit.ok_or_else(|| {
                            CancelTransactionError::SendTransactionError(
                                TransactionQueueSendTransactionError::GasCalculationError,
                            )
                        })?;
                        let bumped_gas_limit = GasLimit::new(
                            original_gas_limit.into_inner() + (original_gas_limit.into_inner() / 5),
                        );

                        let mut cancel_transaction = Transaction {
                            id: cancel_transaction_id,
                            relayer_id: transaction.relayer_id,
                            // Send to self (no-op)
                            to: transactions_queue.relay_address(),
                            from: transactions_queue.relay_address(),
                            value: TransactionValue::zero(),
                            data: TransactionData::empty(),
                            // Use the same nonce as the original transaction to replace it
                            nonce: result.transaction.nonce,
                            // Use original gas + 20% instead of estimating
                            gas_limit: Some(bumped_gas_limit),
                            status: TransactionStatus::PENDING,
                            blobs: None,
                            chain_id: transactions_queue.chain_id(),
                            known_transaction_hash: None,
                            queued_at: Utc::now(),
                            expires_at,
                            sent_at: None,
                            mined_at: None,
                            mined_at_block_number: None,
                            confirmed_at: None,
                            // Use highest speed for faster replacement
                            speed: TransactionSpeed::SUPER,
                            sent_with_max_priority_fee_per_gas: None,
                            sent_with_max_fee_per_gas: None,
                            is_noop: true,
                            sent_with_gas: None,
                            sent_with_blob_gas: None,
                            external_id: Some(format!("cancel_{}", transaction.id)),
                            cancelled_by_transaction_id: None,
                        };

                        info!("cancel_transaction: creating higher gas cancel transaction for inmempool tx with same nonce {:?}", cancel_transaction.nonce);

                        // For cancel transactions, we need to bump the original transaction's gas prices
                        // rather than using a fixed speed, since we're competing with the same nonce
                        let original_gas =
                            result.transaction.sent_with_gas.as_ref().ok_or_else(|| {
                                CancelTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::GasCalculationError,
                                )
                            })?;

                        // Bump original gas prices by 20% to ensure replacement
                        let bumped_max_fee = original_gas.max_fee + (original_gas.max_fee / 5);
                        let bumped_max_priority_fee =
                            original_gas.max_priority_fee + (original_gas.max_priority_fee / 5);

                        let gas_price = GasPriceResult {
                            max_fee: bumped_max_fee,
                            max_priority_fee: bumped_max_priority_fee,
                            min_wait_time_estimate: None,
                            max_wait_time_estimate: None,
                        };

                        // Blob gas price is not needed for cancel transactions (they're simple transfers)
                        let blob_gas_price = None;
                        // Apply gas prices to cancel transaction
                        cancel_transaction.sent_with_gas = Some(gas_price);
                        cancel_transaction.sent_with_blob_gas = blob_gas_price;

                        let transaction_sent = match transactions_queue
                            .send_transaction(&mut self.db, &mut cancel_transaction)
                            .await
                        {
                            Ok(tx_sent) => tx_sent,
                            Err(TransactionQueueSendTransactionError::TransactionSendError(
                                error,
                            )) => {
                                let error_msg = error.to_string().to_lowercase();
                                if error_msg.contains("nonce too low")
                                    || error_msg.contains("nonce is too low")
                                    || error_msg.contains("invalid nonce")
                                    || error_msg.contains("nonce has already been used")
                                    || error_msg.contains("already known")
                                {
                                    warn!("cancel_transaction: nonce synchronization issue detected for relayer {}: {}", transaction.relayer_id, error);

                                    if let Err(sync_error) = self
                                        .recover_nonce_synchronization(
                                            &transaction.relayer_id,
                                            &mut transactions_queue,
                                        )
                                        .await
                                    {
                                        error!("Failed to recover nonce synchronization for relayer {}: {}", transaction.relayer_id, sync_error);
                                        return Err(CancelTransactionError::SendTransactionError(
                                            TransactionQueueSendTransactionError::TransactionSendError(error)
                                        ));
                                    }

                                    info!("Nonce synchronization recovered for relayer {}, cancel transaction will be retried", transaction.relayer_id);
                                    return Err(
                                        CancelTransactionError::NonceSynchronizationRecovered,
                                    );
                                }
                                return Err(CancelTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::TransactionSendError(
                                        error,
                                    ),
                                ));
                            }
                            Err(e) => return Err(CancelTransactionError::SendTransactionError(e)),
                        };

                        // DON'T mark original as CANCELLED yet - that's premature!
                        // We have a race condition: both transactions will compete with same nonce
                        // The monitoring logic will determine which one wins and update statuses accordingly

                        cancel_transaction.status = TransactionStatus::INMEMPOOL;
                        cancel_transaction.known_transaction_hash = Some(transaction_sent.hash);
                        cancel_transaction.sent_at = Some(Utc::now());

                        self.db
                            .save_transaction(&transaction.relayer_id, &cancel_transaction)
                            .await
                            .map_err(CancelTransactionError::CouldNotUpdateTransactionDb)?;

                        transactions_queue
                            .add_competitor_to_inmempool_transaction(
                                &transaction.id,
                                cancel_transaction.clone(),
                                CompetitionType::Cancel,
                            )
                            .await
                            .map_err(CancelTransactionError::SendTransactionError)?;

                        // Now we can safely set the foreign key reference and update the original transaction
                        // For now, we track that a cancellation is pending by setting the cancelled_by_transaction_id
                        // but keep the original status as INMEMPOOL since it's still competing
                        result.transaction.cancelled_by_transaction_id =
                            Some(cancel_transaction_id);

                        self.db
                            .transaction_update(&result.transaction)
                            .await
                            .map_err(CancelTransactionError::CouldNotUpdateTransactionDb)?;

                        self.invalidate_transaction_cache(&transaction.id).await;

                        info!("cancel_transaction: sent cancel tx {} with hash {} and nonce {:?} to replace original tx {}", 
                              cancel_transaction_id, transaction_sent.hash, cancel_transaction.nonce, transaction.id);

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let original_transaction = result.transaction.clone();
                            let cancel_transaction_clone = cancel_transaction.clone();
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager
                                    .on_transaction_cancelled(&original_transaction)
                                    .await;
                                webhook_manager
                                    .on_transaction_sent(&cancel_transaction_clone)
                                    .await;
                            });
                        }

                        Ok(CancelTransactionResult::success(cancel_transaction_id))
                    }
                }
            } else if transactions_queue.is_transaction_mined(&transaction.id).await {
                info!(
                    "cancel_transaction: transaction {} is already mined, cannot cancel",
                    transaction.id
                );
                Ok(CancelTransactionResult::failed())
            } else {
                info!("cancel_transaction: transaction {} not found in any queue", transaction.id);
                Ok(CancelTransactionResult::failed())
            }
        } else {
            Err(CancelTransactionError::RelayerNotFound(transaction.relayer_id))
        }
    }

    /// Replaces an existing transaction with new parameters.
    pub async fn replace_transaction(
        &mut self,
        transaction: &Transaction,
        replace_with: &RelayTransactionRequest,
    ) -> Result<ReplaceTransactionResult, ReplaceTransactionError> {
        if let Some(queue_arc) = self.get_transactions_queue(&transaction.relayer_id) {
            let mut transactions_queue = queue_arc.lock().await;

            if transactions_queue.is_paused() {
                return Err(ReplaceTransactionError::RelayerIsPaused(transaction.relayer_id));
            }

            if let Some(mut result) =
                transactions_queue.get_editable_transaction_by_id(&transaction.id).await
            {
                let _guard = enter_critical_operation().ok_or_else(|| {
                    info!(
                        "replace_transaction: refusing to start during shutdown for transaction {}",
                        transaction.id
                    );
                    ReplaceTransactionError::RelayerIsPaused(transaction.relayer_id)
                })?;

                match result.type_name {
                    EditableTransactionType::Pending => {
                        let original_transaction = result.transaction.clone();
                        self.transaction_replace(&mut result.transaction, replace_with);
                        self.invalidate_transaction_cache(&transaction.id).await;

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let new_transaction = result.transaction.clone();
                            let original_transaction_clone = original_transaction.clone();
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager
                                    .on_transaction_replaced(
                                        &new_transaction,
                                        &original_transaction_clone,
                                    )
                                    .await;
                            });
                        }

                        Ok(ReplaceTransactionResult {
                            success: true,
                            replace_transaction_id: Some(result.transaction.id),
                            replace_transaction_hash: result.transaction.known_transaction_hash,
                        })
                    }
                    EditableTransactionType::Inmempool => {
                        let replace_transaction_id = TransactionId::new();
                        let expires_at = self.expires_at();

                        let mut replace_transaction = Transaction {
                            id: replace_transaction_id,
                            relayer_id: transaction.relayer_id,
                            to: replace_with.to,
                            from: transactions_queue.relay_address(),
                            value: replace_with.value,
                            data: replace_with.data.clone(),
                            // Use the same nonce as the original transaction to replace it
                            nonce: result.transaction.nonce,
                            gas_limit: None, // Will be estimated during send_transaction
                            status: TransactionStatus::PENDING,
                            blobs: replace_with
                                .blobs
                                .as_ref()
                                .map(|blobs| {
                                    blobs
                                        .iter()
                                        .map(|blob_hex| TransactionBlob::from_hex(blob_hex))
                                        .collect::<Result<Vec<_>, _>>()
                                })
                                .transpose()
                                .map_err(|e| {
                                    ReplaceTransactionError::SendTransactionError(
                                TransactionQueueSendTransactionError::TransactionConversionError(
                                    format!("Failed to convert blob hex to TransactionBlob: {}", e)
                                )
                            )
                                })?,
                            chain_id: transactions_queue.chain_id(),
                            known_transaction_hash: None,
                            queued_at: Utc::now(),
                            expires_at,
                            sent_at: None,
                            mined_at: None,
                            mined_at_block_number: None,
                            confirmed_at: None,
                            speed: TransactionSpeed::SUPER, // Use highest speed for faster replacement
                            sent_with_max_priority_fee_per_gas: None,
                            sent_with_max_fee_per_gas: None,
                            is_noop: false,
                            sent_with_gas: None,
                            sent_with_blob_gas: None,
                            external_id: replace_with
                                .external_id
                                .clone()
                                .or_else(|| Some(format!("replace_{}", transaction.id))),
                            cancelled_by_transaction_id: None,
                        };

                        info!("replace_transaction: creating competitive replace transaction for inmempool tx with same nonce {:?}", replace_transaction.nonce);

                        // For replace transactions, we need to bump the original transaction's gas prices and gas limit
                        // rather than using a fixed speed or estimating, since we're competing with the same nonce
                        let original_gas =
                            result.transaction.sent_with_gas.as_ref().ok_or_else(|| {
                                ReplaceTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::GasCalculationError,
                                )
                            })?;

                        // Use original gas limit + 20% to avoid "nonce too low" errors during gas estimation
                        let original_gas_limit = result.transaction.gas_limit.ok_or_else(|| {
                            ReplaceTransactionError::SendTransactionError(
                                TransactionQueueSendTransactionError::GasCalculationError,
                            )
                        })?;
                        let bumped_gas_limit = GasLimit::new(
                            original_gas_limit.into_inner() + (original_gas_limit.into_inner() / 5),
                        );
                        replace_transaction.gas_limit = Some(bumped_gas_limit);

                        // Bump original gas prices by 20% to ensure replacement
                        let bumped_max_fee = original_gas.max_fee + (original_gas.max_fee / 5);
                        let bumped_max_priority_fee =
                            original_gas.max_priority_fee + (original_gas.max_priority_fee / 5);

                        let gas_price = GasPriceResult {
                            max_fee: bumped_max_fee,
                            max_priority_fee: bumped_max_priority_fee,
                            min_wait_time_estimate: None,
                            max_wait_time_estimate: None,
                        };

                        let blob_gas_price = if replace_transaction.is_blob_transaction() {
                            Some(
                                transactions_queue
                                    .compute_blob_gas_price_for_transaction(
                                        &TransactionSpeed::SUPER,
                                        &None,
                                    )
                                    .await
                                    .map_err(|e| {
                                        ReplaceTransactionError::SendTransactionError(e.into())
                                    })?,
                            )
                        } else {
                            None
                        };

                        // Apply gas prices to replace transaction
                        replace_transaction.sent_with_gas = Some(gas_price);
                        replace_transaction.sent_with_blob_gas = blob_gas_price;

                        let transaction_sent = match transactions_queue
                            .send_transaction(&mut self.db, &mut replace_transaction)
                            .await
                        {
                            Ok(tx_sent) => tx_sent,
                            Err(TransactionQueueSendTransactionError::TransactionSendError(
                                error,
                            )) => {
                                let error_msg = error.to_string().to_lowercase();
                                if error_msg.contains("nonce too low")
                                    || error_msg.contains("nonce is too low")
                                    || error_msg.contains("invalid nonce")
                                    || error_msg.contains("nonce has already been used")
                                    || error_msg.contains("already known")
                                {
                                    warn!("replace_transaction: nonce synchronization issue detected for relayer {}: {}", transaction.relayer_id, error);

                                    if let Err(sync_error) = self
                                        .recover_nonce_synchronization(
                                            &transaction.relayer_id,
                                            &mut transactions_queue,
                                        )
                                        .await
                                    {
                                        error!("Failed to recover nonce synchronization for relayer {}: {}", transaction.relayer_id, sync_error);
                                        return Err(ReplaceTransactionError::SendTransactionError(
                                            TransactionQueueSendTransactionError::TransactionSendError(error)
                                        ));
                                    }

                                    info!("Nonce synchronization recovered for relayer {}, replacement transaction will be retried", transaction.relayer_id);
                                    return Err(
                                        ReplaceTransactionError::NonceSynchronizationRecovered,
                                    );
                                }
                                return Err(ReplaceTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::TransactionSendError(
                                        error,
                                    ),
                                ));
                            }
                            Err(e) => return Err(ReplaceTransactionError::SendTransactionError(e)),
                        };

                        replace_transaction.status = TransactionStatus::INMEMPOOL;
                        replace_transaction.known_transaction_hash = Some(transaction_sent.hash);
                        replace_transaction.sent_at = Some(Utc::now());

                        transactions_queue
                            .add_competitor_to_inmempool_transaction(
                                &transaction.id,
                                replace_transaction.clone(),
                                CompetitionType::Replace,
                            )
                            .await
                            .map_err(ReplaceTransactionError::SendTransactionError)?;

                        self.db
                            .save_transaction(&transaction.relayer_id, &replace_transaction)
                            .await
                            .map_err(ReplaceTransactionError::CouldNotUpdateTransactionInDb)?;

                        transactions_queue
                            .add_competitor_to_inmempool_transaction(
                                &transaction.id,
                                replace_transaction.clone(),
                                CompetitionType::Replace,
                            )
                            .await
                            .map_err(ReplaceTransactionError::SendTransactionError)?;

                        result.transaction.cancelled_by_transaction_id =
                            Some(replace_transaction_id);
                        self.db
                            .transaction_update(&result.transaction)
                            .await
                            .map_err(ReplaceTransactionError::CouldNotUpdateTransactionInDb)?;

                        self.invalidate_transaction_cache(&transaction.id).await;

                        info!("replace_transaction: added competitive replace tx {} with hash {} and nonce {:?} to replace original tx {}", 
                              replace_transaction_id, transaction_sent.hash, replace_transaction.nonce, transaction.id);

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let original_transaction = result.transaction.clone();
                            let replace_transaction_clone = replace_transaction.clone();
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager
                                    .on_transaction_replaced(
                                        &replace_transaction_clone,
                                        &original_transaction,
                                    )
                                    .await;
                                webhook_manager
                                    .on_transaction_sent(&replace_transaction_clone)
                                    .await;
                            });
                        }

                        Ok(ReplaceTransactionResult::success(
                            replace_transaction_id,
                            transaction_sent.hash,
                        ))
                    }
                }
            } else {
                Ok(ReplaceTransactionResult::failed())
            }
        } else {
            Err(ReplaceTransactionError::TransactionNotFound(transaction.id))
        }
    }

    async fn recover_nonce_synchronization(
        &mut self,
        relayer_id: &RelayerId,
        transactions_queue: &mut TransactionsQueue,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        info!("Attempting nonce recovery for relayer {}", relayer_id);

        let current_onchain_nonce = transactions_queue
            .get_nonce()
            .await
            .map_err(|e| format!("Failed to get on-chain nonce: {}", e))?;

        let current_internal_nonce = transactions_queue.nonce_manager.get_current_nonce().await;

        warn!(
            "Nonce synchronization issue detected for relayer {}: on-chain nonce is {}, internal nonce is {}",
            relayer_id, current_onchain_nonce.into_inner(), current_internal_nonce.into_inner()
        );

        transactions_queue.nonce_manager.sync_with_onchain_nonce(current_onchain_nonce).await;

        let updated_nonce = transactions_queue.nonce_manager.get_current_nonce().await;
        info!(
            "Nonce recovery completed for relayer {}: updated internal nonce to {}",
            relayer_id,
            updated_nonce.into_inner()
        );

        Ok(())
    }

    pub async fn process_single_pending(
        &mut self,
        relayer_id: &RelayerId,
    ) -> Result<ProcessResult<ProcessPendingStatus>, ProcessPendingTransactionError> {
        if let Some(queue_arc) = self.get_transactions_queue(relayer_id) {
            let mut transactions_queue = queue_arc.lock().await;

            if transactions_queue.is_paused() {
                return Ok(ProcessResult::<ProcessPendingStatus>::other(
                    ProcessPendingStatus::RelayerPaused,
                    Some(&30000), // relayer paused we will wait 30 seconds to get new stuff
                ));
            }

            if let Some(mut transaction) = transactions_queue.get_next_pending_transaction().await {
                let _guard = enter_critical_operation().ok_or_else(|| {
                    info!(
                        "process_single_pending: refusing to start during shutdown for relayer {}",
                        relayer_id
                    );
                    ProcessPendingTransactionError::RelayerTransactionsQueueNotFound(*relayer_id)
                })?;
                if self.has_expired(&transaction) {
                    self.transaction_to_noop(&mut transactions_queue, &mut transaction);
                }

                match transactions_queue.send_transaction(&mut self.db, &mut transaction).await {
                    Ok(transaction_sent) => {
                        transactions_queue.move_pending_to_inmempool(&transaction_sent).await?;

                        self.invalidate_transaction_cache(&transaction.id).await;

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let sent_transaction = Transaction {
                                status: TransactionStatus::INMEMPOOL,
                                known_transaction_hash: Some(transaction_sent.hash),
                                sent_at: Some(Utc::now()),
                                ..transaction
                            };
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager.on_transaction_sent(&sent_transaction).await;
                            });
                        }
                    }
                    Err(e) => {
                        return match e {
                            TransactionQueueSendTransactionError::GasPriceTooHigh => {
                                Ok(ProcessResult::<ProcessPendingStatus>::other(
                                    ProcessPendingStatus::GasPriceTooHigh,
                                    self.relayer_block_times_ms.get(relayer_id), // gas to high check back on the next block - do not do the / 10 part
                                ))
                            }
                            TransactionQueueSendTransactionError::GasCalculationError => {
                                Err(ProcessPendingTransactionError::GasCalculationError(
                                    *relayer_id,
                                    transaction.clone(),
                                ))
                            }
                            TransactionQueueSendTransactionError::TransactionEstimateGasError(
                                error,
                            ) => {
                                self.db
                                    .update_transaction_failed(&transaction.id, &error.to_string())
                                    .await
                                    .map_err(ProcessPendingTransactionError::DbError)?;

                                transactions_queue.move_next_pending_to_failed().await;

                                self.invalidate_transaction_cache(&transaction.id).await;

                                Err(ProcessPendingTransactionError::TransactionEstimateGasError(
                                    error,
                                ))
                            }
                            TransactionQueueSendTransactionError::TransactionSendError(error) => {
                                // Check if this is an insufficient funds error - auto-fail these
                                let error_msg = error.to_string().to_lowercase();
                                if error_msg.contains("insufficient funds")
                                    || error_msg.contains("balance")
                                    || error_msg.contains("overshot")
                                {
                                    info!("process_single_pending: transaction {} failed due to insufficient funds moved to failed", transaction.id);
                                    self.db
                                        .update_transaction_failed(
                                            &transaction.id,
                                            &error.to_string(),
                                        )
                                        .await
                                        .map_err(ProcessPendingTransactionError::DbError)?;

                                    transactions_queue.move_next_pending_to_failed().await;

                                    self.invalidate_transaction_cache(&transaction.id).await;

                                    Err(ProcessPendingTransactionError::SendTransactionError(
                                        TransactionQueueSendTransactionError::TransactionSendError(
                                            error,
                                        ),
                                    ))
                                } else if error_msg.contains("nonce too low")
                                    || error_msg.contains("nonce is too low")
                                    || error_msg.contains("invalid nonce")
                                    || error_msg.contains("nonce has already been used")
                                    || error_msg.contains("already known")
                                {
                                    warn!("process_single_pending: nonce synchronization issue detected for relayer {}: {}", relayer_id, error);

                                    if let Err(sync_error) = self
                                        .recover_nonce_synchronization(
                                            relayer_id,
                                            &mut transactions_queue,
                                        )
                                        .await
                                    {
                                        error!("Failed to recover nonce synchronization for relayer {}: {}", relayer_id, sync_error);
                                        return Err(ProcessPendingTransactionError::SendTransactionError(
                                            TransactionQueueSendTransactionError::TransactionSendError(error),
                                        ));
                                    }

                                    let new_nonce =
                                        transactions_queue.nonce_manager.get_and_increment().await;
                                    transaction.nonce = new_nonce;

                                    transactions_queue
                                        .update_pending_transaction_nonce(
                                            &transaction.id,
                                            new_nonce,
                                        )
                                        .await;

                                    if let Err(db_error) = self
                                        .db
                                        .transaction_update_nonce(&transaction.id, &new_nonce)
                                        .await
                                    {
                                        error!("Failed to persist nonce update to database for transaction {}: {}", transaction.id, db_error);
                                    }

                                    info!("Nonce synchronization recovered for relayer {}, updated pending transaction nonce to {} in queue and database", relayer_id, new_nonce.into_inner());

                                    Ok(ProcessResult::<ProcessPendingStatus>::other(
                                        ProcessPendingStatus::NonceSynchronized,
                                        Some(&100),
                                    ))
                                } else {
                                    // For other send errors (RPC down, etc), keep as temp issue
                                    Err(ProcessPendingTransactionError::SendTransactionError(
                                        TransactionQueueSendTransactionError::TransactionSendError(
                                            error,
                                        ),
                                    ))
                                }
                            }
                            TransactionQueueSendTransactionError::CouldNotUpdateTransactionDb(
                                error,
                            ) => {
                                // just keep the transaction in pending state as could be a bad
                                // db connection or temp
                                // outage
                                Err(ProcessPendingTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::CouldNotUpdateTransactionDb(error),
                                ))
                            }
                            TransactionQueueSendTransactionError::SendTransactionGasPriceError(
                                error,
                            ) => {
                                // should never happen if it does something internal is wrong,
                                // and we don't want to
                                // continue processing the queue
                                // it can stay in a loop forever, so we don't fail pending
                                // transactions
                                Err(ProcessPendingTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::SendTransactionGasPriceError(error),
                                ))
                            }
                            TransactionQueueSendTransactionError::TransactionConversionError(
                                error,
                            ) => {
                                self.db
                                    .update_transaction_failed(&transaction.id, &error)
                                    .await
                                    .map_err(ProcessPendingTransactionError::DbError)?;

                                transactions_queue.move_next_pending_to_failed().await;

                                self.invalidate_transaction_cache(&transaction.id).await;

                                Err(ProcessPendingTransactionError::TransactionEstimateGasError(
                                    RpcError::Transport(TransportErrorKind::Custom(error.into())),
                                ))
                            }
                            TransactionQueueSendTransactionError::SafeProxyError(error) => {
                                self.db
                                    .update_transaction_failed(&transaction.id, &error.to_string())
                                    .await
                                    .map_err(ProcessPendingTransactionError::DbError)?;

                                transactions_queue.move_next_pending_to_failed().await;

                                self.invalidate_transaction_cache(&transaction.id).await;

                                Err(ProcessPendingTransactionError::TransactionEstimateGasError(
                                    RpcError::Transport(TransportErrorKind::Custom(error.into())),
                                ))
                            }
                            TransactionQueueSendTransactionError::NoTransactionInQueue => {
                                // This shouldn't happen in normal flow, but if it does, keep the transaction pending
                                Err(ProcessPendingTransactionError::SendTransactionError(
                                    TransactionQueueSendTransactionError::NoTransactionInQueue,
                                ))
                            }
                        };
                    }
                }

                Ok(ProcessResult::<ProcessPendingStatus>::success())
            } else {
                Ok(ProcessResult::<ProcessPendingStatus>::other(
                    ProcessPendingStatus::NoPendingTransactions,
                    Default::default(),
                ))
            }
        } else {
            Err(ProcessPendingTransactionError::RelayerTransactionsQueueNotFound(*relayer_id))
        }
    }

    /// Processes a single in-mempool transaction for the specified relayer.
    pub async fn process_single_inmempool(
        &mut self,
        relayer_id: &RelayerId,
    ) -> Result<ProcessResult<ProcessInmempoolStatus>, ProcessInmempoolTransactionError> {
        if let Some(queue_arc) = self.get_transactions_queue(relayer_id) {
            let mut transactions_queue = queue_arc.lock().await;

            if let Some(mut transaction) = transactions_queue.get_next_inmempool_transaction().await
            {
                let _guard = enter_critical_operation().ok_or_else(|| {
                    info!(
                        "process_single_inmempool: refusing to start during shutdown for relayer {}",
                        relayer_id
                    );
                    ProcessInmempoolTransactionError::RelayerTransactionsQueueNotFound(*relayer_id)
                })?;
                if let Some(known_transaction_hash) = transaction.known_transaction_hash {
                    match transactions_queue.get_receipt(&known_transaction_hash).await {
                        Ok(Some(receipt)) => {
                            let competition_result = transactions_queue
                                .move_inmempool_to_mining(&transaction.id, &receipt)
                                .await.map_err(ProcessInmempoolTransactionError::MoveInmempoolTransactionToMinedError)?;

                            // Save the winning transaction to database
                            match competition_result.winner_status {
                                TransactionStatus::MINED => {
                                    self.db
                                        .transaction_mined(&competition_result.winner, &receipt)
                                        .await.map_err(|e| ProcessInmempoolTransactionError::CouldNotUpdateTransactionStatusInTheDatabase(*relayer_id, competition_result.winner.clone(), TransactionStatus::MINED, e))?;
                                    self.invalidate_transaction_cache(
                                        &competition_result.winner.id,
                                    )
                                    .await;

                                    // Save the loser transaction to database if there was competition
                                    if let Some(loser) = &competition_result.loser {
                                        self.db
                                            .transaction_update(loser)
                                            .await.map_err(|e| ProcessInmempoolTransactionError::CouldNotUpdateTransactionStatusInTheDatabase(*relayer_id, loser.clone(), loser.status, e))?;
                                        self.invalidate_transaction_cache(&loser.id).await;

                                        info!("Updated loser transaction {} with status {:?} in database", loser.id, loser.status);
                                    }

                                    if let Some(webhook_manager) = &self.webhook_manager {
                                        let webhook_manager = webhook_manager.clone();
                                        let mined_transaction = competition_result.winner.clone();
                                        let receipt_clone = receipt.clone();
                                        tokio::spawn(async move {
                                            let webhook_manager = webhook_manager.lock().await;
                                            webhook_manager
                                                .on_transaction_mined(
                                                    &mined_transaction,
                                                    &receipt_clone,
                                                )
                                                .await;
                                        });
                                    }
                                }
                                TransactionStatus::EXPIRED => {
                                    self.db.transaction_expired(&competition_result.winner.id).await.map_err(|e| ProcessInmempoolTransactionError::CouldNotUpdateTransactionStatusInTheDatabase(*relayer_id, competition_result.winner.clone(), TransactionStatus::EXPIRED, e))?;
                                    self.invalidate_transaction_cache(
                                        &competition_result.winner.id,
                                    )
                                    .await;

                                    if let Some(webhook_manager) = &self.webhook_manager {
                                        let webhook_manager = webhook_manager.clone();
                                        let expired_transaction = competition_result.winner.clone();
                                        tokio::spawn(async move {
                                            let webhook_manager = webhook_manager.lock().await;
                                            webhook_manager
                                                .on_transaction_expired(&expired_transaction)
                                                .await;
                                        });
                                    }
                                }
                                TransactionStatus::FAILED => {
                                    self.db
                                        .update_transaction_failed(&competition_result.winner.id, "Failed onchain")
                                        .await.map_err(|e| ProcessInmempoolTransactionError::CouldNotUpdateTransactionStatusInTheDatabase(*relayer_id, competition_result.winner.clone(), TransactionStatus::FAILED, e))?;
                                    self.invalidate_transaction_cache(
                                        &competition_result.winner.id,
                                    )
                                    .await;

                                    if let Some(webhook_manager) = &self.webhook_manager {
                                        let webhook_manager = webhook_manager.clone();
                                        let failed_transaction = competition_result.winner.clone();
                                        tokio::spawn(async move {
                                            let webhook_manager = webhook_manager.lock().await;
                                            webhook_manager
                                                .on_transaction_failed(&failed_transaction)
                                                .await;
                                        });
                                    }
                                }
                                _ => {}
                            }

                            Ok(ProcessResult::<ProcessInmempoolStatus>::success())
                        }
                        Ok(None) => {
                            if let Some(sent_at) = transaction.sent_at {
                                let elapsed = Utc::now() - sent_at;

                                let at_max_gas_cap =
                                    if let Some(ref sent_gas) = transaction.sent_with_gas {
                                        transactions_queue.is_at_max_gas_price_cap(sent_gas).await
                                    } else {
                                        false
                                    };

                                let at_max_blob_gas_cap = if let Some(ref sent_blob_gas) =
                                    transaction.sent_with_blob_gas
                                {
                                    transactions_queue
                                        .is_at_max_blob_gas_price_cap(sent_blob_gas)
                                        .await
                                } else {
                                    false
                                };

                                if at_max_gas_cap || at_max_blob_gas_cap {
                                    info!(
                                        "Transaction {} has reached maximum gas price cap (gas: {}, blob: {}), skipping gas bump for relayer: {}",
                                        transaction.id, at_max_gas_cap, at_max_blob_gas_cap, transactions_queue.relayer_name()
                                    );
                                    return Ok(ProcessResult::<ProcessInmempoolStatus>::other(
                                        ProcessInmempoolStatus::StillInmempool,
                                        self.relayer_block_times_ms
                                            .get(relayer_id)
                                            .map(|&block_time| block_time / 10)
                                            .as_ref(),
                                    ));
                                }

                                if transactions_queue.should_bump_gas(
                                    elapsed.num_milliseconds() as u64,
                                    &transaction.speed,
                                ) {
                                    let transaction_sent = match transactions_queue
                                        .send_transaction(&mut self.db, &mut transaction)
                                        .await
                                    {
                                        Ok(tx_sent) => tx_sent,
                                        Err(TransactionQueueSendTransactionError::TransactionSendError(error)) => {
                                            let error_msg = error.to_string().to_lowercase();
                                            if error_msg.contains("nonce too low") 
                                                || error_msg.contains("nonce is too low")
                                                || error_msg.contains("invalid nonce")
                                                || error_msg.contains("nonce has already been used")
                                                || error_msg.contains("already known")
                                            {
                                                warn!("process_single_inmempool: nonce synchronization issue detected for relayer {} during gas bump: {}", relayer_id, error);

                                                if let Err(sync_error) = self.recover_nonce_synchronization(relayer_id, &mut transactions_queue).await {
                                                    error!("Failed to recover nonce synchronization for relayer {}: {}", relayer_id, sync_error);
                                                    return Err(ProcessInmempoolTransactionError::SendTransactionError(
                                                        TransactionQueueSendTransactionError::TransactionSendError(error)
                                                    ));
                                                }

                                                let new_nonce = transactions_queue.nonce_manager.get_and_increment().await;
                                                transaction.nonce = new_nonce;

                                                transactions_queue.update_inmempool_transaction_nonce(&transaction.id, new_nonce).await;

                                                if let Err(db_error) = self.db.transaction_update_nonce(&transaction.id, &new_nonce).await {
                                                    error!("Failed to persist nonce update to database for transaction {}: {}", transaction.id, db_error);
                                                }

                                                info!("Nonce synchronization recovered for relayer {}, updated gas bump transaction nonce {} in queue and database", relayer_id, new_nonce.into_inner());

                                                return Ok(ProcessResult::<ProcessInmempoolStatus>::other(
                                                    ProcessInmempoolStatus::NonceSynchronized,
                                                    Some(&100),
                                                ));
                                            }
                                            return Err(ProcessInmempoolTransactionError::SendTransactionError(
                                                TransactionQueueSendTransactionError::TransactionSendError(error)
                                            ));
                                        }
                                        Err(e) => return Err(ProcessInmempoolTransactionError::SendTransactionError(e)),
                                    };

                                    // Update the actual transaction in the inmempool queue
                                    transactions_queue
                                        .update_inmempool_transaction_gas(&transaction_sent)
                                        .await;

                                    // Update the local transaction with the new gas values so subsequent bumps work correctly
                                    transaction.known_transaction_hash =
                                        Some(transaction_sent.hash);
                                    transaction.sent_with_max_fee_per_gas =
                                        Some(transaction_sent.sent_with_gas.max_fee);
                                    transaction.sent_with_max_priority_fee_per_gas =
                                        Some(transaction_sent.sent_with_gas.max_priority_fee);
                                    transaction.sent_with_gas =
                                        Some(transaction_sent.sent_with_gas.clone());
                                    transaction.sent_at = Some(Utc::now());

                                    self.invalidate_transaction_cache(&transaction.id).await;

                                    return Ok(ProcessResult::<ProcessInmempoolStatus>::other(
                                        ProcessInmempoolStatus::GasIncreased,
                                        Default::default(),
                                    ));
                                }
                            }

                            Ok(ProcessResult::<ProcessInmempoolStatus>::other(
                                ProcessInmempoolStatus::StillInmempool,
                                self.relayer_block_times_ms
                                    .get(relayer_id)
                                    .map(|&block_time| block_time / 10)
                                    .as_ref(),
                            ))
                        }
                        Err(e) => {
                            Err(ProcessInmempoolTransactionError::CouldNotGetTransactionReceipt(
                                *relayer_id,
                                transaction.clone(),
                                e,
                            ))
                        }
                    }
                } else {
                    Err(ProcessInmempoolTransactionError::UnknownTransactionHash(
                        *relayer_id,
                        transaction.clone(),
                    ))
                }
            } else {
                Ok(ProcessResult::<ProcessInmempoolStatus>::other(
                    ProcessInmempoolStatus::NoInmempoolTransactions,
                    Default::default(),
                ))
            }
        } else {
            Err(ProcessInmempoolTransactionError::RelayerTransactionsQueueNotFound(*relayer_id))
        }
    }

    /// Processes a single mined transaction for the specified relayer.
    pub async fn process_single_mined(
        &mut self,
        relayer_id: &RelayerId,
    ) -> Result<ProcessResult<ProcessMinedStatus>, ProcessMinedTransactionError> {
        if let Some(queue_arc) = self.get_transactions_queue(relayer_id) {
            let mut transactions_queue = queue_arc.lock().await;

            if let Some(transaction) = transactions_queue.get_next_mined_transaction().await {
                let _guard = enter_critical_operation().ok_or_else(|| {
                    info!(
                        "process_single_mined: refusing to start during shutdown for relayer {}",
                        relayer_id
                    );
                    ProcessMinedTransactionError::RelayerTransactionsQueueNotFound(*relayer_id)
                })?;

                if let Some(mined_at) = transaction.mined_at {
                    let elapsed = Utc::now() - mined_at;
                    if transactions_queue.in_confirmed_range(elapsed.num_milliseconds() as u64) {
                        let receipt = if let Some(tx_hash) = transaction.known_transaction_hash {
                            transactions_queue
                                .get_receipt(&tx_hash)
                                .await
                                .map_err(|e| {
                                    ProcessMinedTransactionError::CouldNotGetTransactionReceipt(
                                        *relayer_id,
                                        transaction.clone(),
                                        e,
                                    )
                                })?
                                .ok_or(
                                    ProcessMinedTransactionError::CouldNotGetTransactionReceipt(
                                        *relayer_id,
                                        transaction.clone(),
                                        RpcError::Transport(TransportErrorKind::Custom(
                                            "No receipt".to_string().into(),
                                        )),
                                    ),
                                )?
                        } else {
                            return Err(
                                ProcessMinedTransactionError::CouldNotGetTransactionReceipt(
                                    *relayer_id,
                                    transaction.clone(),
                                    RpcError::Transport(TransportErrorKind::Custom(
                                        "Transaction hash not found".to_string().into(),
                                    )),
                                ),
                            );
                        };

                        self.db.transaction_confirmed(&transaction.id).await.map_err(|e| {
                            ProcessMinedTransactionError::TransactionConfirmedNotSaveToDatabase(
                                *relayer_id,
                                transaction.clone(),
                                e,
                            )
                        })?;

                        transactions_queue.move_mining_to_confirmed(&transaction.id).await;

                        self.invalidate_transaction_cache(&transaction.id).await;

                        if let Some(webhook_manager) = &self.webhook_manager {
                            let webhook_manager = webhook_manager.clone();
                            let confirmed_transaction = Transaction {
                                status: TransactionStatus::CONFIRMED,
                                confirmed_at: Some(Utc::now()),
                                ..transaction
                            };
                            let receipt_clone = receipt.clone();
                            tokio::spawn(async move {
                                let webhook_manager = webhook_manager.lock().await;
                                webhook_manager
                                    .on_transaction_confirmed(
                                        &confirmed_transaction,
                                        &receipt_clone,
                                    )
                                    .await;
                            });
                        }

                        return Ok(ProcessResult::<ProcessMinedStatus>::success());
                    }

                    Ok(ProcessResult::<ProcessMinedStatus>::other(
                        ProcessMinedStatus::NotConfirmedYet,
                        Default::default(),
                    ))
                } else {
                    Err(ProcessMinedTransactionError::NoMinedAt(*relayer_id, transaction.clone()))
                }
            } else {
                Ok(ProcessResult::<ProcessMinedStatus>::other(
                    ProcessMinedStatus::NoMinedTransactions,
                    Default::default(),
                ))
            }
        } else {
            Err(ProcessMinedTransactionError::RelayerTransactionsQueueNotFound(*relayer_id))
        }
    }
}