forked from doublejumptokyo/go-lbd
/
transactions.go
98 lines (82 loc) · 2.13 KB
/
transactions.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
package lbd
import (
"encoding/json"
"fmt"
"strings"
)
func UnmarshalTransaction(data []byte) (*Transaction, error) {
r := new(Transaction)
return r, json.Unmarshal(data, r)
}
func (r *Transaction) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Transaction struct {
Height int64 `json:"height"`
Txhash string `json:"txhash"`
Index int64 `json:"index"`
Codespace string `json:"codespace"`
Code int64 `json:"code"`
Logs []Log `json:"logs"`
GasWanted int64 `json:"gasWanted"`
GasUsed int64 `json:"gasUsed"`
Tx Tx `json:"tx"`
Timestamp int64 `json:"timestamp"`
}
func (t *Transaction) Check() (err error) {
if t.Code == 0 {
return nil
}
return fmt.Errorf("Transaction failure %s %d", t.Codespace, t.Code)
}
type Log struct {
MsgIndex int64 `json:"msgIndex"`
Success bool `json:"success"`
Log string `json:"log"`
Events []Event `json:"events"`
}
type Event struct {
Type string `json:"type"`
Attributes []Attribute `json:"attributes"`
}
type Attribute struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Tx struct {
Type string `json:"type"`
Value TxValue `json:"value"`
}
type TxValue struct {
Msg []Msg `json:"msg"`
Fee Fee `json:"fee"`
Memo string `json:"memo"`
Signatures []Signature `json:"signatures"`
}
type Fee struct {
Gas int64 `json:"gas"`
Amount []interface{} `json:"amount"`
}
type Msg struct {
Type string `json:"type"`
Value json.RawMessage `json:"value"`
}
type Signature struct {
PubKey PubKey `json:"pubKey"`
Signature string `json:"signature"`
}
type PubKey struct {
Type string `json:"type"`
Value string `json:"value"`
}
func (l LBD) RetrieveTransactionInformation(txHash string) (*Transaction, error) {
r := NewGetRequest("/v1/transactions/" + txHash)
resp, err := l.Do(r, true)
if err != nil {
return nil, err
}
return UnmarshalTransaction(resp.ResponseData)
}
func (l LBD) GetExplorerURL(tx *Transaction) string {
return fmt.Sprintf("https://explorer.blockchain.line.me/%s/transaction/%s", strings.ToLower(string(l.Network)), tx.Txhash)
}