-
Notifications
You must be signed in to change notification settings - Fork 0
/
issuehunter_observer.go
103 lines (93 loc) · 3.02 KB
/
issuehunter_observer.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
package patchverifier
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/issuehunter/ethrpc"
)
func createFilter(ethRpcClient *ethrpc.EthRPC, filter ethrpc.FilterParams) string {
filterID, err := ethRpcClient.EthNewFilter(filter)
if err != nil {
log.Fatal(err)
}
return filterID
}
func CreateFilter(ethRpcClient *ethrpc.EthRPC, contractAddress []string) string {
log.Printf("creating a new event filter for the IssueHunter contract deployed at address %v", contractAddress)
var filter = ethrpc.FilterParams{Address: contractAddress}
filterID := createFilter(ethRpcClient, filter)
log.Printf("new event filter for the IssueHunter contract deployed at address %v: %v", contractAddress, filterID)
return filterID
}
func GetEvents(ethRpcClient *ethrpc.EthRPC, filterID string) []ethrpc.Log {
log.Printf("getting changes for filter %v", filterID)
logs, err := ethRpcClient.EthGetFilterChanges(filterID)
if err != nil {
log.Fatal(err)
}
jsonLogs, _ := json.MarshalIndent(logs, "", " ")
log.Printf("got changes for filter %v: %v", filterID, string(jsonLogs))
return logs
}
func UninstallFilter(ethRpcClient *ethrpc.EthRPC, filterID string) bool {
log.Printf("uninstalling filter %v", filterID)
uninstall, err := ethRpcClient.EthUninstallFilter(filterID)
if err != nil {
log.Fatal(err)
}
if !uninstall {
log.Fatalf("cannot uninstall filter %v", filterID)
}
log.Printf("filter %v uninstalled", filterID)
return uninstall
}
func CreateResolutionProposedFilter(ethRpcClient *ethrpc.EthRPC, contractAddress []string) string {
log.Printf("creating a new filter on ResolutionProposed event for the IssueHunter contract deployed at address %v", contractAddress)
// > web3.sha3('ResolutionProposed(bytes32,address,bytes32)')
// "0x3b2c9742ac31922699b361e5e2c3d23b7762a77db8d2ae4b5d8004c904e9a4b7"
var filter = ethrpc.FilterParams{
Address: contractAddress,
Topics: [][]string{
[]string{"0x3b2c9742ac31922699b361e5e2c3d23b7762a77db8d2ae4b5d8004c904e9a4b7"},
},
}
filterID := createFilter(ethRpcClient, filter)
log.Printf("new event filter for the IssueHunter contract deployed at address %v: %v", contractAddress, filterID)
return filterID
}
func InteractiveLogObserver(url string, contractAddress []string) {
rpcClient := ethrpc.NewEthRPC(url)
filterID := CreateFilter(rpcClient, contractAddress)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("quitting...")
UninstallFilter(rpcClient, filterID)
os.Exit(0)
}()
for {
GetEvents(rpcClient, filterID)
time.Sleep(2 * time.Second)
}
}
func InteractiveResolutionProposedLogObserver(url string, contractAddress []string) {
rpcClient := ethrpc.NewEthRPC(url)
filterID := CreateResolutionProposedFilter(rpcClient, contractAddress)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("quitting...")
UninstallFilter(rpcClient, filterID)
os.Exit(0)
}()
for {
GetEvents(rpcClient, filterID)
time.Sleep(2 * time.Second)
}
}