-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
209 lines (171 loc) · 4.66 KB
/
handler.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package faucet
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"github.com/gnolang/faucet/writer"
httpWriter "github.com/gnolang/faucet/writer/http"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
)
const (
unableToHandleRequest = "unable to handle faucet request"
faucetSuccess = "successfully executed faucet transfer"
)
var (
errInvalidBeneficiary = errors.New("invalid beneficiary address")
errInvalidSendAmount = errors.New("invalid send amount")
)
var amountRegex = regexp.MustCompile(`^\d+ugnot$`)
// defaultHTTPHandler is the default faucet transfer handler
func (f *Faucet) defaultHTTPHandler(w http.ResponseWriter, r *http.Request) {
// Load the requests
requestBody, readErr := io.ReadAll(r.Body)
if readErr != nil {
http.Error(w, "unable to read request", http.StatusBadRequest)
return
}
// Extract the requests
requests, err := extractRequests(requestBody)
if err != nil {
http.Error(
w,
"invalid request body",
http.StatusBadRequest,
)
return
}
// Handle the requests
w.Header().Set("Content-Type", jsonMimeType)
f.handleRequest(
httpWriter.New(f.logger, w),
requests,
)
}
// handleRequest is the common default faucet handler
func (f *Faucet) handleRequest(writer writer.ResponseWriter, requests Requests) {
// Parse all JSON-RPC requests
responses := make(Responses, len(requests))
for i, baseRequest := range requests {
// Log the request
f.logger.Debug(
"incoming request",
"request",
baseRequest,
)
// Extract the beneficiary
beneficiary, err := extractBeneficiary(baseRequest)
if err != nil {
// Save the error response
responses[i] = Response{
Result: unableToHandleRequest,
Error: err.Error(),
}
continue
}
// Extract the send amount
amount, err := extractSendAmount(baseRequest)
if err != nil {
// Save the error response
responses[i] = Response{
Result: unableToHandleRequest,
Error: err.Error(),
}
continue
}
// Check if the amount is set
if amount.IsZero() {
// Drip amount is not set, use
// the max faucet drip amount
amount = f.maxSendAmount
}
// Check if the amount exceeds the max
// drip amount for the faucet
if amount.IsAllGT(f.maxSendAmount) {
// Save the error response
responses[i] = Response{
Result: unableToHandleRequest,
Error: errInvalidSendAmount.Error(),
}
continue
}
// Run the method handler
if err := f.transferFunds(beneficiary, amount); err != nil {
f.logger.Debug(
unableToHandleRequest,
"request",
baseRequest,
"error",
err,
)
responses[i] = Response{
Result: unableToHandleRequest,
Error: err.Error(),
}
continue
}
responses[i] = Response{
Result: faucetSuccess,
}
}
if len(responses) == 1 {
// Write the JSON response as a single response
writer.WriteResponse(responses[0])
return
}
// Write the JSON response as a batch
writer.WriteResponse(responses)
}
// extractRequests extracts the base JSON requests from the request body
func extractRequests(requestBody []byte) (Requests, error) {
// Extract the request
var requests Requests
// Check if the request is a batch request
if err := json.Unmarshal(requestBody, &requests); err != nil {
// Try to get a single JSON request, since this is not a batch
var baseRequest Request
if err := json.Unmarshal(requestBody, &baseRequest); err != nil {
return nil, err
}
requests = Requests{
baseRequest,
}
}
return requests, nil
}
// extractBeneficiary extracts the beneficiary from the base faucet request
func extractBeneficiary(request Request) (crypto.Address, error) {
// Validate the beneficiary address is set
if request.To == "" {
return crypto.Address{}, errInvalidBeneficiary
}
// Validate the beneficiary address is valid
beneficiary, err := crypto.AddressFromBech32(request.To)
if err != nil {
return crypto.Address{}, fmt.Errorf("%w, %w", errInvalidBeneficiary, err)
}
return beneficiary, nil
}
// extractSendAmount extracts the drip amount from the base faucet request, if any
func extractSendAmount(request Request) (std.Coins, error) {
// Check if the amount is set
if request.Amount == "" {
return std.Coins{}, nil
}
// Validate the send amount is valid
if !amountRegex.MatchString(request.Amount) {
return std.Coins{}, errInvalidSendAmount
}
amount, err := std.ParseCoins(request.Amount)
if err != nil {
return std.Coins{}, fmt.Errorf("%w, %w", errInvalidSendAmount, err)
}
return amount, nil
}
// healthcheckHandler is the default health check handler for the faucet
func (f *Faucet) healthcheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}