Skip to content

Commit c042e5a

Browse files
authored
Merge pull request #906 from gobitfly/staging
merge staging->main
2 parents 7e2fd2f + 09b2451 commit c042e5a

22 files changed

+537
-118
lines changed

backend/pkg/api/data_access/dummy.go

Lines changed: 15 additions & 2 deletions
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) {
@@ -467,7 +480,7 @@ func (d *DummyService) GetClientNotifications(ctx context.Context, userId uint64
467480
func (d *DummyService) GetRocketPoolNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationRocketPoolColumn], search string, limit uint64) ([]t.NotificationRocketPoolTableRow, *t.Paging, error) {
468481
return getDummyWithPaging[t.NotificationRocketPoolTableRow]()
469482
}
470-
func (d *DummyService) GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], search string, limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error) {
483+
func (d *DummyService) GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error) {
471484
return getDummyWithPaging[t.NotificationNetworksTableRow]()
472485
}
473486

backend/pkg/api/data_access/notifications.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type NotificationsRepository interface {
1818
GetMachineNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationMachinesColumn], search string, limit uint64) ([]t.NotificationMachinesTableRow, *t.Paging, error)
1919
GetClientNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationClientsColumn], search string, limit uint64) ([]t.NotificationClientsTableRow, *t.Paging, error)
2020
GetRocketPoolNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationRocketPoolColumn], search string, limit uint64) ([]t.NotificationRocketPoolTableRow, *t.Paging, error)
21-
GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], search string, limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error)
21+
GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error)
2222

