quantus-cli 2.1.1

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! `quantus system` subcommand - system information
use crate::{
	chain::client::{ChainConfig, QuantusClient},
	error::QuantusError,
	log_print, log_verbose,
};
use colored::Colorize;
use serde_json::Value;
use subxt::OnlineClient;

// Import ChainHead RPC API
use std::error::Error;
use subxt::{
	backend::{chain_head::ChainHeadRpcMethods, rpc::RpcClient},
	PolkadotConfig,
};

/// Maximum decimal places supported by u128 balance scaling (10^38 fits in u128).
pub const MAX_SUPPORTED_TOKEN_DECIMALS: u64 = 38;

/// Chain native token information structure
#[derive(Debug, Clone)]
pub struct TokenInfo {
	pub symbol: String,
	pub decimals: u8,
	pub ss58_format: Option<u8>,
}

/// Parse and validate chain token properties from RPC `chainspec_v1_properties`.
///
/// Fail closed: missing/out-of-range `tokenDecimals`, empty `tokenSymbol`, and
/// `ss58Format` values above 255 are rejected instead of defaulted or truncated.
pub fn parse_token_info_from_properties(
	properties: &serde_json::Map<String, Value>,
) -> crate::error::Result<TokenInfo> {
	let symbol = properties
		.get("tokenSymbol")
		.and_then(|v| v.as_str())
		.filter(|symbol| !symbol.is_empty())
		.ok_or_else(|| {
			QuantusError::NetworkError("Invalid or missing chain property tokenSymbol".to_string())
		})?
		.to_string();

	let decimals = properties
		.get("tokenDecimals")
		.and_then(|v| v.as_u64())
		.filter(|decimals| *decimals <= MAX_SUPPORTED_TOKEN_DECIMALS)
		.ok_or_else(|| {
			QuantusError::NetworkError(format!(
				"Invalid or missing chain property tokenDecimals; expected an integer between 0 and {MAX_SUPPORTED_TOKEN_DECIMALS}"
			))
		})? as u8;

	let ss58_format = properties
		.get("ss58Format")
		.map(|v| {
			v.as_u64().and_then(|format| u8::try_from(format).ok()).ok_or_else(|| {
				QuantusError::NetworkError(
					"Invalid chain property ss58Format; expected an integer between 0 and 255"
						.to_string(),
				)
			})
		})
		.transpose()?;

	Ok(TokenInfo { symbol, decimals, ss58_format })
}

/// Chain information from ChainHead API
#[derive(Debug, Clone)]
pub struct ChainInfo {
	pub token: TokenInfo,
	pub chain_name: Option<String>,
	pub genesis_hash: Option<String>,
}

/// Client for retrieving token information using ChainHead RPC
pub struct ChainHeadTokenClient {
	rpc: ChainHeadRpcMethods<PolkadotConfig>,
}

impl ChainHeadTokenClient {
	/// Creates a new client from endpoint URL
	pub async fn new(url: &str) -> Result<Self, Box<dyn Error>> {
		let rpc_client = RpcClient::from_url(url).await?;
		let rpc = ChainHeadRpcMethods::<PolkadotConfig>::new(rpc_client);

		Ok(Self { rpc })
	}

	/// Gets native chain token information using ChainHead RPC
	pub async fn get_token_info(&self) -> Result<TokenInfo, Box<dyn Error>> {
		// Get system properties using chainspec_v1_properties
		let properties: serde_json::Map<String, Value> = self.rpc.chainspec_v1_properties().await?;
		Ok(parse_token_info_from_properties(&properties)?)
	}

	/// Gets chain name
	pub async fn get_chain_name(&self) -> Result<String, Box<dyn Error>> {
		Ok(self.rpc.chainspec_v1_chain_name().await?)
	}

	/// Gets genesis hash
	pub async fn get_genesis_hash(&self) -> Result<String, Box<dyn Error>> {
		let hash = self.rpc.chainspec_v1_genesis_hash().await?;
		Ok(format!("{hash:?}")) // Format the hash for display
	}
}

