-
Notifications
You must be signed in to change notification settings - Fork 117
adex tool was introduced #1729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adex tool was introduced #1729
Changes from 77 commits
6cddedb
f480d2d
8ffaf33
7896139
59f0621
4355e72
117a3f2
e890d94
abafc9b
ae4808c
4ef0f6f
523fa9d
7de4766
fa0747b
7ffff62
d536767
6ea0ce9
b371fd7
e0bab19
34d08fc
6addf5f
3f93b33
aaff7f5
4aac321
2cd70d8
04f4f14
addf3b0
11518ca
52767e2
4395e97
65459bb
1cdb693
a6dc002
fd254e3
cb4593e
75d31ca
7a3aa73
3b9f9f3
f289c47
f6b6446
1cc68df
7762435
fd1aecd
6bbdfca
7b84f02
ca170ed
4431f65
f629668
caa9d67
ddcc683
4399211
d060480
da0e693
62288e4
c09e924
c13f0e2
691ae4c
817e9cf
469a4f3
1c4721a
500db1b
2df6f48
cc757a9
e64dcf1
3c95565
3d52467
79d309f
52a19cb
aab6f3c
98befa1
72b440f
688c1e0
c16d5cb
1a386cf
11b14fe
01fe98a
de43f3d
37a1d2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| [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(all(target_family = "unix", not(target_os = "macos")))'.dependencies] | ||
| fork = "0.1" | ||
|
|
||
| [target.'cfg(windows)'.dependencies] | ||
| winapi = { version = "0.3.3", features = ["processthreadsapi", "winnt"] } | ||
| 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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can remove the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
| 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(), | ||
| } | ||
| } | ||
| 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() { | ||
|
onur-ozkan marked this conversation as resolved.
|
||
| 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(); | ||
| } | ||
| 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(); | ||
| } |
| 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) | ||
| } |
| 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(()) | ||
| } | ||
|
onur-ozkan marked this conversation as resolved.
|
||
|
|
||
| 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}"); | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.