blocto / solana-go-sdk

Solana Golang SDK
https://blocto.github.io/solana-go-sdk/
MIT License
349 stars 86 forks source link

how to deserialize the InstructParam? #142

Open yangyile1990 opened 11 months ago

yangyile1990 commented 11 months ago

I use the code

func TransferChecked(param TransferCheckedParam) types.Instruction {
    data, err := bincode.SerializeData(struct {
        Instruction Instruction
        Amount      uint64
        Decimals    uint8
    }{
        Instruction: InstructionTransferChecked,
        Amount:      param.Amount,
        Decimals:    param.Decimals,
    })

then i get

"Data": "DADKmjsAAAAACQ=="

there is a question: how to use "Data": "DADKmjsAAAAACQ==" to DeserializeData to get:

struct {
        Instruction Instruction
        Amount      uint64
        Decimals    uint8
    }{
        Instruction: InstructionTransferChecked,
        Amount:      param.Amount,
        Decimals:    param.Decimals,
    }
yangyile1990 commented 11 months ago

The gpt get me the answer:

package main

import (
    "encoding/base64"
    "fmt"
    "github.com/blocto/solana-go-sdk/pkg/bincode"
    "github.com/blocto/solana-go-sdk/program/token"
)

/*
要对使用该函数进行序列化的数据进行反序列化,需要编写一个与序列化逻辑相匹配的反序列化函数。
以下是一个示例的反序列化函数,假设输入的字节切片为 `data`:
*/
func main() {
    // 创建一个示例结构体
    param := InstructParam{
        Instruction: token.InstructionTransferChecked,
        Amount:      1e9,
        Decimals:    9,
    }
    // 序列化结构体
    data, err := bincode.SerializeData(param)
    if err != nil {
        fmt.Println("Serialization error:", err)
        return
    }
    // 打印序列化后的字节切片
    fmt.Println("Serialized data:", base64.StdEncoding.EncodeToString(data))

    var param2 InstructParam
    err = bincode.DeserializeData(data, &param2)
    if err != nil {
        fmt.Println("Serialization error:", err)
        return
    }
}

type InstructParam struct {
    Instruction token.Instruction
    Amount      uint64
    Decimals    uint8
}

while, my idea show "Unresolved reference 'DeserializeData'".

yangyile1990 commented 11 months ago

I use GetTransactionWithConfig

txID := "5Q9bJdKaMpQd3yorG9i8tkXGjYbHH5GT4T8nWfKTEgwBEcG1Vpw6gA1tLaoup1vXiJEkh9eVGU2TTh5aqHsQYuhe"

    // 连接 Solana 主网
    clientX := client.NewClient("https://api.mainnet-beta.solana.com")

    // 查询交易信息
    txn, err := clientX.GetTransactionWithConfig(context.Background(), txID, client.GetTransactionConfig{
        Commitment: rpc.CommitmentConfirmed,
    })
    utils_check.Done(err)

    out := utils_encoding_json_soft.NeatString(&txn)
    fmt.Println(out)

it return a

"Data": "xzhVJpLzJZ4DAAAAR0hT"

But how to know this transaction is from who to who how many what mint?

yihau commented 11 months ago

for now, you need to parse accounts by yourself. I will try to update this sdk and make this process simpler this week.

yangyile1990 commented 11 months ago

ok!