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
//! Interactive wizard for Neo CLI
//!
//! Provides a user-friendly interface for common blockchain operations

use anyhow::{Context, Result};
use colored::*;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select};
use neo3::sdk::{Neo, Network};
use std::time::Duration;

/// Main wizard entry point
pub async fn run_wizard() -> Result<()> {
	print_banner();

	loop {
		let choices = vec![
			"🌐 Connect to Network",
			"đŸ’ŧ Wallet Operations",
			"💰 Check Balance",
			"📤 Send Transaction",
			"📜 Smart Contract Interaction",
			"🔧 Generate Project",
			"📚 Documentation",
			"❌ Exit",
		];

		let selection = Select::with_theme(&ColorfulTheme::default())
			.with_prompt("What would you like to do?")
			.items(&choices)
			.default(0)
			.interact()?;

		match selection {
			0 => connect_to_network().await?,
			1 => wallet_operations().await?,
			2 => check_balance().await?,
			3 => send_transaction().await?,
			4 => smart_contract_interaction().await?,
			5 => generate_project().await?,
			6 => show_documentation()?,
			7 => {
				println!("\n{}", "👋 Goodbye!".green());
				break;
			},
			_ => unreachable!(),
		}
	}

	Ok(())
}

fn print_banner() {
	println!();
	println!("{}", "╔══════════════════════════════════════╗".cyan());
	println!("{}", format!("║{:^width$}║", "NeoRust Interactive Wizard", width = 38).cyan().bold());
	println!(
		"{}",
		format!("║{:^width$}║", format!("Version {}", env!("CARGO_PKG_VERSION")), width = 38)
			.cyan()
	);
	println!("{}", "╚══════════════════════════════════════╝".cyan());
	println!();
	println!("{}", "Welcome to the NeoRust Interactive Wizard!".green());
	println!("{}", "This tool will help you interact with the Neo blockchain easily.\n".white());
}

async fn connect_to_network() -> Result<()> {
	println!("\n{}", "🌐 Network Connection".cyan().bold());

	let networks = vec!["TestNet", "MainNet", "Custom"];
	let network_choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select network")
		.items(&networks)
		.default(0)
		.interact()?;

	let mut pb = crate::utils_core::create_spinner("Connecting to network...");

	let neo = match network_choice {
		0 => Neo::testnet().await.context("Failed to connect to TestNet")?,
		1 => Neo::mainnet().await.context("Failed to connect to MainNet")?,
		2 => {
			let url: String = Input::with_theme(&ColorfulTheme::default())
				.with_prompt("Enter custom RPC URL")
				.default("https://testnet1.neo.org:443".to_string())
				.interact()?;

			Neo::builder()
				.network(Network::Custom(url))
				.build()
				.await
				.context("Failed to connect to custom network")?
		},
		_ => unreachable!(),
	};

	pb.finish_with_message("Connected successfully!");

	// Get and display block height
	let height = neo.get_block_height().await?;
	println!(
		"✅ {}",
		format!("Connected to {} at block height: {}", networks[network_choice], height).green()
	);

	Ok(())
}

async fn wallet_operations() -> Result<()> {
	println!("\n{}", "đŸ’ŧ Wallet Operations".cyan().bold());

	let operations = vec![
		"Create New Wallet",
		"Import from WIF",
		"Import from Mnemonic",
		"Export Wallet",
		"View Wallet Info",
		"Back to Main Menu",
	];

	let choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select operation")
		.items(&operations)
		.default(0)
		.interact()?;

	match choice {
		0 => create_new_wallet()?,
		1 => import_from_wif()?,
		2 => import_from_mnemonic()?,
		3 => export_wallet()?,
		4 => view_wallet_info()?,
		5 => return Ok(()),
		_ => unreachable!(),
	}

	Ok(())
}

