Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to deserialize the InstructParam? #142

Open
yangyile1990 opened this issue Aug 11, 2023 · 4 comments
Open

how to deserialize the InstructParam? #142

yangyile1990 opened this issue Aug 11, 2023 · 4 comments

Comments

@yangyile1990
Copy link

yangyile1990 commented Aug 11, 2023

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
Copy link
Author

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
Copy link
Author

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
Copy link
Collaborator

yihau commented Aug 14, 2023

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

@yangyile1990
Copy link
Author

ok!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants