Skip to content

Commit cedde44

Browse files
authored
(BEDS-539) fix put network notifications (#902)
1 parent 5871feb commit cedde44

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

backend/pkg/api/data_access/dummy.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,20 @@ func (d *DummyService) GetValidatorDashboardRocketPoolMinipools(ctx context.Cont
396396
}
397397

398398
func (d *DummyService) GetAllNetworks() ([]t.NetworkInfo, error) {
399-
return getDummyData[[]t.NetworkInfo]()
399+
return []types.NetworkInfo{
400+
{
401+
ChainId: 1,
402+
Name: "ethereum",
403+
},
404+
{
405+
ChainId: 100,
406+
Name: "gnosis",
407+
},
408+
{
409+
ChainId: 17000,
410+
Name: "holesky",
411+
},
412+
}, nil
400413
}
401414

402415
func (d *DummyService) GetSearchValidatorByIndex(ctx context.Context, chainId, index uint64) (*t.SearchValidator, error) {

backend/pkg/api/handlers/common.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/gobitfly/beaconchain/pkg/commons/log"
2020
"github.com/gorilla/mux"
2121
"github.com/invopop/jsonschema"
22+
"github.com/shopspring/decimal"
2223
"github.com/xeipuuv/gojsonschema"
2324

2425
"github.com/alexedwards/scs/v2"
@@ -252,6 +253,35 @@ func (v *validationError) checkUint(param, paramName string) uint64 {
252253
return num
253254
}
254255

256+
func (v *validationError) checkWeiDecimal(param, paramName string) decimal.Decimal {
257+
dec := decimal.Zero
258+
// check if only numbers are contained in the string with regex
259+
if !reInteger.MatchString(param) {
260+
v.add(paramName, fmt.Sprintf("given value '%s' is not a wei string (must be positive integer)", param))
261+
return dec
262+
}
263+
dec, err := decimal.NewFromString(param)
264+
if err != nil {
265+
v.add(paramName, fmt.Sprintf("given value '%s' is not a wei string (must be positive integer)", param))
266+
return dec
267+
}
268+
return dec
269+
}
270+
271+
func (v *validationError) checkWeiMinMax(param, paramName string, min, max decimal.Decimal) decimal.Decimal {
272+
dec := v.checkWeiDecimal(param, paramName)
273+
if v.hasErrors() {
274+
return dec
275+
}
276+
if dec.LessThan(min) {
277+
v.add(paramName, fmt.Sprintf("given value '%s' is too small, minimum value is %s", dec, min))
278+
}
279+
if dec.GreaterThan(max) {
280+
v.add(paramName, fmt.Sprintf("given value '%s' is too large, maximum value is %s", dec, max))
281+
}
282+
return dec
283+
}
284+
255285
func (v *validationError) checkBool(param, paramName string) bool {
256286
if param == "" {
257287
return false

backend/pkg/api/handlers/public.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gobitfly/beaconchain/pkg/api/enums"
1212
"github.com/gobitfly/beaconchain/pkg/api/types"
1313
"github.com/gorilla/mux"
14+
"github.com/shopspring/decimal"
1415
)
1516

1617
// All handler function names must include the HTTP method and the path they handle
@@ -2243,7 +2244,7 @@ func (h *HandlerService) PublicPutUserNotificationSettingsGeneral(w http.Respons
22432244
// @Accept json
22442245
// @Produce json
22452246
// @Param network path string true "The networks name or chain ID."
2246-
// @Param request body types.NotificationSettingsNetwork true "Description Todo"
2247+
// @Param request body handlers.PublicPutUserNotificationSettingsNetworks.request true "Description Todo"
22472248
// @Success 200 {object} types.InternalPutUserNotificationSettingsNetworksResponse
22482249
// @Failure 400 {object} types.ApiErrorResponse
22492250
// @Router /users/me/notifications/settings/networks/{network} [put]
@@ -2254,27 +2255,48 @@ func (h *HandlerService) PublicPutUserNotificationSettingsNetworks(w http.Respon
22542255
handleErr(w, r, err)
22552256
return
22562257
}
2257-
var req types.NotificationSettingsNetwork
2258+
type request struct {
2259+
IsGasAboveSubscribed bool `json:"is_gas_above_subscribed"`
2260+
GasAboveThreshold string `json:"gas_above_threshold"`
2261+
IsGasBelowSubscribed bool `json:"is_gas_below_subscribed"`
2262+
GasBelowThreshold string `json:"gas_below_threshold" `
2263+
IsParticipationRateSubscribed bool `json:"is_participation_rate_subscribed"`
2264+
ParticipationRateThreshold float64 `json:"participation_rate_threshold" faker:"boundary_start=0, boundary_end=1"`
2265+
}
2266+
var req request
22582267
if err := v.checkBody(&req, r); err != nil {
22592268
handleErr(w, r, err)
22602269
return
22612270
}
22622271
checkMinMax(&v, req.ParticipationRateThreshold, 0, 1, "participation_rate_threshold")
2263-
22642272
chainId := v.checkNetworkParameter(mux.Vars(r)["network"])
2273+
2274+
minWei := decimal.New(1000000, 1) // 0.001 Gwei
2275+
maxWei := decimal.New(1000000000000, 1) // 1000 Gwei
2276+
gasAboveThreshold := v.checkWeiMinMax(req.GasAboveThreshold, "gas_above_threshold", minWei, maxWei)
2277+
gasBelowThreshold := v.checkWeiMinMax(req.GasBelowThreshold, "gas_below_threshold", minWei, maxWei)
22652278
if v.hasErrors() {
22662279
handleErr(w, r, v)
22672280
return
22682281
}
2269-
err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, req)
2282+
settings := types.NotificationSettingsNetwork{
2283+
IsGasAboveSubscribed: req.IsGasAboveSubscribed,
2284+
GasAboveThreshold: gasAboveThreshold,
2285+
IsGasBelowSubscribed: req.IsGasBelowSubscribed,
2286+
GasBelowThreshold: gasBelowThreshold,
2287+
IsParticipationRateSubscribed: req.IsParticipationRateSubscribed,
2288+
ParticipationRateThreshold: req.ParticipationRateThreshold,
2289+
}
2290+
2291+
err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, settings)
22702292
if err != nil {
22712293
handleErr(w, r, err)
22722294
return
22732295
}
22742296
response := types.InternalPutUserNotificationSettingsNetworksResponse{
22752297
Data: types.NotificationNetwork{
22762298
ChainId: chainId,
2277-
Settings: req,
2299+
Settings: settings,
22782300
},
22792301
}
22802302
returnOk(w, r, response)

0 commit comments

Comments
 (0)