-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
294 lines (255 loc) · 8.99 KB
/
build.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use futures::future::join_all;
use reqwest::Client;
use serde_json::Value;
use std::collections::HashMap;
use std::fs::{read_to_string, write};
use std::path::Path;
use std::time::Duration;
#[derive(Debug)]
pub struct Chain<'a> {
pub name: &'a str,
pub logo: &'a str,
pub gecko: Option<&'a str>,
pub prefix: &'a str,
pub main_denom: String,
pub rpc_url: &'a str,
pub rest_url: &'a str,
pub wss_url: &'a str,
pub decimals: u8,
pub decimals_pow: u64,
pub sdk_version: u8,
pub manual_versioning: bool,
pub json_rpc: Option<&'a str>,
}
#[tokio::main]
async fn main() {
// Create `Client`.
let client = Client::builder().timeout(Duration::from_secs(10)).build().unwrap();
// Read `Chains.yml` file.
let chains_yml_content = read_to_string("Chains.yml").unwrap();
// Make iterable hash maps for chains.
let content_parts: Vec<_> = chains_yml_content
.split("name")
.filter(|part| !part.is_empty())
.map(|part| String::from("name") + part.trim())
.collect();
let chain_maps: Vec<_> = content_parts
.iter()
.map(|part| {
let mut chain_map = HashMap::new();
for line in part.lines() {
if !line.starts_with('#') {
let (key, value) = line.split_once(": ").unwrap();
chain_map.insert(key.trim(), value.trim());
}
}
chain_map
})
.collect();
let jobs: Vec<_> = chain_maps.iter().map(|chain_map| create_chain(chain_map, client.clone())).collect();
let chains = join_all(jobs).await;
update_chains_yml(&chains);
update_state_rs(&chains);
}
async fn create_chain<'a>(chain_map: &HashMap<&'a str, &'a str>, client: Client) -> Chain<'a> {
let name = chain_map.get("name").unwrap();
let logo = chain_map.get("logo").unwrap();
let gecko = chain_map.get("gecko").map(|a| *a);
let prefix = chain_map.get("prefix").unwrap_or(name);
let rpc_url = chain_map.get("rpc").unwrap();
let rest_url = chain_map.get("rest").unwrap();
let wss_url = chain_map.get("wss").unwrap();
let decimals: u8 = chain_map.get("decimals").unwrap_or(&"6").parse().unwrap();
let decimals_pow = 10_u64.pow(decimals as u32 - 4);
let (sdk_version, manual_versioning) = match chain_map.get("version") {
Some(version) => (version[2..4].parse().unwrap(), true),
None => (get_sdk_ver(rest_url, client.clone()).await, false),
};
let main_denom = match chain_map.get("denom") {
Some(denom) => denom.to_string(),
None => get_main_denom(rest_url, client.clone()).await,
};
let json_rpc = chain_map.get("jsonrpc").map(|a| *a);
Chain {
name,
logo,
gecko,
prefix,
main_denom,
rpc_url,
rest_url,
wss_url,
decimals,
decimals_pow,
sdk_version,
manual_versioning,
json_rpc,
}
}
async fn get_sdk_ver(rest_url: &str, client: Client) -> u8 {
let value: Value = client.get(&format!("{rest_url}/node_info")).send().await.unwrap().json().await.unwrap();
value["application_version"]["cosmos_sdk_version"].as_str().unwrap()[3..5]
.parse()
.map_err(|_| format!("manually set the version for '{rest_url}'"))
.unwrap()
}
async fn get_main_denom(rest_url: &str, client: Client) -> String {
let value: Value = client
.get(&format!("{rest_url}/cosmos/staking/v1beta1/params"))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
value["params"]["bond_denom"].as_str().unwrap().to_string()
}
fn update_chains_yml(chains: &[Chain]) {
let mut content = String::new();
for chain in chains {
content += &format!("name: {}\n", chain.name);
if chain.gecko.is_some() {
content += &format!("gecko: {}\n", chain.gecko.unwrap());
} else {
content += "# gecko: no token\n";
};
content += &format!("denom: {}\n", chain.main_denom);
if chain.prefix != chain.name {
content += &format!("prefix: {}\n", chain.prefix);
} else {
content += &format!("# prefix: {}\n", chain.prefix);
};
content += &format!("logo: {}\n", chain.logo);
if chain.decimals != 6 {
content += &format!("decimals: {}\n", chain.decimals);
} else {
content += &format!("# decimals: {}\n", chain.decimals);
}
if chain.manual_versioning {
content += &format!("version: 0.{}\n", chain.sdk_version);
} else {
content += &format!("# version: 0.{}\n", chain.sdk_version);
};
if let Some(jsonrpc) = chain.json_rpc {
content += &format!("jsonrpc: {}\n", jsonrpc);
};
content += &format!("rpc: {}\n", chain.rpc_url);
content += &format!("rest: {}\n", chain.rest_url);
content += &format!("wss: {}\n", chain.wss_url);
content += "\n\n";
}
write("Chains.yml", content).unwrap();
}
fn update_state_rs(chains: &[Chain]) {
let mut state_props = String::new();
let mut new_fn = String::new();
let mut get_fn = String::new();
let mut update_data_fn = String::new();
let mut update_prices_fn = String::new();
let mut update_database_fn = String::new();
let mut subscribe_to_events_fn = String::new();
let mut get_prices_props = String::new();
let path = format!(
"{home}/.backend",
home = std::env::var("HOME").expect("$HOME environment variable must be specified."),
);
for chain in chains {
state_props += &format!("\n {}: Chain,", chain.name);
new_fn += &format!(
r#"
{name}: init_chain! {{
name: "{name}",
gecko: {gecko},
base_prefix: "{fix}",
valoper_prefix: "{fix}valoper",
cons_prefix: "{fix}valcons",
main_denom: "{main_denom}",
rpc_url: "{rpc}",
jsonrpc_url: {jsonrpc},
rest_url: "{rest}",
wss_url: "{wss}",
sdk_version: {ver},
decimals_pow: {dec_pow},
client: client.clone(),
}},"#,
name = chain.name,
gecko = chain
.gecko
.map(|gecko| format!("Some(\"{gecko}\")"))
.unwrap_or_else(|| "None".to_string()),
fix = chain.prefix,
main_denom = chain.main_denom,
rpc = chain.rpc_url,
jsonrpc = chain
.json_rpc
.map(|json_rpc| format!("Some(\"{json_rpc}\")"))
.unwrap_or_else(|| "None".to_string()),
rest = chain.rest_url,
wss = chain.wss_url,
ver = chain.sdk_version,
dec_pow = chain.decimals_pow,
);
get_fn += &format!("\n \"{chain}\" => Ok(self.{chain}.clone()),", chain = chain.name);
update_data_fn += &format!("\n self.{chain}.update_data(),", chain = chain.name);
update_database_fn += &format!("\n self.{chain}.update_validator_database(),", chain = chain.name);
subscribe_to_events_fn += &format!("\n self.{chain}.subscribe_to_events(),", chain = chain.name);
match chain.gecko {
Some(gecko) => {
update_prices_fn += &format!("\n self.{chain}.update_price(prices.get(\"{gecko}\")),", chain = chain.name);
get_prices_props += &format!("\"{gecko}\", ");
}
_ => (),
}
}
let content = format!(
"\
use crate::chain::Chain;
use crate::data::ChainData;
use crate::init_chain;
use crate::utils::get_prices;
use tokio::join;
pub const PATH: &str = \"{path}\";
/// The state of the server.
pub struct State {{{state_props}
reqwest_client: reqwest::Client,
}}
impl State {{
/// Creates a new `State`.
pub fn new() -> State {{
let client = reqwest::Client::new();
State {{{new_fn}
reqwest_client: client,
}}
}}
/// Returns the matched chain.
pub fn get(&self, name: &str) -> Result<Chain, String> {{
match name {{{get_fn}
_ => Err(format!(\"{{name}} is not a supported chain.\")),
}}
}}
/// Updates all the chains' data.
pub async fn update_data(&self) {{
join!({update_data_fn}
);
}}
/// Updates all the prices' of chains.
pub async fn update_prices(&self) {{
let prices = get_prices(self.reqwest_client.clone(), &[{get_prices_props}]).await;
join!({update_prices_fn}
);
}}
/// Updates all the validator databases of chain.
pub async fn update_database(&self) {{
join!({update_database_fn}
);
}}
/// Subscribes to all the events for all the chains.
pub async fn subscribe_to_events(&self) {{
join!({subscribe_to_events_fn}
);
}}
}}
"
);
write(Path::new("src/state.rs"), content).unwrap();
}