这是我的代码,目前解析通过,var mint token.Account结构体,但是原始二进制结构未知,请问该咋办,请问如何解析数据呢:
package main
import (
"encoding/json"
"fmt"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go/programs/token"
"github.com/gorilla/websocket"
"log"
)
func main() {
// WebSocket 连接地址
socketURL := "wss://api.mainnet-beta.solana.com"
// 建立 WebSocket 连接
conn, _, err := websocket.DefaultDialer.Dial(socketURL, nil)
if err != nil {
log.Fatal("无法建立 WebSocket 连接:", err)
}
defer conn.Close()
// 准备订阅请求
subscribeRequest := map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": "programSubscribe",
"params": []interface{}{
"675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8", // 要监听的程序地址
map[string]interface{}{
"encoding": "base64+zstd",
},
},
}
// 发送订阅请求
err = conn.WriteJSON(subscribeRequest)
if err != nil {
log.Fatal("无法发送订阅请求:", err)
}
// 读取和处理订阅的消息
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Fatal("无法读取消息:", err)
}
var a AAAA
json.Unmarshal(message, &a)
var mint token.Account
if len(a.Params.Result.Value.Account.Data) == 0 {
continue
}
err = bin.NewBinDecoder([]byte(a.Params.Result.Value.Account.Data[0])).Decode(&mint)
if err != nil {
fmt.Printf("+v", err.Error())
} else {
fmt.Printf("%+v", mint)
}
bode, _ := json.Marshal(mint)
// 打印解压缩后的数据
fmt.Println("解压缩后的数据:", string(bode))
//fmt.Println("接收到消息:", string(message))
}
}
type AAAA struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params struct {
Result struct {
Context struct {
Slot int `json:"slot"`
} `json:"context"`
Value struct {
Pubkey string `json:"pubkey"`
Account struct {
Lamports int `json:"lamports"`
Data []string `json:"data"`
Owner string `json:"owner"`
Executable bool `json:"executable"`
RentEpoch float64 `json:"rentEpoch"`
Space int `json:"space"`
} `json:"account"`
} `json:"value"`
} `json:"result"`
Subscription int `json:"subscription"`
} `json:"params"`
}