Skip to content
Snippets Groups Projects
Select Git revision
  • ac5be766e02a652fb9f524824118708a926a418e
  • dev default protected
  • release/1.9.1 protected
  • pini-1.8-docker
  • pini-sync-onlypeers
  • duniter-v2s-issue-123-industrialize-releases
  • feature/build-aarch64-nodejs16
  • release/1.8 protected
  • pini-docker
  • ci_tags
  • fix/1448/1.8/txs_not_stored
  • feature/node-20
  • fix/1441/node_summary_with_storage
  • fix/1442/improve_bma_tx_history
  • feature/wotwizard-1.8
  • release/1.9 protected
  • 1.7 protected
  • feature/docker-set-latest protected
  • feature/fast-docker-build-1.8.4
  • fast-docker-build protected
  • feature/dump-distance
  • v1.8.7 protected
  • v1.8.7-rc4 protected
  • v1.8.7-rc3 protected
  • v1.8.7-rc2 protected
  • v1.8.7-rc1 protected
  • v1.8.6 protected
  • v1.7.23 protected
  • v1.8.5 protected
  • v1.8.4 protected
  • v1.8.3 protected
  • v1.8.2 protected
  • v1.8.1 protected
  • v1.8.0 protected
  • v1.8.0-rc1 protected
  • v1.8.0-beta5 protected
  • v1.8.0-beta4 protected
  • v1.8.0-beta3 protected
  • v1.8.0-beta2 protected
  • v1.8.0-beta protected
  • v1.7.21 protected
41 results

index.js

Blame
  • oneshot.rs 4.34 KiB
    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) -> Result<(), GcliError> {
    	// 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<(), subxt::Error> {
    	println!(
    		"balance of oneshot account {} is: {}",
    		data.address(),
    		data.client()
    			.storage()
    			.at_latest()
    			.await?
    			.fetch(
    				&runtime::storage()
    					.oneshot_account()
    					.oneshot_accounts(data.address()),
    			)
    			.await?
    			.unwrap_or(0)
    	);
    
    	Ok(())
    }
    
    /// create oneshot account
    pub async fn create_oneshot_account(
    	data: &Data,
    	balance: u64,
    	dest: AccountId,
    ) -> Result<(), subxt::Error> {
    	submit_call_and_look_event::<
    		runtime::oneshot_account::events::OneshotAccountCreated,
    		StaticPayload<runtime::oneshot_account::calls::types::CreateOneshotAccount>,
    	>(
    		data,
    		&runtime::tx()
    			.oneshot_account()
    			.create_oneshot_account(dest.into(), balance),
    	)
    	.await
    }
    
    /// 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()
    		.at_latest()
    		.await?
    		.fetch(&runtime::storage().system().number())
    		.await?
    		.unwrap();
    	let payload = &runtime::tx().oneshot_account().consume_oneshot_account(
    		number,
    		if dest_oneshot {
    			runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(dest.into())
    		} else {
    			runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into())
    		},
    	);
    	submit_call_and_look_event::<
    		runtime::oneshot_account::events::OneshotAccountConsumed,
    		StaticPayload<runtime::oneshot_account::calls::types::ConsumeOneshotAccount>,
    	>(data, payload)
    	.await
    }
    
    /// consume oneshot account with remaining
    pub async fn consume_oneshot_account_with_remaining(
    	data: &Data,
    	balance: u64,
    	dest: AccountId,
    	dest_oneshot: bool,
    	remaining_to: AccountId,
    	remaining_to_oneshot: bool,
    ) -> Result<(), subxt::Error> {
    	let client = data.client();
    
    	let number = client
    		.storage()
    		.at_latest()
    		.await?
    		.fetch(&runtime::storage().system().number())
    		.await?
    		.unwrap();
    
    	let payload = &runtime::tx()
    		.oneshot_account()
    		.consume_oneshot_account_with_remaining(
    			number,
    			if dest_oneshot {
    				runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(dest.into())
    			} else {
    				runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into())
    			},
    			if remaining_to_oneshot {
    				runtime::runtime_types::pallet_oneshot_account::types::Account::Oneshot(
    					remaining_to.into(),
    				)
    			} else {
    				runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
    					remaining_to.into(),
    				)
    			},
    			balance,
    		);
    
    	submit_call_and_look_event::<
    		runtime::oneshot_account::events::OneshotAccountConsumed,
    		StaticPayload<runtime::oneshot_account::calls::types::ConsumeOneshotAccountWithRemaining>,
    	>(data, payload)
    	.await
    }