forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1729 from rozhkovdmitrii/feature-1682-introduce-adex
adex tool was introduced
- Loading branch information
Showing
19 changed files
with
1,116 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "adex-cli" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Rozhkov Dmitrii <rozhkov@komodoplatform.com>"] | ||
description = "Provides a CLI interface and facilitates interoperating to komodo atomic dex through the mm2 service" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] | ||
clap = "2.33.3" | ||
common = { path = "../common" } | ||
derive_more = "0.99" | ||
env_logger = "0.7.1" | ||
gstuff = { version = "=0.7.4" , features = [ "nightly" ]} | ||
inquire = "0.6" | ||
log = "0.4" | ||
mm2_net = { path = "../mm2_net" } | ||
passwords = "3.1" | ||
serde = "1.0" | ||
serde_json = { version = "1", features = ["preserve_order", "raw_value"] } | ||
sysinfo = "0.28" | ||
tiny-bip39 = "0.8.0" | ||
tokio = { version = "1.20", features = [ "macros" ] } | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winapi = { version = "0.3.3", features = ["processthreadsapi", "winnt"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use clap::{App, Arg, SubCommand}; | ||
use log::error; | ||
use std::env; | ||
|
||
use crate::scenarios::{get_status, init, start_process, stop_process}; | ||
|
||
enum Command { | ||
Init { | ||
mm_coins_path: String, | ||
mm_conf_path: String, | ||
}, | ||
Start { | ||
mm_conf_path: Option<String>, | ||
mm_coins_path: Option<String>, | ||
mm_log: Option<String>, | ||
}, | ||
Stop, | ||
Status, | ||
} | ||
|
||
pub fn process_cli() { | ||
let mut app = App::new(env!("CARGO_PKG_NAME")) | ||
.version(env!("CARGO_PKG_VERSION")) | ||
.author(env!("CARGO_PKG_AUTHORS")) | ||
.about(env!("CARGO_PKG_DESCRIPTION")) | ||
.subcommand( | ||
SubCommand::with_name("init") | ||
.about("Initialize predefined mm2 coin set and configuration") | ||
.arg( | ||
Arg::with_name("mm-coins-path") | ||
.long("mm-coins-path") | ||
.value_name("FILE") | ||
.help("coin set file path") | ||
.default_value("coins"), | ||
) | ||
.arg( | ||
Arg::with_name("mm-conf-path") | ||
.long("mm-conf-path") | ||
.value_name("FILE") | ||
.help("mm2 configuration file path") | ||
.default_value("MM2.json"), | ||
), | ||
) | ||
.subcommand( | ||
SubCommand::with_name("start") | ||
.about("Start mm2 service") | ||
.arg( | ||
Arg::with_name("mm-conf-path") | ||
.long("mm-conf-path") | ||
.value_name("FILE") | ||
.help("mm2 configuration file path"), | ||
) | ||
.arg( | ||
Arg::with_name("mm-coins-path") | ||
.long("mm-coins-path") | ||
.value_name("FILE") | ||
.help("coin set file path"), | ||
) | ||
.arg( | ||
Arg::with_name("mm-log") | ||
.long("mm-log") | ||
.value_name("FILE") | ||
.help("log file path"), | ||
), | ||
) | ||
.subcommand(SubCommand::with_name("stop").about("Stop mm2 instance")) | ||
.subcommand(SubCommand::with_name("status").about("Get mm2 running status")); | ||
|
||
let matches = app.clone().get_matches(); | ||
|
||
let command = match matches.subcommand() { | ||
("init", Some(init_matches)) => { | ||
let mm_coins_path = init_matches.value_of("mm-coins-path").unwrap_or("coins").to_owned(); | ||
let mm_conf_path = init_matches.value_of("mm-conf-path").unwrap_or("MM2.json").to_owned(); | ||
Command::Init { | ||
mm_coins_path, | ||
mm_conf_path, | ||
} | ||
}, | ||
("start", Some(start_matches)) => { | ||
let mm_conf_path = start_matches.value_of("mm-conf-path").map(|s| s.to_owned()); | ||
let mm_coins_path = start_matches.value_of("mm-coins-path").map(|s| s.to_owned()); | ||
let mm_log = start_matches.value_of("mm-log").map(|s| s.to_owned()); | ||
Command::Start { | ||
mm_conf_path, | ||
mm_coins_path, | ||
mm_log, | ||
} | ||
}, | ||
("stop", _) => Command::Stop, | ||
("status", _) => Command::Status, | ||
_ => { | ||
let _ = app | ||
.print_long_help() | ||
.map_err(|error| error!("Failed to print_long_help: {error}")); | ||
return; | ||
}, | ||
}; | ||
|
||
match command { | ||
Command::Init { | ||
mm_coins_path: coins_file, | ||
mm_conf_path: mm2_cfg_file, | ||
} => init(&mm2_cfg_file, &coins_file), | ||
Command::Start { | ||
mm_conf_path: mm2_cfg_file, | ||
mm_coins_path: coins_file, | ||
mm_log: log_file, | ||
} => start_process(&mm2_cfg_file, &coins_file, &log_file), | ||
Command::Stop => stop_process(), | ||
Command::Status => get_status(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use log::LevelFilter; | ||
use std::io::Write; | ||
|
||
pub fn init_logging() { | ||
let mut builder = env_logger::builder(); | ||
let level = std::env::var("RUST_LOG") | ||
.map(|s| s.parse().expect("Failed to parse RUST_LOG")) | ||
.unwrap_or(LevelFilter::Info); | ||
builder | ||
.filter_level(level) | ||
.format(|buf, record| writeln!(buf, "{}", record.args())); | ||
builder.init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[cfg(not(target_arch = "wasm32"))] mod cli; | ||
#[cfg(not(target_arch = "wasm32"))] mod log; | ||
#[cfg(not(target_arch = "wasm32"))] mod scenarios; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
fn main() {} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() { | ||
log::init_logging(); | ||
cli::process_cli(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use common::log::error; | ||
use serde::Serialize; | ||
use std::fs::OpenOptions; | ||
use std::io::Write; | ||
use std::ops::Deref; | ||
|
||
pub fn rewrite_data_file<T>(data: T, file: &str) -> Result<(), ()> | ||
where | ||
T: Deref<Target = [u8]>, | ||
{ | ||
let mut writer = OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.open(file) | ||
.map_err(|error| { | ||
error!("Failed to open {file}: {error}"); | ||
})?; | ||
|
||
writer.write(&data).map_err(|error| { | ||
error!("Failed to write data into {file}: {error}"); | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
pub fn rewrite_json_file<T>(value: &T, file: &str) -> Result<(), ()> | ||
where | ||
T: Serialize, | ||
{ | ||
let data = serde_json::to_vec_pretty(value).map_err(|error| { | ||
error!("Failed to serialize data {error}"); | ||
})?; | ||
rewrite_data_file(data, file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use common::log::{error, info}; | ||
use derive_more::Display; | ||
use mm2_net::transport::slurp_url; | ||
|
||
use super::helpers::rewrite_data_file; | ||
|
||
#[derive(Clone, Copy, Debug, Display)] | ||
pub enum CoinSet { | ||
Empty, | ||
Full, | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
pub async fn init_coins(coins_file: &str) -> Result<(), ()> { | ||
const FULL_COIN_SET_ADDRESS: &str = "https://raw.githubusercontent.com/KomodoPlatform/coins/master/coins"; | ||
const EMPTY_COIN_SET_DATA: &[u8] = b"[]\n"; | ||
let coin_set = inquire_coin_set(coins_file)?; | ||
info!("Start getting mm2 coins"); | ||
let coins_data = match coin_set { | ||
CoinSet::Empty => Vec::<u8>::from(EMPTY_COIN_SET_DATA), | ||
CoinSet::Full => { | ||
info!("Getting coin set from: {FULL_COIN_SET_ADDRESS}"); | ||
let (_status_code, _headers, data) = slurp_url(FULL_COIN_SET_ADDRESS).await.map_err(|error| { | ||
error!("Failed to get coin set from: {FULL_COIN_SET_ADDRESS}, error: {error}"); | ||
})?; | ||
data | ||
}, | ||
}; | ||
|
||
rewrite_data_file(coins_data, coins_file)?; | ||
info!("Got coins data, written into: {coins_file}"); | ||
Ok(()) | ||
} | ||
|
||
fn inquire_coin_set(coins_file: &str) -> Result<CoinSet, ()> { | ||
inquire::Select::new( | ||
format!("Select one of predefined coin sets to save into: {coins_file}").as_str(), | ||
vec![CoinSet::Empty, CoinSet::Full], | ||
) | ||
.with_help_message("Information about the currencies: their ticker symbols, names, ports, addresses, etc.") | ||
.prompt() | ||
.map_err(|error| { | ||
error!("Failed to select coin_set: {error}"); | ||
}) | ||
} |
Oops, something went wrong.