Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Apr 3, 2024
1 parent 4176959 commit bf2772e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 31 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ open = "4"
shell-words = "1"
cargo-util = "0.1.1"
indicatif = "0.17.8"
tracing = "0.1.40"
tracing-indicatif = "0.3.6"
tracing-subscriber = "0.3.18"

wasmparser = "0.200.0"

Expand Down
32 changes: 32 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ use clap::Parser;
use color_eyre::eyre::WrapErr;
use interactive_clap::ToCliArgs;

use indicatif::ProgressStyle;
use tracing_indicatif::IndicatifLayer;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

pub use near_cli_rs::commands;
pub use near_cli_rs::common::{self, CliResult};
pub use near_cli_rs::config;
Expand Down Expand Up @@ -60,6 +66,32 @@ fn main() -> crate::common::CliResult {

color_eyre::install()?;

let indicatif_layer = IndicatifLayer::new().with_progress_style(
ProgressStyle::with_template(
"{spinner:.blue} {span_child_prefix} {span_name} {{{span_fields}}}",
)
.unwrap()
.tick_strings(&[
"▹▹▹▹▹",
"▸▹▹▹▹",
"▹▸▹▹▹",
"▹▹▸▹▹",
"▹▹▹▸▹",
"▹▹▹▹▸",
"▪▪▪▪▪",
]),
);
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer().with_writer(
indicatif_layer
.get_stderr_writer()
.with_max_level(tracing::Level::INFO),
),
)
.with(indicatif_layer)
.init();

#[cfg(feature = "self-update")]
let handle = std::thread::spawn(|| -> color_eyre::eyre::Result<String> {
crate::commands::extensions::self_update::get_latest_version()
Expand Down
35 changes: 4 additions & 31 deletions src/transaction_signature_options/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Send;
pub struct SendContext;

impl SendContext {
#[tracing::instrument(skip_all, name = "Sending transaction ...")]
pub fn from_previous_context(
previous_context: super::SubmitContext,
_scope: &<Send as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
Expand All @@ -26,35 +27,9 @@ impl SendContext {
)
.map_err(color_eyre::Report::msg)?;

// eprintln!("Transaction sent ..."); // long-spinner download iterator
let retries_number = 5;
let mut retries_left = (0..retries_number).rev();
let mut retries = (0..retries_number).rev();
let transaction_info = loop {
let pb = indicatif::ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::from_millis(120));
pb.set_style(
indicatif::ProgressStyle::with_template("{spinner:.blue} {msg}")
.unwrap()
.tick_strings(&[
"▹▹▹▹▹",
"▸▹▹▹▹",
"▹▸▹▹▹",
"▹▹▸▹▹",
"▹▹▹▸▹",
"▹▹▹▹▸",
"▪▪▪▪▪",
]),
);
if retries_left.len() < retries_number {
pb.set_message(format!(
"Sending a transaction ({} retries left) ...",
retries_left.len() + 1
));
} else {
pb.set_message("Sending a transaction ...");
}
std::thread::sleep(std::time::Duration::from_secs(5));

let transaction_info_result = previous_context.network_config.json_rpc_client()
.blocking_call(
near_jsonrpc_client::methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest{
Expand All @@ -63,16 +38,14 @@ impl SendContext {
);
match transaction_info_result {
Ok(response) => {
pb.finish_with_message("Your transaction has been sent successfully.");
break response;
}
Err(ref err) => match crate::common::rpc_transaction_error(err) {
Ok(_) => {
if retries_left.next().is_some() {
std::thread::sleep(std::time::Duration::from_millis(100));
} else {
if retries.next().is_none() {
return Err(color_eyre::eyre::eyre!(err.to_string()));
}
std::thread::sleep(std::time::Duration::from_secs(5));
}
Err(report) => return Err(color_eyre::Report::msg(report)),
},
Expand Down

0 comments on commit bf2772e

Please sign in to comment.