Skip to main content

krusty_kms_common/
network.rs

1//! Network preset configuration for Starknet chains.
2
3use crate::address::Address;
4use crate::chain::ChainId;
5
6/// A preset configuration for a Starknet network.
7#[derive(Debug, Clone)]
8pub struct NetworkPreset {
9    pub chain_id: ChainId,
10    pub rpc_url: String,
11    pub explorer_base_url: String,
12    pub name: String,
13}
14
15impl NetworkPreset {
16    /// Starknet Mainnet defaults.
17    pub fn mainnet() -> Self {
18        Self {
19            chain_id: ChainId::Mainnet,
20            rpc_url: "https://starknet-mainnet.public.blastapi.io/rpc/v0_7".into(),
21            explorer_base_url: "https://voyager.online".into(),
22            name: "Starknet Mainnet".into(),
23        }
24    }
25
26    /// Starknet Sepolia testnet defaults.
27    pub fn sepolia() -> Self {
28        Self {
29            chain_id: ChainId::Sepolia,
30            rpc_url: "https://starknet-sepolia.public.blastapi.io/rpc/v0_7".into(),
31            explorer_base_url: "https://sepolia.voyager.online".into(),
32            name: "Starknet Sepolia".into(),
33        }
34    }
35
36    /// Local devnet defaults.
37    pub fn devnet() -> Self {
38        Self {
39            chain_id: ChainId::Sepolia,
40            rpc_url: "http://127.0.0.1:5050".into(),
41            explorer_base_url: "http://localhost:3000".into(),
42            name: "Devnet".into(),
43        }
44    }
45
46    /// Build an explorer URL for a transaction hash.
47    pub fn explorer_tx_url(&self, hash: &str) -> String {
48        format!("{}/tx/{}", self.explorer_base_url, hash)
49    }
50
51    /// Build an explorer URL for a contract address.
52    pub fn explorer_contract_url(&self, address: &Address) -> String {
53        format!("{}/contract/{}", self.explorer_base_url, address)
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_mainnet_preset() {
63        let net = NetworkPreset::mainnet();
64        assert_eq!(net.chain_id, ChainId::Mainnet);
65        assert!(net.rpc_url.contains("mainnet"));
66    }
67
68    #[test]
69    fn test_sepolia_preset() {
70        let net = NetworkPreset::sepolia();
71        assert_eq!(net.chain_id, ChainId::Sepolia);
72        assert!(net.rpc_url.contains("sepolia"));
73    }
74
75    #[test]
76    fn test_explorer_tx_url() {
77        let net = NetworkPreset::mainnet();
78        let url = net.explorer_tx_url("0xabc");
79        assert_eq!(url, "https://voyager.online/tx/0xabc");
80    }
81
82    #[test]
83    fn test_explorer_contract_url() {
84        let net = NetworkPreset::mainnet();
85        let addr = Address::from_hex("0x123").unwrap();
86        let url = net.explorer_contract_url(&addr);
87        assert!(url.starts_with("https://voyager.online/contract/"));
88    }
89}