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

Implement client functionality for the community pool #3939

Merged
merged 9 commits into from
Mar 28, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3937 Add command to query community-pool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3937 Add route to fetch community-pool
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3937 Implement community pool querier.
16 changes: 16 additions & 0 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,22 @@ paths:
description: Key password is wrong
500:
description: Internal Server Error
/distribution/community_pool:
get:
summary: Community pool parameters
tags:
- ICS24
produces:
- application/json
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Coin"
500:
description: Internal Server Error
/distribution/parameters:
get:
summary: Fee distribution parameters
Expand Down
8 changes: 8 additions & 0 deletions docs/gaia/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,14 @@ To check the current distribution parameters, run:
gaiacli query distr params
```

#### Query distribution Community Pool

To query all coins in the community pool which is under Governance control:

```bash
gaiacli query distr community-pool
```

#### Query outstanding rewards

To check the current outstanding (un-withdrawn) rewards, run:
Expand Down
25 changes: 25 additions & 0 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,28 @@ $ gaiacli query distr rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p cosm
},
}
}

// GetCmdQueryCommunityPool returns the command for fetching community pool info
func GetCmdQueryCommunityPool(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "community-pool",
Args: cobra.NoArgs,
Short: "Query the amount of coins in the community pool",
Long: strings.TrimSpace(`Query all coins in the community pool which is under Governance control.

$ gaiacli query distr community-pool
`),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's weird that in CLI queries we sometimes use routes and sometimes the key (with cliCtx.QueryStore()

if err != nil {
return err
}

var result sdk.DecCoins
cdc.MustUnmarshalJSON(res, &result)
return cliCtx.PrintOutput(result)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
1 change: 1 addition & 0 deletions x/distribution/client/module_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (mc ModuleClient) GetQueryCmd() *cobra.Command {
distCmds.GetCmdQueryValidatorCommission(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryValidatorSlashes(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryDelegatorRewards(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryCommunityPool(mc.storeKey, mc.cdc),
)...)

return distQueryCmd
Expand Down
26 changes: 26 additions & 0 deletions x/distribution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router,
paramsHandlerFn(cliCtx, cdc, queryRoute),
).Methods("GET")

// Get the amount held in the community pool
r.HandleFunc(
"/distribution/community_pool",
communityPoolHandler(cliCtx, cdc, queryRoute),
).Methods("GET")

}

// HTTP request handler to query the total rewards balance from all delegations
Expand Down Expand Up @@ -207,6 +213,26 @@ func paramsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec,
}
}

func communityPoolHandler(cliCtx context.CLIContext, cdc *codec.Codec,
queryRoute string) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

var result sdk.DecCoins
if err := cdc.UnmarshalJSON(res, &result); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cdc, result, cliCtx.Indent)
}
}

// HTTP request handler to query the outstanding rewards
func outstandingRewardsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec,
queryRoute string) http.HandlerFunc {
Expand Down
12 changes: 12 additions & 0 deletions x/distribution/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
QueryDelegatorTotalRewards = "delegator_total_rewards"
QueryDelegatorValidators = "delegator_validators"
QueryWithdrawAddr = "withdraw_addr"
QueryCommunityPool = "community_pool"

ParamCommunityTax = "community_tax"
ParamBaseProposerReward = "base_proposer_reward"
Expand Down Expand Up @@ -54,6 +55,9 @@ func NewQuerier(k Keeper) sdk.Querier {
case QueryWithdrawAddr:
return queryDelegatorWithdrawAddress(ctx, path[1:], req, k)

case QueryCommunityPool:
return queryCommunityPool(ctx, path[1:], req, k)

default:
return nil, sdk.ErrUnknownRequest("unknown distr query endpoint")
}
Expand Down Expand Up @@ -314,3 +318,11 @@ func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.Request

return bz, nil
}

func queryCommunityPool(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
bz, err := k.cdc.MarshalJSON(k.GetFeePoolCommunityCoins(ctx))
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
}
return bz, nil
}
17 changes: 17 additions & 0 deletions x/distribution/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ func getQueriedDelegationRewards(t *testing.T, ctx sdk.Context, cdc *codec.Codec
return
}

func getQueriedCommunityPool(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier) (ptr []byte) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, QueryCommunityPool}, ""),
Data: []byte{},
}

cp, err := querier(ctx, []string{QueryCommunityPool}, query)
require.Nil(t, err)
require.Nil(t, cdc.UnmarshalJSON(cp, &ptr))

return
}

func TestQueries(t *testing.T) {
cdc := codec.New()
ctx, _, keeper, sk, _ := CreateTestInputDefault(t, false, 100)
Expand Down Expand Up @@ -169,4 +182,8 @@ func TestQueries(t *testing.T) {
keeper.AllocateTokensToValidator(ctx, val, tokens)
rewards = getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDec(initial / 2)}}, rewards)

// currently community pool hold nothing so we should return null
communityPool := getQueriedCommunityPool(t, ctx, cdc, querier)
require.Nil(t, communityPool)
}