-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (84 loc) · 2.35 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"path"
"runtime"
"syscall"
"github.com/gorilla/mux"
"github.com/praveenkumarKajla/mocketh/indexer"
"github.com/praveenkumarKajla/mocketh/models"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
// Add Logrus formatting
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
},
})
log.SetReportCaller(true)
log.SetOutput(os.Stdout)
ll, err := logrus.ParseLevel("debug")
if err != nil {
ll = logrus.DebugLevel
}
logrus.SetLevel(ll)
}
func main() {
sigs := make(chan os.Signal, 1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
defer signal.Stop(sigs)
log.Debug("Shutting down", "signal", <-sigs)
cancel()
}()
indexService, err := indexer.New()
if err != nil {
return
}
indexerInstance = indexService
// load bootstrap tokens for indexing
erc20Addresses := indexer.LoadTokensFromConfig()
// Initialize ERC20 tokens for given addresses
if err := indexService.Erc20TokensSubscriber(ctx, erc20Addresses); err != nil {
logrus.Error("Fail to subscribe ERC20Tokens and write to database", "err", err)
return
}
// start the periodic task
indexService.Listen()
// get transfer events through Api
router := mux.NewRouter()
fmt.Println("HI")
router.HandleFunc("/add", addAddress).Methods("POST")
http.ListenAndServe(":8080", router)
}
func addAddress(response http.ResponseWriter, request *http.Request) {
response.Header().Add("content-type", "application/json")
var erc20 models.AccountPayload
logrus.Info(json.NewDecoder(request.Body))
err := json.NewDecoder(request.Body).Decode(&erc20)
if err != nil {
response.WriteHeader(http.StatusBadRequest)
response.Write([]byte(`{"message":"failure"}`))
return
}
logrus.Info(erc20)
err = indexerInstance.GetOrAddErc20(context.Background(), erc20.Address, erc20.Name)
if err != nil {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(err.Error()))
return
}
response.WriteHeader(http.StatusOK)
response.Write([]byte(`{"message":"success"}`))
}
var indexerInstance *indexer.Indexer