Skip to content

Commit

Permalink
feat(staking): implement unstaking JSON-RPC 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 3e9f2a1 commit 674a590
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions node/src/actors/json_rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ use crate::{
json_rpc::Subscriptions,
messages::{
AddCandidates, AddPeers, AddTransaction, AuthorizeStake, BuildDrt, BuildStake,
BuildStakeParams, BuildStakeResponse, BuildVtt, ClearPeers, DropAllPeers,
EstimatePriority, GetBalance, GetBalanceTarget, GetBlocksEpochRange,
GetConsolidatedPeers, GetDataRequestInfo, GetEpoch, GetHighestCheckpointBeacon,
GetItemBlock, GetItemSuperblock, GetItemTransaction, GetKnownPeers,
GetMemoryTransaction, GetMempool, GetNodeStats, GetReputation, GetSignalingInfo,
GetState, GetSupplyInfo, GetUtxoInfo, InitializePeers, IsConfirmedBlock, QueryStake,
QueryStakesParams, Rewind, SnapshotExport, SnapshotImport, StakeAuthorization,
BuildStakeParams, BuildStakeResponse, BuildUnstake, BuildUnstakeParams, BuildVtt,
ClearPeers, DropAllPeers, EstimatePriority, GetBalance, GetBalanceTarget,
GetBlocksEpochRange, GetConsolidatedPeers, GetDataRequestInfo, GetEpoch,
GetHighestCheckpointBeacon, GetItemBlock, GetItemSuperblock, GetItemTransaction,
GetKnownPeers, GetMemoryTransaction, GetMempool, GetNodeStats, GetReputation,
GetSignalingInfo, GetState, GetSupplyInfo, GetUtxoInfo, InitializePeers,
IsConfirmedBlock, QueryStake, QueryStakesParams, Rewind, SnapshotExport,
SnapshotImport, StakeAuthorization,
},
peers_manager::PeersManager,
sessions_manager::SessionsManager,
Expand Down Expand Up @@ -289,6 +290,15 @@ pub fn attach_sensitive_methods<H>(
|params| authorize_stake(params.parse()),
))
});

server.add_actix_method(system, "unstake", move |params| {
Box::pin(if_authorized(
enable_sensitive_methods,
"unstake",
params,
|params| unstake(params.parse()),
))
});
}

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

/// Build an unstake transaction
pub async fn unstake(params: Result<BuildUnstakeParams, Error>) -> JsonRpcResult {
// Short-circuit if parameters are wrong
let params = params?;

let operator = params
.operator
.try_do_magic(|hex_str| PublicKeyHash::from_bech32(get_environment(), &hex_str))
.map_err(internal_error)?;

// Construct a BuildUnstake message that we can relay to the ChainManager for creation of the Unstake transaction
let build_unstake = BuildUnstake {
operator,
value: params.value,
dry_run: params.dry_run,
};

ChainManager::from_registry()
.send(build_unstake)
.map(|res| match res {
Ok(Ok(transaction)) => serde_json::to_value(transaction).map_err(internal_error),
Ok(Err(e)) => {
let err = internal_error_s(e);
Err(err)
}
Err(e) => {
let err = internal_error_s(e);
Err(err)
}
})
.await
}

/// Create a stake authorization for the given address.
///
/// The output of this method is a required argument to call the Stake method.
Expand Down

0 comments on commit 674a590

Please sign in to comment.