Skip to content

Commit 38b7c07

Browse files
jackzampolinAlessio Treglia
authored andcommitted
Implement client functionality for the community pool (#3939)
Fixes: #3937
1 parent f635b1c commit 38b7c07

File tree

10 files changed

+108
-0
lines changed

10 files changed

+108
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#3937 Add command to query community-pool
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#3937 Add route to fetch community-pool
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#3937 Implement community pool querier.

client/lcd/swagger-ui/swagger.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,22 @@ paths:
16321632
description: Key password is wrong
16331633
500:
16341634
description: Internal Server Error
1635+
/distribution/community_pool:
1636+
get:
1637+
summary: Community pool parameters
1638+
tags:
1639+
- ICS24
1640+
produces:
1641+
- application/json
1642+
responses:
1643+
200:
1644+
description: OK
1645+
schema:
1646+
type: array
1647+
items:
1648+
$ref: "#/definitions/Coin"
1649+
500:
1650+
description: Internal Server Error
16351651
/distribution/parameters:
16361652
get:
16371653
summary: Fee distribution parameters

docs/gaia/gaiacli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,14 @@ To check the current distribution parameters, run:
637637
gaiacli query distr params
638638
```
639639

640+
#### Query distribution Community Pool
641+
642+
To query all coins in the community pool which is under Governance control:
643+
644+
```bash
645+
gaiacli query distr community-pool
646+
```
647+
640648
#### Query outstanding rewards
641649

642650
To check the current outstanding (un-withdrawn) rewards, run:

x/distribution/client/cli/query.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,28 @@ $ gaiacli query distr rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p cosm
163163
},
164164
}
165165
}
166+
167+
// GetCmdQueryCommunityPool returns the command for fetching community pool info
168+
func GetCmdQueryCommunityPool(queryRoute string, cdc *codec.Codec) *cobra.Command {
169+
return &cobra.Command{
170+
Use: "community-pool",
171+
Args: cobra.NoArgs,
172+
Short: "Query the amount of coins in the community pool",
173+
Long: strings.TrimSpace(`Query all coins in the community pool which is under Governance control.
174+
175+
$ gaiacli query distr community-pool
176+
`),
177+
RunE: func(cmd *cobra.Command, args []string) error {
178+
cliCtx := context.NewCLIContext().WithCodec(cdc)
179+
180+
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
181+
if err != nil {
182+
return err
183+
}
184+
185+
var result sdk.DecCoins
186+
cdc.MustUnmarshalJSON(res, &result)
187+
return cliCtx.PrintOutput(result)
188+
},
189+
}
190+
}

x/distribution/client/module_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (mc ModuleClient) GetQueryCmd() *cobra.Command {
3131
distCmds.GetCmdQueryValidatorCommission(mc.storeKey, mc.cdc),
3232
distCmds.GetCmdQueryValidatorSlashes(mc.storeKey, mc.cdc),
3333
distCmds.GetCmdQueryDelegatorRewards(mc.storeKey, mc.cdc),
34+
distCmds.GetCmdQueryCommunityPool(mc.storeKey, mc.cdc),
3435
)...)
3536

3637
return distQueryCmd

x/distribution/client/rest/query.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router,
6161
paramsHandlerFn(cliCtx, cdc, queryRoute),
6262
).Methods("GET")
6363

64+
// Get the amount held in the community pool
65+
r.HandleFunc(
66+
"/distribution/community_pool",
67+
communityPoolHandler(cliCtx, cdc, queryRoute),
68+
).Methods("GET")
69+
6470
}
6571

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

216+
func communityPoolHandler(cliCtx context.CLIContext, cdc *codec.Codec,
217+
queryRoute string) http.HandlerFunc {
218+
219+
return func(w http.ResponseWriter, r *http.Request) {
220+
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
221+
if err != nil {
222+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
223+
return
224+
}
225+
226+
var result sdk.DecCoins
227+
if err := cdc.UnmarshalJSON(res, &result); err != nil {
228+
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
229+
return
230+
}
231+
232+
rest.PostProcessResponse(w, cdc, result, cliCtx.Indent)
233+
}
234+
}
235+
210236
// HTTP request handler to query the outstanding rewards
211237
func outstandingRewardsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec,
212238
queryRoute string) http.HandlerFunc {

x/distribution/keeper/querier.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
QueryDelegatorTotalRewards = "delegator_total_rewards"
2121
QueryDelegatorValidators = "delegator_validators"
2222
QueryWithdrawAddr = "withdraw_addr"
23+
QueryCommunityPool = "community_pool"
2324

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

58+
case QueryCommunityPool:
59+
return queryCommunityPool(ctx, path[1:], req, k)
60+
5761
default:
5862
return nil, sdk.ErrUnknownRequest("unknown distr query endpoint")
5963
}
@@ -314,3 +318,11 @@ func queryDelegatorWithdrawAddress(ctx sdk.Context, _ []string, req abci.Request
314318

315319
return bz, nil
316320
}
321+
322+
func queryCommunityPool(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
323+
bz, err := k.cdc.MarshalJSON(k.GetFeePoolCommunityCoins(ctx))
324+
if err != nil {
325+
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
326+
}
327+
return bz, nil
328+
}

x/distribution/keeper/querier_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ func getQueriedDelegationRewards(t *testing.T, ctx sdk.Context, cdc *codec.Codec
109109
return
110110
}
111111

112+
func getQueriedCommunityPool(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier) (ptr []byte) {
113+
query := abci.RequestQuery{
114+
Path: strings.Join([]string{custom, types.QuerierRoute, QueryCommunityPool}, ""),
115+
Data: []byte{},
116+
}
117+
118+
cp, err := querier(ctx, []string{QueryCommunityPool}, query)
119+
require.Nil(t, err)
120+
require.Nil(t, cdc.UnmarshalJSON(cp, &ptr))
121+
122+
return
123+
}
124+
112125
func TestQueries(t *testing.T) {
113126
cdc := codec.New()
114127
ctx, _, keeper, sk, _ := CreateTestInputDefault(t, false, 100)
@@ -169,4 +182,8 @@ func TestQueries(t *testing.T) {
169182
keeper.AllocateTokensToValidator(ctx, val, tokens)
170183
rewards = getQueriedDelegationRewards(t, ctx, cdc, querier, sdk.AccAddress(valOpAddr1), valOpAddr1)
171184
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDec(initial / 2)}}, rewards)
185+
186+
// currently community pool hold nothing so we should return null
187+
communityPool := getQueriedCommunityPool(t, ctx, cdc, querier)
188+
require.Nil(t, communityPool)
172189
}

0 commit comments

Comments
 (0)