openzeppelin_relayer/domain/transaction/evm/
price_calculator.rs

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
//! Gas price calculation module for Ethereum transactions.
//!
//! This module provides functionality for calculating gas prices for different types of Ethereum transactions:
//! - Legacy transactions (using `gas_price`)
//! - EIP1559 transactions (using `max_fee_per_gas` and `max_priority_fee_per_gas`)
//! - Speed-based transactions (automatically choosing between legacy and EIP1559 based on network support)
//!
//! The module implements various pricing strategies and safety mechanisms:
//! - Gas price caps to protect against excessive fees
//! - Dynamic base fee calculations for EIP1559 transactions
//! - Speed-based multipliers for different transaction priorities (SafeLow, Average, Fast, Fastest)
//! - Network-specific block time considerations for fee estimations
//!
//! # Example
//! ```no_run
//! # use your_crate::{PriceCalculator, EvmTransactionData, RelayerRepoModel, EvmGasPriceService};
//! # async fn example<P: EvmProviderTrait>(
//! #     tx_data: &EvmTransactionData,
//! #     relayer: &RelayerRepoModel,
//! #     gas_price_service: &EvmGasPriceService<P>,
//! #     provider: &P
//! # ) -> Result<(), TransactionError> {
//! let price_params = PriceCalculator::get_transaction_price_params(
//!     tx_data,
//!     relayer,
//!     gas_price_service,
//!     provider
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! The module uses EIP1559-specific constants for calculating appropriate gas fees:
//! - Base fee increase factor: 12.5% per block
//! - Maximum base fee multiplier: 10x
//! - Time window for fee calculation: 90 seconds
use crate::{
    constants::DEFAULT_TRANSACTION_SPEED,
    models::{
        evm::Speed, EvmNetwork, EvmTransactionData, EvmTransactionDataTrait, RelayerRepoModel,
        TransactionError, TransactionRepoModel,
    },
    services::{
        gas::{EvmGasPriceServiceTrait, NetworkExtraFeeCalculatorServiceTrait},
        GasPrices, NetworkExtraFeeCalculator,
    },
};

#[cfg(test)]
use mockall::automock;

#[cfg(test)]
use crate::services::gas::MockNetworkExtraFeeCalculatorServiceTrait;

