Skip to content

Commit

Permalink
Add more ergonomic submit_proposal function
Browse files Browse the repository at this point in the history
  • Loading branch information
anchpop committed Jan 14, 2025
1 parent 725b88d commit 99cf436
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions rs/nervous_system/agent/src/nns/management_canister.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

42 changes: 39 additions & 3 deletions rs/nervous_system/agent/src/sns/governance.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use crate::{null_request::NullRequest, CallCanisters};
use ic_base_types::PrincipalId;
use ic_sns_governance::pb::v1::{
manage_neuron, GetMetadataRequest, GetMetadataResponse, GetMode, GetModeResponse,
GetRunningSnsVersionRequest, GetRunningSnsVersionResponse, ManageNeuron, ManageNeuronResponse,
NervousSystemParameters, NeuronId,
manage_neuron, manage_neuron_response, GetMetadataRequest, GetMetadataResponse, GetMode,
GetModeResponse, GetRunningSnsVersionRequest, GetRunningSnsVersionResponse, ManageNeuron,
ManageNeuronResponse, NervousSystemParameters, NeuronId, Proposal, ProposalId,
};
use serde::{Deserialize, Serialize};
use std::error::Error;

#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub struct GovernanceCanister {
pub canister_id: PrincipalId,
}

#[derive(Debug, thiserror::Error)]
pub enum SubmitProposalError<C: Error> {
#[error("Failed to call SNS Governance")]
CallGovernanceError(#[source] C),
#[error("SNS Governance did not confirm that the proposal was made: {0:?}")]
ProposalNotMade(ManageNeuronResponse),
}

impl GovernanceCanister {
pub async fn metadata<C: CallCanisters>(
&self,
Expand Down Expand Up @@ -57,6 +66,33 @@ impl GovernanceCanister {
};
agent.call(self.canister_id, request).await
}

pub async fn submit_proposal<C: CallCanisters>(
&self,
agent: &C,
neuron_id: NeuronId,
proposal: Proposal,
) -> Result<ProposalId, SubmitProposalError<C::Error>> {
let response = self
.manage_neuron(
agent,
neuron_id,
manage_neuron::Command::MakeProposal(proposal),
)
.await
.map_err(SubmitProposalError::CallGovernanceError)?;

if let Some(manage_neuron_response::Command::MakeProposal(
manage_neuron_response::MakeProposalResponse {
proposal_id: Some(proposal_id),
},
)) = response.command
{
Ok(proposal_id)
} else {
Err(SubmitProposalError::ProposalNotMade(response))
}
}
}

impl GovernanceCanister {
Expand Down
21 changes: 5 additions & 16 deletions rs/sns/cli/src/upgrade_sns_controlled_canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ pub async fn exec(args: UpgradeSnsControlledCanisterArgs, agent: &Agent) -> Resu
canister_id: sns.governance.canister_id,
};

let command = manage_neuron::Command::MakeProposal(Proposal {
let proposal = Proposal {
title: format!(
"Upgrade SNS-controlled canister {}",
target_canister_id.get()
),
summary: format!(""),
summary: format!(""), // TODO: allow the user to specify a summary
url: proposal_url.to_string(),
action: Some(Action::UpgradeSnsControlledCanister(
UpgradeSnsControlledCanister {
Expand All @@ -155,23 +155,12 @@ pub async fn exec(args: UpgradeSnsControlledCanisterArgs, agent: &Agent) -> Resu
// TODO: use `uploaded_chunk_hashes` / `sha256_hash`
},
)),
});
};

let ManageNeuronResponse { command } = sns_governance
.manage_neuron(agent, sns_neuron_id.0, command)
let proposal_id = sns_governance
.submit_proposal(agent, sns_neuron_id.0, proposal)
.await?;

let proposal_id = match command {
Some(manage_neuron_response::Command::MakeProposal(
manage_neuron_response::MakeProposalResponse {
proposal_id: Some(proposal_id),
},
)) => proposal_id,
_ => {
bail!("SNS Governance did not confirm that the proposal was made ({command:?}).")
}
};

let proposal_url = format!(
"https://nns.ic0.app/proposal/?u={}&proposal={}",
root_canister_id.get(),
Expand Down

0 comments on commit 99cf436

Please sign in to comment.