Skip to content

Commit 1e333c6

Browse files
TzKT events: Typed messgaes
1 parent 0744d0c commit 1e333c6

File tree

5 files changed

+283
-33
lines changed

5 files changed

+283
-33
lines changed

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,61 @@ func main() {
110110
}
111111
defer tzkt.Close()
112112

113+
if err := tzkt.SubscribeToHead(); err != nil {
114+
panic(err)
115+
}
116+
113117
if err := tzkt.SubscribeToBlocks(); err != nil {
114-
log.Panic(err)
118+
panic(err)
119+
}
120+
121+
if err := tzkt.SubscribeToAccounts("KT1K4EwTpbvYN9agJdjpyJm4ZZdhpUNKB3F6"); err != nil {
122+
panic(err)
123+
}
124+
125+
if err := tzkt.SubscribeToBigMaps(nil, "KT1K4EwTpbvYN9agJdjpyJm4ZZdhpUNKB3F6", ""); err != nil {
126+
panic(err)
127+
}
128+
129+
if err := tzkt.SubscribeToOperations("KT1K4EwTpbvYN9agJdjpyJm4ZZdhpUNKB3F6", events.KindTransaction); err != nil {
130+
panic(err)
115131
}
116132

117133
for msg := range tzkt.Listen() {
118-
log.Println(msg)
134+
switch msg.Type {
135+
case events.MessageTypeData:
136+
137+
switch msg.Channel {
138+
case events.ChannelAccounts:
139+
items := msg.Body.([]events.Account)
140+
for _, item := range items {
141+
log.Println(item)
142+
}
143+
case events.ChannelBigMap:
144+
items := msg.Body.([]events.BigMapUpdate)
145+
for _, item := range items {
146+
log.Println(item)
147+
}
148+
case events.ChannelBlocks:
149+
items := msg.Body.([]events.Block)
150+
for _, item := range items {
151+
log.Println(item)
152+
}
153+
case events.ChannelHead:
154+
head := msg.Body.(events.Head)
155+
log.Println(head)
156+
case events.ChannelOperations:
157+
items := msg.Body.([]interface{})
158+
for _, item := range items {
159+
log.Println(item.(*events.Transaction))
160+
}
161+
}
162+
163+
case events.MessageTypeReorg:
164+
log.Print("reorg")
165+
case events.MessageTypeState:
166+
log.Print("initialized")
167+
}
119168
}
120169
}
121170

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/json-iterator/go v1.1.10
1010
github.com/pkg/errors v0.9.1
1111
github.com/sergi/go-diff v1.2.0
12+
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc
1213
github.com/sirupsen/logrus v1.7.0
1314
github.com/stretchr/testify v1.7.0
1415
github.com/tidwall/gjson v1.8.1

tzkt/events/message.go

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package events
22

3-
import "fmt"
3+
import (
4+
stdJSON "encoding/json"
5+
"fmt"
6+
"time"
7+
8+
"github.com/shopspring/decimal"
9+
)
410

511
// MessageType - TzKT message type
612
type MessageType int
@@ -15,9 +21,9 @@ const (
1521
// Message - message struct
1622
type Message struct {
1723
Channel string
18-
Type MessageType
19-
State uint64
20-
Body interface{}
24+
Type MessageType `json:"type"`
25+
State uint64 `json:"state"`
26+
Body interface{} `json:"data"`
2127
}
2228

2329
// String -
@@ -28,3 +34,141 @@ func (msg Message) String() string {
2834
}
2935
return s
3036
}
37+
38+
// Packet -
39+
type Packet struct {
40+
Type MessageType `json:"type"`
41+
State uint64 `json:"state"`
42+
Data stdJSON.RawMessage `json:"data,omitempty"`
43+
}
44+
45+
// Head -
46+
type Head struct {
47+
Chain string `json:"chain"`
48+
ChainID string `json:"chainId"`
49+
Cycle int64 `json:"cycle"`
50+
Level int64 `json:"level"`
51+
Hash string `json:"hash"`
52+
Protocol string `json:"protocol"`
53+
Timestamp time.Time `json:"timestamp"`
54+
VotingEpoch int64 `json:"votingEpoch"`
55+
VotingPeriod int64 `json:"votingPeriod"`
56+
KnownLevel int64 `json:"knownLevel"`
57+
LastSync time.Time `json:"lastSync"`
58+
Synced bool `json:"synced"`
59+
QuoteLevel int64 `json:"quoteLevel"`
60+
QuoteBtc decimal.Decimal `json:"quoteBtc"`
61+
QuoteEur decimal.Decimal `json:"quoteEur"`
62+
QuoteUsd decimal.Decimal `json:"quoteUsd"`
63+
QuoteCny decimal.Decimal `json:"quoteCny"`
64+
QuoteJpy decimal.Decimal `json:"quoteJpy"`
65+
QuoteKrw decimal.Decimal `json:"quoteKrw"`
66+
QuoteEth decimal.Decimal `json:"quoteEth"`
67+
}
68+
69+
// Block -
70+
type Block struct {
71+
Cycle int64 `json:"cycle"`
72+
Level int64 `json:"level"`
73+
Hash string `json:"hash"`
74+
Timestamp time.Time `json:"timestamp"`
75+
Proto int64 `json:"proto"`
76+
Priority int64 `json:"priority"`
77+
Validations int64 `json:"validations"`
78+
Deposit int64 `json:"deposit"`
79+
Reward int64 `json:"reward"`
80+
Fees int64 `json:"fees"`
81+
NonceRevealed bool `json:"nonceRevealed"`
82+
Baker Address `json:"baker"`
83+
Software struct {
84+
Version string `json:"version"`
85+
Date time.Time `json:"date"`
86+
} `json:"software"`
87+
LbEscapeVote bool `json:"lbEscapeVote"`
88+
LbEscapeEma int64 `json:"lbEscapeEma"`
89+
}
90+
91+
// Address -
92+
type Address struct {
93+
Alias string `json:"alias"`
94+
Address string `json:"address"`
95+
}
96+
97+
// Account -
98+
type Account struct {
99+
Type string `json:"type"`
100+
Address string `json:"address"`
101+
Kind string `json:"kind"`
102+
Tzips []string `json:"tzips"`
103+
Alias string `json:"alias"`
104+
Balance int64 `json:"balance"`
105+
Creator Address `json:"creator"`
106+
NumContracts int64 `json:"numContracts"`
107+
NumDelegations int64 `json:"numDelegations"`
108+
NumOriginations int64 `json:"numOriginations"`
109+
NumTransactions int64 `json:"numTransactions"`
110+
NumReveals int64 `json:"numReveals"`
111+
NumMigrations int64 `json:"numMigrations"`
112+
FirstActivity int64 `json:"firstActivity"`
113+
FirstActivityTime time.Time `json:"firstActivityTime"`
114+
LastActivity int64 `json:"lastActivity"`
115+
LastActivityTime time.Time `json:"lastActivityTime"`
116+
TypeHash int64 `json:"typeHash"`
117+
CodeHash int64 `json:"codeHash"`
118+
}
119+
120+
// BigMapUpdate -
121+
type BigMapUpdate struct {
122+
ID int64 `json:"id"`
123+
Level int64 `json:"level"`
124+
Timestamp time.Time `json:"timestamp"`
125+
Bigmap int64 `json:"bigmap"`
126+
Contract Address `json:"contract"`
127+
Path string `json:"path"`
128+
Action string `json:"action"`
129+
Content *Content `json:"content,omitempty"`
130+
}
131+
132+
// Content -
133+
type Content struct {
134+
Hash string `json:"hash"`
135+
Key string `json:"key"`
136+
Value stdJSON.RawMessage `json:"value"`
137+
}
138+
139+
// Operation -
140+
type Operation struct {
141+
Type string `json:"type"`
142+
}
143+
144+
// Transaction -
145+
type Transaction struct {
146+
Operation
147+
148+
Sender Address `json:"sender"`
149+
Target Address `json:"target"`
150+
Amount decimal.Decimal `json:"amount"`
151+
Parameter Parameter `json:"parameter"`
152+
Timestamp time.Time `json:"timestamp"`
153+
ID int64 `json:"id"`
154+
Level int64 `json:"level"`
155+
Counter int64 `json:"counter"`
156+
GasLimit int64 `json:"gasLimit"`
157+
GasUsed int64 `json:"gasUsed"`
158+
StorageLimit int64 `json:"storageLimit"`
159+
StorageUsed int64 `json:"storageUsed"`
160+
BakerFee int64 `json:"bakerFee"`
161+
StorageFee int64 `json:"storageFee"`
162+
AllocationFee int64 `json:"allocationFee"`
163+
Status string `json:"status"`
164+
Parameters string `json:"parameters"`
165+
Block string `json:"block"`
166+
Hash string `json:"hash"`
167+
HasInternals bool `json:"hasInternals"`
168+
}
169+
170+
// Parameter -
171+
type Parameter struct {
172+
Entrypoint string `json:"entrypoint"`
173+
Value stdJSON.RawMessage `json:"value"`
174+
}

tzkt/events/signalr/message.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package signalr
22

3+
import (
4+
stdJSON "encoding/json"
5+
)
6+
37
// MessageType -
48
type MessageType int
59

@@ -46,9 +50,9 @@ type Message struct {
4650
// Invocation - a `Invocation` message
4751
type Invocation struct {
4852
Message
49-
Target string `json:"target"`
50-
Arguments []interface{} `json:"arguments"`
51-
StreamsID []string `json:"streamIds,omitempty"`
53+
Target string `json:"target"`
54+
Arguments []stdJSON.RawMessage `json:"arguments"`
55+
StreamsID []string `json:"streamIds,omitempty"`
5256
}
5357

5458
// StreamInvocation - a `StreamInvocation` message
@@ -113,12 +117,21 @@ func NewInvocation(id, target string, args ...interface{}) Invocation {
113117
if args == nil {
114118
args = make([]interface{}, 0)
115119
}
120+
121+
arguments := make([]stdJSON.RawMessage, len(args))
122+
for i := range args {
123+
data, err := json.Marshal(args[i])
124+
if err == nil {
125+
arguments[i] = data
126+
}
127+
}
128+
116129
return Invocation{
117130
Message: Message{
118131
Type: Type{MessageTypeInvocation},
119132
ID: id,
120133
},
121134
Target: target,
122-
Arguments: args,
135+
Arguments: arguments,
123136
}
124137
}

0 commit comments

Comments
 (0)