2323
GetNotificationSettings(ctx context.Context, userId uint64) (*t.NotificationSettings, error)
2424
UpdateNotificationSettingsGeneral(ctx context.Context, userId uint64, settings t.NotificationSettingsGeneral) error
@@ -55,8 +55,8 @@ func (d *DataAccessService) GetClientNotifications(ctx context.Context, userId u
5555
func (d *DataAccessService) GetRocketPoolNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationRocketPoolColumn], search string, limit uint64) ([]t.NotificationRocketPoolTableRow, *t.Paging, error) {
5656
return d.dummy.GetRocketPoolNotifications(ctx, userId, cursor, colSort, search, limit)
5757
}
58-
func (d *DataAccessService) GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], search string, limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error) {
59-
return d.dummy.GetNetworkNotifications(ctx, userId, cursor, colSort, search, limit)
58+
func (d *DataAccessService) GetNetworkNotifications(ctx context.Context, userId uint64, cursor string, colSort t.Sort[enums.NotificationNetworksColumn], limit uint64) ([]t.NotificationNetworksTableRow, *t.Paging, error) {
59+
return d.dummy.GetNetworkNotifications(ctx, userId, cursor, colSort, limit)
6060
}
6161
func (d *DataAccessService) GetNotificationSettings(ctx context.Context, userId uint64) (*t.NotificationSettings, error) {
6262
return d.dummy.GetNotificationSettings(ctx, userId)

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: 34 additions & 10 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
@@ -2098,7 +2099,7 @@ func (h *HandlerService) PublicGetUserNotificationClients(w http.ResponseWriter,
20982099
// @Param cursor query string false "Return data for the given cursor value. Pass the `paging.next_cursor`` value of the previous response to navigate to forward, or pass the `paging.prev_cursor`` value of the previous response to navigate to backward."
20992100
// @Param limit query integer false "The maximum number of results that may be returned."
21002101
// @Param sort query string false "The field you want to sort by. Append with `:desc` for descending order." Enums(timestamp, event_type, node_address)
2101-
// @Param search query string false "Search for TODO"
2102+
// @Param search query string false "Search for Node Address"
21022103
// @Success 200 {object} types.InternalGetUserNotificationRocketPoolResponse
21032104
// @Failure 400 {object} types.ApiErrorResponse
21042105
// @Router /users/me/notifications/rocket-pool [get]
@@ -2137,7 +2138,6 @@ func (h *HandlerService) PublicGetUserNotificationRocketPool(w http.ResponseWrit
21372138
// @Param cursor query string false "Return data for the given cursor value. Pass the `paging.next_cursor`` value of the previous response to navigate to forward, or pass the `paging.prev_cursor`` value of the previous response to navigate to backward."
21382139
// @Param limit query integer false "The maximum number of results that may be returned."
21392140
// @Param sort query string false "The field you want to sort by. Append with `:desc` for descending order." Enums(timestamp, event_type)
2140-
// @Param search query string false "Search for TODO"
21412141
// @Success 200 {object} types.InternalGetUserNotificationNetworksResponse
21422142
// @Failure 400 {object} types.ApiErrorResponse
21432143
// @Router /users/me/notifications/networks [get]
@@ -2155,7 +2155,7 @@ func (h *HandlerService) PublicGetUserNotificationNetworks(w http.ResponseWriter
21552155
handleErr(w, r, v)
21562156
return
21572157
}
2158-
data, paging, err := h.dai.GetNetworkNotifications(r.Context(), userId, pagingParams.cursor, *sort, pagingParams.search, pagingParams.limit)
2158+
data, paging, err := h.dai.GetNetworkNotifications(r.Context(), userId, pagingParams.cursor, *sort, pagingParams.limit)
21592159
if err != nil {
21602160
handleErr(w, r, err)
21612161
return
@@ -2218,8 +2218,6 @@ func (h *HandlerService) PublicPutUserNotificationSettingsGeneral(w http.Respons
22182218
checkMinMax(&v, req.MachineStorageUsageThreshold, 0, 1, "machine_storage_usage_threshold")
22192219
checkMinMax(&v, req.MachineCpuUsageThreshold, 0, 1, "machine_cpu_usage_threshold")
22202220
checkMinMax(&v, req.MachineMemoryUsageThreshold, 0, 1, "machine_memory_usage_threshold")
2221-
checkMinMax(&v, req.RocketPoolMaxCollateralThreshold, 0, 1, "rocket_pool_max_collateral_threshold")
2222-
checkMinMax(&v, req.RocketPoolMinCollateralThreshold, 0, 1, "rocket_pool_min_collateral_threshold")
22232221
if v.hasErrors() {
22242222
handleErr(w, r, v)
22252223
return
@@ -2243,7 +2241,7 @@ func (h *HandlerService) PublicPutUserNotificationSettingsGeneral(w http.Respons
22432241
// @Accept json
22442242
// @Produce json
22452243
// @Param network path string true "The networks name or chain ID."
2246-
// @Param request body types.NotificationSettingsNetwork true "Description Todo"
2244+
// @Param request body handlers.PublicPutUserNotificationSettingsNetworks.request true "Description Todo"
22472245
// @Success 200 {object} types.InternalPutUserNotificationSettingsNetworksResponse
22482246
// @Failure 400 {object} types.ApiErrorResponse
22492247
// @Router /users/me/notifications/settings/networks/{network} [put]
@@ -2254,27 +2252,50 @@ func (h *HandlerService) PublicPutUserNotificationSettingsNetworks(w http.Respon
22542252
handleErr(w, r, err)
22552253
return
22562254
}
2257-
var req types.NotificationSettingsNetwork
2255+
type request struct {
2256+
IsGasAboveSubscribed bool `json:"is_gas_above_subscribed"`
2257+
GasAboveThreshold string `json:"gas_above_threshold"`
2258+
IsGasBelowSubscribed bool `json:"is_gas_below_subscribed"`
2259+
GasBelowThreshold string `json:"gas_below_threshold" `
2260+
IsParticipationRateSubscribed bool `json:"is_participation_rate_subscribed"`
2261+
ParticipationRateThreshold float64 `json:"participation_rate_threshold"`
2262+
IsNewRewardRoundSubscribed bool `json:"is_new_reward_round_subscribed"`
2263+
}
2264+
var req request
22582265
if err := v.checkBody(&req, r); err != nil {
22592266
handleErr(w, r, err)
22602267
return
22612268
}
22622269
checkMinMax(&v, req.ParticipationRateThreshold, 0, 1, "participation_rate_threshold")
2263-
22642270
chainId := v.checkNetworkParameter(mux.Vars(r)["network"])
2271+
2272+
minWei := decimal.New(1000000, 1) // 0.001 Gwei
2273+
maxWei := decimal.New(1000000000000, 1) // 1000 Gwei
2274+
gasAboveThreshold := v.checkWeiMinMax(req.GasAboveThreshold, "gas_above_threshold", minWei, maxWei)
2275+
gasBelowThreshold := v.checkWeiMinMax(req.GasBelowThreshold, "gas_below_threshold", minWei, maxWei)
22652276
if v.hasErrors() {
22662277
handleErr(w, r, v)
22672278
return
22682279
}
2269-
err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, req)
2280+
settings := types.NotificationSettingsNetwork{
2281+
IsGasAboveSubscribed: req.IsGasAboveSubscribed,
2282+
GasAboveThreshold: gasAboveThreshold,
2283+
IsGasBelowSubscribed: req.IsGasBelowSubscribed,
2284+
GasBelowThreshold: gasBelowThreshold,
2285+
IsParticipationRateSubscribed: req.IsParticipationRateSubscribed,
2286+
ParticipationRateThreshold: req.ParticipationRateThreshold,
2287+
IsNewRewardRoundSubscribed: req.IsNewRewardRoundSubscribed,
2288+
}
2289+
2290+
err = h.dai.UpdateNotificationSettingsNetworks(r.Context(), userId, chainId, settings)
22702291
if err != nil {
22712292
handleErr(w, r, err)
22722293
return
22732294
}
22742295
response := types.InternalPutUserNotificationSettingsNetworksResponse{
22752296
Data: types.NotificationNetwork{
22762297
ChainId: chainId,
2277-
Settings: req,
2298+
Settings: settings,
22782299
},
22792300
}
22802301
returnOk(w, r, response)
@@ -2469,6 +2490,9 @@ func (h *HandlerService) PublicPutUserNotificationSettingsValidatorDashboard(w h
24692490
vars := mux.Vars(r)
24702491
dashboardId := v.checkPrimaryDashboardId(vars["dashboard_id"])
24712492
groupId := v.checkExistingGroupId(vars["group_id"])
2493+
2494+
checkMinMax(&v, req.MaxCollateralThreshold, 0, 1, "max_collateral_threshold")
2495+
checkMinMax(&v, req.MinCollateralThreshold, 0, 1, "min_collateral_threshold")
24722496
if v.hasErrors() {
24732497
handleErr(w, r, v)
24742498
return

0 commit comments

Comments
 (0)