Skip to main content

Code Examples

Learn NeoRust SDK through practical examples. From basic operations to advanced patterns, explore real-world code samples that you can use in your projects.

🚀 Basic Operations

Essential operations like connecting to the network, creating accounts, and checking balances.

Connect to Neo Network

Establish connection to Neo N3 network and get blockchain info.

connectionrpcbasics
use neo3::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to Neo N3 TestNet
let provider = HttpProvider::new("https://testnet1.neo.org:443")?;
let client = RpcClient::new(provider);

// Get blockchain information
let block_count = client.get_block_count().await?;
let version = client.get_version().await?;

println!("Current block height: {}", block_count);
println!("Node version: {}", version.useragent);
println!("Network: {}", version.network);

Ok(())
}

Create New Account

Generate a new account with private key and address.

accountkeysaddress
use neo3::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new account
let account = Account::create()?;

println!("Address: {}", account.get_address());
println!("Public Key: {}", hex::encode(account.get_public_key().encode_point(true)));
println!("Script Hash: {}", account.get_script_hash());

// Export private key (be careful with this!)
let private_key = account.get_private_key();
println!("Private Key: {}", hex::encode(private_key.as_bytes()));

Ok(())
}

Check Account Balance

Check GAS and NEO balances for any account.

balancegasneo
use neo3::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let provider = HttpProvider::new("https://testnet1.neo.org:443")?;
let client = RpcClient::new(provider);

let address = "NXXXXxxxXXXxxxXXXxxxXXXxxxXXXxxx";
let script_hash = address.to_script_hash()?;

// Check GAS balance
let gas_token = GasToken::new(&client);
let gas_balance = gas_token.balance_of(&script_hash).await?;
let gas_decimals = gas_token.decimals().await?;
println!("GAS Balance: {} ({})", gas_balance, gas_decimals);

// Check NEO balance
let neo_token = NeoToken::new(&client);
let neo_balance = neo_token.balance_of(&script_hash).await?;
println!("NEO Balance: {}", neo_balance);

Ok(())
}

Ready to Build?

Start building your Neo N3 application with NeoRust SDK. Check out our comprehensive documentation and guides.

Additional Resources

📖

API Documentation

Comprehensive API reference with detailed method documentation.

View API Docs →
🎓

Tutorials

Step-by-step tutorials for building complete applications.

Start Learning →
💬

Community

Join our community for help, discussions, and contributions.

Join Discussion →