fn create_new_wallet() -> Result<()> {
	use neo3::neo_wallets::wallet::Wallet;

	println!("\n{}", "Creating new wallet...".yellow());

	let _wallet = Wallet::new();

	println!("✅ {}", "Wallet created successfully!".green());
	println!("\n{}", "Wallet Details:".cyan().bold());

	// Note: In a real implementation, we'd show the actual wallet address
	// For now, we just show that it was created
	println!("  📍 Address: {}", "[Generated Address]".white());
	println!("  🔑 Private Key: {}", "[Secure - Not Displayed]".red());

	let save = Confirm::with_theme(&ColorfulTheme::default())
		.with_prompt("Would you like to save this wallet?")
		.default(true)
		.interact()?;

	if save {
		let path: String = Input::with_theme(&ColorfulTheme::default())
			.with_prompt("Enter file path to save wallet")
			.default("wallet.json".to_string())
			.interact()?;

		// In a real implementation, we'd save the wallet here
		println!("💾 Wallet would be saved to: {}", path.green());
	}

	Ok(())
}

fn import_from_wif() -> Result<()> {
	println!("\n{}", "Importing wallet from WIF...".yellow());

	let _wif: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter WIF private key")
		.interact()?;

	// In a real implementation, we'd import the wallet here
	println!("✅ {}", "Wallet imported successfully!".green());

	Ok(())
}

fn import_from_mnemonic() -> Result<()> {
	println!("\n{}", "Importing wallet from mnemonic...".yellow());

	let _mnemonic: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter mnemonic phrase")
		.interact()?;

	let _passphrase: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter passphrase (optional)")
		.default("".to_string())
		.interact()?;

	// In a real implementation, we'd import the wallet here
	println!("✅ {}", "Wallet imported successfully!".green());

	Ok(())
}

fn export_wallet() -> Result<()> {
	println!("\n{}", "Exporting wallet...".yellow());

	let formats = vec!["JSON (NEP-6)", "WIF", "Mnemonic"];
	let format_choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select export format")
		.items(&formats)
		.default(0)
		.interact()?;

	// In a real implementation, we'd export the wallet here
	println!("✅ {}", format!("Wallet exported as {}", formats[format_choice]).green());

	Ok(())
}

fn view_wallet_info() -> Result<()> {
	println!("\n{}", "Wallet Information".cyan().bold());

	// In a real implementation, we'd show actual wallet info
	println!("  📍 Address: {}", "[Current Address]".white());
	println!("  💰 NEO Balance: {}", "0".white());
	println!("  â›Ŋ GAS Balance: {}", "0".white());
	println!("  📜 Script Hash: {}", "[Script Hash]".white());

	Ok(())
}

async fn check_balance() -> Result<()> {
	println!("\n{}", "💰 Check Balance".cyan().bold());

	let address: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter Neo address")
		.default("NbTiM6h8r99kpRtb428XcsUk1TzKed2gTc".to_string())
		.interact()?;

	let network_choices = vec!["TestNet", "MainNet"];
	let network = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select network")
		.items(&network_choices)
		.default(0)
		.interact()?;

	let mut pb = crate::utils_core::create_spinner("Fetching balance...");

	// Connect to network
	let neo = match network {
		0 => Neo::testnet().await?,
		1 => Neo::mainnet().await?,
		_ => unreachable!(),
	};

	// Get balance
	match neo.get_balance(&address).await {
		Ok(balance) => {
			pb.finish_with_message("Balance fetched!");

			println!("\n{}", "Balance Information:".cyan().bold());
			println!("  📍 Address: {}", address.white());
			println!("  💎 NEO: {} tokens", balance.neo.to_string().green());
			println!("  â›Ŋ GAS: {} tokens", balance.gas.to_string().green());

			if !balance.tokens.is_empty() {
				println!("\n  📜 Other Tokens:");
				for token in balance.tokens {
					println!("     â€ĸ {}: {}", token.symbol, token.amount);
				}
			}
		},
		Err(e) => {
			pb.finish_with_message("Failed to fetch balance");
			println!("❌ Error: {}", e.to_string().red());
		},
	}

	Ok(())
}

