Skip to content
Snippets Groups Projects
oneshot.rs 4.79 KiB
Newer Older
Hugo Trentesaux's avatar
Hugo Trentesaux committed
use crate::*;
/// define oneshot account subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
	/// get balance of oneshot account
	#[default]
	Balance,
	/// create a oneshot account
	Create { balance: u64, dest: AccountId },
	/// consume a oneshot account
	Consume {
		dest: AccountId,
		#[clap(long = "oneshot")]
		dest_oneshot: bool,
	},
	/// consume a oneshot account whith remaining sent to an other account
	ConsumeWithRemaining {
		balance: u64,
		dest: AccountId,
		#[clap(long = "one")]
		dest_oneshot: bool,
		remaining_to: AccountId,
		#[clap(long = "rem-one")]
		remaining_to_oneshot: bool,
	},
}

/// handle oneshot 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::Balance => oneshot_account_balance(&data).await?,
		Subcommand::Create { balance, dest } => {
			data = data.build_client().await?;
			create_oneshot_account(&data, balance, dest).await?;
		}
		Subcommand::Consume { dest, dest_oneshot } => {
			data = data.build_client().await?;
			consume_oneshot_account(&data, dest, dest_oneshot).await?;
		}
		Subcommand::ConsumeWithRemaining {
			balance,
			dest,
			dest_oneshot,
			remaining_to,
			remaining_to_oneshot,
		} => {
			data = data.build_client().await?;
			consume_oneshot_account_with_remaining(
				&data,
				balance,
				dest,
				dest_oneshot,
				remaining_to,
				remaining_to_oneshot,
			)
			.await?;
		}
	};

	Ok(())
}

/// get balance of oneshot account
pub async fn oneshot_account_balance(data: &Data) -> Result<(), anyhow::Error> {
	println!(
		"balance of oneshot account {} is: {}",
		data.address(),
		data.client()
			.storage()
			.fetch(
				&runtime::storage()
					.oneshot_account()
					.oneshot_accounts(data.address()),
				None
			)
			.await?
			.unwrap_or(0)
	);

	Ok(())
}
/// create oneshot account
pub async fn create_oneshot_account(
	balance: u64,
	dest: AccountId,
) -> Result<(), subxt::Error> {
	let progress = data
		.client()
		.tx()
		.sign_and_submit_then_watch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&runtime::tx()
				.oneshot_account()
				.create_oneshot_account(dest.into(), balance),
			&PairSigner::new(data.keypair()),
			BaseExtrinsicParamsBuilder::new(),
		)
		.await?;

	let events = track_progress(progress).await?;
	if let Some(e) =
		events.find_first::<runtime::oneshot_account::events::OneshotAccountCreated>()?
	{
		println!("{e:?}");
	}
	Ok(())
/// consume oneshot account
pub async fn consume_oneshot_account(
	data: &Data,

	dest: AccountId,
	dest_oneshot: bool,
) -> Result<(), subxt::Error> {
	let client = data.client();

	let number = client
		.storage()
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		.fetch(&runtime::storage().system().number(), None)
		.await?
		.unwrap();
	let progress = client
		.tx()
		.sign_and_submit_then_watch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&runtime::tx().oneshot_account().consume_oneshot_account(
				number,
				if dest_oneshot {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
					runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
						dest.into(),
					)
				} else {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
					runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
						dest.into(),
					)
			&PairSigner::new(data.keypair()),
			BaseExtrinsicParamsBuilder::new(),
		)
		.await?;

	let events = track_progress(progress).await?;
	if let Some(e) =
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		events.find_first::<runtime::oneshot_account::events::OneshotAccountConsumed>()?
	{
		println!("{e:?}");
	}
	Ok(())
/// consume oneshot account with remaining
pub async fn consume_oneshot_account_with_remaining(
	balance: u64,
	dest_oneshot: bool,
	remaining_to: AccountId,
	remaining_to_oneshot: bool,
) -> Result<(), subxt::Error> {
	let client = data.client();

	let number = client
		.storage()
Hugo Trentesaux's avatar
Hugo Trentesaux committed
		.fetch(&runtime::storage().system().number(), None)
		.await?
		.unwrap();
	let progress = client
		.tx()
		.sign_and_submit_then_watch(
Hugo Trentesaux's avatar
Hugo Trentesaux committed
			&runtime::tx()
				.oneshot_account()
				.consume_oneshot_account_with_remaining(
					number,
					if dest_oneshot {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
						runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
							dest.into(),
						)
					} else {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
						runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
							dest.into(),
						)
					},
					if remaining_to_oneshot {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
						runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
							remaining_to.into(),
						)
					} else {
Hugo Trentesaux's avatar
Hugo Trentesaux committed
						runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
							remaining_to.into(),
						)
					},
					balance,
				),
			&PairSigner::new(data.keypair()),
			BaseExtrinsicParamsBuilder::new(),
		)
		.await?;

	let events = track_progress(progress).await?;
	if let Some(e) =
		events.find_first::<runtime::oneshot_account::events::OneshotAccountConsumed>()?
	{
		println!("{e:?}");
	}
	Ok(())