Skip to content

Commit

Permalink
Added a new endpoint to Activate an account that was removed by being…
Browse files Browse the repository at this point in the history
… offline
  • Loading branch information
MauricinhoMorales committed Mar 7, 2024
1 parent 538716b commit 13acf6d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ async fn main() -> std::io::Result<()> {
"validator/add_validator",
web::post().to(validator::add_validator),
)
.route(
"validator/activate",
web::post().to(validator::activate_validator),
)
.route("validator/set_keys", web::post().to(validator::set_keys))
.route(
"validator/remove_validator",
Expand Down
38 changes: 38 additions & 0 deletions src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ pub async fn add_validator(
}
}

pub async fn activate_validator(
data: web::Data<AppState>,
req: web::Json<AddValidatorInput>,
) -> error::Result<HttpResponse> {
let pair = get_pair_from_seed(&req.seed)?;
let signer = PairSigner::new(pair);
let validator_public =
sp_core::sr25519::Public::from_str(req.validator_id.as_str()).map_err(map_account_err)?;
let validator_bytes: [u8; 32] = validator_public.0; // Convert Public key to a byte array
let validator_id = subxt::utils::AccountId32::from(validator_bytes); // Create AccountId32 from the byte array
let api = &data.api;

let call = sugarfunge::tx().validator_set().add_validator_again(validator_id);

let result = api
.tx()
.sign_and_submit_then_watch(&call, &signer, Default::default())
.await
.map_err(map_subxt_err)?
.wait_for_finalized_success()
.await
.map_err(map_sf_err)?;

let result = result
.find_first::<sugarfunge::validator_set::events::ValidatorAdditionInitiated>()
.map_err(map_subxt_err)?;

match result {
Some(event) => Ok(HttpResponse::Ok().json(AddValidatorOutput {
validator_id: ValidatorId::from(event.0.to_string()),
})),
None => Ok(HttpResponse::BadRequest().json(RequestError {
message: json!("Failed to find sugarfunge::validator::events::ActivateValidator"),
description: String::new(),
})),
}
}

pub async fn remove_validator(
data: web::Data<AppState>,
req: web::Json<RemoveValidatorInput>,
Expand Down

0 comments on commit 13acf6d

Please sign in to comment.