Solana合约部署及调用

针对 Solana helloworld测试合约 进行编译、部署及调用。
1)安装Solana tool suite:

sh -c "$(curl -sSfL https://release.solana.com/v1.7.10/install)"

设置指向testnet:

$ solana config set --url https://api.testnet.solana.com
// 确认:
$ solana config get
Config File: /Users/xxx/.config/solana/cli/config.yml
RPC URL: https://api.testnet.solana.com 
WebSocket URL: wss://api.testnet.solana.com/ (computed)
Keypair Path: /Users/xxx/.config/solana/id.json 
Commitment: confirmed 

2)编译合约:

$ npm install
$ npm run build:program-rust

> [email protected] build:program-rust /Users/xxx/zyd/example-helloworld
> cargo build-bpf --manifest-path=./src/program-rust/Cargo.toml --bpf-out-dir=dist/program

BPF SDK: /Users/xxx/.local/share/solana/install/releases/1.7.10/solana-release/bin/sdk/bpf
Running: rustup toolchain list -v
Running: rustup toolchain link bpf /Users/xxx/.local/share/solana/install/releases/1.7.10/solana-release/bin/sdk/bpf/dependencies/bpf-tools/rust
Running: cargo +bpf build --target bpfel-unknown-unknown --release
...........

3)部署合约:【$ solana airdrop 1 获取测试token】

$ solana program deploy  dist/program/helloworld.so
=============================================================================
Recover the intermediate account's ephemeral keypair file with
`solana-keygen recover` and the following 12-word seed phrase:
=============================================================================
embark glimpse hidden nasty unfold donor miss relax dawn suggest bargain nest
=============================================================================
To resume a deploy, pass the recovered keypair as
the [PROGRAM_ADDRESS_SIGNER] argument to `solana deploy` or
as the [BUFFER_SIGNER] to `solana program deploy` or `solana write-buffer'.
Or to recover the account's lamports, pass it as the
[BUFFER_ACCOUNT_ADDRESS] argument to `solana program close`.
=============================================================================
Error: Account CiXLP94T6QBrEdevnJrGEDcN3iRaEWMKm7bv76ogpsZm has insufficient funds for spend (0.4265436 SOL) + fee (0.001355 SOL)

$ solana airdrop 1
Requesting airdrop of 1 SOL

Signature: 51NTskGN1QDEbcaVsoKRwvgBWsQCwCBqw41x472fcuBRpWBmje5xD3Pb3oQ3Z63VHFSCJBubohhUTuxqeGxkE9Vr

3.5721014 SOL
$ solana program deploy  dist/program/helloworld.so
Program Id: CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk

$ solana balance
2.71772184 SOL

部署完合约,默认该合约地址的balance不为0 SOL:【直接就从创建者账户中划了2年的租金到合约账号。】

Program Account:
Address	CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk
Balance (SOL)	◎0.00114144
Executable	Yes
Executable Data	Fjy8guoUgsnWmz9WVtGFHg8g96fX9YebRbFWNof8YETo
Upgradeable	Yes
Last Deployed Slot	91,012,034
Upgrade Authority	CiXLP94T6QBrEdevnJrGEDcN3iRaEWMKm7bv76ogpsZm

Program Executable Data Account
Address	Fjy8guoUgsnWmz9WVtGFHg8g96fX9YebRbFWNof8YETo
Balance (SOL)	◎0.85188312
Data (Bytes)	122269
Upgradeable	Yes
Last Deployed Slot	91,012,034
Upgrade Authority	CiXLP94T6QBrEdevnJrGEDcN3iRaEWMKm7bv76ogpsZm

Overview
Address	CiXLP94T6QBrEdevnJrGEDcN3iRaEWMKm7bv76ogpsZm
Balance (SOL)	◎2.71772184
Data (Bytes)	0
Owner	System Program
Executable	No

4)客户端调用合约:

npm run start

> [email protected] start /Users/xxx/zyd/example-helloworld
> ts-node src/client/main.ts

Let's say hello to a Solana account...
Connection to cluster established: https://api.testnet.solana.com { 'feature-set': 660526986, 'solana-core': '1.7.10' }
Using account CiXLP94T6QBrEdevnJrGEDcN3iRaEWMKm7bv76ogpsZm containing 2.71772184 SOL to pay for fees
Using program CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk
Creating account GFJ47ADnEoebbCPiLVBexA8b5DMVoFgEH8tTCc3WTtbZ to say hello to
Saying hello to GFJ47ADnEoebbCPiLVBexA8b5DMVoFgEH8tTCc3WTtbZ
GFJ47ADnEoebbCPiLVBexA8b5DMVoFgEH8tTCc3WTtbZ has been greeted 1 time(s)
Success

5)合约日志(Program Log)为:【合约中的msg!()调试信息】

Program CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk invoke [1]
Hello World Rust program entrypoint
Greeted 1 time(s)!
Program CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk consumed 1265 of 200000 compute units
Program CmMvA2C6zJCvoGhpiDpGcsQPi6m7wqXJM8UjqEmrjvGk success

Solana合约部署及调用_第1张图片

参考资料

[1] Solana Program Library(SPL)
[2] Solana Tutorial | Building Smart Contracts & dApps For The Solana Hackathon

你可能感兴趣的:(区块链,rust,区块链)