新一代公链代表Solana(9) --- 如何用VS Code开发Solana智能合约

本文指导读者如何在VSCode中使用Rust编写Solana智能合约,包括创建工程、添加依赖、编写合约代码、解决编译错误、部署合约以及进行Rust客户端测试的过程。

Solana智能合约可以使用Rust编写,所以要学习使用VS Code开发Solana智能合约,首先你得掌握如何在VS Code上面编写Rust程序。这里同学们可以参考之前的文章:

Rust开发环境搭建Visual Studio Code

Rust从入门到暴走系列

1.创建工程

在自己的workspace下,用cargo创建一个项目, 进入项目目录,添加Solana SDK依赖

luca@LucadeMacBook-Air rust-workspace % cargo new  --lib helloworld                      
     Created library `helloworld` package
luca@LucadeMacBook-Air rust-workspace % 
luca@LucadeMacBook-Air rust-workspace % cd helloworld 
luca@LucadeMacBook-Air helloworld % 
luca@LucadeMacBook-Air helloworld % cargo add solana-program
    Updating crates.io index
      Adding solana-program v1.18.9 to dependencies.
    Updating crates.io index

用VS Code打开项目
在这里插入图片描述

2.编写合约代码

在 src/lib.rs文件中,填入如下合约代码:

use solana_program::{
   
   
    account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg, pubkey::Pubkey,
};

// Declare and export the program's entrypoint
entrypoint!(process_instruction);

// Program entrypoint's implementation
pub fn process_instruction(
    _program_id: &Pubkey, // Public key of the account the hello world program was loaded into
    _accounts: &[AccountInfo], // The account to say hello to
    _instruction_data: &[u8], // Ignored, all helloworld instructions are hellos
) -> ProgramResult {
   
   
    msg!("Hello World Rust program entrypoint");

    Ok(())
}

3.构建项目

在Cargo.toml中添加:

[featur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lucarun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值