-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement additional helpers in ic-nervous-system-agent
- Loading branch information
Showing
17 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::CallCanisters; | ||
use ic_nns_common::pb::v1::ProposalId; | ||
use ic_nns_constants::GOVERNANCE_CANISTER_ID; | ||
use ic_nns_governance_api::pb::v1::{ | ||
GetNeuronsFundAuditInfoRequest, GetNeuronsFundAuditInfoResponse, | ||
}; | ||
|
||
pub async fn get_neurons_fund_audit_info<C: CallCanisters>( | ||
agent: &C, | ||
nns_proposal_id: ProposalId, | ||
) -> Result<GetNeuronsFundAuditInfoResponse, C::Error> { | ||
let request = GetNeuronsFundAuditInfoRequest { | ||
nns_proposal_id: Some(nns_proposal_id), | ||
}; | ||
agent.call(GOVERNANCE_CANISTER_ID, request).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod governance; | ||
pub mod sns_wasm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,82 @@ | ||
use ic_base_types::PrincipalId; | ||
use ic_sns_swap::pb::v1::{ | ||
GetDerivedStateRequest, GetDerivedStateResponse, GetInitRequest, GetInitResponse, | ||
ListSnsNeuronRecipesRequest, ListSnsNeuronRecipesResponse, SnsNeuronRecipe, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
|
||
use crate::CallCanisters; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct SwapCanister { | ||
pub canister_id: PrincipalId, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum ListAllSnsNeuronRecipesError<CallCanistersError: std::error::Error + 'static> { | ||
#[error(transparent)] | ||
CanisterCallError(#[from] CallCanistersError), | ||
#[error("There seem to be too many neuron recipes ({0}).")] | ||
TooManyRecipes(u64), | ||
} | ||
|
||
impl SwapCanister { | ||
pub fn new(canister_id: impl Into<PrincipalId>) -> Self { | ||
let canister_id = canister_id.into(); | ||
Self { canister_id } | ||
} | ||
|
||
pub async fn get_derived_state<C: CallCanisters>( | ||
&self, | ||
agent: &C, | ||
) -> Result<GetDerivedStateResponse, C::Error> { | ||
agent | ||
.call(self.canister_id, GetDerivedStateRequest {}) | ||
.await | ||
} | ||
|
||
pub async fn get_init<C: CallCanisters>(&self, agent: &C) -> Result<GetInitResponse, C::Error> { | ||
agent.call(self.canister_id, GetInitRequest {}).await | ||
} | ||
|
||
pub async fn list_sns_neuron_recipes<C: CallCanisters>( | ||
&self, | ||
agent: &C, | ||
limit: u32, | ||
offset: u64, | ||
) -> Result<ListSnsNeuronRecipesResponse, C::Error> { | ||
agent | ||
.call( | ||
self.canister_id, | ||
ListSnsNeuronRecipesRequest { | ||
limit: Some(limit), | ||
offset: Some(offset), | ||
}, | ||
) | ||
.await | ||
} | ||
|
||
pub async fn list_all_sns_neuron_recipes<C: CallCanisters>( | ||
&self, | ||
agent: &C, | ||
) -> Result<Vec<SnsNeuronRecipe>, ListAllSnsNeuronRecipesError<C::Error>> { | ||
let mut sns_neuron_recipes: Vec<SnsNeuronRecipe> = vec![]; | ||
let batch_size = 10_000_u64; | ||
let num_calls = 100_u64; | ||
for i in 0..num_calls { | ||
let new_sns_neuron_recipes = self | ||
.list_sns_neuron_recipes(agent, batch_size as u32, batch_size * i) | ||
.await | ||
.map_err(ListAllSnsNeuronRecipesError::CanisterCallError)?; | ||
if new_sns_neuron_recipes.sns_neuron_recipes.is_empty() { | ||
return Ok(sns_neuron_recipes); | ||
} else { | ||
sns_neuron_recipes.extend(new_sns_neuron_recipes.sns_neuron_recipes.into_iter()) | ||
} | ||
} | ||
Err(ListAllSnsNeuronRecipesError::TooManyRecipes( | ||
batch_size * num_calls, | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use ic_nervous_system_clients::Request; | ||
|
||
impl Request for crate::pb::v1::GetNeuronsFundAuditInfoRequest { | ||
type Response = crate::pb::v1::GetNeuronsFundAuditInfoResponse; | ||
const METHOD: &'static str = "get_neurons_fund_audit_info"; | ||
const UPDATE: bool = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use ic_nervous_system_clients::Request; | ||
|
||
impl Request for crate::pb::v1::GetDerivedStateRequest { | ||
type Response = crate::pb::v1::GetDerivedStateResponse; | ||
const METHOD: &'static str = "get_derived_state"; | ||
const UPDATE: bool = false; | ||
} | ||
|
||
impl Request for crate::pb::v1::GetInitRequest { | ||
type Response = crate::pb::v1::GetInitResponse; | ||
const METHOD: &'static str = "get_init"; | ||
const UPDATE: bool = false; | ||
} | ||
|
||
impl Request for crate::pb::v1::ListSnsNeuronRecipesRequest { | ||
type Response = crate::pb::v1::ListSnsNeuronRecipesResponse; | ||
const METHOD: &'static str = "list_sns_neuron_recipes"; | ||
const UPDATE: bool = false; | ||
} |