-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_eth_balance.go
122 lines (87 loc) · 2.71 KB
/
handler_eth_balance.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
stdjson "encoding/json"
"fmt"
"log/slog"
"net/http"
"alluvial/ethereum"
"alluvial/json"
"alluvial/metric"
"github.com/ethereum/go-ethereum/common"
"github.com/uptrace/bunrouter"
)
func ethBalance(app *App) handlerEthBalance {
return handlerEthBalance{log: app.log, json: app.json, app: app}
}
type ethBalanceAppInterface interface {
EthAddressIsContract(ctx context.Context, address common.Address) (bool, error)
EthBalance(ctx context.Context, req *requestEthBalance, resp *responseEthBalance) error
}
type handlerEthBalance struct {
log *slog.Logger
json json.Writer
app ethBalanceAppInterface
}
type requestEthBalance struct {
Address common.Address
}
type responseEthBalance struct {
Balance string `json:"balance"`
}
func (h handlerEthBalance) serveJSON(w http.ResponseWriter, r *http.Request) {
metric.EthBalanceCounter.Inc()
var (
ctx = r.Context()
req = &requestEthBalance{}
resp = &responseEthBalance{}
)
if err := h.requestRead(ctx, w, req, r); err != nil {
return
}
isContract, err := h.app.EthAddressIsContract(ctx, req.Address)
if err != nil {
h.log.Error(err.Error(), "req", fmt.Sprintf("%s %s 500 Internal Server Error", r.Method, r.URL.RequestURI()))
h.json.WriteInternalError(w)
return
}
if isContract {
const msg = "ethereum contract address is not supported"
h.log.Warn(msg, "req", fmt.Sprintf("%s %s 400 Bad Request", r.Method, r.URL.RequestURI()))
h.json.WriteBadRequest(w, msg)
return
}
if err = h.app.EthBalance(ctx, req, resp); err != nil {
h.log.Error(err.Error(), "req", fmt.Sprintf("%s %s 500 Internal Server Error", r.Method, r.URL.RequestURI()))
h.json.WriteInternalError(w)
return
}
body, err := stdjson.Marshal(resp)
if err != nil {
h.log.Error(err.Error(), "req", fmt.Sprintf("%s %s 500 Internal Server Error", r.Method, r.URL.RequestURI()))
h.json.WriteInternalError(w)
return
}
if err = h.json.Write(w, r, http.StatusOK, nil, body); err != nil {
return
}
h.log.Info(fmt.Sprintf("%s %s 200 OK", r.Method, r.URL.RequestURI()))
}
func (h handlerEthBalance) requestRead(ctx context.Context, w http.ResponseWriter, req *requestEthBalance, r *http.Request) error {
params := bunrouter.ParamsFromContext(ctx)
addr, found := params.Get("address")
if !found {
const msg = "missing ethereum address"
h.log.Warn(msg, "req", fmt.Sprintf("%s %s 400 Bad Request", r.Method, r.URL.RequestURI()))
h.json.WriteBadRequest(w, msg)
return ErrAbort
}
msg, ethAddr, valid := ethereum.IsValidAddress(addr)
if !valid {
h.log.Warn(msg, "req", fmt.Sprintf("%s %s 400 Bad Request", r.Method, r.URL.RequestURI()))
h.json.WriteBadRequest(w, msg)
return ErrAbort
}
req.Address = ethAddr
return nil
}