在前面的基础中,已经安装好了 rust 和 solana cli。首先确定 rust 版本:
 确定下 solana 的版本,建议使用 2.2.12:
 通过命令切换sh -c "$(curl -sSfL https://release.anza.xyz/v2.2.12/install)" 安装 VS Code 打开 VS Code 的页面,根据自己的系统选择下载对应的版本。 要先安装好 rust,然后再安装 VC Code 的插件。VS Code 安装好后,打开 VS Code,然后在插件的地方 安装 rust-analyzer:

创建工程 在自己的工作目录下,用 cargo 创建一个工程:
cargo new hello_world --lib 然后进入到 hello_world 目录下,添加 solana SDK 的依赖:cargo add solana-program@2.2.0

更新 edition 字段到 2021 ,否则在构建程序时可能会遇到错误。
 并给Cargo.toml增加:[lib] crate-type = ["cdylib", "lib"] 在 src/lib.rs文件中,填入如下合约代码:
use solana_program::{ account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey, }; entrypoint!(process_instruction); pub fn process_instruction( _program_id: &Pubkey, _accounts: &[AccountInfo], _instruction_data: &[u8], ) -> ProgramResult { msg!("Hello, Solana world!"); Ok(()) } 构建 执行:cargo build-sbf 这里可能会报一些错,如果有遇到和我类似的问题,可以参考下面:1. 如果是网络什么的问题,类似url不通,大概率是科学上网的方式有问题,可以尝试换一个app;2. 将Cargo.toml文件的edition = "2024"改成edition = "2021"。

此命令会运行成功后,会生成一个 target/deploy 目录,其中包含两个重要文件:
- 一个
.so 文件(例如,hello_world.so ):这是编译后的 Solana 程序,将作为“智能合约”部署到网络。 - 一个 keypair 文件(例如,
hello_world-keypair.json ):在部署程序时,该 keypair 的公钥用作程序 ID。
 部署 切到本地集群solana config set -ul
 打开一个新终端,运行下面命令,启动本地 validator。solana-test-validator 显示如下
 打开 VS Code 终端,运行下面命令,部署到本地 validatorsolana program deploy target/deploy/helloworld.so Program Id: 73w15hRfWHVansZQ2tEUPaX9phNHEFPT4d34yRzTyu5Y Signature: 3biJfGVZgqog3GDbUdmLh9s4pRz2HNzvXrxt7NgvbbL63wh71ujytBqWAmmMtWY3P2RCcX8fRNEHt8gHocyz4NnY Rust客户端测试首先创建一个 examples 目录和一个 client.rs 文件。mkdir -p examples && touch examples/client.rs 添加 solana-client 和 tokio 依赖项。cargo add solana-sdk@2.2.0 --dev cargo add solana-client@2.2.0 --dev cargo add tokio --dev 下面的代码是 Rust 客户端脚本,它为一个新的 keypair 提供资金以支付交易费用,然后调用 Hello World 程序。use solana_client::rpc_client::RpcClient; use solana_sdk::{ commitment_config::CommitmentConfig, instruction::Instruction, pubkey::Pubkey, signature::{Keypair, Signer}, transaction::Transaction, }; use std::str::FromStr; #[tokio::main] async fn main() { // Program ID (replace with your actual program ID) let program_id = Pubkey::from_str("73w15hRfWHVansZQ2tEUPaX9phNHEFPT4d34yRzTyu5Y").unwrap(); // Connect to the Solana devnet let rpc_url = String::from("http://localhost:8899"); let client = RpcClient::new_with_commitment(rpc_url, CommitmentConfig::confirmed()); // Generate a new keypair for the payer let payer = Keypair::new(); // Request airdrop let airdrop_amount = 1_000_000_000; // 1 SOL let signature = client .request_airdrop(&payer.pubkey(), airdrop_amount) .expect("Failed to request airdrop"); // Wait for airdrop confirmation loop { let confirmed = client.confirm_transaction(&signature).unwrap(); if confirmed { break; } } // Create the instruction let instruction = Instruction::new_with_borsh( program_id, &(), // Empty instruction data vec![], // No accounts needed ); // Add the instruction to new transaction let mut transaction = Transaction::new_with_payer(&[instruction], Some(&payer.pubkey())); transaction.sign(&[&payer], client.get_latest_blockhash().unwrap()); // Send and confirm the transaction match client.send_and_confirm_transaction(&transaction) { Ok(signature) => println!("Transaction Signature: {}", signature), Err(err) => eprintln!("Error sending transaction: {}", err), } } 在 client.rs 里面添加,替换 Program ID:Program Id 在之前发布的时候会显示,如果忘记了,可以通过下面命令获取:solana address -k ./target/deploy/hello_world-keypair.json
 此时 Cargo.toml 文件配置如下:
 运行客户端运行cargo run --example client 显示
 测试 打开 Solana Explorer(本地集群) https://explorer.solana.com/?cluster=custom
将刚刚的 Transaction Signature 贴到搜索框回车:
 可以看到跟之前的Playgroud能够得到一样的效果。

|