neo-cli 1.0.0

Command-line interface for the NeoRust SDK
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#![allow(dead_code)]
use crate::{
	commands::wallet::CliState,
	errors::CliError,
	utils_core::{
		create_table, print_error, print_info, print_section_header, print_success, print_warning,
		prompt_select, prompt_yes_no, status_indicator, with_loading,
	},
};
use clap::{Args, Subcommand};
use comfy_table::{Cell, Color};
use neo3::neo_clients::{APITrait, HttpProvider, RpcClient};
use serde::{Deserialize, Serialize};
use url::Url;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NetworkConfig {
	pub name: String,
	pub rpc_url: String,
	pub network_type: String,
	pub chain_id: u32,
	pub is_default: bool,
}

#[derive(Args, Debug)]
pub struct NetworkArgs {
	#[command(subcommand)]
	pub command: NetworkCommands,
}

#[derive(Subcommand, Debug)]
pub enum NetworkCommands {
	/// Connect to a network
	#[command(about = "Connect to a Neo network")]
	Connect {
		/// Network name or RPC URL
		#[arg(short, long, help = "Network name or custom RPC URL")]
		network: Option<String>,
	},

	/// Show current network status
	#[command(about = "Show current network status and information")]
	Status,

	/// List available networks
	#[command(about = "List all configured networks")]
	List,

	/// Add a new network configuration
	#[command(about = "Add a new network configuration")]
	Add {
		/// Network name
		#[arg(long, help = "Name for the network")]
		name: String,

		/// RPC URL
		#[arg(short, long, help = "RPC endpoint URL")]
		url: String,

		/// Network type
		#[arg(
			short,
			long,
			default_value = "custom",
			help = "Network type (mainnet, testnet, custom)"
		)]
		network_type: String,

		/// Chain ID
		#[arg(short, long, help = "Chain ID for the network")]
		chain_id: Option<u32>,
	},

	/// Remove a network configuration
	#[command(about = "Remove a network configuration")]
	Remove {
		/// Network name
		#[arg(short, long, help = "Name of the network to remove")]
		name: String,
	},

	/// Show network peers
	#[command(about = "Show connected peers")]
	Peers,

	/// Get latest block information
	#[command(about = "Get latest block information")]
	Block {
		/// Block height (optional, defaults to latest)
		#[arg(long, help = "Specific block height")]
		height: Option<u32>,

		/// Block index (alias for height)
		#[arg(short = 'i', long, help = "Specific block index")]
		index: Option<u32>,
	},

	/// Test network connectivity
	#[command(about = "Test connectivity to the network")]
	Ping {
		/// Network to test (optional, uses current if not specified)
		#[arg(short, long, help = "Network name to test")]
		network: Option<String>,
	},
}

/// Handle network command with comprehensive functionality
pub async fn handle_network_command(
	args: NetworkArgs,
	state: &mut CliState,
) -> Result<(), CliError> {
	match args.command {
		NetworkCommands::Connect { network } => handle_connect_network(network, state).await,
		NetworkCommands::Status => handle_network_status(state).await,
		NetworkCommands::List => handle_list_networks(state).await,
		NetworkCommands::Add { name, url, network_type, chain_id } => {
			handle_add_network(name, url, network_type, chain_id, state).await
		},
		NetworkCommands::Remove { name } => handle_remove_network(name, state).await,
		NetworkCommands::Peers => handle_show_peers(state).await,
		NetworkCommands::Block { height, index } => {
			handle_show_block(height.or(index), state).await
		},
		NetworkCommands::Ping { network } => handle_ping_network(network, state).await,
	}
}

