9_Solana-Anchor安装

搭建本地开发环境

https://solana.com/developers/guides/getstarted/setup-local-development

  • 安装Anchor
    • avm: Anchor Version Manager
1
2
3
4
5
6
7
8
9
10
11
12
13
sudo apt-get update && sudo apt-get upgrade && sudo apt-get install -y pkg-config build-essential libudev-dev libssl-dev

cargo install --git https://github.com/coral-xyz/anchor avm --locked --force

# 安装最新版
avm install latest

# 使用最新版
avm use latest

# check the version

anchor --version
  • Setup a localhost blockchain cluster

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    solana-test-validator --help

    # setup localhost blockchain
    solana-test-validator


    # swith to localhost
    solana config set --url localhost

    solana config get

    # set default wallet
    solana config set -k ~/.config/solana/id.json

    # get the airdrop from localhost blockchain
    solana airdrop 2

    # get balance
    solana balance
  • 新建anchor项目
1
anchor init <new-workspace-name>

Anchor程序结构

https://www.solanazh.com/course/7-3

Anchor官方示例:

一个Anchor工程主要包含:

  • “declare_id”宏声明的合约地址,用于创建对象的owner
  • #[derive(Accounts)] 修饰的Account对象,用于表示存储和指令, 包含了指令执行所要用到的账户
  • “program” 模块,这里面写主要的合约处理逻辑

对应到我们之前的HelloWorld,就是要将state和instruction部分用 #[derive(Accounts)] 修饰,将process逻辑放到program模块中,并增加一个合约地址的修饰。

#[program] 修饰的Module即为指令处理模块。其中有一个Context类型,来存放所有的指令参数。比如

  • ctx.accounts 所有的请求keys,也就是AccountMeta数组
  • ctx.program_id 指令中的program_id
  • ctx.remaining_accounts 指令中,没有被下面说的”Accounts”修饰的成员的AccountMeta
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use anchor_lang::prelude::*;

// 程序ID
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

// 指令执行逻辑
#[program]
mod basic_1 {
use super::*;

pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {
let my_account = &mut ctx.accounts.my_account;
my_account.data = data;
Ok(())
}

pub fn update(ctx: Context<Update>, data: u64) -> Result<()> {
let my_account = &mut ctx.accounts.my_account;
my_account.data = data;
Ok(())
}
}


// 包含了Initialize指令所需要的账户(my_account, user, system_program)
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub my_account: Account<'info, MyAccount>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}

// 包含了Update指令所需要的账户(my_account, user, system_program)
#[derive(Accounts)]
pub struct Update<'info> {
#[account(mut)]
pub my_account: Account<'info, MyAccount>,
}

// 数据账户
#[account]
pub struct MyAccount {
pub data: u64,
}
  • Copyrights © 2021-2024 youngqqcn

请我喝杯咖啡吧~