forked from onflow/flow-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_test.go
129 lines (96 loc) · 2.83 KB
/
events_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package emulator_test
import (
"fmt"
"testing"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go-sdk/templates"
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
emulator "github.com/onflow/flow-emulator"
)
func TestEventEmitted(t *testing.T) {
t.Parallel()
t.Run("EmittedFromScript", func(t *testing.T) {
t.Parallel()
// Emitting events in scripts is not supported
b, err := emulator.NewBlockchain()
require.NoError(t, err)
script := []byte(`
pub event MyEvent(x: Int, y: Int)
pub fun main() {
emit MyEvent(x: 1, y: 2)
}
`)
result, err := b.ExecuteScript(script, nil)
assert.NoError(t, err)
require.Error(t, result.Error)
require.Empty(t, result.Events)
})
t.Run("EmittedFromAccount", func(t *testing.T) {
t.Parallel()
b, err := emulator.NewBlockchain(
emulator.WithStorageLimitEnabled(false),
)
require.NoError(t, err)
accountContracts := []templates.Contract{
{
Name: "Test",
Source: `
pub contract Test {
pub event MyEvent(x: Int, y: Int)
pub fun emitMyEvent(x: Int, y: Int) {
emit MyEvent(x: x, y: y)
}
}
`,
},
}
publicKey := b.ServiceKey().AccountKey()
address, err := b.CreateAccount(
[]*flow.AccountKey{publicKey},
accountContracts,
)
assert.NoError(t, err)
script := []byte(fmt.Sprintf(`
import 0x%s
transaction {
execute {
Test.emitMyEvent(x: 1, y: 2)
}
}
`, address.Hex()))
tx := flow.NewTransaction().
SetScript(script).
SetGasLimit(flowgo.DefaultMaxTransactionGasLimit).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address)
err = tx.SignEnvelope(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().Signer())
assert.NoError(t, err)
err = b.AddTransaction(*tx)
assert.NoError(t, err)
result, err := b.ExecuteNextTransaction()
assert.NoError(t, err)
assert.True(t, result.Succeeded())
block, err := b.CommitBlock()
require.NoError(t, err)
addr, _ := common.BytesToAddress(address.Bytes())
location := common.AddressLocation{
Address: addr,
Name: "Test",
}
expectedType := location.TypeID("Test.MyEvent")
events, err := b.GetEventsByHeight(block.Header.Height, string(expectedType))
require.NoError(t, err)
require.Len(t, events, 1)
actualEvent := events[0]
decodedEvent := actualEvent.Value
expectedID := flow.Event{TransactionID: tx.ID(), EventIndex: 0}.ID()
assert.Equal(t, string(expectedType), actualEvent.Type)
assert.Equal(t, expectedID, actualEvent.ID())
assert.Equal(t, cadence.NewInt(1), decodedEvent.Fields[0])
assert.Equal(t, cadence.NewInt(2), decodedEvent.Fields[1])
})
}