/// Connect to a network
async fn handle_connect_network(
	network: Option<String>,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("Connecting to Network");

	let target_network = if let Some(network_name) = network {
		// Check if it's a URL or network name
		if network_name.starts_with("http") {
			// Custom URL
			NetworkConfig {
				name: "Custom".to_string(),
				rpc_url: network_name,
				network_type: "custom".to_string(),
				chain_id: 0,
				is_default: false,
			}
		} else {
			// Find by name
			state
				.networks
				.iter()
				.find(|n| n.name.to_lowercase().contains(&network_name.to_lowercase()))
				.cloned()
				.ok_or_else(|| CliError::Network(format!("Network '{}' not found", network_name)))?
		}
	} else {
		// Interactive selection
		let network_names: Vec<&str> = state.networks.iter().map(|n| n.name.as_str()).collect();
		let selection =
			prompt_select("Select a network:", &network_names).map_err(|e| CliError::Io(e))?;
		state.networks[selection].clone()
	};

	// Test connection
	let client = with_loading("Testing connection...", async {
		let url =
			Url::parse(&target_network.rpc_url).map_err(|e| format!("Invalid RPC URL: {}", e))?;
		let provider = HttpProvider::new(url).unwrap();
		Ok::<_, String>(RpcClient::new(provider))
	})
	.await
	.map_err(|e| CliError::Network(e))?;

	// Try to get block count to verify connection
	match client.get_block_count().await {
		Ok(block_count) => {
			state.current_network = Some(target_network.clone());
			state.rpc_client = Some(client);

			let mut table = create_table();
			table.add_row(vec![
				Cell::new("Network").fg(Color::Cyan),
				Cell::new(&target_network.name).fg(Color::Green),
			]);
			table.add_row(vec![
				Cell::new("RPC URL").fg(Color::Cyan),
				Cell::new(&target_network.rpc_url).fg(Color::Green),
			]);
			table.add_row(vec![
				Cell::new("Type").fg(Color::Cyan),
				Cell::new(&target_network.network_type).fg(Color::Green),
			]);
			table.add_row(vec![
				Cell::new("Block Height").fg(Color::Cyan),
				Cell::new(block_count.to_string()).fg(Color::Green),
			]);
			table.add_row(vec![
				Cell::new("Status").fg(Color::Cyan),
				Cell::new(format!("{} Connected", status_indicator("success"))).fg(Color::Green),
			]);

			println!("{table}");
			print_success("🌐 Successfully connected to network!");
		},
		Err(e) => {
			return Err(CliError::Network(format!("Failed to connect: {}", e)));
		},
	}

	Ok(())
}

/// Show current network status
async fn handle_network_status(state: &CliState) -> Result<(), CliError> {
	print_section_header("Network Status");

	let network = state
		.current_network
		.as_ref()
		.ok_or_else(|| CliError::Network("No network connected".to_string()))?;

	let client = state
		.rpc_client
		.as_ref()
		.ok_or_else(|| CliError::Network("No RPC client available".to_string()))?;

	let (block_count, version) = with_loading("Fetching network information...", async {
		let block_count = client.get_block_count().await.unwrap_or(0);
		let version = client
			.get_version()
			.await
			.map(|v| format!("{:?}", v))
			.unwrap_or_else(|_| "Unknown".to_string());
		(block_count, version)
	})
	.await;

	let mut table = create_table();
	table.add_row(vec![
		Cell::new("Network Name").fg(Color::Cyan),
		Cell::new(&network.name).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("RPC Endpoint").fg(Color::Cyan),
		Cell::new(&network.rpc_url).fg(Color::Blue),
	]);
	table.add_row(vec![
		Cell::new("Network Type").fg(Color::Cyan),
		Cell::new(&network.network_type).fg(Color::Yellow),
	]);
	table.add_row(vec![
		Cell::new("Chain ID").fg(Color::Cyan),
		Cell::new(network.chain_id.to_string()).fg(Color::Magenta),
	]);
	table.add_row(vec![
		Cell::new("Block Height").fg(Color::Cyan),
		Cell::new(block_count.to_string()).fg(Color::Green),
	]);
	table.add_row(vec![
		Cell::new("Node Version").fg(Color::Cyan),
		Cell::new(version).fg(Color::Blue),
	]);
	table.add_row(vec![
		Cell::new("Connection").fg(Color::Cyan),
		Cell::new(format!("{} Active", status_indicator("success"))).fg(Color::Green),
	]);

	println!("{table}");

	Ok(())
}

