Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Handle accounts with zero staking balance on rewards endpoint #76

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading