Skip to content

Commit

Permalink
add a query api to get the total mined token from pot module (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiong-stratos authored Mar 7, 2023
1 parent e37ad12 commit 135f3bd
Show file tree
Hide file tree
Showing 9 changed files with 549 additions and 93 deletions.
13 changes: 13 additions & 0 deletions proto/stratos/pot/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/stratos/pot/v1/params";
}

rpc TotalMinedToken(QueryTotalMinedTokenRequest) returns (QueryTotalMinedTokenResponse) {
option (google.api.http).get = "/stratos/pot/v1/total-mined-token";
}
}

// QueryVolumeReportRequest is request type for the Query/VolumeReport RPC method
Expand Down Expand Up @@ -116,6 +120,15 @@ message QuerySlashingByOwnerResponse {
}


message QueryTotalMinedTokenRequest{}

message QueryTotalMinedTokenResponse{
cosmos.base.v1beta1.Coin total_mined_token = 1 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "total_mined_token",
(gogoproto.moretags) = "yaml:\"total_mined_token\""
];
}



Expand Down
48 changes: 34 additions & 14 deletions x/pot/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"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/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stratosnet/stratos-chain/x/pot/types"
)

Expand All @@ -28,11 +30,40 @@ func GetQueryCmd() *cobra.Command {
potQueryCmd.AddCommand(
GetCmdQueryVolumeReport(),
GetCmdQueryParams(),
GetCmdQueryTotalMinedTokens(),
)

return potQueryCmd
}

func GetCmdQueryTotalMinedTokens() *cobra.Command {
cmd := &cobra.Command{
Use: "total-mined-token",
Args: cobra.NoArgs,
Short: "Query the total mined tokens",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the total mined tokens.`),
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.TotalMinedToken(cmd.Context(), &types.QueryTotalMinedTokenRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryParams implements the params query command.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -72,10 +103,9 @@ $ %s query pot params
// GetCmdQueryVolumeReport implements the query volume report command.
func GetCmdQueryVolumeReport() *cobra.Command {
cmd := &cobra.Command{
Use: "report [flags]", // reporter: []byte
Use: "report [flags]",
Short: "Query volume report hash by epoch",
Long: strings.TrimSpace(
//fmt.Sprintf(`Query volume report hash by reporter.`),
fmt.Sprintf(`Query volume report hash by epoch.`),
),

Expand Down Expand Up @@ -110,16 +140,6 @@ func GetCmdQueryVolumeReport() *cobra.Command {
return cmd
}

//func QueryVolumeReport(cliCtx context.CLIContext, queryRoute string, epoch sdk.Int) (types.ReportInfo, int64, error) {
// route := fmt.Sprintf("custom/%s/%s", queryRoute, keeper.QueryVolumeReport)
// resp, height, err := cliCtx.QueryWithData(route, []byte(epoch.String()))
// if err != nil {
// return types.ReportInfo{}, height, err
// }
// reportRes := types.NewReportInfo(epoch, string(resp))
// return reportRes, height, nil
//}

func checkFlagEpoch(epochStr string) (sdk.Int, error) {
epochInt64, err := strconv.ParseInt(epochStr, 10, 64)
if err != nil {
Expand Down
23 changes: 22 additions & 1 deletion x/pot/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"net/http"
"strconv"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"

"github.com/stratosnet/stratos-chain/x/pot/types"
)

Expand All @@ -18,6 +20,7 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/pot/rewards/wallet/{walletAddress}", getRewardsByWalletAddrHandlerFn(clientCtx, types.QueryRewardsByWalletAddr)).Methods("GET")
r.HandleFunc("/pot/slashing/{walletAddress}", getSlashingByWalletAddressHandlerFn(clientCtx, types.QuerySlashingByWalletAddr)).Methods("GET")
r.HandleFunc("/pot/params", potParamsHandlerFn(clientCtx, types.QueryPotParams)).Methods("GET")
r.HandleFunc("/pot/total-mined-token", getTotalMinedTokenHandlerFn(clientCtx, types.QueryTotalMinedToken)).Methods("GET")
}

// GET request handler to query params of POT module
Expand Down Expand Up @@ -187,3 +190,21 @@ func getSlashingByWalletAddressHandlerFn(clientCtx client.Context, queryPath str
rest.PostProcessResponse(w, cliCtx, res)
}
}

func getTotalMinedTokenHandlerFn(clientCtx client.Context, queryPath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, queryPath)
res, height, err := cliCtx.Query(route)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
8 changes: 4 additions & 4 deletions x/pot/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/rest"
"github.com/gorilla/mux"
)

const (
RestWalletAddress = "wallet_address"
RestEpoch = "epoch"
RestHeight = "height"
RestEpoch = "epoch"
RestHeight = "height"
)

// RegisterRoutes registers pot-related REST handlers to a router
Expand Down
26 changes: 6 additions & 20 deletions x/pot/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

db "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -103,10 +101,7 @@ func (q Querier) RewardsByEpoch(c context.Context, req *types.QueryRewardsByEpoc
return &types.QueryRewardsByEpochResponse{}, status.Error(codes.Internal, err.Error())
}
height := ctx.BlockHeight()
//var rewards []*types.Reward
//for i, v := range res {
// rewards[i] = &v
//}

return &types.QueryRewardsByEpochResponse{Rewards: res, Height: height, Pagination: rewardsPageRes}, nil
}

Expand Down Expand Up @@ -373,18 +368,9 @@ func Paginate(
return res, nil
}

func GetIterator(prefixStore storetypes.KVStore, start []byte, reverse bool) db.Iterator {
if reverse {
var end []byte
if start != nil {
itr := prefixStore.Iterator(start, nil)
defer itr.Close()
if itr.Valid() {
itr.Next()
end = itr.Key()
}
}
return prefixStore.ReverseIterator(nil, end)
}
return prefixStore.Iterator(start, nil)
func (q Querier) TotalMinedToken(c context.Context, _ *types.QueryTotalMinedTokenRequest) (*types.QueryTotalMinedTokenResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
totalMinedToken := q.GetTotalMinedTokens(ctx)

return &types.QueryTotalMinedTokenResponse{TotalMinedToken: totalMinedToken}, nil
}
19 changes: 19 additions & 0 deletions x/pot/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package keeper
import (
gogotypes "github.com/gogo/protobuf/types"

db "github.com/tendermint/tm-db"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

stratos "github.com/stratosnet/stratos-chain/types"
Expand Down Expand Up @@ -216,3 +219,19 @@ func (k Keeper) SetIsReadyToDistributeReward(ctx sdk.Context, isReady bool) {
b := k.cdc.MustMarshalLengthPrefixed(&gogotypes.BoolValue{Value: isReady})
store.Set(types.IsReadyToDistributeReward, b)
}

func GetIterator(prefixStore storetypes.KVStore, start []byte, reverse bool) db.Iterator {
if reverse {
var end []byte
if start != nil {
itr := prefixStore.Iterator(start, nil)
defer itr.Close()
if itr.Valid() {
itr.Next()
end = itr.Key()
}
}
return prefixStore.ReverseIterator(nil, end)
}
return prefixStore.Iterator(start, nil)
}
1 change: 1 addition & 0 deletions x/pot/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
QueryRewardsByWalletAddr = "query_pot_rewards_by_wallet_address"
QuerySlashingByWalletAddr = "query_pot_slashing_by_wallet_address"
QueryPotParams = "query_pot_params"
QueryTotalMinedToken = "query_total_mined_token"
QueryDefaultLimit = 100
)

Expand Down
Loading

0 comments on commit 135f3bd

Please sign in to comment.