diff --git a/src/main.rs b/src/main.rs index 04b32a5..6797e00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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", diff --git a/src/validator.rs b/src/validator.rs index d7204c2..65074c9 100644 --- a/src/validator.rs +++ b/src/validator.rs @@ -54,6 +54,44 @@ pub async fn add_validator( } } +pub async fn activate_validator( + data: web::Data, + req: web::Json, +) -> error::Result { + 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::() + .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, req: web::Json,