1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use client_crab::client::CrabClient;
use client_crab::component::CrabClientComponent;
use client_crab_parachain::client::CrabParachainClient;
use client_crab_parachain::component::CrabParachainClientComponent;
use client_kusama::client::KusamaClient;
use client_kusama::component::KusamaClientComponent;
use serde::{Deserialize, Serialize};
use subquery::types::BridgeName;
use subquery::{Subquery, SubqueryComponent, SubqueryConfig};

use support_common::error::BridgerError;

use crate::types::HexLaneId;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BridgeConfig {
    pub crab: ChainInfoConfig,
    pub kusama: ChainInfoConfig,
    pub crab_parachain: ChainInfoConfig,
    pub relay: RelayConfig,
    pub index: IndexConfig,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RelayConfig {
    /// Hex-encoded lane identifiers that should be served by the complex relay.
    pub lanes: Vec<HexLaneId>,
    pub para_id: u32,
    #[serde(default)]
    pub enable_mandatory: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ChainInfoConfig {
    /// Endpoint
    pub endpoint: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signer: Option<String>,
}

impl TryFrom<ChainInfoConfig> for client_crab::config::ClientConfig {
    type Error = BridgerError;

    fn try_from(config: ChainInfoConfig) -> Result<Self, Self::Error> {
        let relayer_private_key = config.signer.ok_or_else(|| {
            BridgerError::Custom(format!("Missing signer for chain: {}", config.endpoint))
        })?;
        Ok(client_crab::config::ClientConfig {
            endpoint: config.endpoint,
            relayer_private_key,
            relayer_real_account: None,
        })
    }
}

impl TryFrom<ChainInfoConfig> for client_crab_parachain::config::ClientConfig {
    type Error = BridgerError;

    fn try_from(config: ChainInfoConfig) -> Result<Self, Self::Error> {
        let relayer_private_key = config.signer.ok_or_else(|| {
            BridgerError::Custom(format!("Missing signer for chain: {}", config.endpoint))
        })?;
        Ok(client_crab_parachain::config::ClientConfig {
            endpoint: config.endpoint,
            relayer_private_key,
            relayer_real_account: None,
        })
    }
}

impl TryFrom<ChainInfoConfig> for client_kusama::config::ClientConfig {
    type Error = BridgerError;

    fn try_from(config: ChainInfoConfig) -> Result<Self, Self::Error> {
        let relayer_private_key = "//Alice".to_string();
        Ok(client_kusama::config::ClientConfig {
            endpoint: config.endpoint,
            relayer_private_key,
            relayer_real_account: None,
        })
    }
}

impl ChainInfoConfig {
    pub async fn to_crab_client(&self) -> color_eyre::Result<CrabClient> {
        let config = self.clone().try_into()?;
        Ok(CrabClientComponent::component(config).await?)
    }

    pub async fn to_crab_parachain_client(&self) -> color_eyre::Result<CrabParachainClient> {
        let config = self.clone().try_into()?;
        Ok(CrabParachainClientComponent::component(config).await?)
    }

    pub async fn to_kusama_client(&self) -> color_eyre::Result<KusamaClient> {
        let config = self.clone().try_into()?;
        Ok(KusamaClientComponent::component(config).await?)
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct IndexConfig {
    pub crab: SubqueryConfig,
    pub crab_parachain: SubqueryConfig,
    pub kusama: SubqueryConfig,
}

impl IndexConfig {
    pub fn to_crab_subquery(&self) -> Subquery {
        SubqueryComponent::component(self.crab.clone(), BridgeName::CrabParachain)
    }

    pub fn to_crab_parachain_subquery(&self) -> Subquery {
        SubqueryComponent::component(self.crab_parachain.clone(), BridgeName::CrabParachain)
    }

    pub fn to_kusama_subquery(&self) -> Subquery {
        SubqueryComponent::component(self.kusama.clone(), BridgeName::CrabParachain)
    }
}

impl RelayConfig {
    pub fn raw_lanes(&self) -> Vec<[u8; 4]> {
        self.lanes.iter().map(|item| item.0).collect()
    }
}