- 官方
- https://github.com/coral-xyz/anchor
- 文档: https://docs.rs/anchor-lang/latest/anchor_lang/
- CPI: https://www.anchor-lang.com/docs/cross-program-invocations
- 文档书: https://book.anchor-lang.com/anchor_in_depth/CPIs.html
- https://www.anchor-lang.com/docs/solana-playground
- API: https://coral-xyz.github.io/anchor/ts/index.html
- Anchor入门教程: https://www.bilibili.com/video/BV1G9k6Y7EbZ/
- 不会写的,可以看例子: https://beta.solpg.io/
常见账户类型
Account<T>
: 用于存储单一类型数据的账户。适用于存储结构化的数据,如用户信息、配置信息等。#[account] pub struct MyAccount { pub data: u64, pub owner: Pubkey, }
Signer
: 表示一个必须签署交易的账户。用于验证用户身份和权限,如支付账户、权限控制等。#[derive(Accounts)] pub struct MyContext<'info> { #[account(mut)] pub user: Signer<'info>, // 其他账户... }
Program
: 表示一个程序账户。用于引用系统程序或其他依赖的程序。#[derive(Accounts)] pub struct MyContext<'info> { pub system_program: Program<'info, System>, // 其他账户... }
#[derive(Accounts)]
说明:账户属性约束需要注意
#[derive(Accounts)]
pub struct InitializeAccounts<'info> {
#[account(
init, // 表示要创建新账户
seeds = [b"my_seed", user.key().as_ref(),instruction_data.as_ref()], // 如果是 PDA 则必需
bump, // 如果是 PDA 则必需
payer = user, // 必需,指定支付账户创建费用
space = 8 + 8, // 必需,指定账户空间大小
)]
pub pda_counter: Account<'info, Counter>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}