layer_climb_config/
config.rs

1use anyhow::Result;
2use layer_climb_address::{AddrKind, Address};
3use serde::{Deserialize, Serialize};
4use std::{fmt::Display, str::FromStr};
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7pub struct ChainConfig {
8    pub chain_id: ChainId,
9    pub rpc_endpoint: Option<String>,
10    pub grpc_endpoint: Option<String>,
11    // if not specified, will fallback to `grpc_endpoint`
12    pub grpc_web_endpoint: Option<String>,
13    // not micro-units, e.g. 0.025 would be a typical value
14    pub gas_price: f32,
15    pub gas_denom: String,
16    pub address_kind: AddrKind,
17}
18
19impl ChainConfig {
20    pub fn ibc_client_revision(&self) -> Result<u64> {
21        // > Tendermint chains wishing to use revisions to maintain persistent IBC connections even across height-resetting upgrades
22        // > must format their chainIDs in the following manner: {chainID}-{revision_number}
23        // - https://github.com/cosmos/ibc-go/blob/main/docs/docs/01-ibc/01-overview.md#ibc-client-heights
24        Ok(self
25            .chain_id
26            .as_str()
27            .split("-")
28            .last()
29            .and_then(|s| s.parse::<u64>().ok())
30            .unwrap_or_default())
31    }
32
33    pub fn parse_address(&self, value: &str) -> Result<Address> {
34        self.address_kind.parse_address(value)
35    }
36
37    pub fn address_from_pub_key(&self, pub_key: &tendermint::PublicKey) -> Result<Address> {
38        self.address_kind.address_from_pub_key(pub_key)
39    }
40}
41
42#[derive(Deserialize, Serialize, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
43#[serde(transparent)]
44pub struct ChainId(String);
45impl ChainId {
46    pub fn new(id: impl ToString) -> Self {
47        Self(id.to_string())
48    }
49    pub fn as_str(&self) -> &str {
50        &self.0
51    }
52}
53
54impl FromStr for ChainId {
55    type Err = anyhow::Error;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        Ok(Self::new(s))
59    }
60}
61
62impl Display for ChainId {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(f, "{}", self.0)
65    }
66}