Skip to content
Snippets Groups Projects
collective.rs 2.22 KiB
Newer Older
Hugo Trentesaux's avatar
Hugo Trentesaux committed
use crate::*;

use anyhow::Result;
use sp_core::{sr25519::Pair, H256};
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
pub async fn technical_committee_members(data: &Data) -> Result<()> {
	let client = data.client();
	let indexer = data.indexer.clone();

	let parent_hash = client
		.storage()
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		.fetch(&runtime::storage().system().parent_hash(), None)
		.await?
		.unwrap();
	for account_id in client
		.storage()
		.fetch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&runtime::storage().technical_committee().members(),
			Some(parent_hash),
		)
		.await?
		.unwrap_or_default()
	{
		println!(
			"{}",
			if let Some(indexer) = &indexer {
				indexer
					.username_by_pubkey(&account_id.to_string())
					.await
					.ok()
					.flatten()
			} else {
				client
					.storage()
					.fetch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
						&runtime::storage().identity().identity_index_of(&account_id),
						Some(parent_hash),
					)
					.await
					.ok()
					.flatten()
					.map(|identity_id| format!("{identity_id}"))
			}
			.unwrap_or_else(|| account_id.to_string(),)
		);
	}
	Ok(())

// TODO:
// * better formatting (format pubkeys to SS58 and add usernames)
// * display proposals indices
pub async fn technical_committee_proposals(client: &Client) -> Result<()> {
	let parent_hash = client
		.storage()
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		.fetch(&runtime::storage().system().parent_hash(), None)
		.await?
		.unwrap();
	let mut proposals_iter = client
		.storage()
		.iter(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			runtime::storage()
				.technical_committee()
				.proposal_of(H256::default()),
			10,
			Some(parent_hash),
		)
		.await?;
	while let Some((proposal_hash, proposal)) = proposals_iter.next().await? {
		println!("{}", hex::encode(&proposal_hash.0[32..64]));
		println!("{proposal:#?}");
		println!();
	}
	Ok(())

pub async fn technical_committee_vote(
	pair: Pair,
	client: &Client,
	proposal_hash: H256,
	proposal_index: u32,
	vote: bool,
) -> Result<(), subxt::Error> {
	let progress = client
		.tx()
		.sign_and_submit_then_watch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&runtime::tx()
				.technical_committee()
				.vote(proposal_hash, proposal_index, vote),
			&PairSigner::new(pair),
			BaseExtrinsicParamsBuilder::new(),
		)
		.await?;

	let events = track_progress(progress).await?;

	if let Some(e) = events.find_first::<runtime::technical_committee::events::Voted>()? {
		println!("{e:?}");
	}

	Ok(())