Skip to main content

qorechain/
networks.rs

1//! Built-in network presets for the QoreChain Rust SDK.
2//!
3//! Both the `testnet` and `mainnet` presets are fully populated and live; their
4//! endpoints default to localhost ports so the SDK works out of the box against
5//! a locally running node, and callers can override them with real hostnames.
6//! [`get_network`] returns either preset.
7
8use crate::error::{Error, Result};
9
10/// bech32 human-readable prefixes used across QoreChain address types.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Bech32Prefixes {
13    /// Account address prefix (e.g. `qor`).
14    pub account: String,
15    /// Validator operator address prefix (e.g. `qorvaloper`).
16    pub validator: String,
17    /// Validator consensus address prefix (e.g. `qorvalcons`).
18    pub consensus: String,
19}
20
21/// Display and base denomination metadata for the network's staking coin.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct CoinInfo {
24    /// Display denom (e.g. `QOR`).
25    pub display: String,
26    /// Base denom (e.g. `uqor`).
27    pub base: String,
28    /// Decimal exponent (1 display = 10^exponent base).
29    pub exponent: u32,
30}
31
32/// Service endpoints for talking to a network across its supported VMs.
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct Endpoints {
35    /// Cosmos REST (LCD) endpoint.
36    pub rest: String,
37    /// Cosmos gRPC endpoint.
38    pub grpc: String,
39    /// Consensus RPC endpoint.
40    pub rpc: String,
41    /// EVM JSON-RPC endpoint.
42    pub evm_rpc: String,
43    /// EVM WebSocket endpoint.
44    pub evm_ws: String,
45    /// SVM JSON-RPC endpoint.
46    pub svm_rpc: String,
47}
48
49/// A fully described network preset.
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct NetworkConfig {
52    /// Preset name.
53    pub name: String,
54    /// Whether the network is live.
55    pub live: bool,
56    /// Chain ID.
57    pub chain_id: Option<String>,
58    /// bech32 prefixes.
59    pub bech32: Bech32Prefixes,
60    /// Staking coin metadata.
61    pub coin: CoinInfo,
62    /// Service endpoints.
63    pub endpoints: Option<Endpoints>,
64}
65
66fn bech32_prefixes() -> Bech32Prefixes {
67    Bech32Prefixes {
68        account: "qor".into(),
69        validator: "qorvaloper".into(),
70        consensus: "qorvalcons".into(),
71    }
72}
73
74fn coin_info() -> CoinInfo {
75    CoinInfo {
76        display: "QOR".into(),
77        base: "uqor".into(),
78        exponent: 6,
79    }
80}
81
82/// Returns the built-in network presets in stable order: `[testnet, mainnet]`.
83pub fn networks() -> Vec<NetworkConfig> {
84    vec![
85        NetworkConfig {
86            name: "testnet".into(),
87            live: true,
88            chain_id: Some("qorechain-diana".into()),
89            bech32: bech32_prefixes(),
90            coin: coin_info(),
91            endpoints: Some(Endpoints {
92                rest: "http://localhost:1317".into(),
93                grpc: "http://localhost:9090".into(),
94                rpc: "http://localhost:26657".into(),
95                evm_rpc: "http://localhost:8545".into(),
96                evm_ws: "ws://localhost:8546".into(),
97                svm_rpc: "http://localhost:8899".into(),
98            }),
99        },
100        NetworkConfig {
101            name: "mainnet".into(),
102            live: true,
103            chain_id: Some("qorechain-vladi".into()),
104            bech32: bech32_prefixes(),
105            coin: coin_info(),
106            endpoints: Some(Endpoints {
107                rest: "http://localhost:1317".into(),
108                grpc: "http://localhost:9090".into(),
109                rpc: "http://localhost:26657".into(),
110                evm_rpc: "http://localhost:8545".into(),
111                evm_ws: "ws://localhost:8546".into(),
112                svm_rpc: "http://localhost:8899".into(),
113            }),
114        },
115    ]
116}
117
118/// Resolves a network preset by name.
119///
120/// Returns an error if the named network is unknown.
121pub fn get_network(name: &str) -> Result<NetworkConfig> {
122    networks()
123        .into_iter()
124        .find(|n| n.name == name)
125        .ok_or_else(|| Error::UnknownNetwork(name.to_string()))
126}
127
128/// Lists the known network preset names without any liveness check.
129pub fn list_networks() -> Vec<String> {
130    vec!["testnet".into(), "mainnet".into()]
131}