github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/generator/events.go (about) 1 package generator 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/cadence" 7 encoding "github.com/onflow/cadence/encoding/json" 8 "github.com/onflow/cadence/runtime/common" 9 10 "github.com/koko1123/flow-go-1/model/flow" 11 ) 12 13 type Events struct { 14 count uint32 15 ids *Identifiers 16 } 17 18 func EventGenerator() *Events { 19 return &Events{ 20 count: 1, 21 ids: IdentifierGenerator(), 22 } 23 } 24 25 func (g *Events) New() flow.Event { 26 location := common.StringLocation("test") 27 identifier := fmt.Sprintf("FooEvent%d", g.count) 28 typeID := location.TypeID(nil, identifier) 29 30 testEventType := &cadence.EventType{ 31 Location: location, 32 QualifiedIdentifier: identifier, 33 Fields: []cadence.Field{ 34 { 35 Identifier: "a", 36 Type: cadence.IntType{}, 37 }, 38 { 39 Identifier: "b", 40 Type: cadence.StringType{}, 41 }, 42 }, 43 } 44 45 fooString, err := cadence.NewString("foo") 46 if err != nil { 47 panic(fmt.Sprintf("unexpected error while creating cadence string: %s", err)) 48 } 49 50 testEvent := cadence.NewEvent( 51 []cadence.Value{ 52 cadence.NewInt(int(g.count)), 53 fooString, 54 }).WithType(testEventType) 55 56 payload, err := encoding.Encode(testEvent) 57 if err != nil { 58 panic(fmt.Sprintf("unexpected error while encoding events: %s", err)) 59 } 60 event := flow.Event{ 61 Type: flow.EventType(typeID), 62 TransactionID: g.ids.New(), 63 TransactionIndex: g.count, 64 EventIndex: g.count, 65 Payload: payload, 66 } 67 68 g.count++ 69 70 return event 71 }