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
use client_darwinia::component::DarwiniaClientComponent;
use shadow_liketh::component::ShadowComponent;
use shadow_liketh::types::BridgeName;
use support_common::config::{Config, Names};
use support_terminal::output;

use crate::bridge::{DarwiniaEthereumConfig, Extrinsic};
use crate::service::guard::GuardService;

pub async fn handle_guard() -> color_eyre::Result<()> {
    let bridge_config: DarwiniaEthereumConfig = Config::restore(Names::BridgeDarwiniaEthereum)?;
    let config_darwinia = bridge_config.darwinia;

    // Shadow
    let shadow = ShadowComponent::component(
        bridge_config.shadow,
        bridge_config.ethereum,
        bridge_config.web3,
        BridgeName::DarwiniaEthereum,
    )?;

    // Darwinia client
    let client = DarwiniaClientComponent::component(config_darwinia.clone()).await?;

    let extrinsics = GuardService::extrinsics(&client, &shadow).await?;
    for extrinsic in extrinsics {
        if let Extrinsic::GuardVote(pending_block_number, aye) = extrinsic {
            let ex_hash = client
                .runtime()
                .tx()
                .ethereum_relay()
                .vote_pending_relay_header_parcel(pending_block_number, aye)
                .sign_and_submit(client.account().signer())
                .await?;
            if aye {
                output::output_text(format!(
                    "Voted to approve: {}, ex hash: {:?}",
                    pending_block_number, ex_hash
                ));
            } else {
                output::output_text(format!(
                    "Voted to reject: {}, ex hash: {:?}",
                    pending_block_number, ex_hash
                ));
            }
        }
    }
    Ok(())
}