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
use client_crab::client::CrabClient;
use client_crab::component::CrabClientComponent;
use client_darwinia::client::DarwiniaClient;
use client_darwinia::component::DarwiniaClientComponent;
use serde::{Deserialize, Serialize};
use subquery::types::BridgeName;
use subquery::{Subquery, SubqueryComponent, SubqueryConfig};

use crate::types::HexLaneId;

/// Bridge template config
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BridgeConfig {
    /// Darwinia chain
    pub darwinia: ChainInfoConfig,
    /// Crab chain
    pub crab: ChainInfoConfig,
    /// Relay config
    pub relay: RelayConfig,
    /// Index config
    pub index: IndexConfig,
}

/// Chain info
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ChainInfoConfig {
    /// Endpoint
    pub endpoint: String,
    /// Signer
    pub signer: String,
}

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

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

impl From<ChainInfoConfig> for client_crab::config::ClientConfig {
    fn from(config: ChainInfoConfig) -> Self {
        client_crab::config::ClientConfig {
            endpoint: config.endpoint,
            relayer_private_key: config.signer,
            relayer_real_account: None,
        }
    }
}

impl From<ChainInfoConfig> for client_darwinia::config::ClientConfig {
    fn from(config: ChainInfoConfig) -> Self {
        client_darwinia::config::ClientConfig {
            endpoint: config.endpoint,
            relayer_private_key: config.signer,
            relayer_real_account: None,
        }
    }
}

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

    pub async fn to_darwinia_client(&self) -> color_eyre::Result<DarwiniaClient> {
        let config = self.clone().into();
        Ok(DarwiniaClientComponent::component(config).await?)
    }
}

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

    pub fn to_darwinia_subquery(&self) -> Subquery {
        SubqueryComponent::component(self.darwinia.clone(), BridgeName::DarwiniaCrab)
    }
}

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