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

Support for sending funds to the community pool #5249

Merged
merged 27 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d2569c7
Support for sending funds to the community pool
RiccardoM Oct 25, 2019
6929767
Added the REST and CLI txs
RiccardoM Oct 25, 2019
b6bfcab
Reverted back all the changes and edited the blacklisted address as s…
RiccardoM Oct 28, 2019
f4d74b2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into community-…
RiccardoM Nov 13, 2019
f1da9ff
Improved how allowed receiving accounts are specified inside the app
RiccardoM Nov 13, 2019
8b0870c
Merge branch 'master' into community-pool-funding
RiccardoM Nov 13, 2019
8d54b1e
Merge branch 'master' into community-pool-funding
alexanderbez Nov 29, 2019
8cb3615
Merge remote-tracking branch 'origin/community-pool-funding' into com…
RiccardoM Nov 29, 2019
3ea2eef
Added the changelod entry
RiccardoM Nov 29, 2019
31a38a3
Removed custom msg and handler (leftover from first implementation)
RiccardoM Nov 29, 2019
72d3776
Removed custom msg and handler (leftover from first implementation)
RiccardoM Nov 29, 2019
161e861
Removed bank usage from the dist module
RiccardoM Nov 29, 2019
8a6fcf5
Removed state machine changelog entry
RiccardoM Nov 30, 2019
5e13360
Merge branch 'master' into community-pool-funding
alexanderbez Nov 30, 2019
3b095e3
Merge branch 'master' into community-pool-funding
alexanderbez Dec 2, 2019
f4d8e61
Merge branch 'master' of github.com:cosmos/cosmos-sdk into community-…
RiccardoM Dec 9, 2019
a0217ab
Support for sending funds to the community pool
RiccardoM Oct 25, 2019
1c59821
Added the REST and CLI txs
RiccardoM Oct 25, 2019
6aabb39
Reverted to the usage of a custom message to send funds
RiccardoM Dec 9, 2019
a34b963
Update x/distribution/client/cli/tx.go
RiccardoM Dec 9, 2019
4bdfff9
Update x/distribution/keeper/keeper.go
RiccardoM Dec 9, 2019
3f8ce2c
Update x/distribution/client/cli/tx.go
RiccardoM Dec 9, 2019
ea765d2
Update x/distribution/client/cli/tx.go
RiccardoM Dec 9, 2019
53316f7
Update x/distribution/client/cli/tx.go
RiccardoM Dec 9, 2019
5977e15
Fixed some errors
RiccardoM Dec 10, 2019
95b64f1
Fixed some lint errors
RiccardoM Dec 10, 2019
837b5f3
Merge branch 'master' into community-pool-funding
alexanderbez Dec 10, 2019
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
4 changes: 2 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func NewSimApp(
supply.NewAppModule(app.SupplyKeeper, app.AccountKeeper),
gov.NewAppModule(app.GovKeeper, app.SupplyKeeper),
mint.NewAppModule(app.MintKeeper),
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper),
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper, app.BankKeeper),
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
)
Expand Down Expand Up @@ -216,7 +216,7 @@ func NewSimApp(
supply.NewAppModule(app.SupplyKeeper, app.AccountKeeper),
gov.NewAppModule(app.GovKeeper, app.SupplyKeeper),
mint.NewAppModule(app.MintKeeper),
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper),
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper, app.BankKeeper),
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
)
Expand Down
33 changes: 33 additions & 0 deletions x/distribution/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetTxCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
GetCmdWithdrawRewards(cdc),
GetCmdSetWithdrawAddr(cdc),
GetCmdWithdrawAllRewards(cdc, storeKey),
GetCmdFundCommunityPool(cdc),
)...)

return distTxCmd
Expand Down Expand Up @@ -254,3 +255,35 @@ Where proposal.json contains:

return cmd
}

// command to fund the community pool
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
func GetCmdFundCommunityPool(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "fund-community-pool [amount]",
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
Short: "funds the community pool with the specified amount",
Long: strings.TrimSpace(
fmt.Sprintf(`Funds the community pool with the specified amount

Example:
$ %s tx fund-community-pool 100uatom --from mykey
`,
version.ClientName,
),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
txBldr := auth.NewTxBuilderFromCLI().WithTxEncoder(utils.GetTxEncoder(cdc))
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
cliCtx := context.NewCLIContext().WithCodec(cdc)
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved

depositorAddr := cliCtx.GetFromAddress()
amount, err := sdk.ParseCoins(args[0])
if err != nil {
return err
}

msg := types.NewMsgDepositIntoCommunityPool(amount, depositorAddr)
return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
35 changes: 35 additions & 0 deletions x/distribution/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func registerTxRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute strin
withdrawValidatorRewardsHandlerFn(cliCtx),
).Methods("POST")

// Fund the community pool
r.HandleFunc(
"/distribution/community_pool",
withdrawValidatorRewardsHandlerFn(cliCtx),
).Methods("POST")

}

