gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
930 stars 264 forks source link

how to decode the instruction.data? #151

Open yangyile1990 opened 1 year ago

yangyile1990 commented 1 year ago

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_RESPERROR, 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 commented 1 year ago

5Mpkm4FoNsncQG837qF1NvRQZMiB7efBLZ2kqVJJe9FgirWaEo8g6trUbCRceZeJMannXap7ry82nG1epXkk91rV

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

2Ue3QA7ws8Fh != 3Bxs43ZMjSRQLs6o

how to decode the 3Bxs43ZMjSRQLs6o to get:

截屏2023-08-15 09 33 58