Solana上部署合约示例

部署程序

第一步是复制代码。

git clone https://github.com/solana-labs/example-helloworld
cd example-helloworld

完成后,可以设置当前的环境为devnet。这是为Solana开发者们准备的编写和测试合约的网络的测试网。

 solana config set --url https://api.devnet.solana.com

接下来,需要为账户创建一个密钥对。这对于在Solana测试网上部署合约来说是必要的。注意:这种存储密钥对的方法不安全,应该仅用于demo目的。为了安全,系统将提示你输入密码。

 solana-keygen new --force

现在已经创建了一个帐户,可以使用空投程序来获取一些SOL通证。需要一些lamports(部分SOL通证)来部智能合约。 该命令请求获取SOL通证到你新生成的帐户中:

 solana airdrop 5 

现在已准备好构建 hello world 程序。 可以通过运行以下命令来构建它:

npm run build:program-rust
compiling the program
Compiling the program

程序构建完成后,就可以将其部署到devnet上。上一个命令的输出将为你提供接下来需要运行的命令,但它应该类似于下面这种:

 solana program deploy dist/program/helloworld.so

最终结果是成功将 hello world 程序部署到devnet上,并且有一个指定的Program Id。这可以在Solana Devnet 浏览器上进行检查。

Deploying the program
Deploying the program
Viewing the deployed program on the Devnet explorer
在Devnet浏览器上检查部署的程序

与部署的程序交互

为了与部署好的程序交互,hello-world代码库提供了一个简单的客户端。这个客户端是用Typescript编写的,使用了Solana的web3.js库和Solana web3 API。

 

运行客户端

在运行客户端从部署的程序中读取数据之前,还需要安装客户端的依赖项。

npm install

这步完成后,可以开启客户端。

npm run start

可以从输出中看到程序成功执行,并且会展示账户已经打招呼的次数。再运行一次会增加该数值。

Starting the Hello World client to interact with the deployed program

Hello World客户端和部署的程序交互

posted @ 2021-12-09 00:46  SoyWang  阅读(650)  评论(0编辑  收藏  举报