系统:maxOS 10.15.6
科学上网
安装Solana tool suite
sh -c "$(curl -sSfL https://release.solana.com/v1.8.0/install)"
成功以后:
% solana --version
solana-cli 1.8.0 (src:4a8ff62a; feat:1813598585)
后续可以使用solana-install update
来更新solana版本
设置成testnet:
% solana config set --url https://api.testnet.solana.com
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
或者本地的solana:
% solana config set --url localhost
Config File: /Users/walker/.config/solana/cli/config.yml
RPC URL: http://localhost:8899
WebSocket URL: ws://localhost:8900/ (computed)
Keypair Path: /Users/walker/.config/solana/id.json
Commitment: confirmed
使用本地solana需要启动本地-solana-集群
% solana-test-validator
--faucet-sol argument ignored, ledger already exists
Ledger location: test-ledger
Log: test-ledger/validator.log
Identity: 23PX3DegcBYMQsHVRHq3o7Anu55PoGt8mNGB23FUL7p8
Genesis Hash: ARvXyebYawVgpbAJjvXBfjHrdSgcpeJ5DWEFaLu1W4Hm
Version: 1.8.0
Shred Version: 19994
Gossip Address: 127.0.0.1:1024
TPU Address: 127.0.0.1:1027
JSON RPC URL: http://127.0.0.1:8899
⠉ 01:04:49 | Processed Slot: 8090 | Confirmed Slot: 8090 | Finalized Slot: 8058
创建keypair:
solana-keygen new --force
获取测试token
% solana airdrop 1
Requesting airdrop of 1 SOL
Signature: 4SbKZcR3bifVYkEHZPcbySe2kKuBKktLrkW66zrRG6Fx5ygEMASuieFSWPkoBotJuzciUgaRZNQTNX4Tn4zLQVLa
1 SOL
查看交易详情:
% solana confirm -v 4SbKZcR3bifVYkEHZPcbySe2kKuBKktLrkW66zrRG6Fx5ygEMASuieFSWPkoBotJuzciUgaRZNQTNX4Tn4zLQVLa
RPC URL: https://api.testnet.solana.com
Default Signer Path: /Users/walker/.config/solana/id.json
Commitment: confirmed
Transaction executed in slot 97286184:
Block Time: 2021-10-03T04:34:54+08:00
Recent Blockhash: 2qSGsCZm3aNWXVBP4BoUkQH4YDZGswvJYmD9hgNouzTG
Signature 0: 4SbKZcR3bifVYkEHZPcbySe2kKuBKktLrkW66zrRG6Fx5ygEMASuieFSWPkoBotJuzciUgaRZNQTNX4Tn4zLQVLa
Account 0: srw- 4ETf86tK7b4W72f27kNLJLgRWi9UfJjgH4koHGUXMFtn (fee payer)
Account 1: -rw- 66GudFopK5bxiYCA1GTHujayJsn6Y7Xi2Wivtwt43sku
Account 2: -r-x 11111111111111111111111111111111
Instruction 0
Program: 11111111111111111111111111111111 (2)
Account 0: 4ETf86tK7b4W72f27kNLJLgRWi9UfJjgH4koHGUXMFtn (0)
Account 1: 66GudFopK5bxiYCA1GTHujayJsn6Y7Xi2Wivtwt43sku (1)
Transfer { lamports: 1000000000 }
Status: Ok
Fee: ◎0.000005
Account 0 balance: ◎413252.390348847 -> ◎413251.390343847
Account 1 balance: ◎0 -> ◎1
Account 2 balance: ◎0.000000001
Log Messages:
Program 11111111111111111111111111111111 invoke [1]
Program 11111111111111111111111111111111 success
Finalized
编译合约
git clone https://github.com/solana-labs/example-helloworld.git
cd example-helloworld
...
npm install
npm run build:program-rust
部署合约
使用的是本地solana
solana program deploy dist/program/helloworld.so
Program Id: Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8
查看合约信息:
% solana program show Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8
Program Id: Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8
Owner: BPFLoaderUpgradeab1e11111111111111111111111
ProgramData Address: DPvoWA5ksmrCLQgvzSKYWpf7xmvDzZKFdoBe4ENfBgiL
Authority: 66GudFopK5bxiYCA1GTHujayJsn6Y7Xi2Wivtwt43sku
Last Deployed In Slot: 4689
Data Length: 121600 (0x1db00) bytes
Balance: 0.84754008 SOL
客户端调用合约:
npm run start
> [email protected] start
> ts-node src/client/main.ts
Let's say hello to a Solana account...
Connection to cluster established: http://localhost:8899 { 'feature-set': 1813598585, 'solana-core': '1.8.0' }
Using account 66GudFopK5bxiYCA1GTHujayJsn6Y7Xi2Wivtwt43sku containing 499999999.15006477 SOL to pay for fees
Using program Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8
Saying hello to Fp4nekj77n5FWUzUVoCejfj1Di4YiimnuD5uT2n3yt1Y
Fp4nekj77n5FWUzUVoCejfj1Di4YiimnuD5uT2n3yt1Y has been greeted 2 time(s)
Success
查看log日志:
% solana logs
Streaming transaction logs. Confirmed commitment
Transaction executed in slot 5661:
Signature: 53M8st8d364xxjgkdMp31y256x85rc8PnYp3APVWYeQ5KfkLdcnwX5eyzR4dCbvTBe8RGmYqzvtcz7MDEh12tTyn
Status: Ok
Log Messages:
Program Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8 invoke [1]
Program log: Hello World Rust program entrypoint
Program log: Greeted 2 time(s)!
Program Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8 consumed 1268 of 200000 compute units
Program Ab4H2Tsd8j3Rp82sBvFWXa6utWs8n2VbXcu3cvpJ1zc8 success
引用:
solana helloworld测试合约 git
solidity与solana比较 以token合约为展开