github.com/gagliardetto/solana-go@v1.11.0/rpc/getParsedTransaction.go (about) 1 package rpc 2 3 import ( 4 "context" 5 "fmt" 6 7 bin "github.com/gagliardetto/binary" 8 "github.com/gagliardetto/solana-go" 9 ) 10 11 type GetParsedTransactionOpts struct { 12 // Desired commitment. "processed" is not supported. If parameter not provided, the default is "finalized". 13 Commitment CommitmentType `json:"commitment,omitempty"` 14 15 // Max transaction version to return in responses. 16 // If the requested block contains a transaction with a higher version, an error will be returned. 17 MaxSupportedTransactionVersion *uint64 18 } 19 20 type GetParsedTransactionResult struct { 21 Slot uint64 22 BlockTime *solana.UnixTimeSeconds 23 Transaction *ParsedTransaction 24 Meta *ParsedTransactionMeta 25 } 26 27 func (cl *Client) GetParsedTransaction( 28 ctx context.Context, 29 txSig solana.Signature, 30 opts *GetParsedTransactionOpts, 31 ) (out *GetParsedTransactionResult, err error) { 32 params := []interface{}{txSig} 33 obj := M{} 34 if opts != nil { 35 if opts.Commitment != "" { 36 obj["commitment"] = opts.Commitment 37 } 38 if opts.MaxSupportedTransactionVersion != nil { 39 obj["maxSupportedTransactionVersion"] = *opts.MaxSupportedTransactionVersion 40 } 41 } 42 obj["encoding"] = solana.EncodingJSONParsed 43 params = append(params, obj) 44 err = cl.rpcClient.CallForInto(ctx, &out, "getTransaction", params) 45 if err != nil { 46 return nil, err 47 } 48 if out == nil { 49 return nil, ErrNotFound 50 } 51 return 52 } 53 54 func (wrap InstructionInfoEnvelope) MarshalJSON() ([]byte, error) { 55 if wrap.asString != "" { 56 return json.Marshal(wrap.asString) 57 } 58 return json.Marshal(wrap.asInstructionInfo) 59 } 60 61 func (wrap *InstructionInfoEnvelope) UnmarshalJSON(data []byte) error { 62 if len(data) == 0 || (len(data) == 4 && string(data) == "null") { 63 // TODO: is this an error? 64 return nil 65 } 66 67 firstChar := data[0] 68 69 switch firstChar { 70 // Check if first character is `[`, standing for a JSON array. 71 case '"': 72 // It's base64 (or similar) 73 { 74 err := json.Unmarshal(data, &wrap.asString) 75 if err != nil { 76 return err 77 } 78 } 79 case '{': 80 // It's JSON, most likely. 81 { 82 return json.Unmarshal(data, &wrap.asInstructionInfo) 83 } 84 default: 85 return fmt.Errorf("Unknown kind: %v", data) 86 } 87 88 return nil 89 } 90 91 func (obj GetParsedTransactionResult) MarshalWithEncoder(encoder *bin.Encoder) (err error) { 92 err = encoder.WriteUint64(obj.Slot, bin.LE) 93 if err != nil { 94 return err 95 } 96 { 97 if obj.BlockTime == nil { 98 err = encoder.WriteBool(false) 99 if err != nil { 100 return err 101 } 102 } else { 103 err = encoder.WriteBool(true) 104 if err != nil { 105 return err 106 } 107 err = encoder.WriteInt64(int64(*obj.BlockTime), bin.LE) 108 if err != nil { 109 return err 110 } 111 } 112 } 113 { 114 if obj.Transaction == nil { 115 err = encoder.WriteBool(false) 116 if err != nil { 117 return err 118 } 119 } else { 120 err = encoder.WriteBool(true) 121 if err != nil { 122 return err 123 } 124 err = encoder.Encode(obj.Transaction) 125 if err != nil { 126 return err 127 } 128 } 129 } 130 return nil 131 } 132 133 func (obj GetParsedTransactionResult) UnmarshalWithDecoder(decoder *bin.Decoder) (err error) { 134 // Deserialize `Slot`: 135 obj.Slot, err = decoder.ReadUint64(bin.LE) 136 if err != nil { 137 return err 138 } 139 // Deserialize `BlockTime` (optional): 140 { 141 ok, err := decoder.ReadBool() 142 if err != nil { 143 return err 144 } 145 if ok { 146 err = decoder.Decode(&obj.BlockTime) 147 if err != nil { 148 return err 149 } 150 } 151 } 152 { 153 ok, err := decoder.ReadBool() 154 if err != nil { 155 return err 156 } 157 if ok { 158 // NOTE: storing as JSON bytes: 159 buf, err := decoder.ReadByteSlice() 160 if err != nil { 161 return err 162 } 163 err = json.Unmarshal(buf, &obj.Transaction) 164 if err != nil { 165 return err 166 } 167 } 168 } 169 return nil 170 }