#[async_trait::async_trait]
#[cfg_attr(test, automock)]
pub trait PriceCalculatorTrait: Send + Sync {
    async fn get_transaction_price_params(
        &self,
        tx_data: &EvmTransactionData,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError>;

    async fn calculate_bumped_gas_price(
        &self,
        tx: &TransactionRepoModel,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError>;
}

type GasPriceCapResult = (Option<u128>, Option<u128>, Option<u128>);

const PRECISION: u128 = 1_000_000_000; // 10^9 (similar to Gwei)
const MINUTE_AND_HALF_MS: u128 = 90000;
const BASE_FEE_INCREASE_FACTOR_PERCENT: u128 = 125; // 12.5% increase per block (as percentage * 10)
const MAX_BASE_FEE_MULTIPLIER: u128 = 10 * PRECISION; // 10.0 * PRECISION
const MIN_BUMP_PERCENT: u128 = 10;

#[derive(Debug, Clone)]
pub struct PriceParams {
    pub gas_price: Option<u128>,
    pub max_fee_per_gas: Option<u128>,
    pub max_priority_fee_per_gas: Option<u128>,
    pub is_min_bumped: Option<bool>,
    pub extra_fee: Option<u128>,
}

/// Primary struct for calculating gas prices with an injected `EvmGasPriceServiceTrait`.
pub struct PriceCalculator<G: EvmGasPriceServiceTrait> {
    gas_price_service: G,
    network_extra_fee_calculator_service: NetworkExtraFeeCalculator<G::Provider>,
}

#[async_trait::async_trait]
impl<G: EvmGasPriceServiceTrait + Send + Sync> PriceCalculatorTrait for PriceCalculator<G> {
    async fn get_transaction_price_params(
        &self,
        tx_data: &EvmTransactionData,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        self.get_transaction_price_params(tx_data, relayer).await
    }

    async fn calculate_bumped_gas_price(
        &self,
        tx: &TransactionRepoModel,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        self.calculate_bumped_gas_price(tx, relayer).await
    }
}

impl<G: EvmGasPriceServiceTrait> PriceCalculator<G> {
    pub fn new(
        gas_price_service: G,
        network_extra_fee_calculator_service: NetworkExtraFeeCalculator<G::Provider>,
    ) -> Self {
        Self {
            gas_price_service,
            network_extra_fee_calculator_service,
        }
    }

    /// Calculates transaction price parameters based on the transaction type and network conditions.
    ///
    /// This function determines the appropriate gas pricing strategy based on the transaction type:
    /// - For legacy transactions: calculates gas_price
    /// - For EIP1559 transactions: calculates max_fee_per_gas and max_priority_fee_per_gas
    /// - For speed-based transactions: automatically chooses between legacy and EIP1559 based on network support
    ///
    /// # Arguments
    /// * `tx_data` - Transaction data containing type and pricing information
    /// * `relayer` - Relayer configuration including pricing policies and caps
    /// * `gas_price_service` - Service for fetching current gas prices from the network
    /// * `provider` - Network provider for accessing blockchain data
    ///
    /// # Returns
    /// * `Result<PriceParams, TransactionError>` - Calculated price parameters or error
    pub async fn get_transaction_price_params(
        &self,
        tx_data: &EvmTransactionData,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        let price_params = self
            .fetch_price_params_based_on_tx_type(tx_data, relayer)
            .await?;
        let (gas_price_capped, max_fee_per_gas_capped, max_priority_fee_per_gas_capped) = self
            .apply_gas_price_cap(
                price_params.gas_price.unwrap_or_default(),
                price_params.max_fee_per_gas,
                price_params.max_priority_fee_per_gas,
                relayer,
            )?;

        let mut final_params = PriceParams {
            gas_price: gas_price_capped,
            max_fee_per_gas: max_fee_per_gas_capped,
            max_priority_fee_per_gas: max_priority_fee_per_gas_capped,
            is_min_bumped: None,
            extra_fee: None,
        };

        match &self.network_extra_fee_calculator_service {
            NetworkExtraFeeCalculator::None => {}
            _ => {
                let new_tx = tx_data.clone().with_price_params(final_params.clone());
                let extra_fee = self
                    .network_extra_fee_calculator_service
                    .get_extra_fee(&new_tx)
                    .await?;
                final_params.extra_fee = Some(extra_fee.try_into().unwrap_or(0));
            }
        }

        Ok(final_params)
    }

    /// Computes bumped gas price for transaction resubmission, factoring in network conditions.
    ///
    /// This refactor breaks the logic into smaller helper functions for clarity and testability.
    /// Each helper is commented to show how the final gas parameters are derived.
    ///
    /// 1. Determine if the transaction is EIP1559 or Legacy.
    /// 2. Calculate minimum bump requirements (e.g., +10%).
    /// 3. Compare with current network prices to decide how much to bump.
    /// 4. Apply any relayer gas price caps.
    /// 5. Return the final bumped gas parameters.
    ///
    /// The returned PriceParams includes an is_min_bumped flag that indicates whether
    /// the calculated gas parameters meet the minimum bump requirements.
    pub async fn calculate_bumped_gas_price(
        &self,
        tx: &TransactionRepoModel,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        let evm_data = tx.network_data.get_evm_transaction_data()?;
        let network_gas_prices = self.gas_price_service.get_prices_from_json_rpc().await?;
        let relayer_gas_price_cap = relayer
            .policies
            .get_evm_policy()
            .gas_price_cap
            .unwrap_or(u128::MAX);

        // Decide EIP1559 vs Legacy based on presence of maxFeePerGas / maxPriorityFeePerGas vs gasPrice
        let bumped_price_params = match (
            evm_data.max_fee_per_gas,
            evm_data.max_priority_fee_per_gas,
            evm_data.gas_price,
        ) {
            (Some(max_fee), Some(max_priority_fee), _) => {
                // EIP1559
                self.handle_eip1559_bump(
                    &network_gas_prices,
                    relayer_gas_price_cap,
                    evm_data.speed.as_ref(),
                    max_fee,
                    max_priority_fee,
                )?
            }
            (None, None, Some(gas_price)) => {
                // Legacy
                self.handle_legacy_bump(
                    &network_gas_prices,
                    relayer_gas_price_cap,
                    evm_data.speed.as_ref(),
                    gas_price,
                )?
            }
            _ => {
                return Err(TransactionError::InvalidType(
                    "Transaction missing required gas price parameters".to_string(),
                ))
            }
        };

        // Add extra fee if needed
        let mut final_params = bumped_price_params;

        match &self.network_extra_fee_calculator_service {
            NetworkExtraFeeCalculator::None => {}
            _ => {
                let new_tx = evm_data.with_price_params(final_params.clone());
                let extra_fee = self
                    .network_extra_fee_calculator_service
                    .get_extra_fee(&new_tx)
                    .await?;
                final_params.extra_fee = Some(extra_fee.try_into().unwrap_or(0));
            }
        }

        Ok(final_params)
    }

    /// Computes the bumped gas parameters for an EIP-1559 transaction resubmission.
    ///
    /// The function performs the following steps:
    /// 1. Computes the minimum required fee values by increasing the previous fees by 10%.
    /// 2. Retrieves the current network market priority fee for the transaction's speed.
    /// 3. Chooses the new priority fee as either the current market fee (if it meets the 10% increase)
    ///    or the calculated minimum bump.
    /// 4. Computes the new maximum fee using two approaches:
    ///    - Method A: Uses the current base fee, ensuring it meets the minimum bumped max fee.
    ///    - Method B: Computes a recommended max fee based on a network-specific multiplier plus the new priority fee.
    ///      The higher value between these two methods is chosen.
    /// 5. Applies the relayer's gas price cap to both the new priority fee and the new max fee.
    /// 6. Returns the final capped gas parameters.
    ///
    /// Note: All fee values are expected to be in Wei.
    fn handle_eip1559_bump(
        &self,
        network_gas_prices: &GasPrices,
        gas_price_cap: u128,
        maybe_speed: Option<&Speed>,
        max_fee: u128,
        max_priority_fee: u128,
    ) -> Result<PriceParams, TransactionError> {
        let speed = maybe_speed.unwrap_or(&DEFAULT_TRANSACTION_SPEED);

        // Calculate the minimum required fees (10% increase over previous values)
        let min_bump_max_fee = Self::calculate_min_bump(max_fee);
        let min_bump_max_priority = Self::calculate_min_bump(max_priority_fee);

        // Get the current market priority fee for the given speed.
        let current_market_priority =
            Self::get_market_price_for_speed(network_gas_prices, true, speed);

        // Determine the new maxPriorityFeePerGas:
        // Use the current market fee if it is at least the minimum bumped fee,
        // otherwise use the minimum bumped priority fee.
        let bumped_priority_fee = if current_market_priority >= min_bump_max_priority {
            current_market_priority
        } else {
            min_bump_max_priority
        };

        // Compute the new maxFeePerGas using two methods:
        // Method A: Use the current base fee, but ensure it is not lower than the minimum bumped max fee.
        let base_fee_wei = network_gas_prices.base_fee_per_gas;
        let bumped_max_fee_per_gas = if base_fee_wei >= min_bump_max_fee {
            base_fee_wei
        } else {
            min_bump_max_fee
        };

        // Method B: Calculate a recommended max fee based on the base fee multiplied by a network factor,
        // plus the new priority fee.
        let recommended_max_fee_per_gas = calculate_max_fee_per_gas(
            base_fee_wei,
            bumped_priority_fee,
            self.gas_price_service.network(),
        );

        // Choose the higher value from the two methods to be competitive under current network conditions.
        let final_max_fee = std::cmp::max(bumped_max_fee_per_gas, recommended_max_fee_per_gas);

        // Step 5: Apply the gas price cap to both the new priority fee and the new max fee.
        let capped_priority = Self::cap_gas_price(bumped_priority_fee, gas_price_cap);
        let capped_max_fee = Self::cap_gas_price(final_max_fee, gas_price_cap);

        // Check if the capped values still meet the minimum bump requirements
        let is_min_bumped =
            capped_priority >= min_bump_max_priority && capped_max_fee >= min_bump_max_fee;

        // Step 6: Return the final bumped gas parameters.
        Ok(PriceParams {
            gas_price: None,
            max_priority_fee_per_gas: Some(capped_priority),
            max_fee_per_gas: Some(capped_max_fee),
            is_min_bumped: Some(is_min_bumped),
            extra_fee: None,
        })
    }

    /// Handle Legacy bump logic:
    /// 1) Calculate min bump for gasPrice.
    /// 2) Compare with current market price for the given speed.
    /// 3) Apply final caps.
    fn handle_legacy_bump(
        &self,
        network_gas_prices: &GasPrices,
        gas_price_cap: u128,
        maybe_speed: Option<&Speed>,
        gas_price: u128,
    ) -> Result<PriceParams, TransactionError> {
        let speed = maybe_speed.unwrap_or(&Speed::Fast);

        // Minimum bump
        let min_bump_gas_price = Self::calculate_min_bump(gas_price);

        // Current market gas price for chosen speed
        let current_market_price =
            Self::get_market_price_for_speed(network_gas_prices, false, speed);

        let bumped_gas_price = if current_market_price >= min_bump_gas_price {
            current_market_price
        } else {
            min_bump_gas_price
        };

        // Cap
        let capped_gas_price = Self::cap_gas_price(bumped_gas_price, gas_price_cap);

        // Check if the capped value still meets the minimum bump requirement
        let is_min_bumped = capped_gas_price >= min_bump_gas_price;

        Ok(PriceParams {
            gas_price: Some(capped_gas_price),
            max_priority_fee_per_gas: None,
            max_fee_per_gas: None,
            is_min_bumped: Some(is_min_bumped),
            extra_fee: None,
        })
    }
    /// Fetches price params based on the type of transaction (legacy, EIP1559, speed-based).
    async fn fetch_price_params_based_on_tx_type(
        &self,
        tx_data: &EvmTransactionData,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        if tx_data.is_legacy() {
            self.fetch_legacy_price_params(tx_data)
        } else if tx_data.is_eip1559() {
            self.fetch_eip1559_price_params(tx_data)
        } else if tx_data.is_speed() {
            self.fetch_speed_price_params(tx_data, relayer).await
        } else {
            Err(TransactionError::NotSupported(
                "Invalid transaction type".to_string(),
            ))
        }
    }

    /// Handles gas price calculation for legacy transactions.
    ///
    /// # Arguments
    /// * `tx_data` - Transaction data containing the gas price
    ///
    /// # Returns
    /// * `Result<PriceParams, TransactionError>` - Price parameters for legacy transaction
    fn fetch_legacy_price_params(
        &self,
        tx_data: &EvmTransactionData,
    ) -> Result<PriceParams, TransactionError> {
        let gas_price = tx_data.gas_price.ok_or(TransactionError::NotSupported(
            "Gas price is required for legacy transactions".to_string(),
        ))?;
        Ok(PriceParams {
            gas_price: Some(gas_price),
            max_fee_per_gas: None,
            max_priority_fee_per_gas: None,
            is_min_bumped: None,
            extra_fee: None,
        })
    }

    fn fetch_eip1559_price_params(
        &self,
        tx_data: &EvmTransactionData,
    ) -> Result<PriceParams, TransactionError> {
        let max_fee = tx_data
            .max_fee_per_gas
            .ok_or(TransactionError::NotSupported(
                "Max fee per gas is required for EIP1559 transactions".to_string(),
            ))?;
        let max_priority_fee =
            tx_data
                .max_priority_fee_per_gas
                .ok_or(TransactionError::NotSupported(
                    "Max priority fee per gas is required for EIP1559 transactions".to_string(),
                ))?;
        Ok(PriceParams {
            gas_price: None,
            max_fee_per_gas: Some(max_fee),
            max_priority_fee_per_gas: Some(max_priority_fee),
            is_min_bumped: None,
            extra_fee: None,
        })
    }
    /// Handles gas price calculation for speed-based transactions.
    ///
    /// Determines whether to use legacy or EIP1559 pricing based on network configuration
    /// and calculates appropriate gas prices based on the requested speed.
    async fn fetch_speed_price_params(
        &self,
        tx_data: &EvmTransactionData,
        relayer: &RelayerRepoModel,
    ) -> Result<PriceParams, TransactionError> {
        let speed = tx_data
            .speed
            .as_ref()
            .ok_or(TransactionError::NotSupported(
                "Speed is required".to_string(),
            ))?;
        let use_legacy = relayer.policies.get_evm_policy().eip1559_pricing == Some(false)
            || self.gas_price_service.network().is_legacy();

        if use_legacy {
            self.fetch_legacy_speed_params(speed).await
        } else {
            self.fetch_eip1559_speed_params(speed).await
        }
    }

    /// Calculates EIP1559 gas prices based on the requested speed.
    ///
    /// Uses the gas price service to fetch current network conditions and calculates
    /// appropriate max fee and priority fee based on the speed setting.
    async fn fetch_eip1559_speed_params(
        &self,
        speed: &Speed,
    ) -> Result<PriceParams, TransactionError> {
        let prices = self.gas_price_service.get_prices_from_json_rpc().await?;
        let priority_fee = match speed {
            Speed::SafeLow => prices.max_priority_fee_per_gas.safe_low,
            Speed::Average => prices.max_priority_fee_per_gas.average,
            Speed::Fast => prices.max_priority_fee_per_gas.fast,
            Speed::Fastest => prices.max_priority_fee_per_gas.fastest,
        };
        let max_fee = calculate_max_fee_per_gas(
            prices.base_fee_per_gas,
            priority_fee,
            self.gas_price_service.network(),
        );
        Ok(PriceParams {
            gas_price: None,
            max_fee_per_gas: Some(max_fee),
            max_priority_fee_per_gas: Some(priority_fee),
            is_min_bumped: None,
            extra_fee: None,
        })
    }
    /// Calculates legacy gas prices based on the requested speed.
    ///
    /// Uses the gas price service to fetch current gas prices and applies
    /// speed-based multipliers for legacy transactions.
    async fn fetch_legacy_speed_params(
        &self,
        speed: &Speed,
    ) -> Result<PriceParams, TransactionError> {
        let prices = self
            .gas_price_service
            .get_legacy_prices_from_json_rpc()
            .await?;
        let gas_price = match speed {
            Speed::SafeLow => prices.safe_low,
            Speed::Average => prices.average,
            Speed::Fast => prices.fast,
            Speed::Fastest => prices.fastest,
        };
        Ok(PriceParams {
            gas_price: Some(gas_price),
            max_fee_per_gas: None,
            max_priority_fee_per_gas: None,
            is_min_bumped: None,
            extra_fee: None,
        })
    }

    /// Applies gas price caps to the calculated prices.
    ///
    /// Ensures that gas prices don't exceed the configured maximum limits and
    /// maintains proper relationships between different price parameters.
    fn apply_gas_price_cap(
        &self,
        gas_price: u128,
        max_fee_per_gas: Option<u128>,
        max_priority_fee_per_gas: Option<u128>,
        relayer: &RelayerRepoModel,
    ) -> Result<GasPriceCapResult, TransactionError> {
        let gas_price_cap = relayer
            .policies
            .get_evm_policy()
            .gas_price_cap
            .unwrap_or(u128::MAX);

        if let (Some(max_fee), Some(max_priority)) = (max_fee_per_gas, max_priority_fee_per_gas) {
            // Cap the maxFeePerGas
            let capped_max_fee = Self::cap_gas_price(max_fee, gas_price_cap);

            // Ensure maxPriorityFeePerGas < maxFeePerGas to avoid client errors
            let capped_max_priority = Self::cap_gas_price(max_priority, capped_max_fee);
            Ok((None, Some(capped_max_fee), Some(capped_max_priority)))
        } else {
            // Handle legacy transaction
            Ok((
                Some(Self::cap_gas_price(gas_price, gas_price_cap)),
                None,
                None,
            ))
        }
    }

    fn calculate_min_bump(previous_price: u128) -> u128 {
        (previous_price * (100 + MIN_BUMP_PERCENT)) / 100
    }

    fn cap_gas_price(price: u128, cap: u128) -> u128 {
        std::cmp::min(price, cap)
    }

    /// Returns the market price for the given speed. If `is_eip1559` is true, use `max_priority_fee_per_gas`,
    /// otherwise use `legacy_prices`.
    fn get_market_price_for_speed(prices: &GasPrices, is_eip1559: bool, speed: &Speed) -> u128 {
        if is_eip1559 {
            match speed {
                Speed::SafeLow => prices.max_priority_fee_per_gas.safe_low,
                Speed::Average => prices.max_priority_fee_per_gas.average,
                Speed::Fast => prices.max_priority_fee_per_gas.fast,
                Speed::Fastest => prices.max_priority_fee_per_gas.fastest,
            }
        } else {
            match speed {
                Speed::SafeLow => prices.legacy_prices.safe_low,
                Speed::Average => prices.legacy_prices.average,
                Speed::Fast => prices.legacy_prices.fast,
                Speed::Fastest => prices.legacy_prices.fastest,
            }
        }
    }
}

fn get_base_fee_multiplier(network: &EvmNetwork) -> u128 {
    let block_interval_ms = network.average_blocktime().map(|d| d.as_millis()).unwrap();

    // Calculate number of blocks (as integer)
    let n_blocks_int = MINUTE_AND_HALF_MS / block_interval_ms;

    // Calculate number of blocks (fractional part in thousandths)
    let n_blocks_frac = ((MINUTE_AND_HALF_MS % block_interval_ms) * 1000) / block_interval_ms;

    // Calculate multiplier using compound interest formula: (1 + r)^n
    // For integer part: (1 + 0.125)^n_blocks_int
    let mut multiplier = PRECISION;

    // Calculate (1.125)^n_blocks_int using repeated multiplication
    for _ in 0..n_blocks_int {
        multiplier = (multiplier
            * (PRECISION + (PRECISION * BASE_FEE_INCREASE_FACTOR_PERCENT) / 1000))
            / PRECISION;
    }

    // Handle fractional part with linear approximation
    // For fractional part: approximately 1 + (fraction * 0.125)
    if n_blocks_frac > 0 {
        let frac_increase =
            (n_blocks_frac * BASE_FEE_INCREASE_FACTOR_PERCENT * PRECISION) / (1000 * 1000);
        multiplier = (multiplier * (PRECISION + frac_increase)) / PRECISION;
    }

    // Apply maximum cap
    std::cmp::min(multiplier, MAX_BASE_FEE_MULTIPLIER)
}

/// Calculate max fee per gas for EIP1559 transactions (all values in wei)
fn calculate_max_fee_per_gas(
    base_fee_wei: u128,
    max_priority_fee_wei: u128,
    network: &EvmNetwork,
) -> u128 {
    // Get multiplier in fixed-point format
    let multiplier = get_base_fee_multiplier(network);

    // Multiply base fee by multiplier (with proper scaling)
    let multiplied_base_fee = (base_fee_wei * multiplier) / PRECISION;

    // Add priority fee
    multiplied_base_fee + max_priority_fee_wei
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::NetworkTransactionData;
    use crate::models::{
        evm::Speed, EvmNamedNetwork, EvmNetwork, EvmTransactionData, NetworkType, RelayerEvmPolicy,
        RelayerNetworkPolicy, RelayerRepoModel, U256,
    };
    use crate::services::{
        EvmGasPriceService, GasPrices, MockEvmGasPriceServiceTrait, MockEvmProviderTrait,
        SpeedPrices,
    };
    use futures::FutureExt;

    fn create_mock_relayer() -> RelayerRepoModel {
        RelayerRepoModel {
            id: "test-relayer".to_string(),
            name: "Test Relayer".to_string(),
            network: "mainnet".to_string(),
            network_type: NetworkType::Evm,
            address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266".to_string(),
            policies: RelayerNetworkPolicy::Evm(RelayerEvmPolicy::default()),
            paused: false,
            notification_id: None,
            signer_id: "test-signer".to_string(),
            system_disabled: false,
            custom_rpc_urls: None,
        }
    }

    #[tokio::test]
    async fn test_legacy_transaction() {
        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let relayer = create_mock_relayer();
        let gas_price_service =
            EvmGasPriceService::new(provider, EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let tx_data = EvmTransactionData {
            gas_price: Some(20000000000),
            ..Default::default()
        };

        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        // Create the PriceCalculator with the gas_price_service
        let pc = PriceCalculator::new(gas_price_service, NetworkExtraFeeCalculator::None);

        let result = pc.get_transaction_price_params(&tx_data, &relayer).await;
        assert!(result.is_ok());
        let params = result.unwrap();
        assert_eq!(params.gas_price, Some(20000000000));
        assert!(params.max_fee_per_gas.is_none());
        assert!(params.max_priority_fee_per_gas.is_none());
    }

    #[tokio::test]
    async fn test_eip1559_transaction() {
        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let relayer = create_mock_relayer();
        let gas_price_service =
            EvmGasPriceService::new(provider, EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let tx_data = EvmTransactionData {
            gas_price: None,
            max_fee_per_gas: Some(30000000000),
            max_priority_fee_per_gas: Some(2000000000),
            ..Default::default()
        };

        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        // Create the PriceCalculator
        let pc = PriceCalculator::new(gas_price_service, NetworkExtraFeeCalculator::None);

        let result = pc.get_transaction_price_params(&tx_data, &relayer).await;
        assert!(result.is_ok());
        let params = result.unwrap();
        assert!(params.gas_price.is_none());
        assert_eq!(params.max_fee_per_gas, Some(30000000000));
        assert_eq!(params.max_priority_fee_per_gas, Some(2000000000));
    }

    #[tokio::test]
    async fn test_speed_legacy_based_transaction() {
        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());
        provider
            .expect_get_gas_price()
            .returning(|| async { Ok(20000000000) }.boxed());

        let relayer = create_mock_relayer();
        let gas_price_service =
            EvmGasPriceService::new(provider, EvmNetwork::from_named(EvmNamedNetwork::Celo));

        let tx_data = EvmTransactionData {
            gas_price: None,
            speed: Some(Speed::Fast),
            ..Default::default()
        };

        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());
        provider
            .expect_get_gas_price()
            .returning(|| async { Ok(20000000000) }.boxed());

        let pc = PriceCalculator::new(gas_price_service, NetworkExtraFeeCalculator::None);

        let result = pc.get_transaction_price_params(&tx_data, &relayer).await;
        assert!(result.is_ok());
        let params = result.unwrap();
        assert!(
            params.gas_price.is_some()
                || (params.max_fee_per_gas.is_some() && params.max_priority_fee_per_gas.is_some())
        );
    }

    #[tokio::test]
    async fn test_invalid_transaction_type() {
        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let relayer = create_mock_relayer();
        let gas_price_service =
            EvmGasPriceService::new(provider, EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let tx_data = EvmTransactionData {
            gas_price: None,
            ..Default::default()
        };

        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let pc = PriceCalculator::new(gas_price_service, NetworkExtraFeeCalculator::None);

        let result = pc.get_transaction_price_params(&tx_data, &relayer).await;
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            TransactionError::NotSupported(_)
        ));
    }

    #[tokio::test]
    async fn test_gas_price_cap() {
        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let mut relayer = create_mock_relayer();
        let gas_price_service =
            EvmGasPriceService::new(provider, EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        // Update policies with new EVM policy
        let evm_policy = RelayerEvmPolicy {
            gas_price_cap: Some(10000000000),
            eip1559_pricing: Some(true),
            ..RelayerEvmPolicy::default()
        };
        relayer.policies = RelayerNetworkPolicy::Evm(evm_policy);

        let tx_data = EvmTransactionData {
            gas_price: Some(20000000000), // Higher than cap
            ..Default::default()
        };

        let mut provider = MockEvmProviderTrait::new();
        provider
            .expect_get_balance()
            .returning(|_| async { Ok(U256::from(1000000000000000000u128)) }.boxed());

        let pc = PriceCalculator::new(gas_price_service, NetworkExtraFeeCalculator::None);

        let result = pc.get_transaction_price_params(&tx_data, &relayer).await;
        assert!(result.is_ok());
        let params = result.unwrap();
        assert_eq!(params.gas_price, Some(10000000000)); // Should be capped
    }

    #[test]
    fn test_get_base_fee_multiplier() {
        let mainnet = EvmNetwork::from_named(EvmNamedNetwork::Mainnet);
        let multiplier = super::get_base_fee_multiplier(&mainnet);
        // 90s with ~12s blocks = ~7.5 blocks => ~2.4 multiplier
        assert!(multiplier > 2_300_000_000 && multiplier < 2_500_000_000);

        let optimism = EvmNetwork::from_named(EvmNamedNetwork::Optimism);
        let multiplier = super::get_base_fee_multiplier(&optimism);
        // 2s block time => ~45 blocks => capped at 10.0
        assert_eq!(multiplier, MAX_BASE_FEE_MULTIPLIER);
    }

    #[test]
    fn test_calculate_max_fee_per_gas() {
        let network = EvmNetwork::from_named(EvmNamedNetwork::Mainnet);
        let base_fee = 100_000_000_000u128; // 100 Gwei
        let priority_fee = 2_000_000_000u128; // 2 Gwei

        let max_fee = super::calculate_max_fee_per_gas(base_fee, priority_fee, &network);
        println!("max_fee: {:?}", max_fee);
        // With mainnet's multiplier (~2.4):
        // base_fee * multiplier + priority_fee ≈ 100 * 2.4 + 2 ≈ 242 Gwei
        assert!(max_fee > 240_000_000_000 && max_fee < 245_000_000_000);
    }

    #[tokio::test]
    async fn test_handle_eip1559_speed() {
        let mut mock_gas_price_service = MockEvmGasPriceServiceTrait::new();

        // Mock the gas price service's get_prices_from_json_rpc method
        let test_data = [
            (Speed::SafeLow, 1_000_000_000),
            (Speed::Average, 2_000_000_000),
            (Speed::Fast, 3_000_000_000),
            (Speed::Fastest, 4_000_000_000),
        ];
        // Create mock prices
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices {
                safe_low: 10_000_000_000,
                average: 12_500_000_000,
                fast: 15_000_000_000,
                fastest: 20_000_000_000,
            },
            max_priority_fee_per_gas: SpeedPrices {
                safe_low: 1_000_000_000,
                average: 2_000_000_000,
                fast: 3_000_000_000,
                fastest: 4_000_000_000,
            },
            base_fee_per_gas: 50_000_000_000,
        };

        // Mock get_prices_from_json_rpc
        mock_gas_price_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });

