Skip to content

Commit

Permalink
[aptos-cli] Allow for inputting easy to use duration inputs
Browse files Browse the repository at this point in the history
Inputs can be now read in as English.  See:
https://docs.rs/parse_duration/latest/parse_duration/
  • Loading branch information
gregnazario committed Jun 30, 2022
1 parent 12cbfaa commit 3b53225
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
65 changes: 57 additions & 8 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/aptos-workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ itertools = { version = "0.10.3", features = ["use_alloc", "use_std"] }
libc = { version = "0.2.126", features = ["std"] }
log = { version = "0.4.17", default-features = false, features = ["std"] }
memchr = { version = "2.5.0", features = ["std", "use_std"] }
num-bigint = { version = "0.2.6", features = ["std"] }
num-integer = { version = "0.1.45", features = ["std"] }
num-traits = { version = "0.2.15", features = ["std"] }
parking_lot = { version = "0.12.0", features = ["send_guard"] }
Expand Down Expand Up @@ -91,6 +92,7 @@ itertools = { version = "0.10.3", features = ["use_alloc", "use_std"] }
libc = { version = "0.2.126", features = ["std"] }
log = { version = "0.4.17", default-features = false, features = ["std"] }
memchr = { version = "2.5.0", features = ["std", "use_std"] }
num-bigint = { version = "0.2.6", features = ["std"] }
num-integer = { version = "0.1.45", features = ["std"] }
num-traits = { version = "0.2.15", features = ["std"] }
parking_lot = { version = "0.12.0", features = ["send_guard"] }
Expand Down
1 change: 1 addition & 0 deletions crates/aptos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bcs = "0.1.3"
clap = "3.1.8"
hex = "0.4.3"
itertools = "0.10.3"
parse_duration = "2.1.1"
rand = "0.7.3"
reqwest = { version = "0.11.10", features = ["blocking", "json"] }
serde = "1.0.137"
Expand Down
25 changes: 14 additions & 11 deletions crates/aptos/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use async_trait::async_trait;
use clap::Parser;
use std::{
path::PathBuf,
time::{SystemTime, UNIX_EPOCH},
time::{Duration, SystemTime, UNIX_EPOCH},
};

/// Tool for manipulating nodes
Expand Down Expand Up @@ -144,8 +144,10 @@ pub struct IncreaseLockup {
#[clap(flatten)]
pub(crate) txn_options: TransactionOptions,
/// Number of seconds to increase the lockup period by
#[clap(long)]
pub(crate) lockup_timestamp_secs: u64,
///
/// Examples: '1d', '5 days', '1 month'
#[clap(long, parse(try_from_str=parse_duration::parse))]
pub(crate) lockup_duration: Duration,
}

#[async_trait]
Expand All @@ -155,24 +157,25 @@ impl CliCommand<Transaction> for IncreaseLockup {
}

async fn execute(mut self) -> CliTypedResult<Transaction> {
if self.lockup_timestamp_secs
<= SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
{
if self.lockup_duration.is_zero() {
return Err(CliError::CommandArgumentError(
"--lockup-timestamp-secs is in the past".to_string(),
"Must provide a non-zero lockup duration".to_string(),
));
}

let lockup_timestamp_secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.saturating_add(self.lockup_duration.as_secs());

self.txn_options
.submit_script_function(
AccountAddress::ONE,
"Stake",
"increase_lockup",
vec![],
vec![bcs::to_bytes(&self.lockup_timestamp_secs)?],
vec![bcs::to_bytes(&lockup_timestamp_secs)?],
)
.await
}
Expand Down

0 comments on commit 3b53225

Please sign in to comment.