Neo Rust SDK Examples
A collection of code examples demonstrating how to use the Neo Rust SDK
Basic Examples
These examples cover the fundamental functionality of the Neo Rust SDK, perfect for getting started.
Creating a Wallet
Learn how to create and manage Neo wallets.
use neo::prelude::*;
fn main() -> Result<()> {
// Create a new wallet
let mut wallet = Wallet::new();
// Create a new account in the wallet
let account = wallet.create_account(None)?;
println!("New wallet created with account: {}", account.address());
// Save the wallet to a file
wallet.save("my_wallet.json", "password123")?;
println!("Wallet saved to my_wallet.json");
Ok(())
}
Checking Wallet Balance
Query the balance of NEO and GAS in a wallet.
use neo::prelude::*;
async fn main() -> Result<()> {
// Load a wallet
let wallet = Wallet::load("my_wallet.json", "password123")?;
let account = wallet.default_account()?;
// Connect to Neo node
let client = NeoClient::connect_to_testnet().await?;
// Check NEO balance
let neo_balance = client.get_neo_balance(account.address()).await?;
// Check GAS balance
let gas_balance = client.get_gas_balance(account.address()).await?;
println!("NEO Balance: {}", neo_balance);
println!("GAS Balance: {}", gas_balance);
Ok(())
}
Creating a Transaction
Build and sign a Neo transaction.
use neo::prelude::*;
async fn main() -> Result<()> {
// Load a wallet
let wallet = Wallet::load("my_wallet.json", "password123")?;
let account = wallet.default_account()?;
// Connect to Neo node
let client = NeoClient::connect_to_testnet().await?;
let block_count = client.get_block_count().await?;
// Create a transaction to transfer NEO
let tx = TransactionBuilder::new()
.version(0)
.nonce(1234)
.valid_until_block(block_count + 100)
.sender(account.address())
.script(script_builder::build_transfer_script(
"neo",
account.address(),
"NbnjKGMBJzJ6j5PHeYhjJDaQ5Vy5UYu4Fv",
1 * 100000000 // 1 NEO
))
.sign(account)
.build();
// Send the transaction
let result = client.send_transaction(tx).await?;
println!("Transaction sent: {}", result);
Ok(())
}
Smart Contract Examples
Learn how to deploy and interact with smart contracts on the Neo blockchain.
Invoking a Contract
Call methods on a deployed smart contract.
use neo::prelude::*;
async fn main() -> Result<()> {
let wallet = Wallet::load("my_wallet.json", "password123")?;
let account = wallet.default_account()?;
// Connect to Neo node
let client = NeoClient::connect_to_testnet().await?;
// Invoke contract method
let result = client
.invoke_function(
"0xd2a4cff31913016155e38e474a2c06d08be276cf", // Contract hash
"balanceOf", // Method name
[
ContractParameter::hash160(account.address())
],
None, // No need to sign for read-only operations
)
.await?;
println!("Contract invocation result: {:?}", result);
Ok(())
}
Deploying a Contract
Deploy a smart contract to the Neo blockchain.
use neo::prelude::*;
use std::fs;
async fn main() -> Result<()> {
let wallet = Wallet::load("my_wallet.json", "password123")?;
let account = wallet.default_account()?;
// Connect to Neo node
let client = NeoClient::connect_to_testnet().await?;
// Read contract NEF file
let nef_bytes = fs::read("contract.nef")?;
let nef = NEF::from_bytes(&nef_bytes)?;
// Read contract manifest
let manifest_json = fs::read_to_string("contract.manifest.json")?;
let manifest = ContractManifest::from_json(&manifest_json)?;
// Deploy the contract
let result = client.deploy_contract(nef, manifest, account).await?;
println!("Contract deployed: {}", result.contract_hash);
Ok(())
}
Working with NEP-17 Tokens
Interact with NEP-17 fungible tokens.
use neo::prelude::*;
async fn main() -> Result<()> {
let wallet = Wallet::load("my_wallet.json", "password123")?;
let account = wallet.default_account()?;
// Connect to Neo node
let client = NeoClient::connect_to_testnet().await?;
// Create a NEP-17 token instance
let token_hash = "0xd2a4cff31913016155e38e474a2c06d08be276cf";
let token = Nep17Token::new(token_hash, client.clone());
// Get token info
let symbol = token.symbol().await?;
let decimals = token.decimals().await?;
let total_supply = token.total_supply().await?;
println!("Token: {} ({})", symbol, token_hash);
println!("Decimals: {}", decimals);
println!("Total Supply: {}", total_supply);
// Get account balance
let balance = token.balance_of(account.address()).await?;
println!("Your Balance: {}", balance);
Ok(())
}
Advanced Examples
Explore advanced features of the Neo Rust SDK for more complex use cases.
Working with Multi-Signature Accounts
Create and use multi-signature accounts for enhanced security.
View ExampleComplete Projects
Full project examples showcasing real-world applications built with the Neo Rust SDK.
Simple NEP-17 Token
A complete implementation of a fungible token following the NEP-17 standard.
View Project