Skip to content

Commit 674a590

Browse files
Tommytrgaesedepece
authored andcommitted
feat(staking): implement unstaking JSON-RPC method
1 parent 3e9f2a1 commit 674a590

File tree

1 file changed

+50
-7
lines changed
  • node/src/actors/json_rpc

1 file changed

+50
-7
lines changed

node/src/actors/json_rpc/api.rs

+50-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ use crate::{
4040
json_rpc::Subscriptions,
4141
messages::{
4242
AddCandidates, AddPeers, AddTransaction, AuthorizeStake, BuildDrt, BuildStake,
43-
BuildStakeParams, BuildStakeResponse, BuildVtt, ClearPeers, DropAllPeers,
44-
EstimatePriority, GetBalance, GetBalanceTarget, GetBlocksEpochRange,
45-
GetConsolidatedPeers, GetDataRequestInfo, GetEpoch, GetHighestCheckpointBeacon,
46-
GetItemBlock, GetItemSuperblock, GetItemTransaction, GetKnownPeers,
47-
GetMemoryTransaction, GetMempool, GetNodeStats, GetReputation, GetSignalingInfo,
48-
GetState, GetSupplyInfo, GetUtxoInfo, InitializePeers, IsConfirmedBlock, QueryStake,
49-
QueryStakesParams, Rewind, SnapshotExport, SnapshotImport, StakeAuthorization,
43+
BuildStakeParams, BuildStakeResponse, BuildUnstake, BuildUnstakeParams, BuildVtt,
44+
ClearPeers, DropAllPeers, EstimatePriority, GetBalance, GetBalanceTarget,
45+
GetBlocksEpochRange, GetConsolidatedPeers, GetDataRequestInfo, GetEpoch,
46+
GetHighestCheckpointBeacon, GetItemBlock, GetItemSuperblock, GetItemTransaction,
47+
GetKnownPeers, GetMemoryTransaction, GetMempool, GetNodeStats, GetReputation,
48+
GetSignalingInfo, GetState, GetSupplyInfo, GetUtxoInfo, InitializePeers,
49+
IsConfirmedBlock, QueryStake, QueryStakesParams, Rewind, SnapshotExport,
50+
SnapshotImport, StakeAuthorization,
5051
},
5152
peers_manager::PeersManager,
5253
sessions_manager::SessionsManager,
@@ -289,6 +290,15 @@ pub fn attach_sensitive_methods<H>(
289290
|params| authorize_stake(params.parse()),
290291
))
291292
});
293+
294+
server.add_actix_method(system, "unstake", move |params| {
295+
Box::pin(if_authorized(
296+
enable_sensitive_methods,
297+
"unstake",
298+
params,
299+
|params| unstake(params.parse()),
300+
))
301+
});
292302
}
293303

294304
fn extract_topic_and_params(params: Params) -> Result<(String, Value), Error> {
@@ -2028,6 +2038,39 @@ pub async fn stake(params: Result<BuildStakeParams, Error>) -> JsonRpcResult {
20282038
.await
20292039
}
20302040

2041+
/// Build an unstake transaction
2042+
pub async fn unstake(params: Result<BuildUnstakeParams, Error>) -> JsonRpcResult {
2043+
// Short-circuit if parameters are wrong
2044+
let params = params?;
2045+
2046+
let operator = params
2047+
.operator
2048+
.try_do_magic(|hex_str| PublicKeyHash::from_bech32(get_environment(), &hex_str))
2049+
.map_err(internal_error)?;
2050+
2051+
// Construct a BuildUnstake message that we can relay to the ChainManager for creation of the Unstake transaction
2052+
let build_unstake = BuildUnstake {
2053+
operator,
2054+
value: params.value,
2055+
dry_run: params.dry_run,
2056+
};
2057+
2058+
ChainManager::from_registry()
2059+
.send(build_unstake)
2060+
.map(|res| match res {
2061+
Ok(Ok(transaction)) => serde_json::to_value(transaction).map_err(internal_error),
2062+
Ok(Err(e)) => {
2063+
let err = internal_error_s(e);
2064+
Err(err)
2065+
}
2066+
Err(e) => {
2067+
let err = internal_error_s(e);
2068+
Err(err)
2069+
}
2070+
})
2071+
.await
2072+
}
2073+
20312074
/// Create a stake authorization for the given address.
20322075
///
20332076
/// The output of this method is a required argument to call the Stake method.

0 commit comments

Comments
 (0)