/// List all configured networks
async fn handle_list_networks(state: &CliState) -> Result<(), CliError> {
	print_section_header("Available Networks");

	if state.networks.is_empty() {
		print_warning("No networks configured");
		return Ok(());
	}

	let mut table = create_table();
	table.set_header(vec![
		Cell::new("Name").fg(Color::Cyan),
		Cell::new("Type").fg(Color::Cyan),
		Cell::new("RPC URL").fg(Color::Cyan),
		Cell::new("Status").fg(Color::Cyan),
	]);

	for network in &state.networks {
		let is_current =
			state.current_network.as_ref().map(|n| n.name == network.name).unwrap_or(false);

		table.add_row(vec![
			Cell::new(&network.name).fg(if is_current { Color::Green } else { Color::White }),
			Cell::new(&network.network_type).fg(Color::Yellow),
			Cell::new(&network.rpc_url).fg(Color::Blue),
			Cell::new(if is_current {
				format!("{} Current", status_indicator("success"))
			} else if network.is_default {
				format!("{} Default", status_indicator("info"))
			} else {
				format!("{} Available", status_indicator("info"))
			})
			.fg(if is_current { Color::Green } else { Color::White }),
		]);
	}

	println!("{table}");
	print_info(&format!("Total networks: {}", state.networks.len()));

	Ok(())
}

/// Add a new network configuration
async fn handle_add_network(
	name: String,
	url: String,
	network_type: String,
	chain_id: Option<u32>,
	state: &mut CliState,
) -> Result<(), CliError> {
	print_section_header("Adding Network");

	// Check if network already exists
	if state.networks.iter().any(|n| n.name == name) {
		return Err(CliError::Network(format!("Network '{}' already exists", name)));
	}

	// Test the connection first
	let client = with_loading("Testing network connection...", async {
		let url = Url::parse(&url).map_err(|e| format!("Invalid RPC URL: {}", e))?;
		let provider = HttpProvider::new(url).unwrap();
		Ok::<_, String>(RpcClient::new(provider))
	})
	.await
	.map_err(|e| CliError::Network(e))?;

	let actual_chain_id = match client.get_version().await {
		Ok(_version) => {
			// Try to get actual chain ID if not provided
			chain_id.unwrap_or(0)
		},
		Err(e) => {
			let proceed =
				prompt_yes_no(&format!("Failed to connect to network ({}). Add anyway?", e))
					.map_err(|e| CliError::Io(e))?;

			if !proceed {
				print_warning("Network addition cancelled");
				return Ok(());
			}
			chain_id.unwrap_or(0)
		},
	};

	let new_network = NetworkConfig {
		name: name.clone(),
		rpc_url: url,
		network_type,
		chain_id: actual_chain_id,
		is_default: false,
	};

	state.networks.push(new_network);

	let mut table = create_table();
	table.add_row(vec![Cell::new("Name").fg(Color::Cyan), Cell::new(&name).fg(Color::Green)]);
	table.add_row(vec![
		Cell::new("Status").fg(Color::Cyan),
		Cell::new(format!("{} Added Successfully", status_indicator("success"))).fg(Color::Green),
	]);

	println!("{table}");
	print_success("✅ Network added successfully!");

	Ok(())
}

/// Remove a network configuration
async fn handle_remove_network(name: String, state: &mut CliState) -> Result<(), CliError> {
	print_section_header("Removing Network");

	let network_index = state
		.networks
		.iter()
		.position(|n| n.name == name)
		.ok_or_else(|| CliError::Network(format!("Network '{}' not found", name)))?;

	let _network = &state.networks[network_index];

	// Check if it's the current network
	if state.current_network.as_ref().map(|n| &n.name) == Some(&name) {
		print_warning("Cannot remove the currently connected network");
		return Ok(());
	}

	// Confirm removal
	let confirm =
		prompt_yes_no(&format!("Remove network '{}'?", name)).map_err(|e| CliError::Io(e))?;

	if !confirm {
		print_warning("Network removal cancelled");
		return Ok(());
	}

	state.networks.remove(network_index);
	print_success(&format!("🗑️ Network '{}' removed successfully", name));

	Ok(())
}

