Repository navigation
Expand file tree
/
Copy pathobserver.go
More file actions
132 lines (122 loc) · 3.89 KB
/
Copy pathobserver.go
File metadata and controls
132 lines (122 loc) · 3.89 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package api
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/trustwallet/blockatlas/pkg/blockatlas"
"github.com/trustwallet/blockatlas/pkg/ginutils"
"github.com/trustwallet/blockatlas/platform"
"github.com/trustwallet/blockatlas/storage"
"strconv"
)
func SetupObserverAPI(router gin.IRouter, db *storage.Storage) {
router.Use(ginutils.TokenAuthMiddleware(viper.GetString("observer.auth")))
router.POST("/webhook/register", addCall(db))
router.DELETE("/webhook/register", deleteCall(db))
router.GET("/status", statusCall(db))
}
// @Summary Create a webhook
// @ID create_webhook
// @Description Create a webhook for addresses transactions
// @Accept json
// @Produce json
// @Tags observer,subscriptions
// @Param subscriptions body blockatlas.Webhook true "Accounts subscriptions"
// @Param Authorization header string true "Bearer authorization header" default(Bearer test)
// @Header 200 {string} Authorization {token}
// @Success 200 {object} blockatlas.Observer
// @Router /observer/v1/webhook/register [post]
func addCall(storage storage.Addresses) func(c *gin.Context) {
if storage == nil {
return nil
}
return func(c *gin.Context) {
var req blockatlas.Webhook
if c.BindJSON(&req) != nil {
return
}
if len(req.Subscriptions) == 0 {
ginutils.RenderSuccess(c, blockatlas.Observer{Message: "Added", Status: true})
return
}
subs := parseSubscriptions(req.Subscriptions, req.Webhook)
go storage.AddSubscriptions(subs)
ginutils.RenderSuccess(c, blockatlas.Observer{Message: "Added", Status: true})
}
}
// @Summary Delete a webhook
// @ID delete_webhook
// @Description Delete a webhook for addresses transactions
// @Accept json
// @Produce json
// @Tags observer,subscriptions
// @Param subscriptions body blockatlas.Webhook true "Accounts subscriptions"
// @Param Authorization header string true "Bearer authorization header" default(Bearer test)
// @Header 200 {string} Authorization {token}
// @Success 200 {object} blockatlas.Observer
// @Router /observer/v1/webhook/register [delete]
func deleteCall(storage storage.Addresses) func(c *gin.Context) {
if storage == nil {
return nil
}
return func(c *gin.Context) {
var req blockatlas.Webhook
if c.BindJSON(&req) != nil {
return
}
if len(req.Subscriptions) == 0 {
ginutils.RenderSuccess(c, blockatlas.Observer{Message: "Deleted", Status: true})
return
}
subs := parseSubscriptions(req.Subscriptions, req.Webhook)
go storage.DeleteSubscriptions(subs)
ginutils.RenderSuccess(c, blockatlas.Observer{Message: "Deleted", Status: true})
}
}
// @Summary Get coin status
// @ID coin_status
// @Description Get coin status
// @Accept json
// @Produce json
// @Tags observer,subscriptions
// @Param Authorization header string true "Bearer authorization header" default(Bearer test)
// @Header 200 {string} Authorization {token}
// @Success 200 {object} blockatlas.CoinStatus
// @Router /observer/v1/status [get]
func statusCall(storage storage.Tracker) func(c *gin.Context) {
if storage == nil {
return nil
}
return func(c *gin.Context) {
result := make(map[string]blockatlas.CoinStatus)
for _, api := range platform.BlockAPIs {
coin := api.Coin()
num, err := storage.GetBlockNumber(coin.ID)
var status blockatlas.CoinStatus
if err != nil {
status = blockatlas.CoinStatus{Error: err.Error()}
} else if num == 0 {
status = blockatlas.CoinStatus{Error: "no blocks"}
} else {
status = blockatlas.CoinStatus{Height: num}
}
result[coin.Handle] = status
}
ginutils.RenderSuccess(c, result)
}
}
func parseSubscriptions(subscriptions map[string][]string, webhook string) (subs []blockatlas.Subscription) {
for coinStr, perCoin := range subscriptions {
coin, err := strconv.Atoi(coinStr)
if err != nil {
continue
}
for _, addr := range perCoin {
subs = append(subs, blockatlas.Subscription{
Coin: uint(coin),
Address: addr,
Webhook: webhook,
})
}
}
return
}