From 97f6e57f559511ac898f8fe6424b1d16919b9d97 Mon Sep 17 00:00:00 2001 From: Mrsirdev <111534516+Mrsirdev@users.noreply.github.com> Date: Mon, 23 Oct 2023 13:45:30 +0100 Subject: [PATCH] chore: Handle accounts with zero staking balance on rewards endpoint (#76) --- CHANGELOG.md | 1 + api/handler/v1/erc20.go | 6 +++++- api/handler/v1/network.go | 16 +++++++++++----- api/handler/v1/staking.go | 16 +++++++++++++++- api/handler/v1/transaction.go | 7 ++++++- api/handler/v1/validators.go | 6 ++++-- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11912f2..18a4b4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/api/handler/v1/erc20.go b/api/handler/v1/erc20.go index 1431293..8ebc3a8 100644 --- a/api/handler/v1/erc20.go +++ b/api/handler/v1/erc20.go @@ -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{}, @@ -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 } diff --git a/api/handler/v1/network.go b/api/handler/v1/network.go index 3b6cf3e..d848857 100644 --- a/api/handler/v1/network.go +++ b/api/handler/v1/network.go @@ -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 { @@ -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 } diff --git a/api/handler/v1/staking.go b/api/handler/v1/staking.go index a116c7d..cff0189 100644 --- a/api/handler/v1/staking.go +++ b/api/handler/v1/staking.go @@ -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) @@ -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) } diff --git a/api/handler/v1/transaction.go b/api/handler/v1/transaction.go index ba8d0a5..a6cc855 100644 --- a/api/handler/v1/transaction.go +++ b/api/handler/v1/transaction.go @@ -6,6 +6,7 @@ package v1 import ( "encoding/base64" "encoding/json" + "errors" "fmt" "math/big" "sort" @@ -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 diff --git a/api/handler/v1/validators.go b/api/handler/v1/validators.go index 0efb84a..f52b29e 100644 --- a/api/handler/v1/validators.go +++ b/api/handler/v1/validators.go @@ -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 } @@ -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) }