1use std::time::Duration;
8
9use alloy::{
10 network::AnyNetwork,
11 primitives::{Bytes, TxKind, Uint},
12 providers::{
13 fillers::{BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller},
14 Identity, Provider, ProviderBuilder, RootProvider,
15 },
16 rpc::{
17 client::ClientBuilder,
18 types::{BlockNumberOrTag, FeeHistory, TransactionInput, TransactionRequest},
19 },
20 transports::http::Http,
21};
22
23type EvmProviderType = FillProvider<
24 JoinFill<
25 Identity,
26 JoinFill<GasFiller, JoinFill<BlobGasFiller, JoinFill<NonceFiller, ChainIdFiller>>>,
27 >,
28 RootProvider<AnyNetwork>,
29 AnyNetwork,
30>;
31use async_trait::async_trait;
32use eyre::Result;
33use reqwest::ClientBuilder as ReqwestClientBuilder;
34use serde_json;
35use tracing::debug;
36
37use super::rpc_selector::RpcSelector;
38use super::{retry_rpc_call, ProviderConfig, RetryConfig};
39use crate::{
40 constants::{
41 DEFAULT_HTTP_CLIENT_CONNECT_TIMEOUT_SECONDS,
42 DEFAULT_HTTP_CLIENT_HTTP2_KEEP_ALIVE_INTERVAL_SECONDS,
43 DEFAULT_HTTP_CLIENT_HTTP2_KEEP_ALIVE_TIMEOUT_SECONDS,
44 DEFAULT_HTTP_CLIENT_POOL_IDLE_TIMEOUT_SECONDS, DEFAULT_HTTP_CLIENT_POOL_MAX_IDLE_PER_HOST,
45 DEFAULT_HTTP_CLIENT_TCP_KEEPALIVE_SECONDS,
46 },
47 models::{
48 BlockResponse, EvmTransactionData, RpcConfig, TransactionError, TransactionReceipt, U256,
49 },
50 services::provider::{is_retriable_error, should_mark_provider_failed},
51 utils::mask_url,
52};
53
54use crate::utils::{create_secure_redirect_policy, validate_safe_url};
55
56#[cfg(test)]
57use mockall::automock;
58
59use super::ProviderError;
60
61#[derive(Clone)]
65pub struct EvmProvider {
66 selector: RpcSelector,
68 timeout_seconds: u64,
70 retry_config: RetryConfig,
72}
73
74#[async_trait]
79#[cfg_attr(test, automock)]
80#[allow(dead_code)]
81pub trait EvmProviderTrait: Send + Sync {
82 fn get_configs(&self) -> Vec<RpcConfig>;
83 async fn get_balance(&self, address: &str) -> Result<U256, ProviderError>;
88
89 async fn get_block_number(&self) -> Result<u64, ProviderError>;
91
92 async fn estimate_gas(&self, tx: &EvmTransactionData) -> Result<u64, ProviderError>;
97
98 async fn get_gas_price(&self) -> Result<u128, ProviderError>;
100
101 async fn send_transaction(&self, tx: TransactionRequest) -> Result<String, ProviderError>;
106
107 async fn send_raw_transaction(&self, tx: &[u8]) -> Result<String, ProviderError>;
112
113 async fn health_check(&self) -> Result<bool, ProviderError>;
115
116 async fn get_transaction_count(&self, address: &str) -> Result<u64, ProviderError>;
121
122 async fn get_fee_history(
129 &self,
130 block_count: u64,
131 newest_block: BlockNumberOrTag,
132 reward_percentiles: Vec<f64>,
133 ) -> Result<FeeHistory, ProviderError>;
134
135 async fn get_block_by_number(&self) -> Result<BlockResponse, ProviderError>;
137
138 async fn get_transaction_receipt(
143 &self,
144 tx_hash: &str,
145 ) -> Result<Option<TransactionReceipt>, ProviderError>;
146
147 async fn call_contract(&self, tx: &TransactionRequest) -> Result<Bytes, ProviderError>;
152
153 async fn raw_request_dyn(
159 &self,
160 method: &str,
161 params: serde_json::Value,
162 ) -> Result<serde_json::Value, ProviderError>;
163}
164
165impl EvmProvider {
166 pub fn new(config: ProviderConfig) -> Result<Self, ProviderError> {
174 if config.rpc_configs.is_empty() {
175 return Err(ProviderError::NetworkConfiguration(
176 "At least one RPC configuration must be provided".to_string(),
177 ));
178 }
179
180 RpcConfig::validate_list(&config.rpc_configs)
181 .map_err(|e| ProviderError::NetworkConfiguration(format!("Invalid URL: {e}")))?;
182
183 let selector = RpcSelector::new(
185 config.rpc_configs,
186 config.failure_threshold,
187 config.pause_duration_secs,
188 config.failure_expiration_secs,
189 )
190 .map_err(|e| {
191 ProviderError::NetworkConfiguration(format!("Failed to create RPC selector: {e}"))
192 })?;
193
194 let retry_config = RetryConfig::from_env();
195
196 Ok(Self {
197 selector,
198 timeout_seconds: config.timeout_seconds,
199 retry_config,
200 })
201 }
202
203 pub fn get_configs(&self) -> Vec<RpcConfig> {
208 self.selector.get_configs()
209 }
210
211 fn initialize_provider(&self, url: &str) -> Result<EvmProviderType, ProviderError> {
213 let allowed_hosts = crate::config::ServerConfig::get_rpc_allowed_hosts();
215 let block_private_ips = crate::config::ServerConfig::get_rpc_block_private_ips();
216 validate_safe_url(url, &allowed_hosts, block_private_ips).map_err(|e| {
217 ProviderError::NetworkConfiguration(format!("RPC URL security validation failed: {e}"))
218 })?;
219
220 debug!("Initializing provider for URL: {}", mask_url(url));
221 let rpc_url = url
222 .parse()
223 .map_err(|e| ProviderError::NetworkConfiguration(format!("Invalid URL format: {e}")))?;
224
225 let client = ReqwestClientBuilder::new()
227 .timeout(Duration::from_secs(self.timeout_seconds))
228 .connect_timeout(Duration::from_secs(DEFAULT_HTTP_CLIENT_CONNECT_TIMEOUT_SECONDS))
229 .pool_max_idle_per_host(DEFAULT_HTTP_CLIENT_POOL_MAX_IDLE_PER_HOST)
230 .pool_idle_timeout(Duration::from_secs(DEFAULT_HTTP_CLIENT_POOL_IDLE_TIMEOUT_SECONDS))
231 .tcp_keepalive(Duration::from_secs(DEFAULT_HTTP_CLIENT_TCP_KEEPALIVE_SECONDS))
232 .http2_keep_alive_interval(Some(Duration::from_secs(
233 DEFAULT_HTTP_CLIENT_HTTP2_KEEP_ALIVE_INTERVAL_SECONDS,
234 )))
235 .http2_keep_alive_timeout(Duration::from_secs(
236 DEFAULT_HTTP_CLIENT_HTTP2_KEEP_ALIVE_TIMEOUT_SECONDS,
237 ))
238 .use_rustls_tls()
239 .redirect(create_secure_redirect_policy())
242 .build()
243 .map_err(|e| ProviderError::Other(format!("Failed to build HTTP client: {e}")))?;
244
245 let mut transport = Http::new(rpc_url);
246 transport.set_client(client);
247
248 let is_local = transport.guess_local();
249 let client = ClientBuilder::default().transport(transport, is_local);
250
251 let provider = ProviderBuilder::new()
252 .network::<AnyNetwork>()
253 .connect_client(client);
254
255 Ok(provider)
256 }
257
258 async fn retry_rpc_call<T, F, Fut>(
262 &self,
263 operation_name: &str,
264 operation: F,
265 ) -> Result<T, ProviderError>
266 where
267 F: Fn(EvmProviderType) -> Fut,
268 Fut: std::future::Future<Output = Result<T, ProviderError>>,
269 {
270 tracing::debug!(
273 "Starting RPC operation '{}' with timeout: {}s",
274 operation_name,
275 self.timeout_seconds
276 );
277
278 retry_rpc_call(
279 &self.selector,
280 operation_name,
281 is_retriable_error,
282 should_mark_provider_failed,
283 |url| match self.initialize_provider(url) {
284 Ok(provider) => Ok(provider),
285 Err(e) => Err(e),
286 },
287 operation,
288 Some(self.retry_config.clone()),
289 )
290 .await
291 }
292}
293
294impl AsRef<EvmProvider> for EvmProvider {
295 fn as_ref(&self) -> &EvmProvider {
296 self
297 }
298}
299
300#[async_trait]
301impl EvmProviderTrait for EvmProvider {
302 fn get_configs(&self) -> Vec<RpcConfig> {
303 self.get_configs()
304 }
305
306 async fn get_balance(&self, address: &str) -> Result<U256, ProviderError> {
307 let parsed_address = address
308 .parse::<alloy::primitives::Address>()
309 .map_err(|e| ProviderError::InvalidAddress(e.to_string()))?;
310
311 self.retry_rpc_call("get_balance", move |provider| async move {
312 provider
313 .get_balance(parsed_address)
314 .await
315 .map_err(ProviderError::from)
316 })
317 .await
318 }
319
320 async fn get_block_number(&self) -> Result<u64, ProviderError> {
321 self.retry_rpc_call("get_block_number", |provider| async move {
322 provider
323 .get_block_number()
324 .await
325 .map_err(ProviderError::from)
326 })
327 .await
328 }
329
330 async fn estimate_gas(&self, tx: &EvmTransactionData) -> Result<u64, ProviderError> {
331 let transaction_request = TransactionRequest::try_from(tx)
332 .map_err(|e| ProviderError::Other(format!("Failed to convert transaction: {e}")))?;
333
334 self.retry_rpc_call("estimate_gas", move |provider| {
335 let tx_req = transaction_request.clone();
336 async move {
337 provider
338 .estimate_gas(tx_req.into())
339 .await
340 .map_err(ProviderError::from)
341 }
342 })
343 .await
344 }
345
346 async fn get_gas_price(&self) -> Result<u128, ProviderError> {
347 self.retry_rpc_call("get_gas_price", |provider| async move {
348 provider.get_gas_price().await.map_err(ProviderError::from)
349 })
350 .await
351 }
352
353 async fn send_transaction(&self, tx: TransactionRequest) -> Result<String, ProviderError> {
354 let pending_tx = self
355 .retry_rpc_call("send_transaction", move |provider| {
356 let tx_req = tx.clone();
357 async move {
358 provider
359 .send_transaction(tx_req.into())
360 .await
361 .map_err(ProviderError::from)
362 }
363 })
364 .await?;
365
366 let tx_hash = pending_tx.tx_hash().to_string();
367 Ok(tx_hash)
368 }
369
370 async fn send_raw_transaction(&self, tx: &[u8]) -> Result<String, ProviderError> {
371 let pending_tx = self
372 .retry_rpc_call("send_raw_transaction", move |provider| {
373 let tx_data = tx.to_vec();
374 async move {
375 provider
376 .send_raw_transaction(&tx_data)
377 .await
378 .map_err(ProviderError::from)
379 }
380 })
381 .await?;
382
383 let tx_hash = pending_tx.tx_hash().to_string();
384 Ok(tx_hash)
385 }
386
387 async fn health_check(&self) -> Result<bool, ProviderError> {
388 match self.get_block_number().await {
389 Ok(_) => Ok(true),
390 Err(e) => Err(e),
391 }
392 }
393
394 async fn get_transaction_count(&self, address: &str) -> Result<u64, ProviderError> {
395 let parsed_address = address
396 .parse::<alloy::primitives::Address>()
397 .map_err(|e| ProviderError::InvalidAddress(e.to_string()))?;
398
399 self.retry_rpc_call("get_transaction_count", move |provider| async move {
400 provider
401 .get_transaction_count(parsed_address)
402 .await
403 .map_err(ProviderError::from)
404 })
405 .await
406 }
407
408 async fn get_fee_history(
409 &self,
410 block_count: u64,
411 newest_block: BlockNumberOrTag,
412 reward_percentiles: Vec<f64>,
413 ) -> Result<FeeHistory, ProviderError> {
414 self.retry_rpc_call("get_fee_history", move |provider| {
415 let reward_percentiles_clone = reward_percentiles.clone();
416 async move {
417 provider
418 .get_fee_history(block_count, newest_block, &reward_percentiles_clone)
419 .await
420 .map_err(ProviderError::from)
421 }
422 })
423 .await
424 }
425
426 async fn get_block_by_number(&self) -> Result<BlockResponse, ProviderError> {
427 let block_result = self
428 .retry_rpc_call("get_block_by_number", |provider| async move {
429 provider
430 .get_block_by_number(BlockNumberOrTag::Latest)
431 .await
432 .map_err(ProviderError::from)
433 })
434 .await?;
435
436 match block_result {
437 Some(block) => Ok(block),
438 None => Err(ProviderError::Other("Block not found".to_string())),
439 }
440 }
441
442 async fn get_transaction_receipt(
443 &self,
444 tx_hash: &str,
445 ) -> Result<Option<TransactionReceipt>, ProviderError> {
446 let parsed_tx_hash = tx_hash
447 .parse::<alloy::primitives::TxHash>()
448 .map_err(|e| ProviderError::Other(format!("Invalid transaction hash: {e}")))?;
449
450 self.retry_rpc_call("get_transaction_receipt", move |provider| async move {
451 provider
452 .get_transaction_receipt(parsed_tx_hash)
453 .await
454 .map_err(ProviderError::from)
455 })
456 .await
457 }
458
459 async fn call_contract(&self, tx: &TransactionRequest) -> Result<Bytes, ProviderError> {
460 self.retry_rpc_call("call_contract", move |provider| {
461 let tx_req = tx.clone();
462 async move {
463 provider
464 .call(tx_req.into())
465 .await
466 .map_err(ProviderError::from)
467 }
468 })
469 .await
470 }
471
472 async fn raw_request_dyn(
473 &self,
474 method: &str,
475 params: serde_json::Value,
476 ) -> Result<serde_json::Value, ProviderError> {
477 self.retry_rpc_call("raw_request_dyn", move |provider| {
478 let params_clone = params.clone();
479 async move {
480 let params_raw = serde_json::value::to_raw_value(¶ms_clone).map_err(|e| {
482 ProviderError::Other(format!("Failed to serialize params: {e}"))
483 })?;
484
485 let result = provider
486 .raw_request_dyn(std::borrow::Cow::Owned(method.to_string()), ¶ms_raw)
487 .await
488 .map_err(ProviderError::from)?;
489
490 serde_json::from_str(result.get())
492 .map_err(|e| ProviderError::Other(format!("Failed to deserialize result: {e}")))
493 }
494 })
495 .await
496 }
497}
498
499impl TryFrom<&EvmTransactionData> for TransactionRequest {
500 type Error = TransactionError;
501 fn try_from(tx: &EvmTransactionData) -> Result<Self, Self::Error> {
502 Ok(TransactionRequest {
503 from: Some(tx.from.clone().parse().map_err(|_| {
504 TransactionError::InvalidType("Invalid address format".to_string())
505 })?),
506 to: Some(TxKind::Call(
507 tx.to
508 .clone()
509 .unwrap_or("".to_string())
510 .parse()
511 .map_err(|_| {
512 TransactionError::InvalidType("Invalid address format".to_string())
513 })?,
514 )),
515 gas_price: tx
516 .gas_price
517 .map(|gp| {
518 Uint::<256, 4>::from(gp)
519 .try_into()
520 .map_err(|_| TransactionError::InvalidType("Invalid gas price".to_string()))
521 })
522 .transpose()?,
523 value: Some(Uint::<256, 4>::from(tx.value)),
524 input: TransactionInput::from(tx.data_to_bytes()?),
525 nonce: tx
526 .nonce
527 .map(|n| {
528 Uint::<256, 4>::from(n)
529 .try_into()
530 .map_err(|_| TransactionError::InvalidType("Invalid nonce".to_string()))
531 })
532 .transpose()?,
533 chain_id: Some(tx.chain_id),
534 max_fee_per_gas: tx
535 .max_fee_per_gas
536 .map(|mfpg| {
537 Uint::<256, 4>::from(mfpg).try_into().map_err(|_| {
538 TransactionError::InvalidType("Invalid max fee per gas".to_string())
539 })
540 })
541 .transpose()?,
542 max_priority_fee_per_gas: tx
543 .max_priority_fee_per_gas
544 .map(|mpfpg| {
545 Uint::<256, 4>::from(mpfpg).try_into().map_err(|_| {
546 TransactionError::InvalidType(
547 "Invalid max priority fee per gas".to_string(),
548 )
549 })
550 })
551 .transpose()?,
552 ..Default::default()
553 })
554 }
555}
556
557#[cfg(test)]
558mod tests {
559 use super::*;
560 use alloy::primitives::Address;
561 use futures::FutureExt;
562 use lazy_static::lazy_static;
563 use std::str::FromStr;
564 use std::sync::Mutex;
565
566 lazy_static! {
567 static ref EVM_TEST_ENV_MUTEX: Mutex<()> = Mutex::new(());
568 }
569
570 struct EvmTestEnvGuard {
571 _mutex_guard: std::sync::MutexGuard<'static, ()>,
572 }
573
574 impl EvmTestEnvGuard {
575 fn new(mutex_guard: std::sync::MutexGuard<'static, ()>) -> Self {
576 std::env::set_var(
577 "API_KEY",
578 "test_api_key_for_evm_provider_new_this_is_long_enough_32_chars",
579 );
580 std::env::set_var("REDIS_URL", "redis://test-dummy-url-for-evm-provider");
581
582 Self {
583 _mutex_guard: mutex_guard,
584 }
585 }
586 }
587
588 impl Drop for EvmTestEnvGuard {
589 fn drop(&mut self) {
590 std::env::remove_var("API_KEY");
591 std::env::remove_var("REDIS_URL");
592 }
593 }
594
595 fn setup_test_env() -> EvmTestEnvGuard {
597 let guard = EVM_TEST_ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
598 EvmTestEnvGuard::new(guard)
599 }
600
601 #[tokio::test]
602 async fn test_reqwest_error_conversion() {
603 let client = reqwest::Client::new();
605 let result = client
606 .get("https://www.openzeppelin.com/")
607 .timeout(Duration::from_millis(1))
608 .send()
609 .await;
610
611 assert!(
612 result.is_err(),
613 "Expected the send operation to result in an error."
614 );
615 let err = result.unwrap_err();
616
617 assert!(
618 err.is_timeout(),
619 "The reqwest error should be a timeout. Actual error: {err:?}"
620 );
621
622 let provider_error = ProviderError::from(err);
623 assert!(
624 matches!(provider_error, ProviderError::Timeout),
625 "ProviderError should be Timeout. Actual: {provider_error:?}"
626 );
627 }
628
629 #[test]
630 fn test_address_parse_error_conversion() {
631 let err = "invalid-address".parse::<Address>().unwrap_err();
633 let provider_error = ProviderError::InvalidAddress(err.to_string());
635 assert!(matches!(provider_error, ProviderError::InvalidAddress(_)));
636 }
637
638 #[test]
639 fn test_new_provider() {
640 let _env_guard = setup_test_env();
641
642 let config = ProviderConfig::new(
643 vec![RpcConfig::new("http://localhost:8545".to_string())],
644 30,
645 3,
646 60,
647 60,
648 );
649 let provider = EvmProvider::new(config);
650 assert!(provider.is_ok());
651
652 let config = ProviderConfig::new(
654 vec![RpcConfig::new("invalid-url".to_string())],
655 30,
656 3,
657 60,
658 60,
659 );
660 let provider = EvmProvider::new(config);
661 assert!(provider.is_err());
662 }
663
664 #[test]
665 fn test_new_provider_with_timeout() {
666 let _env_guard = setup_test_env();
667
668 let config = ProviderConfig::new(
670 vec![RpcConfig::new("http://localhost:8545".to_string())],
671 30,
672 3,
673 60,
674 60,
675 );
676 let provider = EvmProvider::new(config);
677 assert!(provider.is_ok());
678
679 let config = ProviderConfig::new(
681 vec![RpcConfig::new("invalid-url".to_string())],
682 30,
683 3,
684 60,
685 60,
686 );
687 let provider = EvmProvider::new(config);
688 assert!(provider.is_err());
689
690 let config = ProviderConfig::new(
692 vec![RpcConfig::new("http://localhost:8545".to_string())],
693 0,
694 3,
695 60,
696 60,
697 );
698 let provider = EvmProvider::new(config);
699 assert!(provider.is_ok());
700
701 let config = ProviderConfig::new(
703 vec![RpcConfig::new("http://localhost:8545".to_string())],
704 3600,
705 3,
706 60,
707 60,
708 );
709 let provider = EvmProvider::new(config);
710 assert!(provider.is_ok());
711 }
712
713 #[test]
714 fn test_transaction_request_conversion() {
715 let tx_data = EvmTransactionData {
716 from: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string(),
717 to: Some("0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string()),
718 gas_price: Some(1000000000),
719 value: Uint::<256, 4>::from(1000000000),
720 data: Some("0x".to_string()),
721 nonce: Some(1),
722 chain_id: 1,
723 gas_limit: Some(21000),
724 hash: None,
725 signature: None,
726 speed: None,
727 max_fee_per_gas: None,
728 max_priority_fee_per_gas: None,
729 raw: None,
730 };
731
732 let result = TransactionRequest::try_from(&tx_data);
733 assert!(result.is_ok());
734
735 let tx_request = result.unwrap();
736 assert_eq!(
737 tx_request.from,
738 Some(Address::from_str("0x742d35Cc6634C0532925a3b844Bc454e4438f44e").unwrap())
739 );
740 assert_eq!(tx_request.chain_id, Some(1));
741 }
742
743 #[tokio::test]
744 async fn test_mock_provider_methods() {
745 let mut mock = MockEvmProviderTrait::new();
746
747 mock.expect_get_balance()
748 .with(mockall::predicate::eq(
749 "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
750 ))
751 .times(1)
752 .returning(|_| async { Ok(U256::from(100)) }.boxed());
753
754 mock.expect_get_block_number()
755 .times(1)
756 .returning(|| async { Ok(12345) }.boxed());
757
758 mock.expect_get_gas_price()
759 .times(1)
760 .returning(|| async { Ok(20000000000) }.boxed());
761
762 mock.expect_health_check()
763 .times(1)
764 .returning(|| async { Ok(true) }.boxed());
765
766 mock.expect_get_transaction_count()
767 .with(mockall::predicate::eq(
768 "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
769 ))
770 .times(1)
771 .returning(|_| async { Ok(42) }.boxed());
772
773 mock.expect_get_fee_history()
774 .with(
775 mockall::predicate::eq(10u64),
776 mockall::predicate::eq(BlockNumberOrTag::Latest),
777 mockall::predicate::eq(vec![25.0, 50.0, 75.0]),
778 )
779 .times(1)
780 .returning(|_, _, _| {
781 async {
782 Ok(FeeHistory {
783 oldest_block: 100,
784 base_fee_per_gas: vec![1000],
785 gas_used_ratio: vec![0.5],
786 reward: Some(vec![vec![500]]),
787 base_fee_per_blob_gas: vec![1000],
788 blob_gas_used_ratio: vec![0.5],
789 })
790 }
791 .boxed()
792 });
793
794 let balance = mock
796 .get_balance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")
797 .await;
798 assert!(balance.is_ok());
799 assert_eq!(balance.unwrap(), U256::from(100));
800
801 let block_number = mock.get_block_number().await;
802 assert!(block_number.is_ok());
803 assert_eq!(block_number.unwrap(), 12345);
804
805 let gas_price = mock.get_gas_price().await;
806 assert!(gas_price.is_ok());
807 assert_eq!(gas_price.unwrap(), 20000000000);
808
809 let health = mock.health_check().await;
810 assert!(health.is_ok());
811 assert!(health.unwrap());
812
813 let count = mock
814 .get_transaction_count("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")
815 .await;
816 assert!(count.is_ok());
817 assert_eq!(count.unwrap(), 42);
818
819 let fee_history = mock
820 .get_fee_history(10, BlockNumberOrTag::Latest, vec![25.0, 50.0, 75.0])
821 .await;
822 assert!(fee_history.is_ok());
823 let fee_history = fee_history.unwrap();
824 assert_eq!(fee_history.oldest_block, 100);
825 assert_eq!(fee_history.gas_used_ratio, vec![0.5]);
826 }
827
828 #[tokio::test]
829 async fn test_mock_transaction_operations() {
830 let mut mock = MockEvmProviderTrait::new();
831
832 let tx_data = EvmTransactionData {
834 from: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string(),
835 to: Some("0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string()),
836 gas_price: Some(1000000000),
837 value: Uint::<256, 4>::from(1000000000),
838 data: Some("0x".to_string()),
839 nonce: Some(1),
840 chain_id: 1,
841 gas_limit: Some(21000),
842 hash: None,
843 signature: None,
844 speed: None,
845 max_fee_per_gas: None,
846 max_priority_fee_per_gas: None,
847 raw: None,
848 };
849
850 mock.expect_estimate_gas()
851 .with(mockall::predicate::always())
852 .times(1)
853 .returning(|_| async { Ok(21000) }.boxed());
854
855 mock.expect_send_raw_transaction()
857 .with(mockall::predicate::always())
858 .times(1)
859 .returning(|_| async { Ok("0x123456789abcdef".to_string()) }.boxed());
860
861 let gas_estimate = mock.estimate_gas(&tx_data).await;
863 assert!(gas_estimate.is_ok());
864 assert_eq!(gas_estimate.unwrap(), 21000);
865
866 let tx_hash = mock.send_raw_transaction(&[0u8; 32]).await;
867 assert!(tx_hash.is_ok());
868 assert_eq!(tx_hash.unwrap(), "0x123456789abcdef");
869 }
870
871 #[test]
872 fn test_invalid_transaction_request_conversion() {
873 let tx_data = EvmTransactionData {
874 from: "invalid-address".to_string(),
875 to: Some("0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string()),
876 gas_price: Some(1000000000),
877 value: Uint::<256, 4>::from(1000000000),
878 data: Some("0x".to_string()),
879 nonce: Some(1),
880 chain_id: 1,
881 gas_limit: Some(21000),
882 hash: None,
883 signature: None,
884 speed: None,
885 max_fee_per_gas: None,
886 max_priority_fee_per_gas: None,
887 raw: None,
888 };
889
890 let result = TransactionRequest::try_from(&tx_data);
891 assert!(result.is_err());
892 }
893
894 #[tokio::test]
895 async fn test_mock_additional_methods() {
896 let mut mock = MockEvmProviderTrait::new();
897
898 mock.expect_health_check()
900 .times(1)
901 .returning(|| async { Ok(true) }.boxed());
902
903 mock.expect_get_transaction_count()
905 .with(mockall::predicate::eq(
906 "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
907 ))
908 .times(1)
909 .returning(|_| async { Ok(42) }.boxed());
910
911 mock.expect_get_fee_history()
913 .with(
914 mockall::predicate::eq(10u64),
915 mockall::predicate::eq(BlockNumberOrTag::Latest),
916 mockall::predicate::eq(vec![25.0, 50.0, 75.0]),
917 )
918 .times(1)
919 .returning(|_, _, _| {
920 async {
921 Ok(FeeHistory {
922 oldest_block: 100,
923 base_fee_per_gas: vec![1000],
924 gas_used_ratio: vec![0.5],
925 reward: Some(vec![vec![500]]),
926 base_fee_per_blob_gas: vec![1000],
927 blob_gas_used_ratio: vec![0.5],
928 })
929 }
930 .boxed()
931 });
932
933 let health = mock.health_check().await;
935 assert!(health.is_ok());
936 assert!(health.unwrap());
937
938 let count = mock
940 .get_transaction_count("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")
941 .await;
942 assert!(count.is_ok());
943 assert_eq!(count.unwrap(), 42);
944
945 let fee_history = mock
947 .get_fee_history(10, BlockNumberOrTag::Latest, vec![25.0, 50.0, 75.0])
948 .await;
949 assert!(fee_history.is_ok());
950 let fee_history = fee_history.unwrap();
951 assert_eq!(fee_history.oldest_block, 100);
952 assert_eq!(fee_history.gas_used_ratio, vec![0.5]);
953 }
954
955 #[test]
956 fn test_is_retriable_error_json_rpc_retriable_codes() {
957 let retriable_codes = vec![
959 (-32002, "Resource unavailable"),
960 (-32005, "Limit exceeded"),
961 (-32603, "Internal error"),
962 ];
963
964 for (code, message) in retriable_codes {
965 let error = ProviderError::RpcErrorCode {
966 code,
967 message: message.to_string(),
968 };
969 assert!(
970 is_retriable_error(&error),
971 "Error code {code} should be retriable"
972 );
973 }
974 }
975
976 #[test]
977 fn test_is_retriable_error_json_rpc_non_retriable_codes() {
978 let non_retriable_codes = vec![
980 (-32000, "insufficient funds"),
981 (-32000, "execution reverted"),
982 (-32000, "already known"),
983 (-32000, "nonce too low"),
984 (-32000, "invalid sender"),
985 (-32001, "Resource not found"),
986 (-32003, "Transaction rejected"),
987 (-32004, "Method not supported"),
988 (-32700, "Parse error"),
989 (-32600, "Invalid request"),
990 (-32601, "Method not found"),
991 (-32602, "Invalid params"),
992 ];
993
994 for (code, message) in non_retriable_codes {
995 let error = ProviderError::RpcErrorCode {
996 code,
997 message: message.to_string(),
998 };
999 assert!(
1000 !is_retriable_error(&error),
1001 "Error code {code} with message '{message}' should NOT be retriable"
1002 );
1003 }
1004 }
1005
1006 #[test]
1007 fn test_is_retriable_error_json_rpc_32000_specific_cases() {
1008 let test_cases = vec![
1011 (
1012 "tx already exists in cache",
1013 false,
1014 "Transaction already in mempool",
1015 ),
1016 ("already known", false, "Duplicate transaction submission"),
1017 (
1018 "insufficient funds for gas * price + value",
1019 false,
1020 "User needs more funds",
1021 ),
1022 ("execution reverted", false, "Smart contract rejected"),
1023 ("nonce too low", false, "Transaction already processed"),
1024 ("invalid sender", false, "Configuration issue"),
1025 ("gas required exceeds allowance", false, "Gas limit too low"),
1026 (
1027 "replacement transaction underpriced",
1028 false,
1029 "Need higher gas price",
1030 ),
1031 ];
1032
1033 for (message, should_retry, description) in test_cases {
1034 let error = ProviderError::RpcErrorCode {
1035 code: -32000,
1036 message: message.to_string(),
1037 };
1038 assert_eq!(
1039 is_retriable_error(&error),
1040 should_retry,
1041 "{}: -32000 with '{}' should{} be retriable",
1042 description,
1043 message,
1044 if should_retry { "" } else { " NOT" }
1045 );
1046 }
1047 }
1048
1049 #[tokio::test]
1050 async fn test_call_contract() {
1051 let mut mock = MockEvmProviderTrait::new();
1052
1053 let tx = TransactionRequest {
1054 from: Some(Address::from_str("0x742d35Cc6634C0532925a3b844Bc454e4438f44e").unwrap()),
1055 to: Some(TxKind::Call(
1056 Address::from_str("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC").unwrap(),
1057 )),
1058 input: TransactionInput::from(
1059 hex::decode("a9059cbb000000000000000000000000742d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000de0b6b3a7640000").unwrap()
1060 ),
1061 ..Default::default()
1062 };
1063
1064 mock.expect_call_contract()
1066 .with(mockall::predicate::always())
1067 .times(1)
1068 .returning(|_| {
1069 async {
1070 Ok(Bytes::from(
1071 hex::decode(
1072 "0000000000000000000000000000000000000000000000000000000000000001",
1073 )
1074 .unwrap(),
1075 ))
1076 }
1077 .boxed()
1078 });
1079
1080 let result = mock.call_contract(&tx).await;
1081 assert!(result.is_ok());
1082
1083 let data = result.unwrap();
1084 assert_eq!(
1085 hex::encode(data),
1086 "0000000000000000000000000000000000000000000000000000000000000001"
1087 );
1088 }
1089}