quantus-cli 1.4.0

Command line interface and library for interacting with the Quantus Network
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! `quantus call` subcommand - generic pallet calls
use crate::{
	chain::quantus_subxt, error::QuantusError, log_error, log_print, log_success, log_verbose,
	wallet::QuantumKeyPair,
};
use colored::Colorize;
use serde_json::Value;
use sp_core::crypto::{AccountId32, Ss58Codec};

/// Execute a generic call to any pallet
pub async fn execute_generic_call(
	quantus_client: &crate::chain::client::QuantusClient,
	pallet: &str,
	call: &str,
	args: Vec<Value>,
	from_keypair: &QuantumKeyPair,
	tip: Option<String>,
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	log_print!("🚀 Executing generic call");
	log_print!("Pallet: {}", pallet.bright_green());
	log_print!("Call: {}", call.bright_cyan());
	log_print!("From: {}", from_keypair.to_account_id_ss58check().bright_yellow());
	if let Some(tip) = &tip {
		log_print!("Tip: {}", tip.bright_magenta());
	}

	// Convert our QuantumKeyPair to subxt Signer
	let _signer = from_keypair
		.to_subxt_signer()
		.map_err(|e| QuantusError::NetworkError(format!("Failed to convert keypair: {e:?}")))?;

	// Validate pallet/call exists in metadata
	let metadata = quantus_client.client().metadata();
	let pallet_metadata = metadata
		.pallet_by_name(pallet)
		.ok_or_else(|| QuantusError::Generic(format!("Pallet '{pallet}' not found in metadata")))?;

	log_verbose!("✅ Found pallet '{}' with index {}", pallet, pallet_metadata.index());

	// Find the call in the pallet
	let call_metadata = pallet_metadata.call_variant_by_name(call).ok_or_else(|| {
		QuantusError::Generic(format!("Call '{call}' not found in pallet '{pallet}'"))
	})?;

	log_verbose!("✅ Found call '{}' with index {}", call, call_metadata.index);

	// Parse tip amount if provided
	let tip_amount = if let Some(tip_str) = &tip { tip_str.parse::<u128>().ok() } else { None };

	// Create and submit extrinsic based on pallet and call
	log_print!("🔧 Creating extrinsic for {}.{}", pallet, call);

	let tx_hash = match (pallet, call) {
		// Balances pallet calls
		("Balances", "transfer_allow_death") =>
			submit_balance_transfer(
				quantus_client,
				from_keypair,
				&args,
				false,
				tip_amount,
				execution_mode,
			)
			.await?,
		("Balances", "transfer_keep_alive") =>
			submit_balance_transfer(
				quantus_client,
				from_keypair,
				&args,
				true,
				tip_amount,
				execution_mode,
			)
			.await?,

		// System pallet calls
		("System", "remark") =>
			submit_system_remark(quantus_client, from_keypair, &args, tip_amount, execution_mode)
				.await?,

		// TechCollective pallet calls
		("TechCollective", "add_member") =>
			submit_tech_collective_add_member(quantus_client, from_keypair, &args, execution_mode)
				.await?,
		("TechCollective", "remove_member") =>
			submit_tech_collective_remove_member(
				quantus_client,
				from_keypair,
				&args,
				execution_mode,
			)
			.await?,
		("TechCollective", "vote") =>
			submit_tech_collective_vote(quantus_client, from_keypair, &args, execution_mode).await?,

		// ReversibleTransfers pallet calls
		("ReversibleTransfers", "schedule_transfer") =>
			submit_reversible_transfer(quantus_client, from_keypair, &args, execution_mode).await?,

		// Scheduler pallet calls
		("Scheduler", "schedule") =>
			submit_scheduler_schedule(quantus_client, from_keypair, &args).await?,
		("Scheduler", "cancel") =>
			submit_scheduler_cancel(quantus_client, from_keypair, &args).await?,

		// Generic fallback for unknown calls
		(_, _) => {
			log_error!(
				"❌ Pallet '{}' or call '{}' is not supported yet in SubXT implementation",
				pallet,
				call
			);
			log_print!("💡 Supported pallets in SubXT:");
			log_print!("   • Balances: transfer_allow_death, transfer_keep_alive");
			log_print!("   • System: remark");
			log_print!("   • TechCollective: add_member, remove_member, vote");
			log_print!("   • ReversibleTransfers: schedule_transfer");
			log_print!("   • Scheduler: schedule, cancel");
			log_print!("💡 For other calls, use the original 'quantus call' command");
			return Err(QuantusError::Generic(format!(
				"Unsupported pallet/call combination in SubXT: {pallet}.{call}"
			)));
		},
	};

	log_success!("🎉 SubXT transaction submitted successfully!");
	log_print!("📋 Transaction hash: {}", format!("0x{}", hex::encode(tx_hash)).bright_yellow());

	Ok(tx_hash)
}

