Solana 开发学习之通过RPC与Solana交互
相关链接
JSON-RPC 2.0 规范
JSON-RPC 是一种无状态、轻量级远程过程调用 (RPC) 协议。该规范主要定义了几种数据结构及其处理规则。它与传输无关,因为这些概念可以在同一进程中、通过套接字、通过 http 或在许多不同的消息传递环境中使用。它使用JSON ( RFC 4627 ) 作为数据格式。
接口RPC
节点相关接口
获取集群节点信息
通过getClusterNodes方法可以获得当前网络内,集群节点的相关信息,比如验证者的key,节点IP,节点版本等。
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc": "2.0", "id": 1, "method": "getClusterNodes" } ' {"jsonrpc":"2.0","result":[{"featureSet":3580551090,"gossip":"67.209.54.90:8001","pubkey":"7pbH563fFai2Gm8aXGi27Toj1i7x55rGp7QQ8ZQt6C7i","pubsub":null,"rpc":null,"shredVersion":503,"tpu":"67.209.54.90:8004","tpuQuic":"67.209.54.90:8010","version":"1.17.21"},{"featureSet":3580551090,"gossip":"37.27.61.250:8000","pubkey":"HPpYXZ944SXpJB3Tb7Zzy2K7YD45zGREsGqPtEP43xBx","pubsub":null,"rpc":null,"shredVersion":503,"tpu":"37.27.61.250:8003","tpuQuic":"37.27.61.250:8009","version":"1.17.22"}, ...... {"featureSet":3011420684,"gossip":"69.197.5.60:8001","pubkey":"FKizb2faoz57ym1bTWcZhei3aUZu7eU5AiY1EYoZsok6","pubsub":null,"rpc":null,"shredVersion":503,"tpu":null,"tpuQuic":null,"version":"1.17.5"}],"id":1}
区块相关接口
获取当前区块高度
通过getBlockHeight可以获取当前的区块高度
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}
获取最近的Block Hash
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}
获取指定高度block的信息
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc": "2.0","id":1, "method":"getBlock", "params": [ 174302734, { "encoding": "jsonParsed", "maxSupportedTransactionVersion":0, "transactionDetails":"full", "rewards":false } ] } '
获取指定block的确认状态
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc": "2.0", "id": 1, "method": "getBlockCommitment", "params":[174302734] } ' {"jsonrpc":"2.0","result":{"commitment":null,"totalStake":158091345604635247},"id":1}
一次性获取多个Block的信息
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc": "2.0", "id": 1, "method": "getBlocks", "params": [ 174302734, 174302735 ] } ' {"jsonrpc":"2.0","result":[174302734,174302735],"id":1}
分页获取Block
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc": "2.0", "id":1, "method":"getBlocksWithLimit", "params":[174302734, 3] } ' {"jsonrpc":"2.0","result":[174302734,174302735,174302736],"id":1}
Slot和Epoch相关接口
获取当前Epoch信息
epoch在一般POS中比较常见,表示这个周期内,一些参与验证的节点信息是固定的,如果有新 节点或者节点权重变更,将在下一个epoch中生效。
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getEpochInfo"} ' {"jsonrpc":"2.0","result":{"absoluteSlot":280331471,"blockHeight":268627796,"epoch":648,"slotIndex":395471,"slotsInEpoch":432000,"transactionCount":13011134475},"id":1}
获取Epoch的调度信息
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getEpochSchedule" } ' {"jsonrpc":"2.0","result":{"firstNormalEpoch":0,"firstNormalSlot":0,"leaderScheduleSlotOffset":432000,"slotsPerEpoch":432000,"warmup":false},"id":1}
获取最新Slot
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' {"jsonrpc":"2.0","id":1, "method":"getSlot"} ' {"jsonrpc":"2.0","result":280333661,"id":1}
账号相关接口
获取Account信息
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}0
"executable"表示 是否为可执行合约
"lamports"表示余额,这里精度*10^9
所有普通账号的Owner都是系统根账号: "11111111111111111111111111111111"
获取账号余额
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}1
获取某个合约管理的所有Account
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}2
SPL-Token相关接口
获取某个Token Account账号的余额
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}3
交易相关接口
返回分类账中的当前交易计数
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}4
返回已确认交易的交易详细信息
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}5
推送RPC
accountSubscribe : 订阅Account的变化,比如lamports
logsSubscribe : 订阅交易的日志
programSubscribe : 订阅合约Account的变化
signatureSubscribe : 订阅签名状态变化
slotSubscribe : 订阅slot的变化
每个事件,还有对应的Unsubscribe动作,取消订阅。将上面的Subscribe替换成Unsubscribe即可。
安装wscat
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}6
建立连接
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}7
订阅合约所属于Account事件
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}8
练习
通过curl和wscat命令行来模拟一个监视钱包动作
创建账号
SOL账号: 6SWBzQWZndeaCKg3AzbY3zkvapCu9bHFZv12iiRoGvCD
SPL-Token(Mint Account): E7eHC3g4QsFXuaBe3X2wVr54yEvHK8K8fq6qrgB64djx
Token Account: HDv1RgdHjrjSdnTFJsMqQGPcKTiuF7zLjhNaSd7ihbKh
实时展示余额变化(订阅SOL余额变化)
获取1sol
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "jsonrpc":"2.0","id":1, "method":"getBlockHeight" } ' {"jsonrpc":"2.0","result":268621259,"id":1}9
订阅Account变化
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}0
列出已知SPL-Token的余额
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}1
获取SPL-Token下有多少 Token Account:
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}2
实时展示SPL-Token余额变化
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}3
转账交易
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}4
注意:转账2次,第一次监听连接断开故没有监控到。
查询余额
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}5
websocket 监控收到
curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d ' { "id":1, "jsonrpc":"2.0", "method":"getLatestBlockhash", "params":[ { "commitment":"processed" } ] } ' {"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.21","slot":280325472},"value":{"blockhash":"9ebRPaCY2pcKAPhWzjDtmLArbSzAH1Mb5n8PZzXKbW8X","lastValidBlockHeight":268622097}},"id":1}6