-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (45 loc) · 1.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
package main
import (
"context"
"github.com/hyknerf/ethereum-parser/observer"
"github.com/hyknerf/ethereum-parser/parser"
router2 "github.com/hyknerf/ethereum-parser/router"
"github.com/hyknerf/ethereum-parser/store"
"net/http"
"sync"
"time"
)
const (
blockObserverInterval = 2 * time.Second
)
func main() {
ctx := context.Background()
newBlockStream := make(chan int64)
txHashStream := make(chan string)
httpClient := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 20,
},
Timeout: 30 * time.Second,
}
memStore := store.NewMemStore()
// register Uniswap address so it is not empty
_ = memStore.AddObservedAddress("0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD")
defaultParser := parser.NewDefaultParser(memStore)
blockNumObs := observer.NewBlockNumberObserver(ctx, newBlockStream, memStore, httpClient, blockObserverInterval)
blockObs := observer.NewNewBlockObserver(ctx, newBlockStream, txHashStream, memStore, httpClient)
txObs := observer.NewTransactionObserver(ctx, txHashStream, memStore, httpClient)
wg := &sync.WaitGroup{}
blockNumObs.Work(wg)
blockObs.Work(wg)
txObs.Work(wg)
router := router2.NewRouter(defaultParser)
http.HandleFunc("GET /block", router.BlockHandler)
http.HandleFunc("POST /subscribe-address", router.SubscribeAddressHandler)
http.HandleFunc("GET /transactions", router.TransactionHandler)
err := http.ListenAndServe(":8090", nil)
if err != nil {
panic(err)
}
wg.Wait()
}