/// Gets complete chain information using ChainHead API
pub async fn get_complete_chain_info(node_url: &str) -> crate::error::Result<ChainInfo> {
	match ChainHeadTokenClient::new(node_url).await {
		Ok(client) => {
			let token_info = client.get_token_info().await.map_err(|e| {
				crate::error::QuantusError::NetworkError(format!(
					"ChainHead token info failed: {e:?}"
				))
			})?;

			let chain_name = client.get_chain_name().await.ok();
			let genesis_hash = client.get_genesis_hash().await.ok();

			Ok(ChainInfo { token: token_info, chain_name, genesis_hash })
		},
		Err(e) => {
			log_verbose!("❌ ChainHead client creation failed: {:?}", e);
			Err(crate::error::QuantusError::NetworkError(format!("ChainHead client failed: {e:?}")))
		},
	}
}

/// Get system information including ChainHead data
pub async fn get_system_info(quantus_client: &QuantusClient) -> crate::error::Result<()> {
	log_verbose!("🔍 Querying system information...");

	// Get complete chain information from ChainHead API using the actual node_url
	let chain_info = get_complete_chain_info(quantus_client.node_url()).await?;

	// Get metadata information
	let metadata = quantus_client.client().metadata();
	let pallets: Vec<_> = metadata.pallets().collect();

	log_print!("🏗️  Chain System Information:");
	log_print!(
		"   💰 Token: {} ({} decimals)",
		chain_info.token.symbol.bright_yellow(),
		chain_info.token.decimals.to_string().bright_cyan()
	);

	if let Some(ss58_format) = chain_info.token.ss58_format {
		log_print!("   🔢 SS58 Format: {}", ss58_format.to_string().bright_magenta());
	}

	if let Some(name) = &chain_info.chain_name {
		log_print!("   🔗 Chain: {}", name.bright_green());
	}

	if let Some(hash) = &chain_info.genesis_hash {
		log_print!("   🧬 Genesis: {}...", hash[..16].bright_cyan());
	}

	log_print!("   📦 Pallets: {}", pallets.len().to_string());
	log_print!("   🔧 Runtime: Substrate-based");

	log_verbose!("💡 Use 'quantus metadata' to explore all available pallets and calls");

	log_verbose!("✅ System info retrieved successfully!");

	Ok(())
}

