/
flowcontractaudits.go
106 lines (98 loc) · 3.11 KB
/
flowcontractaudits.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
package flowcontractaudits
import (
"github.com/nakji-network/connectors/flow"
"github.com/onflow/cadence"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
var Types = []proto.Message{
&AuditorCreated{},
&VoucherCreated{},
&VoucherUsed{},
&VoucherRemoved{},
}
type SmartContract struct{}
func (c SmartContract) Address() string {
return "e467b9dd11fa00df"
}
func (c SmartContract) Name() string {
return "FlowContractAudits"
}
func (c SmartContract) Events() []string {
return []string{
"A.e467b9dd11fa00df.FlowContractAudits.AuditorCreated",
"A.e467b9dd11fa00df.FlowContractAudits.VoucherCreated",
"A.e467b9dd11fa00df.FlowContractAudits.VoucherUsed",
"A.e467b9dd11fa00df.FlowContractAudits.VoucherRemoved",
}
}
func (c SmartContract) Message(vLog *flow.Log) proto.Message {
switch vLog.Type.EventName {
case "AuditorCreated":
msg := &AuditorCreated{
Ts: timestamppb.New(vLog.Timestamp),
BlockNumber: vLog.Height,
TxID: vLog.TransactionID[:],
LogIndex: uint64(vLog.TransactionIndex),
}
return msg
case "VoucherCreated":
address := vLog.Value.Fields[0].(cadence.Optional)
recurrent := vLog.Value.Fields[1].(cadence.Bool)
expiryBlockHeight := vLog.Value.Fields[2].(cadence.Optional)
codeHash := vLog.Value.Fields[3].(cadence.String)
msg := &VoucherCreated{
Ts: timestamppb.New(vLog.Timestamp),
BlockNumber: vLog.Height,
TxID: vLog.TransactionID[:],
LogIndex: uint64(vLog.TransactionIndex),
Recurrent: bool(recurrent),
CodeHash: string(codeHash),
}
if address.Value != nil {
msg.Address = address.Value.(cadence.Address).Bytes()
}
if expiryBlockHeight.Value != nil {
msg.ExpiryBlockHeight = uint64(expiryBlockHeight.Value.(cadence.UInt64))
}
return msg
case "VoucherUsed":
address := vLog.Value.Fields[0].(cadence.Address)
key := vLog.Value.Fields[1].(cadence.String)
recurrent := vLog.Value.Fields[2].(cadence.Bool)
expiryBlockHeight := vLog.Value.Fields[3].(cadence.Optional)
msg := &VoucherUsed{
Ts: timestamppb.New(vLog.Timestamp),
BlockNumber: vLog.Height,
TxID: vLog.TransactionID[:],
LogIndex: uint64(vLog.TransactionIndex),
Address: address.Bytes(),
Key: string(key),
Recurrent: bool(recurrent),
}
if expiryBlockHeight.Value != nil {
msg.ExpiryBlockHeight = uint64(expiryBlockHeight.Value.(cadence.UInt64))
}
return msg
case "VoucherRemoved":
key := vLog.Value.Fields[0].(cadence.String)
recurrent := vLog.Value.Fields[1].(cadence.Bool)
expiryBlockHeight := vLog.Value.Fields[2].(cadence.Optional)
msg := &VoucherRemoved{
Ts: timestamppb.New(vLog.Timestamp),
BlockNumber: vLog.Height,
TxID: vLog.TransactionID[:],
LogIndex: uint64(vLog.TransactionIndex),
Key: string(key),
Recurrent: bool(recurrent),
}
if expiryBlockHeight.Value != nil {
msg.ExpiryBlockHeight = uint64(expiryBlockHeight.Value.(cadence.UInt64))
}
return msg
default:
log.Error().Msgf("invalid event: %s", vLog.Type.EventName)
return nil
}
}