Skip to content

Commit

Permalink
chore: Handle accounts with zero staking balance on rewards endpoint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrsirdev authored Oct 23, 2023
1 parent 01c72ed commit 97f6e57
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

- (chore) [fse-792] Handle accounts with zero staking balance on rewards endpoint
- (chore) [fse-710] Bundle all price cron API calls into a single API call for all tokens
- (chore) [fse-710] Fetch evmos 24h price change and return it on the ERC20ModuleBalance endpoint

Expand Down
6 changes: 5 additions & 1 deletion api/handler/v1/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func getTotalBalance(balance ERC20Entry) decimal.Dec {
return res
}

func buildValuesResponse(values string) string {
return "{\"values\":" + values + "}"
}

func ERC20ModuleEmptyBalance(ctx *fasthttp.RequestCtx) {
container := ModuleBalanceContainer{
values: map[string]ERC20Entry{},
Expand Down Expand Up @@ -290,7 +294,7 @@ func ERC20TokensByNameInternal(name string) (string, error) {

for _, v := range val {
if strings.Contains(v.URL, name) {
res := "{\"values\":" + v.Content + "}"
res := buildValuesResponse(v.Content)
db.RedisSetERC20TokensByName(name, res)
return res, nil
}
Expand Down
16 changes: 11 additions & 5 deletions api/handler/v1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ import (
"github.com/valyala/fasthttp"
)

type ValuesResponse struct {
Values interface{} `json:"values"`
}

func NetworkConfig(ctx *fasthttp.RequestCtx) {
networkConfigs, err := resources.GetNetworkConfigs()
if err != nil {
sendResponse(buildErrorResponse(err.Error()), nil, ctx)
return
}
stringRes, err := json.Marshal(networkConfigs)
stringRes, err := json.Marshal(
&ValuesResponse{
Values: networkConfigs,
},
)
if err != nil {
sendResponse("", err, ctx)
return
}

res := "{\"values\":" + string(stringRes) + "}"

sendResponse(res, nil, ctx)
sendResponse(string(stringRes), nil, ctx)
}

type SourceParams struct {
Expand Down Expand Up @@ -68,7 +74,7 @@ func NetworkConfigByNameInternal(name string) (string, error) {

for _, v := range val {
if strings.Contains(v.URL, name) {
res := "{\"values\":" + v.Content + "}"
res := buildValuesResponse(v.Content)
db.RedisSetNetworkConfigByName(name, res)
return res, nil
}
Expand Down
16 changes: 15 additions & 1 deletion api/handler/v1/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type StakingRewardsResponse struct {
} `json:"total"`
}

type ValueResponse struct {
Value interface{} `json:"value"`
}

func TotalStakingByAddress(ctx *fasthttp.RequestCtx) {
address := paramToString("address", ctx)

Expand All @@ -55,7 +59,17 @@ func TotalStakingByAddress(ctx *fasthttp.RequestCtx) {

totalStaked := blockchain.GetTotalStake(stakingResponse)

res := "{\"value\":\"" + totalStaked + "\"}"
stringRes, err := json.Marshal(
&ValueResponse{
Value: totalStaked,
},
)
if err != nil {
sendResponse("", err, ctx)
return
}

res := string(stringRes)
sendResponse(res, err, ctx)
}

Expand Down
7 changes: 6 additions & 1 deletion api/handler/v1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package v1
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
Expand Down Expand Up @@ -757,8 +758,12 @@ func Rewards(ctx *fasthttp.RequestCtx) {
length = len(sortedRewards)
}

msgs := make([]sdk.Msg, length)
if len(sortedRewards) == 0 {
sendResponse("account does not have staking balance", errors.New(""), ctx)
return
}

msgs := make([]sdk.Msg, length)
for k, v := range sortedRewards {
if k > 6 {
break
Expand Down
6 changes: 4 additions & 2 deletions api/handler/v1/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ type ValidatorAPIResponse struct {

func AllValidators(ctx *fasthttp.RequestCtx) {
if validators, err := db.RedisGetAllValidators("EVMOS"); err == nil {
sendResponse("{\"values\":"+validators+"}", err, ctx)
res := buildValuesResponse(validators)
sendResponse(res, err, ctx)
return
}

Expand Down Expand Up @@ -109,5 +110,6 @@ func AllValidators(ctx *fasthttp.RequestCtx) {
validatorsJSON := string(validatorsByte)

db.RedisSetAllValidators("EVMOS", validatorsJSON)
sendResponse("{\"values\":"+validatorsJSON+"}", err, ctx)
validatorsRes := buildValuesResponse(validatorsJSON)
sendResponse(validatorsRes, err, ctx)
}

0 comments on commit 97f6e57

Please sign in to comment.