本文基于前述 Solana 账户初始化内容,扩展了如何通过 Anchor 框架实现账户数据的读写操作,通过新增 set() 函数展示数据写入流程,优化代码结构,并介绍了使用命令行和程序内方法读取账户数据的技术细节,最终实现了一个简单的计数器功能。
在前文《Solana 与 Anchor 中的账户初始化》中,我们探讨了如何初始化账户以持久化存储数据。本文将展示如何向已初始化的账户写入数据,并读取其内容。
以下基于之前的代码,新增 set() 函数,用于更新 MyStorage 中的 x 值:
use anchor_lang::prelude::*;
use std::mem::size_of;
declare_id!("9XZGDi1imvGFV3bLuDJK1bkUht51GPCRsVxMe4DpqWEo");
#[program]
pub mod basic_storage {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
Ok(())
}
// 新增:写入 x 值
pub fn set(ctx: Context<Set>, new_x: u64) -> Result<()> {
ctx.accounts.my_storage.x = new_x;
Ok(())
}
}
// 新增:Set 账户结构
#[derive(Accounts)]
pub struct Set<'info> {
#[account(mut, seeds = [], bump)]
pub my_storage: Account<'info, MyStorage>,
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
payer = signer,
space = size_of::<MyStorage>() + 8,
seeds = [],
bump
)]
pub my_storage: Account<'info, MyStorage>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct MyStorage {
x: u64,
}
测试代码更新,新增调用 set(10):
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { BasicStorage } from "../target/types/basic_storage";
import { BN } from "@coral-xyz/anchor";
describe("basic_storage", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.BasicStorage as Program<BasicStorage>;
it("Is initialized!", async () => {
const seeds = []
const [myStorage, _bump] = anchor.web3.PublicKey.findProgramAddressSync(seeds, program.programId);
console.log("the storage account address is", myStorage.toBase58());
await program.methods.initialize().accounts({ myStorage: myStorage }).rpc();
});
it("set the value", async () => {
const seeds = []
const [myStorage, _bump] = anchor.web3.PublicKey.findProgramAddressSync(seeds, program.programId);
await program.methods.set(new BN(10)).accounts({ myStorage: myStorage }).rpc();
})
});
写入逻辑
ctx.accounts.my_storage.x = new_x 的工作原理:
幕后流程:
Set 结构
Set 仅需一个可变引用 my_storage,比 Initialize 更简洁:
注意:尽管程序可推导地址,Solana 运行时要求客户端显式提供账户,后续文章将详解。
若需更新多个字段,重复 ctx.accounts.my_storage 显得冗余:
ctx.accounts.my_storage.x = new_x;
ctx.accounts.my_storage.y = new_y;
可使用 Rust 的可变引用(&mut)简化:
pub fn set(ctx: Context<Set>, new_x: u64) -> Result<()> {
let my_storage = &mut ctx.accounts.my_storage; // 获取可变引用
my_storage.x = new_x;
Ok(())
}
在本地验证器运行时,使用以下命令查看账户:
solana account 2m3p5Y58CDYnJ9k32tVRW9vY4M8Vfabk6eqM7zgQLKRh # 替换为测试输出的地址
输出示例:
Public Key: 2m3p5Y58CDYnJ9k32tVRW9vY4M8Vfabk6eqM7zgQLKRh
Balance: 0.00100224 SOL
Owner: 9XZGDi1imvGFV3bLuDJK1bkUht51GPCRsVxMe4DpqWEo
Executable: false
Rent Epoch: 18446744073709551615
Length: 16 (0x10) bytes
0000: 1c f2 3b 85 43 19 31 28 0a 00 00 00 00 00 00 00 ..;.C.1(........
添加 print_x() 函数读取 x:
pub fn print_x(ctx: Context<PrintX>) -> Result<()> {
let x = ctx.accounts.my_storage.x;
msg!("The value of x is {}", x);
Ok(())
}
#[derive(Accounts)]
pub struct PrintX<'info> {
pub my_storage: Account<'info, MyStorage>, // 无需 mut,仅读取
}
测试中添加以下行:
await program.methods.printX().accounts({myStorage: myStorage}).rpc();
运行后,Solana 日志将显示 x 的值: The value of x is 10。
【笔记配套代码】 https://github.com/0xE1337/rareskills_evm_to_solana 【参考资料】 https://learnblockchain.cn/column/119 https://www.rareskills.io/solana-tutorial
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!