Skip to content

Commit 999cc1b

Browse files
DaniPopesgrandizzy
andauthored
chore(test-utils): simplify next calls (#9361)
Co-authored-by: grandizzy <[email protected]>
1 parent 3a95440 commit 999cc1b

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

crates/test-utils/src/rpc.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ static ETHERSCAN_OPTIMISM_KEYS: LazyLock<Vec<&'static str>> =
9191
LazyLock::new(|| vec!["JQNGFHINKS1W7Y5FRXU4SPBYF43J3NYK46"]);
9292

9393
/// Returns the next index to use.
94-
fn next() -> usize {
94+
fn next_idx() -> usize {
9595
static NEXT_INDEX: AtomicUsize = AtomicUsize::new(0);
9696
NEXT_INDEX.fetch_add(1, Ordering::SeqCst)
9797
}
9898

99-
fn num_keys() -> usize {
100-
INFURA_KEYS.len() + ALCHEMY_KEYS.len()
99+
/// Returns the next item in the list to use.
100+
fn next<T>(list: &[T]) -> &T {
101+
&list[next_idx() % list.len()]
101102
}
102103

103104
/// Returns the next _mainnet_ rpc endpoint in inline
@@ -142,21 +143,17 @@ fn next_archive_endpoint(is_ws: bool) -> String {
142143
let rpc_env_vars = env::var(env_urls).unwrap_or_default();
143144
if !rpc_env_vars.is_empty() {
144145
let urls = rpc_env_vars.split(',').collect::<Vec<&str>>();
145-
let idx = next() % urls.len();
146-
urls[idx].to_string()
146+
next(&urls).to_string()
147147
} else if is_ws {
148-
let idx = next() % ALCHEMY_KEYS.len();
149-
format!("wss://eth-mainnet.g.alchemy.com/v2/{}", ALCHEMY_KEYS[idx])
148+
format!("wss://eth-mainnet.g.alchemy.com/v2/{}", next(&ALCHEMY_KEYS))
150149
} else {
151-
let idx = next() % ALCHEMY_KEYS.len();
152-
format!("https://eth-mainnet.g.alchemy.com/v2/{}", ALCHEMY_KEYS[idx])
150+
format!("https://eth-mainnet.g.alchemy.com/v2/{}", next(&ALCHEMY_KEYS))
153151
}
154152
}
155153

156154
/// Returns the next etherscan api key
157155
pub fn next_mainnet_etherscan_api_key() -> String {
158-
let idx = next() % ETHERSCAN_MAINNET_KEYS.len();
159-
ETHERSCAN_MAINNET_KEYS[idx].to_string()
156+
next_etherscan_api_key(NamedChain::Mainnet)
160157
}
161158

162159
/// Returns the next etherscan api key for given chain.
@@ -165,8 +162,7 @@ pub fn next_etherscan_api_key(chain: NamedChain) -> String {
165162
Optimism => &ETHERSCAN_OPTIMISM_KEYS,
166163
_ => &ETHERSCAN_MAINNET_KEYS,
167164
};
168-
let idx = next() % keys.len();
169-
keys[idx].to_string()
165+
next(keys).to_string()
170166
}
171167

172168
fn next_url(is_ws: bool, chain: NamedChain) -> String {
@@ -176,7 +172,7 @@ fn next_url(is_ws: bool, chain: NamedChain) -> String {
176172
return "https://mainnet.base.org".to_string();
177173
}
178174

179-
let idx = next() % num_keys();
175+
let idx = next_idx() % (INFURA_KEYS.len() + ALCHEMY_KEYS.len());
180176
let is_infura = idx < INFURA_KEYS.len();
181177

182178
let key = if is_infura { INFURA_KEYS[idx] } else { ALCHEMY_KEYS[idx - INFURA_KEYS.len()] };

0 commit comments

Comments
 (0)