-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
55 lines (45 loc) · 1.19 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
package main
import (
"context"
"io"
"os"
"github.com/ethereum/go-ethereum/log"
"github.com/flashbots/go-utils/blocksub"
"golang.org/x/exp/slog"
)
var (
httpURI = os.Getenv("ETH_HTTP") // usually port 8545
wsURI = os.Getenv("ETH_WS") // usually port 8546
logJSON = os.Getenv("LOG_JSON") == "1"
logDebug = os.Getenv("DEBUG") == "1"
)
func logSetup() {
logLevel := log.LevelInfo
if logDebug {
logLevel = log.LevelDebug
}
output := io.Writer(os.Stderr)
var handler slog.Handler = log.NewTerminalHandlerWithLevel(output, logLevel, true)
if logJSON {
handler = log.JSONHandler(output)
}
log.SetDefault(log.NewLogger(handler))
}
func main() {
logSetup()
DemoSimpleSub(httpURI, wsURI)
// DemoMultiSub(httpURI, wsURI)
}
func DemoSimpleSub(httpURI, wsURI string) {
// Create and start a BlockSub instance
blocksub := blocksub.NewBlockSub(context.Background(), httpURI, wsURI)
blocksub.DebugOutput = true
if err := blocksub.Start(); err != nil {
log.Crit(err.Error())
}
// Create a subscription to new headers
sub := blocksub.Subscribe(context.Background())
for header := range sub.C {
log.Info("new header", "number", header.Number.Uint64(), "hash", header.Hash().Hex())
}
}