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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use eyre::eyre;
use serde_json as json;

use crate::chain::exec::simple_exec;
use crate::error::{handle_generic_error, Error};

/// Register a new interchain account controlled by the given account
/// over the given connection.
pub fn register_interchain_account(
    chain_id: &str,
    command_path: &str,
    home_path: &str,
    rpc_listen_address: &str,
    from: &str,
    connection_id: &str,
) -> Result<(), Error> {
    let args = &[
        "--home",
        home_path,
        "--node",
        rpc_listen_address,
        "--output",
        "json",
        "tx",
        "intertx",
        "register",
        "--from",
        from,
        "--connection-id",
        connection_id,
        "--chain-id",
        chain_id,
        "--keyring-backend",
        "test",
        "-y",
    ];

    let res = simple_exec(chain_id, command_path, args)?.stdout;
    check_result_code(&res)?;

    Ok(())
}

/// Query the address of the interchain account
/// corresponding to the given controller account.
pub fn query_interchain_account(
    chain_id: &str,
    command_path: &str,
    home_path: &str,
    rpc_listen_address: &str,
    account: &str,
    connection_id: &str,
) -> Result<String, Error> {
    let args = &[
        "--home",
        home_path,
        "--node",
        rpc_listen_address,
        "--output",
        "json",
        "query",
        "intertx",
        "interchainaccounts",
        connection_id,
        account,
    ];

    let res = simple_exec(chain_id, command_path, args)?.stdout;
    let json_res = json::from_str::<json::Value>(&res).map_err(handle_generic_error)?;

    let address = json_res
        .get("interchain_account_address")
        .ok_or_else(|| eyre!("expected `interchain_account_address` field"))?
        .as_str()
        .ok_or_else(|| eyre!("expected string field"))?;

    Ok(address.to_string())
}

/// Submit a msg from a controller account over an ICA channel
/// using the given connection.
pub fn interchain_submit(
    chain_id: &str,
    command_path: &str,
    home_path: &str,
    rpc_listen_address: &str,
    from: &str,
    connection_id: &str,
    msg: &str,
) -> Result<(), Error> {
    let args = &[
        "--home",
        home_path,
        "--node",
        rpc_listen_address,
        "--output",
        "json",
        "tx",
        "intertx",
        "submit",
        msg,
        "--connection-id",
        connection_id,
        "--from",
        from,
        "--chain-id",
        chain_id,
        "--keyring-backend",
        "test",
        "-y",
    ];

    let res = simple_exec(chain_id, command_path, args)?.stdout;
    check_result_code(&res)?;

    Ok(())
}

/// Check that a command succeeded, by ensuring that the JSON emitted
/// contains a `code` integer field set to 0.
fn check_result_code(res: &str) -> Result<(), Error> {
    let json_res = json::from_str::<json::Value>(res).map_err(handle_generic_error)?;

    let code = json_res
        .get("code")
        .ok_or_else(|| eyre!("expected `code` field"))?
        .as_i64()
        .ok_or_else(|| eyre!("expected integer field"))?;

    if code == 0 {
        Ok(())
    } else {
        let raw_log = json_res
            .get("raw_log")
            .ok_or_else(|| eyre!("expected `raw_log` field"))?
            .as_str()
            .ok_or_else(|| eyre!("expected string field"))?;

        Err(Error::generic(eyre!("{}", raw_log)))
    }
}