目录
Welcome to Code Block's blog
本篇文章主要介绍了
[Anchor安装和基础测试]
博主广交技术好友,喜欢的可以关注一下
一、编写目的
Anchor是一个SOL链的开发框架,可以很方便的完成链上程序(Program)的编写,并且可以进行快速的前端测试。但安装时需要很多步骤,并且在测试时也有些错误需要注意。在这里对步骤和相关版本进行记录,作为记录和过程分享。
二、安装步骤
测试使用版本
| RustUp | 1.27.1 |
| Node.js | v18.20.4 |
| solana-cli | 2.0.16 |
| anchor-cli | 0.30.1 |
2.1 安装Rust
编写Program链上程序需要使用rust语言,所以需要先进行Rust语言环境安装,使用以下命令完成安装:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
这里的安装速度可能会很慢,所以可以使用国内的镜像库:
设置rustup镜像
# 临时替换 export RUSTUP_UPDATE_ROOT=https://mirrors.aliyun.com/rustup/rustup export RUSTUP_DIST_SERVER=https://mirrors.aliyun.com/rustup
# 永久替换 # bash用户 echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.aliyun.com/rustup/rustup' >> ~/.bash_profile echo 'export RUSTUP_DIST_SERVER=https://mirrors.aliyun.com/rustup' >> ~/.bash_profile source ~/.bash_profile # zsh用户 echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.aliyun.com/rustup/rustup' >> ~/.zshrc echo 'export RUSTUP_DIST_SERVER=https://mirrors.aliyun.com/rustup' >> ~/.zshrc source ~/.zshrc
安装Rust
# 使用阿里云安装脚本 curl --proto '=https' --tlsv1.2 -sSf https://mirrors.aliyun.com/repo/rust/rustup-init.sh | sh
页面出现
Rust is installed now. Great!
表示安装成功!
2.2 安装node.js
安装node.js是为了使用其中的yarn命令,这里可以在Node.js — Run JavaScript Everywhere
官网进行下载压缩包,解压后通过软链接的方式使node命令生效.
2.3 安装Solana-CLI
安装solana的客户端程序,以完成solana相关命令的使用.使用以下命令进行安装:
sh -c "$(curl -sSfL https://release.anza.xyz/stable/install)"
安装完成后会输出以下内容(这里的export命令可能会不同):
Close and reopen your terminal to apply the PATH changes or run the following in your existing shell: export PATH="/Users/test/.local/share/solana/install/active_release/bin:$PATH"
运行export命令即可完成安装.
打印版本号:
solana --version
输出:
solana-cli 1.18.22 (src:9efdd74b; feat:4215500110, client:Agave)
2.4 安装Anchor CLI
Anchor使用AVM管理工具进行管理,所以先使用以下命令进行AVM安装:
cargo install --git https://github.com/coral-xyz/anchor avm --force
安装完成后同样使用avm --version命令进行测试输出版本.
使用以下命令安装和使用anchor的最新版本:
avm install latest avm use latest
安装完成后使用 anchor --version命令进行版本打印以进行打印版本测试.
三、Program测试
使用以下命令创建一个测试项目
anchor init test-program
创建完成后,创建一个新的项目文件夹,使用vscode打开该文件夹,文件的目录如下:
编辑
这里的programs/test-program/src/libs.rs为要编写program合约代码,初始化内容如下:
use anchor_lang::prelude::*; #定义program地址,部署的地址 declare_id!("EFEJVy8RKikt28Xf7APGsrJLvkeKuBMGQ9yY3iTNCSFG"); #标记为program程序入口 #[program] pub mod test_program { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { msg!("Greetings from: {:?}", ctx.program_id); Ok(()) } } #标记为传递的Accounts结构体 #[derive(Accounts)] pub struct Initialize {}
这里功能为调用initialize方法,并输出当前program_id.即:
EFEJVy8RKikt28Xf7APGsrJLvkeKuBMGQ9yY3iTNCSFG
tests/test-program.ts文件夹内为测试文件,内容如下:
import * as anchor from "@coral-xyz/anchor"; import { Program } from "@coral-xyz/anchor"; import { TestProgram } from "../target/types/test_program"; describe("test-program", () => { // Configure the client to use the local cluster. anchor.setProvider(anchor.AnchorProvider.env()); const program = anchor.workspace.TestProgram as Program<TestProgram>; it("Is initialized!", async () => { // Add your test here. const tx = await program.methods.initialize().rpc(); console.log("Your transaction signature", tx); }); });
在文件夹内使用anchor test进行测试,这里会自动调用(anchor build)进行program部署并在测试环境下发送一个方法请求(模拟请求链上程序),获得一个打印输出:
编辑
要查看具体的链上调用成功的信息(tx),可以使用以下内容获取具体的transaction信息:
const logs = await anchor.AnchorProvider.env().connection.getParsedTransaction(tx, { commitment: "confirmed", }); console.log("Solana Logs", logs);
编辑
可以看到这里模拟的链上执行打印出了
Greetings from: EFEJVy8RKikt28Xf7APGsrJLvkeKuBMGQ9yY3iTNCSFG
表示链上程序被正常执行。
四、可能出现的问题
Solana-CLI可能会使用dev net或test net节点,需要配置为本地环境,运行以下命令将Solana-CLI配置为本地测试环境.
编辑
本代码均在测试网络进行,不涉及任何如投资等方面的建议!
如果你对区块链感兴趣,可以浏览我的专栏:区块链
感谢您的关注和收藏!!!!!!
编辑