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 decode the instruction.data? #151

Open
yangyile1990 opened this issue Aug 15, 2023 · 1 comment
Open

how to decode the instruction.data? #151

yangyile1990 opened this issue Aug 15, 2023 · 1 comment

Comments

@yangyile1990
Copy link

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

type TransferInstructParam struct {
Instruction token.Instruction
Lamports uint64
}

type TransferInstructParam2 struct {
Instruction token.Instruction
Lamports uint64
Seed string
ProgramID common.PublicKey
}

func DecodeParam[T any](data []byte) (*T, error) {
var param T
err := bin.NewBinDecoder(data).Decode(&param)
if err != nil {
return nil, err
}
return &param, nil
}

func EncodeParam(param any) (string, *cerror.CError) {
buf := new(bytes.Buffer)
if err := bin.NewBinEncoder(buf).Encode(param); err != nil {
return "", cerror.NewError(cerror.CODE_PARAM_ERROR, err.Error())
}
return base58.Encode(buf.Bytes()), nil
}

txSig, err := solana.SignatureFromBase58(txid)
if err != nil {
return nil, cerror.NewError(cerror.CODE_PARAM_ERROR, err.Error())
}
out, err := s.client3.GetTransaction(context.TODO(), txSig, &rpc3.GetTransactionOpts{
Encoding: solana.EncodingBase64,
})
if err != nil {
return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, err.Error())
}
txn, err := out.Transaction.GetTransaction()
if err != nil {
return nil, cerror.NewError(cerror.CODE_NODE_RESP_ERROR, err.Error())
}
tmg := txn.Message
for _, v := range tmg.Instructions {
program, err := txn.ResolveProgramIDIndex(v.ProgramIDIndex)
if err != nil {
return nil, cerror.NewError(cerror.CODE_NODE_RESP_ERROR, err.Error())
}
fmt.Println(program.String())
switch program.String() {
case common.SystemProgramID.String():
param, err := DecodeParamTransferInstructParam
if err != nil {
return nil, cerror.NewError(cerror.CODE_NODE_RESP_ERROR, err.Error())
}
fmt.Println(cjson.NeatString(param))

		encode, erc := EncodeParam(param)
		if erc != nil {
			return nil, erc
		}
		fmt.Println(encode)

	case common.TokenProgramID.String():
		param, err := DecodeParam[TokenInstructParam](v.Data)
		if err != nil {
			return nil, cerror.NewError(cerror.CODE_NODE_RESP_ERROR, err.Error())
		}
		fmt.Println(cjson.NeatString(param))

		encode, erc := EncodeParam(param)
		if erc != nil {
			return nil, erc
		}
		fmt.Println(encode)
	default:
		//param, err := DecodeParam(v.Data)
		//if err != nil {
		//	continue
		//}
		//fmt.Println(cjson.NeatString(param))
		continue
	}
}
fmt.Println(cjson.NeatString(txn))
@yangyile1990
Copy link
Author

5Mpkm4FoNsncQG837qF1NvRQZMiB7efBLZ2kqVJJe9FgirWaEo8g6trUbCRceZeJMannXap7ry82nG1epXkk91rV

{
"Instruction": 2,
"Lamports": 167772160000
}

2Ue3QA7ws8Fh != 3Bxs43ZMjSRQLs6o

how to decode the 3Bxs43ZMjSRQLs6o to get:

截屏2023-08-15 09 33 58

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

1 participant