eigen_client_eth/
instrumented_client.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
use crate::client::BackendClient;
use alloy::consensus::TxEnvelope;
use alloy::providers::{Provider, ProviderBuilder, RootProvider};
use alloy::pubsub::{PubSubFrontend, Subscription};
use alloy::rpc::types::eth::{
    Block, BlockNumberOrTag, FeeHistory, Filter, Header, Log, SyncStatus, Transaction,
    TransactionReceipt, TransactionRequest,
};
use alloy::transports::http::{Client, Http};
use alloy::transports::ws::WsConnect;
use alloy::transports::{TransportError, TransportResult};
use alloy_json_rpc::{RpcParam, RpcReturn};
use alloy_primitives::{Address, BlockHash, BlockNumber, Bytes, ChainId, B256, U256, U64};
use alloy_rlp::Encodable;
use eigen_logging::get_test_logger;
use eigen_metrics_collectors_rpc_calls::RpcCallsMetrics as RpcCallsCollector;
use hex;
use std::time::Instant;
use thiserror::Error;
use url::Url;

const PENDING_TAG: &str = "pending";

/// This struct represents an instrumented client that can be used to interact with an Ethereum node.
/// It provides a set of methods to interact with the node and measures the duration of the calls.
pub struct InstrumentedClient {
    http_client: Option<RootProvider<Http<Client>>>,
    ws_client: Option<RootProvider<PubSubFrontend>>,
    rpc_collector: RpcCallsCollector,
    net_version: u64,
}

/// Possible errors raised in signer creation
#[derive(Error, Debug)]
pub enum InstrumentedClientError {
    #[error("invalid url")]
    InvalidUrl,
    #[error("error getting version")]
    ErrorGettingVersion,
    #[error("error running command")]
    CommandError,
}

#[async_trait::async_trait]
impl BackendClient for InstrumentedClient {
    type Error = InstrumentedClientError;

    /// Returns the latest block number.
    ///
    /// # Returns
    ///
    /// The latest block number.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC call fails.
    async fn block_number(&self) -> Result<BlockNumber, Self::Error> {
        self.instrument_function("eth_blockNumber", ())
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get block number", err.to_string().as_str())
            })
            .map_err(|_err| InstrumentedClientError::CommandError)
            .map(|result: U64| result.to())
    }

    /// Returns the block having the given block number.
    ///
    /// # Arguments
    ///
    /// * `number` - The block number.
    ///
    /// # Returns
    ///
    /// The block having the given block number.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC call fails.
    async fn block_by_number(
        &self,
        number: BlockNumberOrTag,
    ) -> Result<Option<Block>, Self::Error> {
        self.instrument_function("eth_getBlockByNumber", (number, true))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get block by number", err.to_string().as_str())
            })
            .map_err(|_err| InstrumentedClientError::CommandError)
    }
}

impl InstrumentedClient {
    /// Creates a new instance of the InstrumentedClient.
    ///
    /// # Arguments
    ///
    /// * `url` - The URL of the RPC server.
    ///
    /// # Returns
    ///
    /// A new instance of the InstrumentedClient.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid or if there is an error getting the version.
    pub async fn new(url: &str) -> Result<Self, InstrumentedClientError> {
        let url = Url::parse(url).map_err(|_| InstrumentedClientError::InvalidUrl)?;
        let http_client = ProviderBuilder::new().on_http(url);
        let net_version = http_client
            .get_net_version()
            .await
            .map_err(|_| InstrumentedClientError::ErrorGettingVersion)?;

        let rpc_collector = RpcCallsCollector::new(get_test_logger().clone());
        Ok(InstrumentedClient {
            http_client: Some(http_client),
            ws_client: None,
            rpc_collector,
            net_version,
        })
    }

    /// Creates a new instance of the InstrumentedClient that supports ws connection.
    ///
    /// # Arguments
    ///
    /// * `url` - The ws URL of the RPC server .
    ///
    /// # Returns
    ///
    /// A new instance of the InstrumentedClient.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid or if there is an error getting the version.
    pub async fn new_ws(url: &str) -> Result<Self, InstrumentedClientError> {
        let url = Url::parse(url).map_err(|_| InstrumentedClientError::InvalidUrl)?;
        let ws_connect = WsConnect::new(url);

        let ws_client = ProviderBuilder::new().on_ws(ws_connect).await.unwrap();
        let net_version = ws_client
            .get_net_version()
            .await
            .map_err(|_| InstrumentedClientError::ErrorGettingVersion)?;

        let rpc_collector = RpcCallsCollector::new(get_test_logger().clone());
        Ok(InstrumentedClient {
            http_client: None,
            ws_client: Some(ws_client),
            rpc_collector,
            net_version,
        })
    }