/// Submit balance transfer
async fn submit_balance_transfer(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	keep_alive: bool,
	tip: Option<u128>,
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 2 {
		return Err(QuantusError::Generic(
			"Balances transfer requires 2 arguments: [to_address, amount]".to_string(),
		));
	}

	let to_address = args[0].as_str().ok_or_else(|| {
		QuantusError::Generic("First argument must be a string (to_address)".to_string())
	})?;

	let amount: u128 = args[1].as_str().unwrap_or("0").parse().map_err(|_| {
		QuantusError::Generic("Second argument must be a number (amount)".to_string())
	})?;

	// Convert to AccountId32
	let (to_account_id, _) = AccountId32::from_ss58check_with_version(to_address)
		.map_err(|e| QuantusError::Generic(format!("Invalid to_address: {e:?}")))?;

	// Convert to subxt_core AccountId32
	let to_account_id_bytes: [u8; 32] = *to_account_id.as_ref();
	let to_account_id_subxt = subxt::ext::subxt_core::utils::AccountId32::from(to_account_id_bytes);

	// Create and submit the transfer call
	if keep_alive {
		let transfer_call = quantus_subxt::api::tx().balances().transfer_keep_alive(
			subxt::ext::subxt_core::utils::MultiAddress::Id(to_account_id_subxt),
			amount,
		);
		crate::cli::common::submit_transaction(
			quantus_client,
			from_keypair,
			transfer_call,
			tip,
			execution_mode,
		)
		.await
	} else {
		let transfer_call = quantus_subxt::api::tx().balances().transfer_allow_death(
			subxt::ext::subxt_core::utils::MultiAddress::Id(to_account_id_subxt),
			amount,
		);
		crate::cli::common::submit_transaction(
			quantus_client,
			from_keypair,
			transfer_call,
			tip,
			execution_mode,
		)
		.await
	}
}

/// Submit system remark
async fn submit_system_remark(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	tip: Option<u128>,
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 1 {
		return Err(QuantusError::Generic(
			"System remark requires 1 argument: [remark]".to_string(),
		));
	}

	let remark = args[0]
		.as_str()
		.ok_or_else(|| QuantusError::Generic("Argument must be a string (remark)".to_string()))?;

	let remark_call = quantus_subxt::api::tx().system().remark(remark.as_bytes().to_vec());

	crate::cli::common::submit_transaction(
		quantus_client,
		from_keypair,
		remark_call,
		tip,
		execution_mode,
	)
	.await
}

/// Submit tech collective add member
async fn submit_tech_collective_add_member(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 1 {
		return Err(QuantusError::Generic(
			"TechCollective add_member requires 1 argument: [member_address]".to_string(),
		));
	}

	let member_address = args[0].as_str().ok_or_else(|| {
		QuantusError::Generic("Argument must be a string (member_address)".to_string())
	})?;

	let (member_account_id, _) = AccountId32::from_ss58check_with_version(member_address)
		.map_err(|e| QuantusError::Generic(format!("Invalid member_address: {e:?}")))?;

	// Convert to subxt_core AccountId32
	let member_account_id_bytes: [u8; 32] = *member_account_id.as_ref();
	let member_account_id_subxt =
		subxt::ext::subxt_core::utils::AccountId32::from(member_account_id_bytes);

	let call = quantus_subxt::api::tx()
		.tech_collective()
		.add_member(subxt::ext::subxt_core::utils::MultiAddress::Id(member_account_id_subxt));

	crate::cli::common::submit_transaction(quantus_client, from_keypair, call, None, execution_mode)
		.await
}

