From cedde44bf65dde9d05f3c548f7387d3d34fb3195 Mon Sep 17 00:00:00 2001 From: Lucca <109136188+LuccaBitfly@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:58:38 +0200 Subject: [PATCH] (BEDS-539) fix put network notifications (#902) --- backend/pkg/api/data_access/dummy.go | 15 ++++++++++++- backend/pkg/api/handlers/common.go | 30 ++++++++++++++++++++++++++ backend/pkg/api/handlers/public.go | 32 +++++++++++++++++++++++----- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/backend/pkg/api/data_access/dummy.go b/backend/pkg/api/data_access/dummy.go index b3743d335..b82c03e47 100644 --- a/backend/pkg/api/data_access/dummy.go +++ b/backend/pkg/api/data_access/dummy.go @@ -396,7 +396,20 @@ func (d *DummyService) GetValidatorDashboardRocketPoolMinipools(ctx context.Cont } func (d *DummyService) GetAllNetworks() ([]t.NetworkInfo, error) { - return getDummyData[[]t.NetworkInfo]() + return []types.NetworkInfo{ + { + ChainId: 1, + Name: "ethereum", + }, + { + ChainId: 100, + Name: "gnosis", + }, + { + ChainId: 17000, + Name: "holesky", + }, + }, nil } func (d *DummyService) GetSearchValidatorByIndex(ctx context.Context, chainId, index uint64) (*t.SearchValidator, error) { diff --git a/backend/pkg/api/handlers/common.go b/backend/pkg/api/handlers/common.go index 1af4a9234..6b0d9e3c5 100644 --- a/backend/pkg/api/handlers/common.go +++ b/backend/pkg/api/handlers/common.go @@ -19,6 +19,7 @@ import ( "github.com/gobitfly/beaconchain/pkg/commons/log" "github.com/gorilla/mux" "github.com/invopop/jsonschema" + "github.com/shopspring/decimal" "github.com/xeipuuv/gojsonschema" "github.com/alexedwards/scs/v2" @@ -252,6 +253,35 @@ func (v *validationError) checkUint(param, paramName string) uint64 { return num } +func (v *validationError) checkWeiDecimal(param, paramName string) decimal.Decimal { + dec := decimal.Zero + // check if only numbers are contained in the string with regex + if !reInteger.MatchString(param) { + v.add(paramName, fmt.Sprintf("given value '%s' is not a wei string (must be positive integer)", param)) + return dec + } + dec, err := decimal.NewFromString(param) + if err != nil { + v.add(paramName, fmt.Sprintf("given value '%s' is not a wei string (must be positive integer)", param)) + return dec + } + return dec +} + +func (v *validationError) checkWeiMinMax(param, paramName string, min, max decimal.Decimal) decimal.Decimal { + dec := v.checkWeiDecimal(param, paramName) + if v.hasErrors() { + return dec + } + if dec.LessThan(min) { + v.add(paramName, fmt.Sprintf("given value '%s' is too small, minimum value is %s", dec, min)) + } + if dec.GreaterThan(max) { + v.add(paramName, fmt.Sprintf("given value '%s' is too large, maximum value is %s", dec, max)) + } + return dec +} + func (v *validationError) checkBool(param, paramName string) bool { if param == "" { return false diff --git a/backend/pkg/api/handlers/public.go b/backend/pkg/api/handlers/public.go index 53e20447f..30fb5a8ca 100644 --- a/backend/pkg/api/handlers/public.go +++ b/backend/pkg/api/handlers/public.go @@ -11,6 +11,7 @@ import ( "github.com/gobitfly/beaconchain/pkg/api/enums" "github.com/gobitfly/beaconchain/pkg/api/types" "github.com/gorilla/mux" + "github.com/shopspring/decimal" ) // 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 // @Accept json // @Produce json // @Param network path string true "The networks name or chain ID." -// @Param request body types.NotificationSettingsNetwork true "Description Todo" +// @Param request body handlers.PublicPutUserNotificationSettingsNetworks.request true "Description Todo" // @Success 200 {object} types.InternalPutUserNotificationSettingsNetworksResponse // @Failure 400 {object} types.ApiErrorResponse // @Router /users/me/notifications/settings/networks/{network} [put] @@ -2254,19 +2255,40 @@ func (h *HandlerService) PublicPutUserNotificationSettingsNetworks(w http.Respon handleErr(w, r, err) return } - var req types.NotificationSettingsNetwork + type request struct { + IsGasAboveSubscribed bool `json:"is_gas_above_subscribed"` + GasAboveThreshold string `json:"gas_above_threshold"` + IsGasBelowSubscribed bool `json:"is_gas_below_subscribed"` + GasBelowThreshold string `json:"gas_below_threshold" ` + IsParticipationRateSubscribed bool `json:"is_participation_rate_subscribed"` + ParticipationRateThreshold float64 `json:"participation_rate_threshold" faker:"boundary_start=0, boundary_end=1"` + } + var req request if err := v.checkBody(&req, r); err != nil { handleErr(w, r, err) return } checkMinMax(&v, req.ParticipationRateThreshold, 0, 1, "participation_rate_threshold") - chainId := v.checkNetworkParameter(mux.Vars(r)["network"]) + + minWei := decimal.New(1000000, 1) // 0.001 Gwei + maxWei := decimal.New(1000000000000, 1) // 1000 Gwei + gasAboveThreshold := v.checkWeiMinMax(req.GasAboveThreshold, "gas_above_threshold", minWei, maxWei) + gasBelowThreshold := v.checkWeiMinMax(req.GasBelowThreshold, "gas_below_threshold", minWei, maxWei) if v.hasErrors() { handleErr(w, r, v) return } - err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, req) + settings := types.NotificationSettingsNetwork{ + IsGasAboveSubscribed: req.IsGasAboveSubscribed, + GasAboveThreshold: gasAboveThreshold, + IsGasBelowSubscribed: req.IsGasBelowSubscribed, + GasBelowThreshold: gasBelowThreshold, + IsParticipationRateSubscribed: req.IsParticipationRateSubscribed, + ParticipationRateThreshold: req.ParticipationRateThreshold, + } + + err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, settings) if err != nil { handleErr(w, r, err) return @@ -2274,7 +2296,7 @@ func (h *HandlerService) PublicPutUserNotificationSettingsNetworks(w http.Respon response := types.InternalPutUserNotificationSettingsNetworksResponse{ Data: types.NotificationNetwork{ ChainId: chainId, - Settings: req, + Settings: settings, }, } returnOk(w, r, response)