/// Get detailed chain parameters information
pub async fn get_detailed_chain_params(
	quantus_client: &crate::chain::client::QuantusClient,
	show_raw_data: bool,
) -> crate::error::Result<()> {
	log_print!("🔧 Runtime Information:");

	// Get genesis hash
	let genesis_hash = quantus_client.get_genesis_hash().await?;
	log_print!("   🧬 Genesis hash: {}", genesis_hash.to_string().bright_cyan());

	// Get runtime version
	let (spec_version, transaction_version) = quantus_client.get_runtime_version().await?;
	log_print!("   📋 Spec version: {}", spec_version.to_string().bright_green());
	log_print!("   🔄 Transaction version: {}", transaction_version.to_string().bright_yellow());

	// Get current block info
	use jsonrpsee::core::client::ClientT;
	let current_block: serde_json::Value = quantus_client
		.rpc_client()
		.request::<serde_json::Value, [(); 0]>("chain_getHeader", [])
		.await
		.map_err(|e| {
			crate::error::QuantusError::NetworkError(format!(
				"Failed to fetch current block: {e:?}"
			))
		})?;

	if let Some(block_number_str) = current_block["number"].as_str() {
		if let Ok(block_number) = u64::from_str_radix(&block_number_str[2..], 16) {
			log_print!("   📦 Current block: {}", block_number.to_string().bright_blue());

			// Calculate era based on block number
			let period = 64u64;
			let phase = block_number % period;
			log_print!("   ⏰ Era: period={}, phase={}", period, phase);
			log_print!(
				"   💡 Transaction era: Era::Mortal({}, {}) or Era::Immortal",
				period,
				phase
			);
		}
	}

	// Get full runtime info
	let runtime_info: serde_json::Value = quantus_client
		.rpc_client()
		.request::<serde_json::Value, [(); 0]>("state_getRuntimeVersion", [])
		.await
		.map_err(|e| {
			crate::error::QuantusError::NetworkError(format!("Failed to fetch runtime info: {e:?}"))
		})?;

	if show_raw_data {
		log_verbose!("📋 Full runtime info: {:?}", runtime_info);
	}

	log_print!("📋 Runtime Details:");
	log_print!(
		"   🏷️  Spec name: {}",
		runtime_info["specName"].as_str().unwrap_or("unknown").bright_magenta()
	);
	log_print!(
		"   🔧 Implementation name: {}",
		runtime_info["implName"].as_str().unwrap_or("unknown").bright_cyan()
	);
	log_print!(
		"   📦 Implementation version: {}",
		runtime_info["implVersion"].as_u64().unwrap_or(0).to_string().bright_yellow()
	);
	log_print!(
		"   ✍️  Authoring version: {}",
		runtime_info["authoringVersion"].as_u64().unwrap_or(0).to_string().bright_blue()
	);
	log_print!(
		"   🗂️  State version: {}",
		runtime_info["stateVersion"].as_u64().unwrap_or(0).to_string().bright_green()
	);
	log_print!(
		"   💻 System version: {}",
		runtime_info["systemVersion"].as_u64().unwrap_or(0).to_string().bright_red()
	);

	// Get chain properties
	let chain_props: serde_json::Value = quantus_client
		.rpc_client()
		.request::<serde_json::Value, [(); 0]>("system_properties", [])
		.await
		.map_err(|e| {
			crate::error::QuantusError::NetworkError(format!(
				"Failed to fetch chain properties: {e:?}"
			))
		})?;

	if show_raw_data {
		log_verbose!("🔗 Chain properties: {:?}", chain_props);
	}

	if show_raw_data {
		log_verbose!("📦 Current block: {:?}", current_block);
	}

	// Show block details
	log_print!("📦 Block Details:");
	if let Some(parent_hash) = current_block["parentHash"].as_str() {
		log_print!("   🔗 Parent hash: {}...", parent_hash[..16].bright_cyan());
	}
	if let Some(state_root) = current_block["stateRoot"].as_str() {
		log_print!("   🗂️  State root: {}...", state_root[..16].bright_magenta());
	}
	if let Some(extrinsics_root) = current_block["extrinsicsRoot"].as_str() {
		log_print!("   📄 Extrinsics root: {}...", extrinsics_root[..16].bright_yellow());
	}

	Ok(())
}

/// Get detailed metadata statistics
pub async fn get_metadata_stats(client: &OnlineClient<ChainConfig>) -> crate::error::Result<()> {
	log_verbose!("🔍 Getting metadata statistics...");

	let metadata = client.metadata();
	let pallets: Vec<_> = metadata.pallets().collect();

	log_verbose!("🔍 SubXT metadata: {} pallets available", pallets.len());

	log_print!("📊 Metadata Statistics:");
	log_print!("   📦 Total pallets: {}", pallets.len());
	log_print!("   🔗 Metadata version: SubXT (type-safe)");

	// Count calls across all pallets
	let mut total_calls = 0;
	for pallet in &pallets {
		if let Some(calls) = pallet.call_variants() {
			total_calls += calls.len();
		}
	}

	log_print!("   🎯 Total calls: {}", total_calls);
	log_print!("   ⚡ API: Type-safe SubXT");

	Ok(())
}

/// Handle system command
pub async fn handle_system_command(node_url: &str) -> crate::error::Result<()> {
	log_print!("🚀 System Information");
	let quantus_client = QuantusClient::new(node_url).await?;
	get_system_info(&quantus_client).await?;

	Ok(())
}

