diff --git a/src/canister/upgrade_user_token_sns_canister.rs b/src/canister/upgrade_user_token_sns_canister.rs index 6cf96ec..fbff71d 100644 --- a/src/canister/upgrade_user_token_sns_canister.rs +++ b/src/canister/upgrade_user_token_sns_canister.rs @@ -10,7 +10,7 @@ use ic_agent::Agent; use ic_sns_governance::init::GovernanceCanisterInitPayloadBuilder; use k256::elliptic_curve::rand_core::le; use serde::{Deserialize, Serialize}; -use std::{error::Error, sync::Arc, vec}; +use std::{error::Error, sync::Arc, time::Duration, vec}; use yral_canisters_client::{ individual_user_template::{DeployedCdaoCanisters, IndividualUserTemplate}, platform_orchestrator::{self, PlatformOrchestrator}, @@ -191,6 +191,28 @@ pub async fn setup_sns_canisters_of_a_user_canister_for_upgrade( Ok(()) } +pub async fn verify_if_proposal_executed_successfully_impl( + agent: &Agent, + qstash_client: &QStashClient, + verify_proposal_request: VerifyUpgradeProposalRequest, +) -> Result> { + let sns_governance = SnsGovernance(verify_proposal_request.sns_canisters.governance, agent); + + let proposal_executed_successfully = check_if_the_proposal_executed_successfully( + &sns_governance, + verify_proposal_request.proposal_id, + ) + .await?; + + if proposal_executed_successfully { + qstash_client + .upgrade_sns_creator_dao_canister(verify_proposal_request.sns_canisters) + .await?; + } + + Ok(proposal_executed_successfully) +} + async fn upgrade_sns_governance_canister_with_custom_wasm( agent: &Agent, governance_canister_id: Principal, @@ -219,6 +241,9 @@ async fn upgrade_sns_governance_canister_with_custom_wasm( .start_canister(&governance_canister_id) .await?; + //wait for the canister to startup + tokio::time::sleep(Duration::from_secs(5)).await; + upgrade_result } @@ -405,7 +430,7 @@ pub async fn is_upgrade_required( .deployed_version .ok_or("deployed version not found")?; - let result = check_if_version_matches_deployed_canister_version(deployed_version); + let result = !check_if_version_matches_deployed_canister_version(deployed_version); Ok(result) } @@ -476,11 +501,16 @@ pub async fn recharge_canisters( pub async fn upgrade_user_token_sns_canister_impl( agent: &Agent, - governance_canister_id: String, -) -> Result> { - let governance_canister_id_principal = Principal::from_text(governance_canister_id)?; + qstash_client: &QStashClient, + sns_canisters: SnsCanisters, +) -> Result<(), Box> { + let sns_governance = SnsGovernance(sns_canisters.governance, agent); + + let is_upgrade_required = is_upgrade_required(&sns_governance).await?; - let sns_governance = SnsGovernance(governance_canister_id_principal, agent); + if !is_upgrade_required { + return Ok(()); + } let neuron_list = sns_governance .list_neurons(ListNeurons { @@ -516,7 +546,15 @@ pub async fn upgrade_user_token_sns_canister_impl( if let Command1::MakeProposal(proposal_id) = proposal_id { let proposal_id_u64 = proposal_id.proposal_id.ok_or("proposal id not found")?.id; - Ok(proposal_id_u64) + let verify_request = VerifyUpgradeProposalRequest { + sns_canisters: sns_canisters, + proposal_id: proposal_id_u64, + }; + + qstash_client + .verify_sns_canister_upgrade_proposal(verify_request) + .await?; + Ok(()) } else { Err(format!("{:?}", proposal_id).into()) } diff --git a/src/qstash/mod.rs b/src/qstash/mod.rs index e159d4d..78a42cb 100644 --- a/src/qstash/mod.rs +++ b/src/qstash/mod.rs @@ -33,7 +33,8 @@ use crate::{ check_if_the_proposal_executed_successfully, is_upgrade_required, setup_sns_canisters_of_a_user_canister_for_upgrade, upgrade_user_token_sns_canister_for_entire_network_impl, - upgrade_user_token_sns_canister_impl, SnsCanisters, VerifyUpgradeProposalRequest, + upgrade_user_token_sns_canister_impl, verify_if_proposal_executed_successfully_impl, + SnsCanisters, VerifyUpgradeProposalRequest, }, consts::ICP_LEDGER_CANISTER_ID, events::{ @@ -311,26 +312,14 @@ async fn upgrade_sns_creator_dao_canister( State(state): State>, Json(req): Json, ) -> Result { - let governance_canister_id = req.governance.to_text(); - let result = - upgrade_user_token_sns_canister_impl(&state.agent, governance_canister_id.clone()).await; + upgrade_user_token_sns_canister_impl(&state.agent, &state.qstash_client, req).await; match result { - Ok(proposal_id) => { - let verify_request = VerifyUpgradeProposalRequest { - sns_canisters: req, - proposal_id, - }; - state - .qstash_client - .verify_sns_canister_upgrade_proposal(verify_request) - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - + Ok(()) => { let response = Response::builder() .status(StatusCode::OK) - .body(format!("upgrade proposal id: {} submitted", proposal_id).into()) + .body(format!("upgrade proposal id submitted").into()) .unwrap(); Ok(response) @@ -350,37 +339,18 @@ async fn verify_sns_canister_upgrade_proposal( State(state): State>, Json(verify_sns_canister_proposal_request): Json, ) -> Result { - let governance_canister_principal = verify_sns_canister_proposal_request - .sns_canisters - .governance; - - let sns_governance = SnsGovernance(governance_canister_principal, &state.agent); - - let result = check_if_the_proposal_executed_successfully( - &sns_governance, - verify_sns_canister_proposal_request.proposal_id, + let result = verify_if_proposal_executed_successfully_impl( + &state.agent, + &state.qstash_client, + verify_sns_canister_proposal_request, ) .await; - let is_upgrade_required = is_upgrade_required(&sns_governance).await.unwrap_or(true); - match result { - Ok(executed) if executed => { - if is_upgrade_required { - state - .qstash_client - .upgrade_sns_creator_dao_canister( - verify_sns_canister_proposal_request.sns_canisters, - ) - .await - .map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?; - } - - Ok(Response::builder() - .status(StatusCode::OK) - .body("Proposal executed successfully".into()) - .unwrap()) - } + Ok(executed) if executed => Ok(Response::builder() + .status(StatusCode::OK) + .body("Proposal executed successfully".into()) + .unwrap()), Ok(_) => Err(StatusCode::BAD_REQUEST), Err(e) => Ok(Response::builder()