From 83f6df47f351c7ac707a6ded186b02e2abf1fe98 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Wed, 7 Jun 2023 12:54:42 +0200
Subject: [PATCH] WIP add claim_ud command

---
 src/commands.rs          |  1 +
 src/commands/account.rs  | 12 ++++++------
 src/commands/identity.rs |  1 -
 src/commands/ud.rs       | 42 ++++++++++++++++++++++++++++++++++++++++
 src/main.rs              |  9 ++++++---
 5 files changed, 55 insertions(+), 10 deletions(-)
 create mode 100644 src/commands/ud.rs

diff --git a/src/commands.rs b/src/commands.rs
index ea2a962..1384ca8 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -10,3 +10,4 @@ pub mod runtime;
 pub mod smith;
 pub mod sudo;
 pub mod transfer;
+pub mod ud;
diff --git a/src/commands/account.rs b/src/commands/account.rs
index 3bad919..101e088 100644
--- a/src/commands/account.rs
+++ b/src/commands/account.rs
@@ -1,8 +1,7 @@
 use crate::*;
 
-use anyhow::Result;
-
-pub async fn get_balance(data: Data) -> Result<()> {
+/// get balance
+pub async fn get_balance(data: Data) -> Result<(), anyhow::Error> {
 	let account_id = data.address();
 	let account_info = get_account_info(data.client(), &account_id).await?;
 	if let Some(account_info) = account_info {
@@ -16,12 +15,13 @@ pub async fn get_balance(data: Data) -> Result<()> {
 	Ok(())
 }
 
+/// get account info
 pub async fn get_account_info(
 	client: &Client,
 	account_id: &AccountId,
-) -> Result<Option<AccountInfo>> {
-	Ok(client
+) -> Result<Option<AccountInfo>, subxt::Error> {
+	client
 		.storage()
 		.fetch(&runtime::storage().system().account(account_id), None)
-		.await?)
+		.await
 }
diff --git a/src/commands/identity.rs b/src/commands/identity.rs
index 0a3a430..efa5c43 100644
--- a/src/commands/identity.rs
+++ b/src/commands/identity.rs
@@ -7,7 +7,6 @@ use crate::runtime::runtime_types::sp_core::sr25519::Signature;
 use crate::runtime::runtime_types::sp_runtime::MultiSignature;
 use sp_core::{crypto::AccountId32, sr25519::Pair};
 use std::str::FromStr;
-use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
 
 pub async fn get_identity(
 	data: &Data,
diff --git a/src/commands/ud.rs b/src/commands/ud.rs
new file mode 100644
index 0000000..4f6fc13
--- /dev/null
+++ b/src/commands/ud.rs
@@ -0,0 +1,42 @@
+use crate::*;
+
+pub async fn claim_ud(data: Data) -> Result<(), anyhow::Error> {
+	let progress = data
+		.client()
+		.tx()
+		.sign_and_submit_then_watch(
+			&runtime::tx().universal_dividend().claim_uds(),
+			&PairSigner::new(data.keypair()),
+			BaseExtrinsicParamsBuilder::new(),
+		)
+		.await?;
+
+	let events = track_progress(progress).await?;
+
+	if let Some(e) = events.find_first::<runtime::universal_dividend::events::UdsClaimed>()? {
+		println!("{e:?}");
+	}
+	Ok(())
+}
+
+#[derive(Clone, Default, Debug, clap::Parser)]
+pub enum Subcommand {
+	#[default]
+	/// Claim uds
+	ClaimUds,
+}
+
+/// handle ud commands
+pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> {
+	// build indexer because it is needed for all subcommands
+	let mut data = data.build_client().await?;
+	// match subcommand
+	match command {
+		Subcommand::ClaimUds => {
+            data = data.build_keypair();
+			claim_ud(data).await?;
+		}
+	};
+
+    Ok(())
+}
diff --git a/src/main.rs b/src/main.rs
index 9d16872..c069b16 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,6 +12,8 @@ use data::*;
 use keys::*;
 use serde::Deserialize;
 use sp_core::{sr25519::Pair, Pair as _, H256};
+use subxt::blocks::ExtrinsicEvents;
+use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner, TxStatus};
 
 #[cfg(feature = "gdev")]
 #[subxt::subxt(
@@ -99,9 +101,6 @@ pub struct Args {
 	network: Option<String>,
 }
 
-use subxt::blocks::ExtrinsicEvents;
-use subxt::tx::TxStatus;
-
 /// track progress of transaction on the network
 /// until it is in block with success or failure
 pub async fn track_progress(
@@ -277,6 +276,9 @@ pub enum Subcommand {
 	RuntimeInfo,
 	/// Check current block
 	CurrentBlock,
+	/// Universal Dividend subcommands
+	#[clap(subcommand)]
+	Ud(commands::ud::Subcommand),
 	/// Indexer subcommands
 	#[clap(subcommand)]
 	Indexer(indexer::Subcommand),
@@ -600,6 +602,7 @@ async fn main() -> Result<(), GcliError> {
 					.unwrap()
 			);
 		}
+		Subcommand::Ud(subcommand) => commands::ud::handle_command(data, subcommand).await?,
 		Subcommand::Indexer(subcommand) => indexer::handle_command(data, subcommand).await?,
 		Subcommand::Config(subcommand) => conf::handle_command(data, subcommand)?,
 	}
-- 
GitLab