Skip to content

Commit

Permalink
feat(staking): implement unstaking CLI method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg authored and aesedepece committed Jun 21, 2024
1 parent 674a590 commit bb22e43
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
13 changes: 13 additions & 0 deletions build.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM ubuntu:22.04

# Install needed dependencies
RUN apt update && \
apt upgrade -y && \
apt install -y protobuf-compiler wget curl build-essential openssl libssl-dev pkg-config libclang-dev

# Configure openssl
RUN pkg-config openssl

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
RUN echo 'source $HOME/.cargo/env' >> $HOME/.bashrc
45 changes: 42 additions & 3 deletions src/cli/node/json_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ use witnet_data_structures::{
fee::Fee,
get_environment,
proto::ProtobufConvert,
transaction::{DRTransaction, StakeTransaction, Transaction, VTTransaction},
transaction::{
DRTransaction, StakeTransaction, Transaction, UnstakeTransaction, VTTransaction,
},
transaction_factory::NodeBalance,
types::SequentialId,
utxo_pool::{UtxoInfo, UtxoSelectionStrategy},
Expand All @@ -45,8 +47,9 @@ use witnet_node::actors::{
AddrType, GetBlockChainParams, GetTransactionOutput, PeersResult, QueryStakesArgument,
},
messages::{
AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildVtt, GetBalanceTarget,
GetReputationResult, MagicEither, SignalingInfo, StakeAuthorization,
AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildUnstakeParams,
BuildVtt, GetBalanceTarget, GetReputationResult, MagicEither, SignalingInfo,
StakeAuthorization,
},
};
use witnet_rad::types::RadonTypes;
Expand Down Expand Up @@ -1052,6 +1055,42 @@ pub fn authorize_st(addr: SocketAddr, withdrawer: Option<String>) -> Result<(),
Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn send_ut(
addr: SocketAddr,
value: u64,
operator: MagicEither<String, PublicKeyHash>,
dry_run: bool,
) -> Result<(), failure::Error> {
let mut stream = start_client(addr)?;
let mut id = SequentialId::initialize(1u8);

let build_unstake_params = BuildUnstakeParams {
operator,
value,
dry_run,
};

// Finally ask the node to create the transaction.
let (_, (request, response)): (UnstakeTransaction, _) = issue_method(
"unstake",
Some(build_unstake_params),
&mut stream,
id.next(),
)?;

// On dry run mode, print the request, otherwise, print the response.
// This is kept like this strictly for backwards compatibility.
// TODO: wouldn't it be better to always print the response or both?
if dry_run {
println!("{}", request);
} else {
println!("{}", response);
}

Ok(())
}

pub fn master_key_export(
addr: SocketAddr,
write_to_path: Option<&Path>,
Expand Down
25 changes: 25 additions & 0 deletions src/cli/node/with_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ pub fn exec_cmd(
validator,
withdrawer,
} => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer),
Command::Unstake {
node,
value,
operator,
dry_run,
} => rpc::send_ut(
node.unwrap_or(default_jsonrpc),
value,
MagicEither::Left(operator),
dry_run,
),
}
}

Expand Down Expand Up @@ -804,6 +815,20 @@ pub enum Command {
#[structopt(short = "w", long = "withdrawer")]
withdrawer: Option<String>,
},
Unstake {
/// Socket address of the Witnet node to query
#[structopt(short = "n", long = "node")]
node: Option<SocketAddr>,
/// Value
#[structopt(long = "value")]
value: u64,
/// Node address operating the staked coins
#[structopt(long = "operator")]
operator: String,
/// Print the request that would be sent to the node and exit without doing anything
#[structopt(long = "dry-run")]
dry_run: bool,
},
}

#[derive(Debug, StructOpt)]
Expand Down

0 comments on commit bb22e43

Please sign in to comment.