From 50978a2e9bec4572a7efdeb6eb1fe795c1d525f4 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 2 Feb 2024 23:41:41 +0800 Subject: [PATCH] add for query validators --- CHANGELOG.md | 2 +- x/staking/client/cli/query.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d90c2d2684f7..99ea19697c2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,7 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client/tx) [#18852](https://github.com/cosmos/cosmos-sdk/pull/18852) Add `WithFromName` to tx factory. * (types) [#18875](https://github.com/cosmos/cosmos-sdk/pull/18875) Speedup coins.Sort() if len(coins) <= 1 * (testutil) [#18930](https://github.com/cosmos/cosmos-sdk/pull/18930) Add NodeURI for clientCtx. -* (cli) [#](https://github.com/cosmos/cosmos-sdk/pull/) Add validator_address when query validator info. +* (cli) [#199](https://github.com/cosmos/cosmos-sdk/pull/199) Add validator_address when query validator info. ## [v0.47.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.7) - 2023-12-20 diff --git a/x/staking/client/cli/query.go b/x/staking/client/cli/query.go index 2955f805626b..24842e76df69 100644 --- a/x/staking/client/cli/query.go +++ b/x/staking/client/cli/query.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -140,7 +141,34 @@ $ %s query staking validators return err } - return clientCtx.PrintProto(result) + newResult := &struct { + Validators []interface{} `json:"validators"` + Pagination *query.PageResponse `json:"pagination,omitempty"` + }{make([]interface{}, 0, len(result.Validators)), result.Pagination} + for _, validator := range result.Validators { + validator := validator + bytes, err := clientCtx.Codec.MarshalJSON(&validator) + if err != nil { + return err + } + var data map[string]interface{} + err = json.Unmarshal(bytes, &data) + if err != nil { + return err + } + valAddr, err := sdk.ValAddressFromBech32(validator.OperatorAddress) + if err != nil { + return err + } + data["validator_address"] = sdk.AccAddress(valAddr).String() + newResult.Validators = append(newResult.Validators, data) + } + + bytes, err := json.Marshal(newResult) + if err != nil { + return err + } + return clientCtx.PrintBytes(bytes) }, }