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