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

add the community pool in x/distribution #14

Merged
merged 2 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Build
vendor
build
tools/
tools/bin/*
examples/build/*
docs/_build
Expand Down Expand Up @@ -66,4 +67,4 @@ dependency-graph.png
contract_tests/*
dev/cache/*
go.sum
result.txt
result.txt
4 changes: 4 additions & 0 deletions app/genesis/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@
"withdraw_infos": null
},
"distribution": {
"community_tax": "0.02000000",
"delegator_withdraw_infos": [],
"fee_pool": {
"community_pool": []
},
"previous_proposer": "",
"validator_accumulated_commissions": [],
"withdraw_addr_enabled": true
Expand Down
5 changes: 3 additions & 2 deletions app/protocol/protocol_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (
distr.AppModuleBasic{},
gov.NewAppModuleBasic(
upgradeClient.ProposalHandler, paramsclient.ProposalHandler,
dexClient.DelistProposalHandler,
dexClient.DelistProposalHandler, distr.ProposalHandler,
),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
Expand Down Expand Up @@ -313,7 +313,8 @@ func (p *ProtocolV0) produceKeepers() {
govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler).
AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(&p.paramsKeeper)).
AddRoute(dex.RouterKey, dex.NewProposalHandler(&p.dexKeeper)).
AddRoute(upgrade.RouterKey, upgrade.NewAppUpgradeProposalHandler(&p.upgradeKeeper))
AddRoute(upgrade.RouterKey, upgrade.NewAppUpgradeProposalHandler(&p.upgradeKeeper)).
AddRoute(distr.RouterKey,distr.NewCommunityPoolSpendProposalHandler(p.distrKeeper))
govProposalHandlerRouter := keeper.NewProposalHandlerRouter()
govProposalHandlerRouter.AddRoute(params.RouterKey, &p.paramsKeeper).
AddRoute(dex.RouterKey, &p.dexKeeper).
Expand Down
5 changes: 5 additions & 0 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package distribution

import (
"github.com/okex/okchain/x/distribution/client"
"github.com/okex/okchain/x/distribution/keeper"
"github.com/okex/okchain/x/distribution/types"
)
Expand Down Expand Up @@ -43,6 +44,7 @@ var (
ErrNilValidatorAddr = types.ErrNilValidatorAddr
ErrNoValidatorCommission = types.ErrNoValidatorCommission
ErrSetWithdrawAddrDisabled = types.ErrSetWithdrawAddrDisabled
InitialFeePool = types.InitialFeePool
xiangjianmeng marked this conversation as resolved.
Show resolved Hide resolved
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
Expand All @@ -53,9 +55,11 @@ var (
InitialValidatorAccumulatedCommission = types.InitialValidatorAccumulatedCommission

// variable aliases
FeePoolKey = keeper.FeePoolKey
ProposerKey = keeper.ProposerKey
DelegatorWithdrawAddrPrefix = keeper.DelegatorWithdrawAddrPrefix
ValidatorAccumulatedCommissionPrefix = keeper.ValidatorAccumulatedCommissionPrefix
ParamStoreKeyCommunityTax = keeper.ParamStoreKeyCommunityTax
ParamStoreKeyWithdrawAddrEnabled = keeper.ParamStoreKeyWithdrawAddrEnabled
ModuleCdc = types.ModuleCdc
EventTypeSetWithdrawAddress = types.EventTypeSetWithdrawAddress
Expand All @@ -65,6 +69,7 @@ var (
AttributeKeyWithdrawAddress = types.AttributeKeyWithdrawAddress
AttributeKeyValidator = types.AttributeKeyValidator
AttributeValueCategory = types.AttributeValueCategory
ProposalHandler = client.ProposalHandler
)

type (
Expand Down
31 changes: 31 additions & 0 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
distQueryCmd.AddCommand(client.GetCommands(
GetCmdQueryParams(queryRoute, cdc),
GetCmdQueryValidatorCommission(queryRoute, cdc),
GetCmdQueryCommunityPool(queryRoute, cdc),
)...)

return distQueryCmd
Expand Down Expand Up @@ -92,3 +93,33 @@ $ %s query distr commission okchainvaloper1alq9na49n9yycysh889rl90g9nhe58lcs50wu
},
}
}

// 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(
fmt.Sprintf(`Query all coins in the community pool which is under Governance control.

Example:
$ %s query distr community-pool
`,
version.ClientName,
),
),
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)
if err != nil {
return err
}

var result sdk.DecCoins
cdc.MustUnmarshalJSON(res, &result)
return cliCtx.PrintOutput(result)
},
}
}
64 changes: 62 additions & 2 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/spf13/cobra"

"github.com/okex/okchain/x/distribution/types"
"github.com/okex/okchain/x/gov"
"github.com/spf13/cobra"
)

// GetTxCmd returns the transaction commands for this module
Expand Down Expand Up @@ -98,3 +98,63 @@ $ %s tx distr withdraw-rewards okchainvaloper1alq9na49n9yycysh889rl90g9nhe58lcs5
}
return cmd
}

// GetCmdSubmitProposal implements the command to submit a community-pool-spend proposal
func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "community-pool-spend [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a community pool spend proposal",
Long: strings.TrimSpace(
fmt.Sprintf(`Submit a community pool spend proposal along with an initial deposit.
The proposal details must be supplied via a JSON file.

Example:
$ %s tx gov submit-proposal community-pool-spend <path/to/proposal.json> --from=<key_or_address>

Where proposal.json contains:

{
"title": "Community Pool Spend",
"description": "Pay me some Atoms!",
"recipient": "okchain5afhd6gxevu37mkqcvvsj8qeylhn0rz46zdlq",
"amount": [
{
"denom": "stake",
"amount": "10000"
}
],
"deposit": [
{
"denom": "stake",
"amount": "10000"
}
]
}
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContext().WithCodec(cdc)

proposal, err := ParseCommunityPoolSpendProposalJSON(cdc, args[0])
if err != nil {
return err
}

from := cliCtx.GetFromAddress()
content := types.NewCommunityPoolSpendProposal(proposal.Title, proposal.Description, proposal.Recipient, proposal.Amount)

msg := gov.NewMsgSubmitProposal(content, proposal.Deposit, from)
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}

return cmd
}
35 changes: 35 additions & 0 deletions x/distribution/client/cli/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"io/ioutil"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type (
// CommunityPoolSpendProposalJSON defines a CommunityPoolSpendProposal with a deposit
CommunityPoolSpendProposalJSON struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Recipient sdk.AccAddress `json:"recipient" yaml:"recipient"`
Amount sdk.DecCoins `json:"amount" yaml:"amount"`
xiangjianmeng marked this conversation as resolved.
Show resolved Hide resolved
Deposit sdk.DecCoins `json:"deposit" yaml:"deposit"`
}
)

// ParseCommunityPoolSpendProposalJSON reads and parses a CommunityPoolSpendProposalJSON from a file.
func ParseCommunityPoolSpendProposalJSON(cdc *codec.Codec, proposalFile string) (CommunityPoolSpendProposalJSON, error) {
proposal := CommunityPoolSpendProposalJSON{}

contents, err := ioutil.ReadFile(proposalFile)
if err != nil {
return proposal, err
}

if err := cdc.UnmarshalJSON(contents, &proposal); err != nil {
return proposal, err
}

return proposal, nil
}
11 changes: 9 additions & 2 deletions x/distribution/client/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import (

// QueryParams actually queries distribution params.
func QueryParams(cliCtx context.CLIContext, queryRoute string) (PrettyParams, error) {
route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawAddrEnabled)
route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamCommunityTax)

retCommunityTax, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawAddrEnabled)
retWithdrawAddrEnabled, _, err := cliCtx.QueryWithData(route, []byte{})
if err != nil {
return PrettyParams{}, err
}

return newPrettyParams(retWithdrawAddrEnabled), nil
return newPrettyParams(retCommunityTax, retWithdrawAddrEnabled), nil
}

// QueryValidatorCommission returns a validator's commission.
Expand Down
8 changes: 6 additions & 2 deletions x/distribution/client/common/pretty_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ import (

// PrettyParams is the struct for CLI output
type PrettyParams struct {
CommunityTax json.RawMessage `json:"community_tax"`
WithdrawAddrEnabled json.RawMessage `json:"withdraw_addr_enabled"`
}

// newPrettyParams creates a new PrettyParams
func newPrettyParams(withdrawAddrEnabled json.RawMessage) PrettyParams {
func newPrettyParams(communityTax, withdrawAddrEnabled json.RawMessage) PrettyParams {
return PrettyParams{
CommunityTax: communityTax,
WithdrawAddrEnabled: withdrawAddrEnabled,
}
}

// String returns the params string
func (pp PrettyParams) String() string {
return fmt.Sprintf(`Distribution Params:
Withdraw Addr Enabled: %s`, pp.WithdrawAddrEnabled)
Community Tax: %s
Withdraw Addr Enabled: %s`,
pp.CommunityTax, pp.WithdrawAddrEnabled)
}
12 changes: 12 additions & 0 deletions x/distribution/client/proposal_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

import (
"github.com/okex/okchain/x/distribution/client/cli"
"github.com/okex/okchain/x/distribution/client/rest"
govclient "github.com/okex/okchain/x/gov/client"
)

// param change proposal handler
var (
ProposalHandler = govclient.NewProposalHandler(cli.GetCmdSubmitProposal, rest.ProposalRESTHandler)
)
32 changes: 32 additions & 0 deletions x/distribution/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"

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

"github.com/okex/okchain/x/distribution/client/common"
Expand Down Expand Up @@ -31,6 +32,12 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute st
"/distribution/parameters",
paramsHandlerFn(cliCtx, queryRoute),
).Methods("GET")

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

// HTTP request handler to query a delegation rewards
Expand Down Expand Up @@ -76,6 +83,31 @@ func paramsHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerF
}
}

// HTTP request handler to query the community pool coins
func communityPoolHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, 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 := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, result)
}
}

// HTTP request handler to query the accumulated commission of one single validator
func accumulatedCommissionHandlerFn(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading