Skip to content

Commit

Permalink
(BEDS-539) fix put network notifications (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuccaBitfly authored Oct 1, 2024
1 parent 5871feb commit cedde44
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
15 changes: 14 additions & 1 deletion backend/pkg/api/data_access/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions backend/pkg/api/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions backend/pkg/api/handlers/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -2254,27 +2255,48 @@ 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
}
response := types.InternalPutUserNotificationSettingsNetworksResponse{
Data: types.NotificationNetwork{
ChainId: chainId,
Settings: req,
Settings: settings,
},
}
returnOk(w, r, response)
Expand Down

0 comments on commit cedde44

Please sign in to comment.