Skip to main content

yldfi_common/
lib.rs

1//! # yldfi-common
2//!
3//! Shared utilities for yldfi-rs API clients.
4//!
5//! ## Modules
6//!
7//! - [`retry`] - Retry utilities with exponential backoff
8//! - [`eth`] - Ethereum address and transaction hash validation
9//! - [`chains`] - EVM chain ID and name mappings
10//! - [`units`] - Wei/Gwei/Ether conversion utilities
11//!
12//! ## Retry Utilities
13//!
14//! ```no_run
15//! use yldfi_common::{with_retry, RetryConfig, RetryableError};
16//!
17//! // Implement RetryableError for your error type
18//! struct MyError;
19//! impl RetryableError for MyError {
20//!     fn is_retryable(&self) -> bool { true }
21//! }
22//!
23//! async fn example() {
24//!     let config = RetryConfig::default();
25//!     let result = with_retry(&config, || async {
26//!         Ok::<_, MyError>("success")
27//!     }).await;
28//! }
29//! ```
30//!
31//! ## Ethereum Utilities
32//!
33//! ```
34//! use yldfi_common::eth::{is_valid_address, normalize_address, is_valid_tx_hash};
35//!
36//! assert!(is_valid_address("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"));
37//! assert_eq!(
38//!     normalize_address("0xABC123..."),
39//!     None // Invalid length
40//! );
41//! ```
42//!
43//! ## Chain Mappings
44//!
45//! ```
46//! use yldfi_common::chains::Chain;
47//!
48//! let chain = Chain::from_id(1);
49//! assert_eq!(chain, Chain::Ethereum);
50//! assert_eq!(chain.name(), "ethereum");
51//! assert_eq!(chain.native_currency(), "ETH");
52//! ```
53//!
54//! ## Unit Conversions
55//!
56//! ```
57//! use yldfi_common::units::{to_wei, from_wei, parse_units, format_units};
58//!
59//! // Convert 1.5 ETH to wei
60//! let wei = to_wei("1.5", 18).unwrap();
61//! assert_eq!(wei, "1500000000000000000");
62//!
63//! // Convert wei back to ETH
64//! let eth = from_wei("1500000000000000000", 18);
65//! assert_eq!(eth, "1.5");
66//! ```
67
68pub mod api;
69pub mod chains;
70pub mod eth;
71pub mod http;
72pub mod rate_limit;
73pub mod retry;
74pub mod units;
75
76pub use retry::{with_retry, with_simple_retry, RetryConfig, RetryError, RetryableError};
77
78// Re-export HTTP utilities
79pub use http::{
80    build_client, build_client_with_proxy, build_default_client, HttpClientConfig, HttpError,
81};
82
83// Re-export commonly used eth utilities at crate root
84pub use eth::{
85    is_valid_address, is_valid_tx_hash, normalize_address, Address, AddressParseError,
86    HttpStatusKind, TxHash, TxHashParseError,
87};
88
89// Re-export Chain at crate root for convenience
90pub use chains::Chain;
91
92// Re-export API utilities
93pub use api::{
94    extract_retry_after, handle_error_response, ApiConfig, ApiError, ApiResult, BaseClient,
95    ConfigValidationError, NoDomainError, SecretApiKey,
96};
97
98// Re-export Wei amount type
99pub use units::{Wei, WeiParseError};