/// Submit tech collective remove member
async fn submit_tech_collective_remove_member(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 1 {
		return Err(QuantusError::Generic(
			"TechCollective remove_member requires 1 argument: [member_address]".to_string(),
		));
	}

	let member_address = args[0].as_str().ok_or_else(|| {
		QuantusError::Generic("Argument must be a string (member_address)".to_string())
	})?;

	let (member_account_id, _) = AccountId32::from_ss58check_with_version(member_address)
		.map_err(|e| QuantusError::Generic(format!("Invalid member_address: {e:?}")))?;

	// Convert to subxt_core AccountId32
	let member_account_id_bytes: [u8; 32] = *member_account_id.as_ref();
	let member_account_id_subxt =
		subxt::ext::subxt_core::utils::AccountId32::from(member_account_id_bytes);

	let call = quantus_subxt::api::tx().tech_collective().remove_member(
		subxt::ext::subxt_core::utils::MultiAddress::Id(member_account_id_subxt),
		0u16, // Default rank
	);

	crate::cli::common::submit_transaction(quantus_client, from_keypair, call, None, execution_mode)
		.await
}

/// Submit tech collective vote
async fn submit_tech_collective_vote(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 2 {
		return Err(QuantusError::Generic(
			"TechCollective vote requires 2 arguments: [referendum_index, aye]".to_string(),
		));
	}

	let referendum_index: u32 = args[0].as_u64().unwrap_or(0) as u32;
	let aye = args[1].as_bool().unwrap_or(false);

	let vote_call = quantus_subxt::api::tx().tech_collective().vote(referendum_index, aye);

	crate::cli::common::submit_transaction(
		quantus_client,
		from_keypair,
		vote_call,
		None,
		execution_mode,
	)
	.await
}

/// Submit reversible transfer
async fn submit_reversible_transfer(
	quantus_client: &crate::chain::client::QuantusClient,
	from_keypair: &QuantumKeyPair,
	args: &[Value],
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<subxt::utils::H256> {
	if args.len() != 2 {
		return Err(QuantusError::Generic(
			"ReversibleTransfers schedule_transfer requires 2 arguments: [to_address, amount]"
				.to_string(),
		));
	}

	let to_address = args[0].as_str().ok_or_else(|| {
		QuantusError::Generic("First argument must be a string (to_address)".to_string())
	})?;

	let amount: u128 = args[1].as_str().unwrap_or("0").parse().map_err(|_| {
		QuantusError::Generic("Second argument must be a number (amount)".to_string())
	})?;

	let (to_account_id, _) = AccountId32::from_ss58check_with_version(to_address)
		.map_err(|e| QuantusError::Generic(format!("Invalid to_address: {e:?}")))?;

	// Convert to subxt_core AccountId32
	let to_account_id_bytes: [u8; 32] = *to_account_id.as_ref();
	let to_account_id_subxt = subxt::ext::subxt_core::utils::AccountId32::from(to_account_id_bytes);

	let schedule_call = quantus_subxt::api::tx().reversible_transfers().schedule_transfer(
		subxt::ext::subxt_core::utils::MultiAddress::Id(to_account_id_subxt),
		amount,
	);

	crate::cli::common::submit_transaction(
		quantus_client,
		from_keypair,
		schedule_call,
		None,
		execution_mode,
	)
	.await
}

/// Submit scheduler schedule
async fn submit_scheduler_schedule(
	_quantus_client: &crate::chain::client::QuantusClient,
	_from_keypair: &QuantumKeyPair,
	_args: &[Value],
) -> crate::error::Result<subxt::utils::H256> {
	log_error!("❌ Scheduler calls through generic call are complex");
	log_print!("💡 Use dedicated scheduler commands for complex scheduling");
	Err(QuantusError::Generic(
		"Scheduler calls not supported in generic call - use scheduler commands".to_string(),
	))
}

/// Submit scheduler cancel
async fn submit_scheduler_cancel(
	_quantus_client: &crate::chain::client::QuantusClient,
	_from_keypair: &QuantumKeyPair,
	_args: &[Value],
) -> crate::error::Result<subxt::utils::H256> {
	log_error!("❌ Scheduler calls through generic call are complex");
	log_print!("💡 Use dedicated scheduler commands for scheduling operations");
	Err(QuantusError::Generic(
		"Scheduler calls not supported in generic call - use scheduler commands".to_string(),
	))
}

/// Handle generic call command execution
pub async fn handle_generic_call(
	pallet: &str,
	call: &str,
	args: Vec<Value>,
	keypair: &QuantumKeyPair,
	tip: Option<String>,
	node_url: &str,
	execution_mode: crate::cli::common::ExecutionMode,
) -> crate::error::Result<()> {
	log_print!("🚀 Generic Call");

	let quantus_client = crate::chain::client::QuantusClient::new(node_url).await?;

	execute_generic_call(&quantus_client, pallet, call, args, keypair, tip, execution_mode).await?;

	Ok(())
}