Skip to content

Commit bb22e43

Browse files
Tommytrgaesedepece
authored andcommitted
feat(staking): implement unstaking CLI method
1 parent 674a590 commit bb22e43

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

build.dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM ubuntu:22.04
2+
3+
# Install needed dependencies
4+
RUN apt update && \
5+
apt upgrade -y && \
6+
apt install -y protobuf-compiler wget curl build-essential openssl libssl-dev pkg-config libclang-dev
7+
8+
# Configure openssl
9+
RUN pkg-config openssl
10+
11+
# Install Rust
12+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
13+
RUN echo 'source $HOME/.cargo/env' >> $HOME/.bashrc

src/cli/node/json_rpc_client.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use witnet_data_structures::{
3333
fee::Fee,
3434
get_environment,
3535
proto::ProtobufConvert,
36-
transaction::{DRTransaction, StakeTransaction, Transaction, VTTransaction},
36+
transaction::{
37+
DRTransaction, StakeTransaction, Transaction, UnstakeTransaction, VTTransaction,
38+
},
3739
transaction_factory::NodeBalance,
3840
types::SequentialId,
3941
utxo_pool::{UtxoInfo, UtxoSelectionStrategy},
@@ -45,8 +47,9 @@ use witnet_node::actors::{
4547
AddrType, GetBlockChainParams, GetTransactionOutput, PeersResult, QueryStakesArgument,
4648
},
4749
messages::{
48-
AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildVtt, GetBalanceTarget,
49-
GetReputationResult, MagicEither, SignalingInfo, StakeAuthorization,
50+
AuthorizeStake, BuildDrt, BuildStakeParams, BuildStakeResponse, BuildUnstakeParams,
51+
BuildVtt, GetBalanceTarget, GetReputationResult, MagicEither, SignalingInfo,
52+
StakeAuthorization,
5053
},
5154
};
5255
use witnet_rad::types::RadonTypes;
@@ -1052,6 +1055,42 @@ pub fn authorize_st(addr: SocketAddr, withdrawer: Option<String>) -> Result<(),
10521055
Ok(())
10531056
}
10541057

1058+
#[allow(clippy::too_many_arguments)]
1059+
pub fn send_ut(
1060+
addr: SocketAddr,
1061+
value: u64,
1062+
operator: MagicEither<String, PublicKeyHash>,
1063+
dry_run: bool,
1064+
) -> Result<(), failure::Error> {
1065+
let mut stream = start_client(addr)?;
1066+
let mut id = SequentialId::initialize(1u8);
1067+
1068+
let build_unstake_params = BuildUnstakeParams {
1069+
operator,
1070+
value,
1071+
dry_run,
1072+
};
1073+
1074+
// Finally ask the node to create the transaction.
1075+
let (_, (request, response)): (UnstakeTransaction, _) = issue_method(
1076+
"unstake",
1077+
Some(build_unstake_params),
1078+
&mut stream,
1079+
id.next(),
1080+
)?;
1081+
1082+
// On dry run mode, print the request, otherwise, print the response.
1083+
// This is kept like this strictly for backwards compatibility.
1084+
// TODO: wouldn't it be better to always print the response or both?
1085+
if dry_run {
1086+
println!("{}", request);
1087+
} else {
1088+
println!("{}", response);
1089+
}
1090+
1091+
Ok(())
1092+
}
1093+
10551094
pub fn master_key_export(
10561095
addr: SocketAddr,
10571096
write_to_path: Option<&Path>,

src/cli/node/with_node.rs

+25
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ pub fn exec_cmd(
297297
validator,
298298
withdrawer,
299299
} => rpc::query_stakes(node.unwrap_or(default_jsonrpc), validator, withdrawer),
300+
Command::Unstake {
301+
node,
302+
value,
303+
operator,
304+
dry_run,
305+
} => rpc::send_ut(
306+
node.unwrap_or(default_jsonrpc),
307+
value,
308+
MagicEither::Left(operator),
309+
dry_run,
310+
),
300311
}
301312
}
302313

@@ -804,6 +815,20 @@ pub enum Command {
804815
#[structopt(short = "w", long = "withdrawer")]
805816
withdrawer: Option<String>,
806817
},
818+
Unstake {
819+
/// Socket address of the Witnet node to query
820+
#[structopt(short = "n", long = "node")]
821+
node: Option<SocketAddr>,
822+
/// Value
823+
#[structopt(long = "value")]
824+
value: u64,
825+
/// Node address operating the staked coins
826+
#[structopt(long = "operator")]
827+
operator: String,
828+
/// Print the request that would be sent to the node and exit without doing anything
829+
#[structopt(long = "dry-run")]
830+
dry_run: bool,
831+
},
807832
}
808833

809834
#[derive(Debug, StructOpt)]

0 commit comments

Comments
 (0)