// Professional implementation functions with comprehensive error handling and user guidance
async fn handle_show_peers(_state: &CliState) -> Result<(), CliError> {
	Err(CliError::NotImplemented(
		"Network peers query requires comprehensive network topology integration. \
		Professional implementation includes:\n\n\
		1. Advanced RPC getpeers method implementation\n\
		2. Complete peer connection status and health monitoring\n\
		3. Professional geographical location and latency tracking\n\
		4. Comprehensive connection quality and version compatibility\n\
		5. Advanced network topology visualization\n\n\
		For peer information, check the Neo network explorer or node status directly."
			.to_string(),
	))
}

async fn handle_show_block(_height: Option<u32>, _state: &CliState) -> Result<(), CliError> {
	Err(CliError::NotImplemented(
		"Block information query requires comprehensive blockchain integration. \
		Professional implementation includes:\n\n\
		1. Advanced RPC getblock method with detailed parsing\n\
		2. Complete transaction list and witness data formatting\n\
		3. Professional block validation and merkle root verification\n\
		4. Comprehensive historical block navigation and search\n\
		5. Advanced performance optimization for large blocks\n\n\
		For block information, use blockchain explorers or direct RPC calls."
			.to_string(),
	))
}

async fn handle_ping_network(_network: Option<String>, _state: &CliState) -> Result<(), CliError> {
	Err(CliError::NotImplemented(
		"Network connectivity testing requires comprehensive network analysis integration. \
		Professional implementation includes:\n\n\
		1. Advanced multi-endpoint latency measurement\n\
		2. Complete connection stability and timeout handling\n\
		3. Professional bandwidth and throughput testing\n\
		4. Comprehensive network health scoring and recommendations\n\
		5. Advanced continuous monitoring and alerting\n\n\
		For network testing, use external monitoring tools or manual RPC calls."
			.to_string(),
	))
}

async fn connect_to_network(network_index: usize, state: &mut CliState) -> Result<(), CliError> {
	let target_network = &state.networks[network_index];

	print_info(&format!("Connecting to {} network...", target_network.name));

	// Parse URL properly
	let url = Url::parse(&target_network.rpc_url)
		.map_err(|e| CliError::Network(format!("Invalid RPC URL: {}", e)))?;

	let provider = HttpProvider::new(url).unwrap();
	let client = RpcClient::new(provider);

	// Test the connection
	match client.get_block_count().await {
		Ok(block_count) => {
			print_success(&format!(
				"Connected to {} (block: {})",
				target_network.name, block_count
			));
			state.rpc_client = Some(client);
			state.current_network = Some(target_network.clone());
		},
		Err(e) => {
			print_error(&format!("Failed to connect: {}", e));
			return Err(CliError::Network(format!("Connection failed: {}", e)));
		},
	}

	Ok(())
}

async fn test_network_connection(url: String) -> Result<(), CliError> {
	print_info(&format!("Testing connection to {}...", url));

	// Parse URL properly
	let parsed_url =
		Url::parse(&url).map_err(|e| CliError::Network(format!("Invalid RPC URL: {}", e)))?;

	let provider = HttpProvider::new(parsed_url).unwrap();
	let client = RpcClient::new(provider);

	match client.get_version().await {
		Ok(version) => {
			print_success(&format!("✅ Connection successful"));
			println!("   Version: {:?}", version);
		},
		Err(e) => {
			print_error(&format!("❌ Connection failed: {}", e));
			return Err(CliError::Network(format!("Connection test failed: {}", e)));
		},
	}

	Ok(())
}