Skip to content

Commit 4c95d02

Browse files
authored
feat(utils): Make SlotCalculator instantiable from a chain name (#67)
Right now we have to do error-prone configs everywhere. With this, we won't. Closes ENG-1139
1 parent 743137f commit 4c95d02

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/utils/calc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::utils::from_env::FromEnv;
2+
use signet_constants::KnownChains;
3+
use std::str::FromStr;
24

35
/// A slot calculator, which can calculate slot numbers, windows, and offsets
46
/// for a given chain.
@@ -273,6 +275,22 @@ impl SlotCalculator {
273275
}
274276
}
275277

278+
impl From<KnownChains> for SlotCalculator {
279+
fn from(value: KnownChains) -> Self {
280+
match value {
281+
KnownChains::Pecorino => SlotCalculator::pecorino_host(),
282+
}
283+
}
284+
}
285+
286+
impl FromStr for SlotCalculator {
287+
type Err = signet_constants::ParseChainError;
288+
289+
fn from_str(s: &str) -> Result<Self, Self::Err> {
290+
Ok(SlotCalculator::from(KnownChains::from_str(s)?))
291+
}
292+
}
293+
276294
#[cfg(test)]
277295
mod tests {
278296
use super::*;

0 commit comments

Comments
 (0)