-
Notifications
You must be signed in to change notification settings - Fork 5
/
events.go
48 lines (41 loc) · 1.11 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
// Copyright 2020 Stafi Protocol
// SPDX-License-Identifier: LGPL-3.0-only
package ethtest
import (
"context"
"fmt"
"math/big"
"github.com/ChainSafe/log15"
eth "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
utils "github.com/stafiprotocol/chainbridge/shared/ethereum"
)
func WatchEvent(client *utils.Client, bridge common.Address, subStr utils.EventSig) {
fmt.Printf("Watching for event: %s\n", subStr)
query := eth.FilterQuery{
FromBlock: big.NewInt(0),
Addresses: []common.Address{bridge},
Topics: [][]common.Hash{
{subStr.GetTopic()},
},
}
ch := make(chan ethtypes.Log)
sub, err := client.Client.SubscribeFilterLogs(context.Background(), query, ch)
if err != nil {
log15.Error("Failed to subscribe to event", "event", subStr)
return
}
defer sub.Unsubscribe()
for {
select {
case evt := <-ch:
fmt.Printf("%s (block: %d): %#v\n", subStr, evt.BlockNumber, evt.Topics)
case err := <-sub.Err():
if err != nil {
log15.Error("Subscription error", "event", subStr, "err", err)
return
}
}
}
}