Skip to content

feat: impl FromStr for chain constants #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use chains::test_utils;

mod types;
pub use types::{
ConfigError, HostConstants, PairedHeights, PermissionedToken, PredeployTokens, RollupConstants,
SignetConstants, SignetEnvironmentConstants, SignetSystemConstants, MINTER_ADDRESS,
ConfigError, HostConstants, KnownChains, PairedHeights, ParseChainError, PermissionedToken,
PredeployTokens, RollupConstants, SignetConstants, SignetEnvironmentConstants,
SignetSystemConstants, MINTER_ADDRESS,
};
36 changes: 36 additions & 0 deletions crates/constants/src/types/chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::str::FromStr;

/// The list of known chains as a string.
const KNOWN_CHAINS: &str = "pecorino, test";

/// Error type for parsing struct from a chain name.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ParseChainError {
/// The chain name is not supported.
#[error("chain name {0} is not parseable. supported chains: {KNOWN_CHAINS}")]
ChainNotSupported(String),
}

/// Known chains for the Signet system.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum KnownChains {
/// Pecorino chain.
Pecorino,
/// Test chain.
#[cfg(any(test, feature = "test-utils"))]
Test,
}

impl FromStr for KnownChains {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim().to_lowercase();
match s.as_str() {
"pecorino" => Ok(Self::Pecorino),
#[cfg(any(test, feature = "test-utils"))]
"test" => Ok(Self::Test),
_ => Err(ParseChainError::ChainNotSupported(s)),
}
}
}
16 changes: 15 additions & 1 deletion crates/constants/src/types/environment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use crate::{KnownChains, ParseChainError};
use std::{borrow::Cow, str::FromStr};

/// Signet Environment constants.
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -48,6 +49,19 @@ impl SignetEnvironmentConstants {
}
}

impl FromStr for SignetEnvironmentConstants {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
16 changes: 15 additions & 1 deletion crates/constants/src/types/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::types::{ConfigError, PredeployTokens};
use crate::types::{ConfigError, KnownChains, ParseChainError, PredeployTokens};
use alloy::{genesis::Genesis, primitives::Address};
use serde_json::Value;
use std::str::FromStr;

/// System addresses and other configuration details on the host chain.
///
Expand Down Expand Up @@ -124,3 +125,16 @@ impl HostConstants {
self.tokens
}
}

impl FromStr for HostConstants {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}
30 changes: 30 additions & 0 deletions crates/constants/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use host::HostConstants;
mod rollup;
pub use rollup::{RollupConstants, MINTER_ADDRESS};

mod chains;
pub use chains::{KnownChains, ParseChainError};

mod tokens;
pub use tokens::{PermissionedToken, PredeployTokens};

Expand All @@ -20,6 +23,7 @@ use alloy::{
genesis::Genesis,
primitives::{Address, U256},
};
use std::str::FromStr;

/// Signet constants.
///
Expand Down Expand Up @@ -213,6 +217,19 @@ impl SignetSystemConstants {
}
}

impl FromStr for SignetSystemConstants {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}

/// All constants pertaining to the Signet system.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignetConstants {
Expand Down Expand Up @@ -262,3 +279,16 @@ impl SignetConstants {
&self.environment
}
}

impl FromStr for SignetConstants {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}
16 changes: 15 additions & 1 deletion crates/constants/src/types/rollup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::types::{ConfigError, PredeployTokens};
use crate::types::{ConfigError, KnownChains, ParseChainError, PredeployTokens};
use alloy::{
genesis::Genesis,
primitives::{address, Address},
};
use serde_json::Value;
use std::str::FromStr;

/// System address with permission to mint tokens. This is the address from
/// which the node will issue transactions to mint ETH or ERC20 tokens.
Expand Down Expand Up @@ -105,3 +106,16 @@ impl RollupConstants {
MINTER_ADDRESS
}
}

impl FromStr for RollupConstants {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}
19 changes: 18 additions & 1 deletion crates/extract/src/test_utils/host_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ use reth::{
},
providers::{Chain, ExecutionOutcome},
};
use signet_types::{constants::SignetSystemConstants, AggregateFills};
use signet_types::{
constants::{KnownChains, ParseChainError, SignetSystemConstants},
AggregateFills,
};
use signet_zenith::{Passage, RollupOrders, Transactor};
use std::{
borrow::Borrow,
str::FromStr,
sync::atomic::{AtomicU64, Ordering},
};

Expand Down Expand Up @@ -406,6 +410,19 @@ impl HostBlockSpec {
}
}

impl FromStr for HostBlockSpec {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}

fn to_receipt<T>(address: Address, t: &T) -> Receipt
where
for<'a> &'a T: Into<LogData>,
Expand Down
16 changes: 15 additions & 1 deletion crates/extract/src/test_utils/ru_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use alloy::{
signers::local::PrivateKeySigner,
};
use reth::primitives::TransactionSigned;
use signet_types::constants::SignetSystemConstants;
use signet_types::constants::{KnownChains, ParseChainError, SignetSystemConstants};
use signet_zenith::Zenith::{self};
use std::str::FromStr;

/// A block spec for the Ru chain.
///
Expand Down Expand Up @@ -144,3 +145,16 @@ impl RuBlockSpec {
}
}
}

impl FromStr for RuBlockSpec {
type Err = ParseChainError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let chain: KnownChains = s.parse()?;
match chain {
KnownChains::Pecorino => Ok(Self::pecorino()),
#[cfg(any(test, feature = "test-utils"))]
KnownChains::Test => Ok(Self::test()),
}
}
}