/// Handle extended system commands with additional info
pub async fn handle_system_extended_command(
	node_url: &str,
	show_runtime: bool,
	show_metadata: bool,
	show_rpc_methods: bool,
	verbose: bool,
) -> crate::error::Result<()> {
	log_print!("🚀 Extended System Information");

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

	// Basic system info
	get_system_info(&quantus_client).await?;

	if show_runtime {
		log_print!("");
		get_detailed_chain_params(&quantus_client, verbose).await?;
	}

	if show_metadata {
		log_print!("");
		get_metadata_stats(quantus_client.client()).await?;
	}

	if show_rpc_methods {
		log_print!("");
		list_rpc_methods(&quantus_client).await?;
	}

	Ok(())
}

/// List all available JSON-RPC methods from the connected node
async fn list_rpc_methods(quantus_client: &QuantusClient) -> crate::error::Result<()> {
	use jsonrpsee::core::client::ClientT;

	log_print!("🧭 JSON-RPC Methods exposed by node:");

	let response: serde_json::Value = quantus_client
		.rpc_client()
		.request::<serde_json::Value, [(); 0]>("rpc_methods", [])
		.await
		.map_err(|e| {
			crate::error::QuantusError::NetworkError(format!("Failed to fetch rpc_methods: {e:?}"))
		})?;

	if let Some(methods) = response.get("methods").and_then(|v| v.as_array()) {
		for method in methods {
			if let Some(name) = method.as_str() {
				log_print!("\t{}", name);
			}
		}
		if let Some(version) = response.get("version").and_then(|v| v.as_u64()) {
			log_verbose!("RPC methods schema version: {}", version);
		}
	} else if let Some(array) = response.as_array() {
		for method in array {
			if let Some(name) = method.as_str() {
				log_print!("\t{}", name);
			}
		}
	} else {
		log_print!("   (no methods returned or unexpected format)");
		log_verbose!("rpc_methods raw response: {:?}", response);
	}

	Ok(())
}

#[cfg(test)]
mod tests {
	use super::*;
	use serde_json::json;

	fn props(value: serde_json::Value) -> serde_json::Map<String, Value> {
		value.as_object().expect("test properties must be an object").clone()
	}

	#[test]
	fn parse_token_info_accepts_valid_properties() {
		let info = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "QUAN",
			"tokenDecimals": 12,
			"ss58Format": 42,
		})))
		.expect("valid token properties must be accepted");
		assert_eq!(info.symbol, "QUAN");
		assert_eq!(info.decimals, 12);
		assert_eq!(info.ss58_format, Some(42));
	}

	#[test]
	fn parse_token_info_rejects_missing_token_decimals() {
		let err = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "QUAN",
		})))
		.unwrap_err();
		assert!(
			err.to_string().contains("tokenDecimals"),
			"expected missing tokenDecimals rejection, got: {err}"
		);
	}

	#[test]
	fn parse_token_info_rejects_out_of_range_token_decimals() {
		let err = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "QUAN",
			"tokenDecimals": MAX_SUPPORTED_TOKEN_DECIMALS + 1,
		})))
		.unwrap_err();
		assert!(
			err.to_string().contains("tokenDecimals"),
			"expected out-of-range tokenDecimals rejection, got: {err}"
		);
	}

	#[test]
	fn parse_token_info_rejects_empty_symbol() {
		let err = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "",
			"tokenDecimals": 12,
		})))
		.unwrap_err();
		assert!(
			err.to_string().contains("tokenSymbol"),
			"expected empty symbol rejection, got: {err}"
		);
	}

	#[test]
	fn parse_token_info_rejects_ss58_format_above_255() {
		let err = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "QUAN",
			"tokenDecimals": 12,
			"ss58Format": 256,
		})))
		.unwrap_err();
		assert!(
			err.to_string().contains("ss58Format"),
			"expected ss58Format>255 rejection, got: {err}"
		);
	}

	#[test]
	fn parse_token_info_allows_missing_ss58_format() {
		let info = parse_token_info_from_properties(&props(json!({
			"tokenSymbol": "QUAN",
			"tokenDecimals": 0,
		})))
		.expect("ss58Format is optional");
		assert_eq!(info.decimals, 0);
		assert_eq!(info.ss58_format, None);
	}
}