-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmain.go
132 lines (106 loc) · 3.43 KB
/
main.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
130
131
132
/*
* Flow Go SDK
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"github.com/onflow/flow-go-sdk/access/http"
"github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/examples"
)
func main() {
deployedContract, runScriptTx := preapreDemo()
demo(deployedContract, runScriptTx)
}
func demo(deployedContract *flow.Account, runScriptTx *flow.Transaction) {
ctx := context.Background()
flowClient, err := http.NewClient(http.EmulatorHost)
examples.Handle(err)
// Query for account creation events by type
result, err := flowClient.GetEventsForHeightRange(ctx, "flow.AccountCreated", 0, 30)
printEvents(result, err)
customType := flow.NewEventTypeFactory().
WithEventName("Add").
WithContractName("EventDemo").
WithAddress(deployedContract.Address).
String()
result, err = flowClient.GetEventsForHeightRange(ctx, customType, 0, 10)
printEvents(result, err)
// Get events directly from transaction result
txResult, err := flowClient.GetTransactionResult(ctx, runScriptTx.ID())
examples.Handle(err)
printEvent(txResult.Events)
}
func printEvents(result []flow.BlockEvents, err error) {
examples.Handle(err)
for _, block := range result {
printEvent(block.Events)
}
}
func printEvent(events []flow.Event) {
for _, event := range events {
fmt.Printf("\n\nType: %s", event.Type)
fmt.Printf("\nValues: %v", event.Value)
fmt.Printf("\nTransaction ID: %s", event.TransactionID)
}
}
func preapreDemo() (*flow.Account, *flow.Transaction) {
ctx := context.Background()
flowClient, err := http.NewClient(http.EmulatorHost)
examples.Handle(err)
acctAddr, acctKey, acctSigner := examples.RandomAccount(flowClient)
// Deploy a contract with an event defined
contract := `
access(all) contract EventDemo {
access(all) event Add(x: Int, y: Int, sum: Int)
access(all) fun add(x: Int, y: Int) {
let sum = x + y
emit Add(x: x, y: y, sum: sum)
}
}
`
contractAccount := examples.CreateAccountWithContracts(flowClient,
nil, []templates.Contract{{
Name: "EventDemo",
Source: contract,
}})
// Send a tx that emits the event in the deployed contract
script := fmt.Sprintf(`
import EventDemo from 0x%s
transaction {
prepare(signer: auth(Storage) &Account) {}
execute {
EventDemo.add(x: 2, y: 3)
}
}
`, contractAccount.Address.Hex())
referenceBlockID := examples.GetReferenceBlockId(flowClient)
runScriptTx := flow.NewTransaction().
SetScript([]byte(script)).
SetPayer(acctAddr).
AddAuthorizer(acctAddr).
SetReferenceBlockID(referenceBlockID).
SetProposalKey(acctAddr, acctKey.Index, acctKey.SequenceNumber)
err = runScriptTx.SignEnvelope(acctAddr, acctKey.Index, acctSigner)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *runScriptTx)
examples.Handle(err)
examples.WaitForSeal(ctx, flowClient, runScriptTx.ID())
return contractAccount, runScriptTx
}