async fn send_transaction() -> Result<()> {
	println!("\n{}", "📤 Send Transaction".cyan().bold());

	println!("{}", "âš ī¸  This feature requires a wallet with funds.".yellow());

	let proceed = Confirm::with_theme(&ColorfulTheme::default())
		.with_prompt("Do you want to continue?")
		.default(false)
		.interact()?;

	if !proceed {
		return Ok(());
	}

	let from: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter sender address")
		.interact()?;

	let to: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter recipient address")
		.interact()?;

	let tokens = vec!["NEO", "GAS"];
	let token_choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select token to send")
		.items(&tokens)
		.default(1)
		.interact()?;

	let amount: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt(format!("Enter amount of {} to send", tokens[token_choice]))
		.interact()?;

	println!("\n{}", "Transaction Summary:".cyan().bold());
	println!("  From: {}", from.white());
	println!("  To: {}", to.white());
	println!("  Amount: {} {}", amount.white(), tokens[token_choice].white());

	let confirm = Confirm::with_theme(&ColorfulTheme::default())
		.with_prompt("Confirm transaction?")
		.default(false)
		.interact()?;

	if confirm {
		println!("✅ {}", "Transaction would be sent (dry run mode)".green());
	} else {
		println!("❌ {}", "Transaction cancelled".red());
	}

	Ok(())
}

async fn smart_contract_interaction() -> Result<()> {
	println!("\n{}", "📜 Smart Contract Interaction".cyan().bold());

	let operations = vec![
		"Deploy Contract",
		"Invoke Read-Only Method",
		"Invoke Method (Transaction)",
		"Get Contract Info",
		"Back to Main Menu",
	];

	let choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select operation")
		.items(&operations)
		.default(0)
		.interact()?;

	match choice {
		0 => {
			println!("đŸ“Ļ Contract deployment wizard coming soon!");
		},
		1 => {
			println!("đŸ‘ī¸ Read-only invocation wizard coming soon!");
		},
		2 => {
			println!("âœī¸ Transaction invocation wizard coming soon!");
		},
		3 => {
			println!("â„šī¸ Contract info viewer coming soon!");
		},
		4 => return Ok(()),
		_ => unreachable!(),
	}

	Ok(())
}

async fn generate_project() -> Result<()> {
	println!("\n{}", "🔧 Generate Project".cyan().bold());

	let templates = vec![
		"Basic Neo dApp",
		"NEP-17 Token",
		"NFT Collection (NEP-11)",
		"DeFi Protocol",
		"Oracle Consumer",
		"Custom Template",
	];

	let template_choice = Select::with_theme(&ColorfulTheme::default())
		.with_prompt("Select project template")
		.items(&templates)
		.default(0)
		.interact()?;

	let project_name: String = Input::with_theme(&ColorfulTheme::default())
		.with_prompt("Enter project name")
		.default("my-neo-project".to_string())
		.interact()?;

	let mut pb = crate::utils_core::create_spinner("Generating project...");

	// Simulate project generation
	tokio::time::sleep(Duration::from_secs(2)).await;

	pb.finish_with_message("Project generated!");

	println!(
		"\n✅ {} '{}'",
		format!("Successfully generated {} project", templates[template_choice]).green(),
		project_name.cyan()
	);

	println!("\n{}", "Next steps:".cyan().bold());
	println!("  1. cd {}", project_name);
	println!("  2. cargo build");
	println!("  3. cargo test");
	println!("  4. neo-cli deploy");

	Ok(())
}

fn show_documentation() -> Result<()> {
	println!("\n{}", "📚 Documentation".cyan().bold());

	let docs = vec![
		("Getting Started", "https://github.com/R3E-Network/NeoRust#getting-started"),
		("API Documentation", "https://docs.rs/neo3"),
		("Examples", "https://github.com/R3E-Network/NeoRust/tree/master/examples"),
		("Neo Developer Docs", "https://developers.neo.org"),
		("Discord Community", "https://discord.gg/neo"),
	];

	println!("\n{}", "Available Resources:".cyan());
	for (name, url) in docs {
		println!("  📖 {}: {}", name.white(), url.blue());
	}

	println!("\n💡 {}", "Tip: Use 'neo-cli --help' for command-line options".yellow());

	Ok(())
}