        // Mock the network method
        let network = EvmNetwork::from_named(EvmNamedNetwork::Mainnet);
        mock_gas_price_service
            .expect_network()
            .return_const(network);

        // Construct our PriceCalculator with the mocked gas service
        let pc = PriceCalculator::new(mock_gas_price_service, NetworkExtraFeeCalculator::None);

        for (speed, expected_priority_fee) in test_data {
            // Call our internal fetch_eip1559_speed_params, which replaced handle_eip1559_speed
            let result = pc.fetch_eip1559_speed_params(&speed).await;
            assert!(result.is_ok());
            let params = result.unwrap();
            // Verify max_priority_fee matches expected value
            assert_eq!(params.max_priority_fee_per_gas, Some(expected_priority_fee));

            // Verify max_fee calculation
            // max_fee = base_fee * multiplier + priority_fee
            // ≈ (50 * 2.4 + priority_fee_in_gwei) Gwei
            let max_fee = params.max_fee_per_gas.unwrap();
            let expected_base_portion = 120_000_000_000; // ~50 Gwei * 2.4
            assert!(max_fee < expected_base_portion + expected_priority_fee + 2_000_000_000);
        }
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_eip1559_basic() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices {
                safe_low: 8_000_000_000,
                average: 10_000_000_000,
                fast: 12_000_000_000,
                fastest: 15_000_000_000,
            },
            max_priority_fee_per_gas: SpeedPrices {
                safe_low: 1_000_000_000,
                average: 2_000_000_000,
                fast: 3_000_000_000,
                fastest: 4_000_000_000,
            },
            base_fee_per_gas: 50_000_000_000,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let mut relayer = create_mock_relayer();
        // Example cap to demonstrate bump capping
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(300_000_000_000u128),
            ..Default::default()
        });

        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    max_fee_per_gas: Some(100_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert!(bumped.max_fee_per_gas.unwrap() >= 110_000_000_000); // >= 10% bump
        assert!(bumped.max_priority_fee_per_gas.unwrap() >= 2_200_000_000); // >= 10% bump
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_eip1559_market_lower_than_min_bump() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices::default(),
            max_priority_fee_per_gas: SpeedPrices {
                safe_low: 1_500_000_000, // market priority
                average: 2_500_000_000,
                fast: 2_700_000_000,
                fastest: 3_000_000_000,
            },
            base_fee_per_gas: 30_000_000_000,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let relayer = create_mock_relayer();

        // Old max_priority_fee: 2.0 Gwei, new market is 1.5 Gwei (less)
        // Should use min bump (2.2 Gwei) instead
        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    max_fee_per_gas: Some(20_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::SafeLow),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert!(bumped.max_priority_fee_per_gas.unwrap() >= 2_200_000_000);
        assert!(bumped.max_fee_per_gas.unwrap() > 20_000_000_000);
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_legacy_basic() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices {
                safe_low: 10_000_000_000,
                average: 12_000_000_000,
                fast: 14_000_000_000,
                fastest: 18_000_000_000,
            },
            max_priority_fee_per_gas: SpeedPrices::default(),
            base_fee_per_gas: 0,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let relayer = create_mock_relayer();
        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    gas_price: Some(10_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert!(bumped.gas_price.unwrap() >= 11_000_000_000); // at least 10% bump
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_missing_params() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();

        // Add the missing expectation for get_prices_from_json_rpc
        mock_service
            .expect_get_prices_from_json_rpc()
            .times(1)
            .returning(|| Box::pin(async { Ok(GasPrices::default()) }));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let relayer = create_mock_relayer();
        // Both max_fee_per_gas, max_priority_fee_per_gas, and gas_price absent
        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    gas_price: None,
                    max_fee_per_gas: None,
                    max_priority_fee_per_gas: None,
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let result = pc.calculate_bumped_gas_price(&tx, &relayer).await;
        assert!(result.is_err());
        if let Err(TransactionError::InvalidType(msg)) = result {
            assert!(msg.contains("missing required gas price parameters"));
        } else {
            panic!("Expected InvalidType error");
        }
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_capped() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices::default(),
            max_priority_fee_per_gas: SpeedPrices {
                safe_low: 4_000_000_000,
                average: 5_000_000_000,
                fast: 6_000_000_000,
                fastest: 8_000_000_000,
            },
            base_fee_per_gas: 100_000_000_000,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let mut relayer = create_mock_relayer();
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(105_000_000_000),
            ..Default::default()
        });

        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    max_fee_per_gas: Some(90_000_000_000),
                    max_priority_fee_per_gas: Some(4_000_000_000),
                    speed: Some(Speed::Fastest),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        // Normally, we'd expect ~ (100 Gwei * 2.4) + 8 Gwei > 248 Gwei. We'll cap it at 105 Gwei.
        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert!(bumped.max_fee_per_gas.unwrap() <= 105_000_000_000);
        assert!(bumped.max_priority_fee_per_gas.unwrap() <= 105_000_000_000);
    }

    #[tokio::test]
    async fn test_is_min_bumped_flag_eip1559() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices::default(),
            max_priority_fee_per_gas: SpeedPrices {
                safe_low: 1_000_000_000,
                average: 2_000_000_000,
                fast: 3_000_000_000,
                fastest: 4_000_000_000,
            },
            base_fee_per_gas: 40_000_000_000,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let mut relayer = create_mock_relayer();

        // Case 1: Price high enough - should result in is_min_bumped = true
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(200_000_000_000u128),
            ..Default::default()
        });

        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    max_fee_per_gas: Some(50_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert_eq!(
            bumped.is_min_bumped,
            Some(true),
            "Should be min bumped when prices are high enough"
        );

        // Case 2: Gas price cap too low - should result in is_min_bumped = false
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(50_000_000_000u128), // Cap is below the min bump for max_fee_per_gas
            ..Default::default()
        });

        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    max_fee_per_gas: Some(50_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        // Since min bump is 10%, original was 50 Gwei, min is 55 Gwei, but cap is 50 Gwei
        assert_eq!(
            bumped.is_min_bumped,
            Some(false),
            "Should not be min bumped when cap is too low"
        );
    }

    #[tokio::test]
    async fn test_is_min_bumped_flag_legacy() {
        let mut mock_service = MockEvmGasPriceServiceTrait::new();
        let mock_prices = GasPrices {
            legacy_prices: SpeedPrices {
                safe_low: 8_000_000_000,
                average: 10_000_000_000,
                fast: 12_000_000_000,
                fastest: 15_000_000_000,
            },
            max_priority_fee_per_gas: SpeedPrices::default(),
            base_fee_per_gas: 0,
        };
        mock_service
            .expect_get_prices_from_json_rpc()
            .returning(move || {
                let prices = mock_prices.clone();
                Box::pin(async move { Ok(prices) })
            });
        mock_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        let pc = PriceCalculator::new(mock_service, NetworkExtraFeeCalculator::None);
        let mut relayer = create_mock_relayer();

        // Case 1: Regular case, cap is high enough
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(100_000_000_000u128),
            ..Default::default()
        });

        let tx = TransactionRepoModel {
            network_data: {
                let evm_data = EvmTransactionData {
                    gas_price: Some(10_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                };
                NetworkTransactionData::Evm(evm_data)
            },
            ..Default::default()
        };

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert_eq!(
            bumped.is_min_bumped,
            Some(true),
            "Should be min bumped with sufficient cap"
        );

        // Case 2: Cap too low
        relayer.policies = RelayerNetworkPolicy::Evm(RelayerEvmPolicy {
            gas_price_cap: Some(10_000_000_000u128), // Same as original, preventing the 10% bump
            ..Default::default()
        });

        let bumped = pc.calculate_bumped_gas_price(&tx, &relayer).await.unwrap();
        assert_eq!(
            bumped.is_min_bumped,
            Some(false),
            "Should not be min bumped with insufficient cap"
        );
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_with_extra_fee() {
        // Set up mock gas price service
        let mut mock_gas_service = MockEvmGasPriceServiceTrait::new();
        mock_gas_service
            .expect_get_prices_from_json_rpc()
            .returning(|| {
                Box::pin(async {
                    Ok(GasPrices {
                        legacy_prices: SpeedPrices {
                            safe_low: 10_000_000_000,
                            average: 12_000_000_000,
                            fast: 14_000_000_000,
                            fastest: 18_000_000_000,
                        },
                        max_priority_fee_per_gas: SpeedPrices::default(),
                        base_fee_per_gas: 40_000_000_000,
                    })
                })
            });
        mock_gas_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        // Create a PriceCalculator without extra fee service first
        let pc = PriceCalculator::new(mock_gas_service, NetworkExtraFeeCalculator::None);

        // Create test transaction and relayer
        let relayer = create_mock_relayer();
        let tx = TransactionRepoModel {
            network_data: {
                NetworkTransactionData::Evm(EvmTransactionData {
                    max_fee_per_gas: Some(50_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                })
            },
            ..Default::default()
        };

        // Call the method under test
        let result = pc.calculate_bumped_gas_price(&tx, &relayer).await;

        // Verify extra fee is None when no extra fee service is used
        assert!(result.is_ok());
        let price_params = result.unwrap();
        assert_eq!(price_params.extra_fee, None);
    }

    #[tokio::test]
    async fn test_calculate_bumped_gas_price_with_mock_extra_fee() {
        // Set up mock gas price service
        let mut mock_gas_service = MockEvmGasPriceServiceTrait::new();
        mock_gas_service
            .expect_get_prices_from_json_rpc()
            .returning(|| {
                Box::pin(async {
                    Ok(GasPrices {
                        legacy_prices: SpeedPrices {
                            safe_low: 10_000_000_000,
                            average: 12_000_000_000,
                            fast: 14_000_000_000,
                            fastest: 18_000_000_000,
                        },
                        max_priority_fee_per_gas: SpeedPrices::default(),
                        base_fee_per_gas: 40_000_000_000,
                    })
                })
            });
        mock_gas_service
            .expect_network()
            .return_const(EvmNetwork::from_named(EvmNamedNetwork::Mainnet));

        // Create mock extra fee service
        let mut mock_extra_fee_service = MockNetworkExtraFeeCalculatorServiceTrait::new();

        // Set up expected return value for the extra fee
        let expected_extra_fee = U256::from(123456789u64);
        mock_extra_fee_service
            .expect_get_extra_fee()
            .returning(move |_| Box::pin(async move { Ok(expected_extra_fee) }));

        // Use the Mock variant from NetworkExtraFeeCalculator enum
        let extra_fee_calculator = NetworkExtraFeeCalculator::Mock(mock_extra_fee_service);

        // Create the price calculator with the mock extra fee service
        let pc = PriceCalculator::new(mock_gas_service, extra_fee_calculator);

        // Create test transaction and relayer
        let relayer = create_mock_relayer();
        let tx = TransactionRepoModel {
            network_data: {
                NetworkTransactionData::Evm(EvmTransactionData {
                    max_fee_per_gas: Some(50_000_000_000),
                    max_priority_fee_per_gas: Some(2_000_000_000),
                    speed: Some(Speed::Fast),
                    ..Default::default()
                })
            },
            ..Default::default()
        };

        // Call the method under test
        let result = pc.calculate_bumped_gas_price(&tx, &relayer).await;

        // Verify extra fee was properly included
        assert!(result.is_ok());
        let price_params = result.unwrap();
        assert_eq!(
            price_params.extra_fee,
            Some(expected_extra_fee.try_into().unwrap_or(0))
        );
    }
}