type (
Expand All @@ -50,6 +56,11 @@ type (
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
WithdrawAddress sdk.AccAddress `json:"withdraw_address" yaml:"withdraw_address"`
}

fundCommunityPoolReq struct {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Amount sdk.Coins `json:"amount" yaml:"amount"`
}
)

// Withdraw delegator rewards
Expand Down Expand Up @@ -177,6 +188,30 @@ func withdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext) http.HandlerFu
}
}

// Fund the community pool
func fundCommunityPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
return func(w http.ResponseWriter, r *http.Request) {
var req fundCommunityPoolReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}

req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}

fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

msg := types.NewMsgDepositIntoCommunityPool(req.Amount, fromAddr)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, []sdk.Msg{msg})
}
}

// Auxiliary

func checkDelegatorAddressVar(w http.ResponseWriter, r *http.Request) (sdk.AccAddress, bool) {
Expand Down
26 changes: 25 additions & 1 deletion x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

func NewHandler(k keeper.Keeper) sdk.Handler {
func NewHandler(k keeper.Keeper, bankKeeper bank.Keeper) sdk.Handler {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())

Expand All @@ -23,6 +24,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case types.MsgWithdrawValidatorCommission:
return handleMsgWithdrawValidatorCommission(ctx, msg, k)

case types.MsgDepositIntoCommunityPool:
return handleMsgDepositIntoCommunityPool(ctx, msg, k, bankKeeper)

default:
errMsg := fmt.Sprintf("unrecognized distribution message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
Expand Down Expand Up @@ -83,6 +87,26 @@ func handleMsgWithdrawValidatorCommission(ctx sdk.Context, msg types.MsgWithdraw
return sdk.Result{Events: ctx.EventManager().Events()}
}

func handleMsgDepositIntoCommunityPool(ctx sdk.Context, msg types.MsgDepositIntoCommunityPool, k keeper.Keeper, bankK bank.Keeper) sdk.Result {
if _, err := bankK.SubtractCoins(ctx, msg.Depositor, msg.Amount); err != nil {
return err.Result()
}

pool := k.GetFeePool(ctx)
pool.CommunityPool = pool.CommunityPool.Add(sdk.NewDecCoins(msg.Amount))
k.SetFeePool(ctx, pool)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
),
)

return sdk.Result{Events: ctx.EventManager().Events()}
}

func NewCommunityPoolSpendProposalHandler(k Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) sdk.Error {
switch c := content.(type) {
Expand Down
7 changes: 5 additions & 2 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"math/rand"

"github.com/cosmos/cosmos-sdk/x/bank"
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gorilla/mux"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -99,15 +100,17 @@ type AppModule struct {

keeper Keeper
supplyKeeper types.SupplyKeeper
bankKeeper bank.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper) AppModule {
func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper, bankKeeper bank.Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
AppModuleSimulation: AppModuleSimulation{},
keeper: keeper,
supplyKeeper: supplyKeeper,
bankKeeper: bankKeeper,
}
}

Expand All @@ -128,7 +131,7 @@ func (AppModule) Route() string {

// NewHandler returns an sdk.Handler for the distribution module.
func (am AppModule) NewHandler() sdk.Handler {
return NewHandler(am.keeper)
return NewHandler(am.keeper, am.bankKeeper)
}

// QuerierRoute returns the distribution module's querier route name.
Expand Down
38 changes: 38 additions & 0 deletions x/distribution/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,41 @@ func (msg MsgWithdrawValidatorCommission) ValidateBasic() sdk.Error {
}
return nil
}

// msg struct for delegation withdraw from a single validator
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
type MsgDepositIntoCommunityPool struct {
Amount sdk.Coins `json:"amount" yaml:"amount"`
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
}

func NewMsgDepositIntoCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) MsgDepositIntoCommunityPool {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return MsgDepositIntoCommunityPool{
Amount: amount,
Depositor: depositor,
}
}

func (msg MsgDepositIntoCommunityPool) Route() string { return ModuleName }
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgDepositIntoCommunityPool) Type() string { return "deposit_into_community_pool" }
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

// Return address that must sign over msg.GetSignBytes()
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgDepositIntoCommunityPool) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Depositor}
}

// get the bytes for the message signer to sign on
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgDepositIntoCommunityPool) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// quick validity check
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
func (msg MsgDepositIntoCommunityPool) ValidateBasic() sdk.Error {
if !msg.Amount.IsValid() {
return sdk.ErrInvalidCoins(msg.Amount.String())
}
if msg.Depositor.Empty() {
return sdk.ErrInvalidAddress(msg.Depositor.String())
}
return nil
}