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
use eyre::eyre;
use serde::Serialize;
use serde_json as json;
use ibc::core::ics24_host::identifier::ConnectionId;
use crate::error::{handle_generic_error, Error};
use crate::prelude::WalletAddress;
use super::ChainDriver;
pub fn register_interchain_account(
driver: &ChainDriver,
from: &WalletAddress,
connection_id: &ConnectionId,
) -> Result<(), Error> {
let args = &[
"--home",
&driver.home_path,
"--node",
&driver.rpc_listen_address(),
"--output",
"json",
"tx",
"intertx",
"register",
"--from",
&from.0,
"--connection-id",
connection_id.as_str(),
"--chain-id",
driver.chain_id.as_str(),
"--keyring-backend",
"test",
"-y",
];
let res = driver.exec(args)?.stdout;
check_result_code(&res)?;
Ok(())
}
pub fn query_interchain_account(
driver: &ChainDriver,
account: &WalletAddress,
connection_id: &ConnectionId,
) -> Result<WalletAddress, Error> {
let args = &[
"--home",
&driver.home_path,
"--node",
&driver.rpc_listen_address(),
"--output",
"json",
"query",
"intertx",
"interchainaccounts",
connection_id.as_str(),
&account.0,
];
let res = driver.exec(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(WalletAddress(address.to_string()))
}
pub fn interchain_submit<T: Serialize>(
driver: &ChainDriver,
from: &WalletAddress,
connection_id: &ConnectionId,
msg: &T,
) -> Result<(), Error> {
let msg_json = serde_json::to_string_pretty(msg).unwrap();
println!("{}", msg_json);
let args = &[
"--home",
&driver.home_path,
"--node",
&driver.rpc_listen_address(),
"--output",
"json",
"tx",
"intertx",
"submit",
&msg_json,
"--connection-id",
connection_id.as_str(),
"--from",
&from.0,
"--chain-id",
driver.chain_id.as_str(),
"--keyring-backend",
"test",
"-y",
];
let res = driver.exec(args)?.stdout;
check_result_code(&res)?;
Ok(())
}
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)))
}
}