-
Notifications
You must be signed in to change notification settings - Fork 178
/
events.go
66 lines (55 loc) · 1.28 KB
/
events.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
package generator
import (
"fmt"
"github.com/onflow/cadence"
encoding "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/model/flow"
)
type Events struct {
count uint32
ids *Identifiers
}
func EventGenerator() *Events {
return &Events{
count: 1,
ids: IdentifierGenerator(),
}
}
func (g *Events) New() flow.Event {
location := common.StringLocation("test")
identifier := fmt.Sprintf("FooEvent%d", g.count)
typeID := location.TypeID(identifier)
testEventType := &cadence.EventType{
Location: location,
QualifiedIdentifier: identifier,
Fields: []cadence.Field{
{
Identifier: "a",
Type: cadence.IntType{},
},
{
Identifier: "b",
Type: cadence.StringType{},
},
},
}
testEvent := cadence.NewEvent(
[]cadence.Value{
cadence.NewInt(int(g.count)),
cadence.NewString("foo"),
}).WithType(testEventType)
payload, err := encoding.Encode(testEvent)
if err != nil {
panic(fmt.Sprintf("unexpected error while encoding events: %s", err))
}
event := flow.Event{
Type: flow.EventType(typeID),
TransactionID: g.ids.New(),
TransactionIndex: g.count,
EventIndex: g.count,
Payload: payload,
}
g.count++
return event
}