    /// Creates a new instance of the InstrumentedClient from an existing client (`RootProvider`).
    ///
    /// # Arguments
    ///
    /// * `client` - The existing client (`RootProvider`).
    ///
    /// # Returns
    ///
    /// A new instance of the InstrumentedClient.
    ///
    /// # Errors
    ///
    /// Returns an error if there is an error getting the version.
    pub async fn new_from_client(
        client: RootProvider<Http<Client>>,
    ) -> Result<Self, InstrumentedClientError> {
        let net_version = client
            .get_net_version()
            .await
            .map_err(|_| InstrumentedClientError::ErrorGettingVersion)?;

        let rpc_collector = RpcCallsCollector::new(get_test_logger().clone());
        Ok(InstrumentedClient {
            http_client: Some(client),
            ws_client: None,
            rpc_collector,
            net_version,
        })
    }

    /// Returns the chain ID.
    ///
    /// # Returns
    ///
    /// The chain ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the RPC call fails.
    pub async fn chain_id(&self) -> TransportResult<ChainId> {
        self.instrument_function("eth_chainId", ())
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get chain id", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Returns the balance of the account at the given block number.
    ///
    /// # Arguments
    ///
    /// * `account` - The account address.
    /// * `block_number` - The block number.
    ///
    /// # Returns
    ///
    /// The balance of the account at the given block number.
    pub async fn balance_at(
        &self,
        account: Address,
        block_number: BlockNumberOrTag,
    ) -> TransportResult<U256> {
        self.instrument_function("eth_getBalance", (account, block_number))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get balance", err.to_string().as_str())
            })
    }

    /// Returns the block having the given block hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - The block hash.
    ///
    /// # Returns
    ///
    /// The block having the given block hash.
    pub async fn block_by_hash(&self, hash: BlockHash) -> TransportResult<Option<Block>> {
        self.instrument_function("eth_getBlockByHash", (hash, true))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get block by hash", err.to_string().as_str())
            })
    }

    /// Executes a message call transaction.
    ///
    /// # Arguments
    ///
    /// * `call` - The message call to be executed
    /// * `block_number` - The block height at which the call runs. *Note:* in case this argument is n
    ///
    /// # Returns
    ///
    /// The returned value of the executed contract.
    pub async fn call_contract(
        &self,
        call: TransactionRequest,
        block_number: BlockNumberOrTag,
    ) -> TransportResult<Bytes> {
        self.instrument_function("eth_call", (call, block_number))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to call contract", err.to_string().as_str())
            })
    }

    /// Returns the compiled bytecode of a smart contract given its address and block number.
    ///
    /// # Arguments
    ///
    /// * `address` - The address of the smart contract.
    /// * `block_number` - The block number.
    ///
    /// # Returns
    ///
    /// The compiled bytecode of the smart contract with the given address and block number.
    pub async fn code_at(
        &self,
        address: Address,
        block_number: BlockNumberOrTag,
    ) -> TransportResult<Bytes> {
        self.instrument_function("eth_getCode", (address, block_number))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get code", err.to_string().as_str())
            })
    }

    /// Estimates the gas needed to execute a specific transaction.
    ///
    /// # Arguments
    ///
    /// * `tx` - The transaction from which the gas consumption is estimated.
    ///
    /// # Returns
    ///
    /// The estimated gas.
    pub async fn estimate_gas(&self, tx: TransactionRequest) -> TransportResult<u64> {
        self.instrument_function("eth_estimateGas", (tx,))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to estimate gas", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Returns a collection of historical gas information.
    ///
    /// # Arguments
    ///
    /// * `block_count` - The number of blocks to include in the collection.
    /// * `last_block` - The last block number to include in the collection.
    /// * `reward_percentiles` - A sorted list of percentage points used to
    ///   sample the effective priority fees per gas from each block. The samples are
    ///   taken in ascending order and weighted by gas usage. The list is sorted increasingly.
    pub async fn fee_history(
        &self,
        block_count: u64,
        last_block: BlockNumberOrTag,
        reward_percentiles: &[f64],
    ) -> TransportResult<FeeHistory> {
        self.instrument_function(
            "eth_feeHistory",
            (block_count, last_block, reward_percentiles),
        )
        .await
        .inspect_err(|err| {
            self.rpc_collector
                .logger()
                .error("Failed to get fee history", err.to_string().as_str())
        })
    }
    /// Executes a filter query.
    ///
    /// # Arguments
    ///
    /// * `filter` - The filter query to be executed.
    ///
    /// # Returns
    ///
    /// A vector of logs.
    pub async fn filter_logs(&self, filter: Filter) -> TransportResult<Vec<Log>> {
        self.instrument_function("eth_getLogs", (filter,))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get filter logs", err.to_string().as_str())
            })
    }

    /// Returns the block header with the given hash.
    ///
    /// # Arguments
    ///
    /// * `hash` - The block hash.
    ///
    /// # Returns
    ///
    /// The block header.
    pub async fn header_by_hash(&self, hash: B256) -> TransportResult<Header> {
        let transaction_detail = false;
        self.instrument_function("eth_getBlockByHash", (hash, transaction_detail))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get header by hash", err.to_string().as_str())
            })
    }

    /// Returns a block header with the given block number.
    ///
    /// # Arguments
    ///
    /// * `block_number` - The block number.
    ///
    /// # Returns
    ///
    /// The block header.
    pub async fn header_by_number(
        &self,
        block_number: BlockNumberOrTag,
    ) -> TransportResult<Header> {
        let transaction_detail = false;
        self.instrument_function("eth_getBlockByNumber", (block_number, transaction_detail))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get header by number", err.to_string().as_str())
            })
    }

    /// Returns the nonce of the given account.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the account.
    /// * `block_number` - The block number from where the nonce is taken.
    ///
    /// # Returns
    ///
    /// The nonce of the account.
    pub async fn nonce_at(
        &self,
        account: Address,
        block_number: BlockNumberOrTag,
    ) -> TransportResult<u64> {
        self.instrument_function("eth_getTransactionCount", (account, block_number))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get nonce", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Returns the wei balance of the given account in the pending state.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the account.
    ///
    /// # Returns
    ///
    /// The wei balance of the account.
    pub async fn pending_balance_at(&self, account: Address) -> TransportResult<U256> {
        self.instrument_function("eth_getBalance", (account, PENDING_TAG))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get pending balance", err.to_string().as_str())
            })
    }

    /// Executes a message call transaction using the EVM.
    /// The state seen by the contract call is the pending state.
    ///
    /// # Arguments
    ///
    /// * `call` - The message call to be executed
    ///
    /// # Returns
    ///
    /// The returned value of the executed contract.
    pub async fn pending_call_contract(&self, call: TransactionRequest) -> TransportResult<Bytes> {
        self.call_contract(call, BlockNumberOrTag::Pending).await
    }

    /// Returns the contract code of the given account in the pending state.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the contract.
    ///
    /// # Returns
    ///
    /// The contract code.
    pub async fn pending_code_at(&self, account: Address) -> TransportResult<Bytes> {
        self.instrument_function("eth_getCode", (account, PENDING_TAG))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get pending code", err.to_string().as_str())
            })
    }

    /// Returns the account nonce of the given account in the pending state.
    /// This is the nonce that should be used for the next transaction.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the account.
    ///
    /// # Returns
    ///
    /// * The nonce of the account in the pending state.
    pub async fn pending_nonce_at(&self, account: Address) -> TransportResult<u64> {
        self.instrument_function("eth_getTransactionCount", (account, PENDING_TAG))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get pending nonce", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Returns the value of key in the contract storage of the given account in the pending state.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the contract.
    /// * `key` - The position in the storage.
    ///
    /// # Returns
    ///
    /// The value of the storage position at the provided address.
    pub async fn pending_storage_at(&self, account: Address, key: U256) -> TransportResult<U256> {
        self.instrument_function("eth_getStorageAt", (account, key, PENDING_TAG))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get pending storage", err.to_string().as_str())
            })
    }

    /// Returns the total number of transactions in the pending state.
    ///
    /// # Returns
    ///
    /// The number of pending transactions.
    pub async fn pending_transaction_count(&self) -> TransportResult<u64> {
        self.instrument_function("eth_getBlockTransactionCountByNumber", (PENDING_TAG,))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get transaction count", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Sends a signed transaction into the pending pool for execution.
    ///
    /// # Arguments
    ///
    /// * `tx` - The transaction to be executed.
    ///
    /// # Returns
    ///
    /// The hash of the given transaction.
    pub async fn send_transaction(&self, tx: TxEnvelope) -> TransportResult<B256> {
        let mut encoded_tx = Vec::new();
        tx.encode(&mut encoded_tx);
        self.instrument_function("eth_sendRawTransaction", (hex::encode(encoded_tx),))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to send transaction", err.to_string().as_str())
            })
    }

    /// Returns the value of key in the contract storage of the given account.
    ///
    /// # Arguments
    ///
    /// * `account` - The address of the contract.
    /// * `key` - The position in the storage.
    /// * `block_number` - The block number from which the storage is taken.
    ///
    /// # Returns
    ///
    /// The value of the storage position at the provided address.
    pub async fn storage_at(
        &self,
        account: Address,
        key: U256,
        block_number: U256,
    ) -> TransportResult<U256> {
        self.instrument_function("eth_getStorageAt", (account, key, block_number))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get storage", err.to_string().as_str())
            })
    }

    /// Subscribes to the results of a streaming filter query.
    /// *Note:* this method fails if the InstrumentedClient does not use a web socket client.
    /// # Arguments
    ///
    /// * `filter` - A filter query.
    ///
    /// # Returns
    ///
    /// The subscription.
    ///
    /// # Errors
    ///
    /// * If ws_client is `None`.
    pub async fn subscribe_filter_logs<R: RpcReturn>(
        &self,
        filter: Filter,
    ) -> TransportResult<Subscription<R>> {
        let id: U256 = self
            .instrument_function("eth_subscribe", ("logs", filter))
            .await
            .inspect_err(|err| {
                self.rpc_collector.logger().error(
                    "Failed to get logs subscription id",
                    err.to_string().as_str(),
                )
            })?;
        if let Some(ws_client) = self.ws_client.as_ref() {
            ws_client.get_subscription(id.into()).await
        } else {
            Err(TransportError::UnsupportedFeature(
                "http client does not support eth_subscribe calls.",
            ))
        }
    }

    /// Subscribes to notifications about the current blockchain head.
    /// *Note:* this method fails if the InstrumentedClient does not use a web socket client.
    ///
    /// # Returns
    ///
    /// The subscription.
    ///
    /// # Errors
    ///
    /// * If ws_client is `None`.
    pub async fn subscribe_new_head<R: RpcReturn>(&self) -> TransportResult<Subscription<R>> {
        let id: U256 = self
            .instrument_function("eth_subscribe", ("newHeads",))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to subscribe new head", err.to_string().as_str())
            })?;
        if let Some(ws_client) = self.ws_client.as_ref() {
            ws_client.get_subscription(id.into()).await
        } else {
            Err(TransportError::UnsupportedFeature(
                "http client does not support eth_subscribe calls.",
            ))
        }
    }

    /// Retrieves the currently suggested gas price.
    ///
    /// # Returns
    ///
    /// The currently suggested gas price.
    pub async fn suggest_gas_price(&self) -> TransportResult<u64> {
        self.instrument_function("eth_gasPrice", ())
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to suggest gas price", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Retrieves the currently suggested gas tip cap after EIP1559.
    ///
    /// # Returns
    ///
    /// The currently suggested gas price.
    pub async fn suggest_gas_tip_cap(&self) -> TransportResult<u64> {
        self.instrument_function("eth_maxPriorityFeePerGas", ())
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to suggest gas tip cap", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Retrieves the current progress of the sync algorithm.
    /// If there's no sync currently running, it returns None.
    ///
    /// # Returns
    ///
    /// The current progress of the sync algorithm.
    pub async fn sync_progress(&self) -> TransportResult<SyncStatus> {
        self.instrument_function("eth_syncing", ())
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get sync progress", err.to_string().as_str())
            })
    }

    /// Returns the transaction with the given hash.
    ///
    /// # Arguments
    ///
    /// * `tx_hash` - The transaction hash.
    ///
    /// # Returns
    ///
    /// The transaction with the given hash.
    pub async fn transaction_by_hash(&self, tx_hash: B256) -> TransportResult<Transaction> {
        self.instrument_function("eth_getTransactionByHash", (tx_hash,))
            .await
            .inspect_err(|err| {
                self.rpc_collector.logger().error(
                    "Failed to get transaction by hash",
                    err.to_string().as_str(),
                )
            })
    }

    /// Returns the total number of transactions in the given block.
    ///
    /// # Arguments
    ///
    /// * `block_hash` - The block hash.
    ///
    /// # Returns
    ///
    /// The number of transactions in the given block.
    pub async fn transaction_count(&self, block_hash: B256) -> TransportResult<u64> {
        self.instrument_function("eth_getBlockTransactionCountByHash", (block_hash,))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get transaction count", err.to_string().as_str())
            })
            .map(|result: U64| result.to())
    }

    /// Returns a single transaction at index in the given block.
    ///
    /// # Arguments
    ///
    /// * `block_hash` - The block hash.
    /// * `index` - The index of the transaction in the block.
    ///
    /// # Returns
    ///
    /// The transaction at index in the given block.
    pub async fn transaction_in_block(
        &self,
        block_hash: B256,
        index: u64,
    ) -> TransportResult<Transaction> {
        self.instrument_function("eth_getTransactionByBlockHashAndIndex", (block_hash, index))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get transaction", err.to_string().as_str())
            })
    }

    /// Returns the receipt of a transaction by transaction hash.
    /// *Note:* the receipt is not available for pending transactions.
    ///
    /// # Arguments
    ///
    /// * `tx_hash` - The hash of the transaction.
    ///
    /// # Returns
    ///
    /// The transaction receipt.
    pub async fn transaction_receipt(&self, tx_hash: B256) -> TransportResult<TransactionReceipt> {
        self.instrument_function("eth_getTransactionReceipt", (tx_hash,))
            .await
            .inspect_err(|err| {
                self.rpc_collector
                    .logger()
                    .error("Failed to get receipt", err.to_string().as_str())
            })
    }

    /// Instrument a function call with the given method name and parameters.
    ///
    /// This function will measure the duration of the call and report it to the metrics collector.
    ///
    /// # Arguments
    ///
    /// * `rpc_method_name` - The name of the RPC method being called.
    /// * `params` - The parameters to pass to the RPC method.
    ///
    /// # Returns
    ///
    /// The result of the RPC call.
    async fn instrument_function<'async_trait, P, R>(
        &self,
        rpc_method_name: &str,
        params: P,
    ) -> TransportResult<R>
    where
        P: RpcParam + 'async_trait,
        R: RpcReturn + 'async_trait,
    {
        let start = Instant::now();
        let method_string = String::from(rpc_method_name);

        // send the request with the provided client (http or ws)
        let result = match (self.http_client.as_ref(), self.ws_client.as_ref()) {
            (Some(http_client), _) => http_client.raw_request(method_string.into(), params).await,
            (_, Some(ws_client)) => ws_client.raw_request(method_string.into(), params).await,
            (_, _) => unreachable!(),
        };

        let rpc_request_duration = start.elapsed();

        // we only observe the duration of successful calls (even though this is not well defined in the spec)
        self.rpc_collector.set_rpc_request_duration_seconds(
            rpc_method_name,
            self.net_version.to_string().as_str(),
            rpc_request_duration.as_secs_f64(),
        );
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy::consensus::{SignableTransaction, TxLegacy};
    use alloy::network::TxSignerSync;
    use alloy::primitives::{bytes, TxKind::Call, U256};
    use alloy::rpc::types::eth::{
        pubsub::SubscriptionResult, BlockId, BlockNumberOrTag, BlockTransactionsKind,
    };
    use alloy_primitives::address;
    use eigen_common::get_provider;
    use eigen_signer::signer::Config;
    use eigen_testing_utils::anvil::start_anvil_container;
    use eigen_testing_utils::transaction::wait_transaction;
    use tokio;

    #[tokio::test]
    async fn test_suggest_gas_tip_cap() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let fee_per_gas = instrumented_client.suggest_gas_tip_cap().await.unwrap();
        let expected_fee_per_gas = get_provider(&http_endpoint)
            .get_max_priority_fee_per_gas()
            .await
            .unwrap();
        assert_eq!(expected_fee_per_gas as u64, fee_per_gas);
    }

    #[tokio::test]
    async fn test_gas_price() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let gas_price = instrumented_client.suggest_gas_price().await.unwrap();
        let expected_gas_price = provider.clone().get_gas_price().await.unwrap();
        assert_eq!(gas_price, expected_gas_price as u64);
    }

    #[tokio::test]
    async fn test_sync_status() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let sync_status = instrumented_client.sync_progress().await.unwrap();
        let expected_sync_status = provider.clone().syncing().await.unwrap();
        assert_eq!(expected_sync_status, sync_status);
    }

    #[tokio::test]
    async fn test_chain_id() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let expected_chain_id = provider.clone().get_chain_id().await.unwrap();
        let chain_id = instrumented_client.chain_id().await.unwrap();

        assert_eq!(expected_chain_id, chain_id);
    }

    #[tokio::test]
    async fn test_balance_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        let expected_balance_at = provider.get_balance(address).await.unwrap();
        let balance_at = instrumented_client
            .balance_at(address, BlockNumberOrTag::Latest)
            .await
            .unwrap();

        assert_eq!(expected_balance_at, balance_at);
    }

    #[tokio::test]
    async fn test_subscribe_new_head() {
        let (_container, _http_endpoint, ws_endpoint) = start_anvil_container().await;

        let instrumented_client = InstrumentedClient::new_ws(&ws_endpoint).await.unwrap();
        let subscription: TransportResult<Subscription<SubscriptionResult>> =
            instrumented_client.subscribe_new_head().await;
        assert!(subscription.is_ok())
    }

    #[tokio::test]
    async fn test_subscribe_filter_logs() {
        let (_container, http_endpoint, ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new_ws(&ws_endpoint).await.unwrap();
        let address = provider.clone().get_accounts().await.unwrap()[0];
        let filter = Filter::new().address(address.to_string().parse::<Address>().unwrap());

        let subscription: TransportResult<Subscription<SubscriptionResult>> =
            instrumented_client.subscribe_filter_logs(filter).await;

        assert!(subscription.is_ok());
    }

    #[tokio::test]
    async fn test_block_by_hash() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        // get the hash from the last block
        let hash = provider
            .get_block(BlockId::latest(), BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap()
            .header
            .hash;

        let expected_block = provider
            .get_block_by_hash(hash, BlockTransactionsKind::Full)
            .await
            .unwrap();
        let block = instrumented_client.block_by_hash(hash).await.unwrap();

        assert_eq!(expected_block, block);
    }

    #[tokio::test]
    async fn test_block_by_number() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let block_number = 1;

        let expected_block = provider
            .get_block_by_number(block_number.into(), BlockTransactionsKind::Full)
            .await
            .unwrap();
        let block = instrumented_client
            .block_by_number(block_number.into())
            .await
            .unwrap();

        assert_eq!(expected_block, block);
    }

    #[tokio::test]
    async fn test_transaction_count() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let block = provider
            .get_block(BlockId::latest(), BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap();

        let block_hash = block.header.hash;
        let tx_count = instrumented_client
            .transaction_count(B256::from_slice(block_hash.as_slice()))
            .await
            .unwrap();
        let expected_tx_count = block.transactions.len();

        assert_eq!(tx_count, expected_tx_count as u64);
    }

    /// This test tests the following methods
    /// * `send_transaction`
    /// * `transaction_by_hash`
    /// * `transaction_receipt`
    /// * `transaction_in_block`
    #[tokio::test]
    async fn test_transaction_methods() {
        let (_container, rpc_url, _ws_endpoint) = start_anvil_container().await;
        let instrumented_client = InstrumentedClient::new(&rpc_url).await.unwrap();

        // build the transaction
        let to = address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720");
        let mut tx = TxLegacy {
            to: Call(to),
            value: U256::from(0),
            gas_limit: 2_000_000,
            nonce: 0x69, // nonce queried from the sender account
            gas_price: 21_000_000_000,
            input: bytes!(),
            chain_id: Some(31337),
        };

        let private_key_hex =
            "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string();
        let config = Config::PrivateKey(private_key_hex);
        let signer = Config::signer_from_config(config).unwrap();
        let signature = signer.sign_transaction_sync(&mut tx).unwrap();
        let signed_tx = tx.into_signed(signature);
        let tx: TxEnvelope = TxEnvelope::from(signed_tx);

        // test send_transaction
        let tx_hash = instrumented_client.send_transaction(tx).await;
        assert!(tx_hash.is_ok());
        let tx_hash = B256::from_slice(tx_hash.unwrap().as_ref());

        // test transaction_by_hash
        let tx_by_hash = instrumented_client.transaction_by_hash(tx_hash).await;
        assert!(tx_by_hash.is_ok());

        wait_transaction(&rpc_url, tx_hash).await.unwrap();

        // test transaction_receipt
        let receipt = instrumented_client.transaction_receipt(tx_hash).await;
        assert!(receipt.is_ok());
        let receipt = receipt.unwrap();

        // test transaction_in_block
        let tx_by_block = instrumented_client
            .transaction_in_block(
                receipt.block_hash.unwrap(),
                receipt.transaction_index.unwrap(),
            )
            .await;
        assert!(tx_by_block.is_ok());
    }

    #[tokio::test]
    async fn test_estimate_gas() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let accounts = provider.get_accounts().await.unwrap();
        let from = accounts.first().unwrap();
        let to = accounts.get(1).unwrap();

        // build the transaction
        let tx = TxLegacy {
            to: Call(*to),
            value: U256::from(0),
            gas_limit: 2_000_000,
            nonce: 0,
            gas_price: 1_000_000,
            input: bytes!(),
            chain_id: Some(31337),
        };
        let tx_request: TransactionRequest = tx.clone().into();
        let tx_request = tx_request.from(*from);

        let expected_estimated_gas = provider.clone().estimate_gas(&tx_request).await.unwrap();
        let estimated_gas = instrumented_client.estimate_gas(tx_request).await.unwrap();
        assert_eq!(expected_estimated_gas, estimated_gas);
    }

    #[tokio::test]
    async fn test_call_contract_and_pending_call_contract() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let anvil = provider.clone();
        let accounts = anvil.get_accounts().await.unwrap();
        let from = accounts.first().unwrap();
        let to = accounts.get(1).unwrap();

        let nonce = instrumented_client
            .nonce_at(*from, BlockNumberOrTag::Latest)
            .await
            .unwrap();

        // build the transaction
        let tx = TxLegacy {
            to: Call(*to),
            value: U256::from(0),
            gas_limit: 1_000_000,
            nonce,
            gas_price: 21_000_000_000,
            input: bytes!(),
            chain_id: Some(31337),
        };
        let tx_request: TransactionRequest = tx.clone().into();
        let tx_request = tx_request.from(*from);

        // test call_contract
        let expected_bytes = anvil.call(&tx_request).await.unwrap();
        let bytes = instrumented_client
            .call_contract(tx_request.clone(), BlockNumberOrTag::Earliest)
            .await
            .unwrap();
        assert_eq!(expected_bytes, bytes);

        // test pending_call_contract
        let bytes = instrumented_client.pending_call_contract(tx_request).await;
        assert!(bytes.is_ok());
    }

    #[tokio::test]
    async fn test_filter_logs() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.clone().get_accounts().await.unwrap()[0];
        let filter = Filter::new().address(address.to_string().parse::<Address>().unwrap());

        let expected_logs = provider.clone().get_logs(&filter).await.unwrap();
        let logs = instrumented_client.filter_logs(filter).await.unwrap();

        assert_eq!(expected_logs, logs);
    }

    #[tokio::test]
    async fn test_storage_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let account = provider.clone().get_accounts().await.unwrap()[0];
        let expected_storage = provider
            .clone()
            .get_storage_at(account, U256::ZERO)
            .await
            .unwrap();

        let storage = instrumented_client
            .storage_at(account, U256::ZERO, U256::ZERO)
            .await
            .unwrap();

        assert_eq!(expected_storage, storage);
    }

    #[tokio::test]
    async fn test_block_number() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let expected_block_number = provider.clone().get_block_number().await.unwrap();
        let block_number = instrumented_client.block_number().await.unwrap();

        assert_eq!(expected_block_number, block_number);
    }

    #[tokio::test]
    async fn test_code_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        let expected_code = provider.get_code_at(address).await.unwrap();
        let code = instrumented_client
            .code_at(address, BlockNumberOrTag::Latest)
            .await
            .unwrap();

        assert_eq!(expected_code, code);
    }

    #[tokio::test]
    async fn test_fee_history() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let block_count = 4;
        let last_block = BlockNumberOrTag::Latest;
        let reward_percentiles = [0.2, 0.3];

        let expected_fee_history = provider
            .get_fee_history(block_count, last_block, &reward_percentiles)
            .await
            .unwrap();
        let fee_history = instrumented_client
            .fee_history(block_count, last_block, &reward_percentiles)
            .await
            .unwrap();

        assert_eq!(expected_fee_history, fee_history);
    }

    #[tokio::test]
    async fn test_header_by_hash() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let hash = provider
            .get_block(BlockId::latest(), BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap()
            .header
            .hash;
        let expected_header = provider
            .get_block_by_hash(hash, BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap()
            .header;
        let header = instrumented_client.header_by_hash(hash).await.unwrap();

        assert_eq!(expected_header, header);
    }

    #[tokio::test]
    async fn test_header_by_number() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let block_number = BlockNumberOrTag::Earliest;

        let header = instrumented_client
            .header_by_number(block_number)
            .await
            .unwrap();

        let expected_header = provider
            .get_block_by_number(block_number, BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap()
            .header;

        assert_eq!(expected_header, header);
    }

    #[tokio::test]
    async fn test_nonce_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        let expected_nonce = provider.get_transaction_count(address).await.unwrap();
        let nonce = instrumented_client
            .nonce_at(address, BlockNumberOrTag::Latest)
            .await
            .unwrap();

        assert_eq!(expected_nonce, nonce);
    }

    #[tokio::test]
    async fn test_pending_balance_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        // TODO: currently comparing "pending" balance with "latest" balance. Check for pending transactions?
        let expected_balance = provider.get_balance(address).await.unwrap();
        let balance = instrumented_client
            .pending_balance_at(address)
            .await
            .unwrap();

        assert_eq!(expected_balance, balance);
    }

    #[tokio::test]
    async fn test_pending_code_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        // TODO: currently comparing "pending" with "latest". Check for pending transactions?
        let expected_code = provider.get_code_at(address).await.unwrap();
        let code = instrumented_client.pending_code_at(address).await.unwrap();

        assert_eq!(expected_code, code);
    }

    #[tokio::test]
    async fn test_pending_nonce_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];

        // TODO: currently comparing "pending" with "latest". Check for pending transactions?
        let expected_pending_nonce_at = provider.get_transaction_count(address).await.unwrap();
        let pending_nonce_at = instrumented_client.pending_nonce_at(address).await.unwrap();

        assert_eq!(expected_pending_nonce_at, pending_nonce_at);
    }

    #[tokio::test]
    async fn test_pending_storage_at() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();
        let address = provider.get_accounts().await.unwrap()[0];
        let key = U256::from(10);

        // TODO: currently comparing "pending" with "latest". Check for pending transactions?
        // TODO: set storage and check change
        let expected_pending_storage_at = provider.get_storage_at(address, key).await.unwrap();
        let pending_storage_at = instrumented_client
            .pending_storage_at(address, key)
            .await
            .unwrap();

        assert_eq!(expected_pending_storage_at, pending_storage_at);
    }

    #[tokio::test]
    async fn test_pending_transaction_count() {
        let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
        let provider = get_provider(&http_endpoint);

        let instrumented_client = InstrumentedClient::new(&http_endpoint).await.unwrap();

        let expected_transaction_count: u64 = provider
            .get_block_by_number(BlockNumberOrTag::Pending, BlockTransactionsKind::Hashes)
            .await
            .unwrap()
            .unwrap()
            .transactions
            .len() as u64;

        let transaction_count = instrumented_client.pending_transaction_count().await;

        assert_eq!(expected_transaction_count, transaction_count.unwrap());
    }
}