Skip to content

Commit d2569c7

Browse files
committed
Support for sending funds to the community pool
1 parent cf8816e commit d2569c7

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

simapp/app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func NewSimApp(
182182
supply.NewAppModule(app.SupplyKeeper, app.AccountKeeper),
183183
gov.NewAppModule(app.GovKeeper, app.SupplyKeeper),
184184
mint.NewAppModule(app.MintKeeper),
185-
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper),
185+
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper, app.BankKeeper),
186186
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
187187
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
188188
)
@@ -216,7 +216,7 @@ func NewSimApp(
216216
supply.NewAppModule(app.SupplyKeeper, app.AccountKeeper),
217217
gov.NewAppModule(app.GovKeeper, app.SupplyKeeper),
218218
mint.NewAppModule(app.MintKeeper),
219-
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper),
219+
distr.NewAppModule(app.DistrKeeper, app.SupplyKeeper, app.BankKeeper),
220220
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.SupplyKeeper),
221221
slashing.NewAppModule(app.SlashingKeeper, app.StakingKeeper),
222222
)

x/distribution/handler.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"fmt"
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/cosmos/cosmos-sdk/x/bank"
78
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
89
"github.com/cosmos/cosmos-sdk/x/distribution/types"
910
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
1011
)
1112

12-
func NewHandler(k keeper.Keeper) sdk.Handler {
13+
func NewHandler(k keeper.Keeper, bankKeeper bank.Keeper) sdk.Handler {
1314
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
1415
ctx = ctx.WithEventManager(sdk.NewEventManager())
1516

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

27+
case types.MsgDepositIntoCommunityPool:
28+
return handleMsgDepositIntoCommunityPool(ctx, msg, k, bankKeeper)
29+
2630
default:
2731
errMsg := fmt.Sprintf("unrecognized distribution message type: %T", msg)
2832
return sdk.ErrUnknownRequest(errMsg).Result()
@@ -83,6 +87,26 @@ func handleMsgWithdrawValidatorCommission(ctx sdk.Context, msg types.MsgWithdraw
8387
return sdk.Result{Events: ctx.EventManager().Events()}
8488
}
8589

90+
func handleMsgDepositIntoCommunityPool(ctx sdk.Context, msg types.MsgDepositIntoCommunityPool, k keeper.Keeper, bankK bank.Keeper) sdk.Result {
91+
if _, err := bankK.SubtractCoins(ctx, msg.Depositor, msg.Amount); err != nil {
92+
return err.Result()
93+
}
94+
95+
pool := k.GetFeePool(ctx)
96+
pool.CommunityPool = pool.CommunityPool.Add(sdk.NewDecCoins(msg.Amount))
97+
k.SetFeePool(ctx, pool)
98+
99+
ctx.EventManager().EmitEvent(
100+
sdk.NewEvent(
101+
sdk.EventTypeMessage,
102+
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
103+
sdk.NewAttribute(sdk.AttributeKeySender, msg.Depositor.String()),
104+
),
105+
)
106+
107+
return sdk.Result{Events: ctx.EventManager().Events()}
108+
}
109+
86110
func NewCommunityPoolSpendProposalHandler(k Keeper) govtypes.Handler {
87111
return func(ctx sdk.Context, content govtypes.Content) sdk.Error {
88112
switch c := content.(type) {

x/distribution/module.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"math/rand"
66

7+
"github.com/cosmos/cosmos-sdk/x/bank"
78
"github.com/gorilla/mux"
89
"github.com/spf13/cobra"
910

@@ -99,15 +100,17 @@ type AppModule struct {
99100

100101
keeper Keeper
101102
supplyKeeper types.SupplyKeeper
103+
bankKeeper bank.Keeper
102104
}
103105

104106
// NewAppModule creates a new AppModule object
105-
func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper) AppModule {
107+
func NewAppModule(keeper Keeper, supplyKeeper types.SupplyKeeper, bankKeeper bank.Keeper) AppModule {
106108
return AppModule{
107109
AppModuleBasic: AppModuleBasic{},
108110
AppModuleSimulation: AppModuleSimulation{},
109111
keeper: keeper,
110112
supplyKeeper: supplyKeeper,
113+
bankKeeper: bankKeeper,
111114
}
112115
}
113116

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

129132
// NewHandler returns an sdk.Handler for the distribution module.
130133
func (am AppModule) NewHandler() sdk.Handler {
131-
return NewHandler(am.keeper)
134+
return NewHandler(am.keeper, am.bankKeeper)
132135
}
133136

134137
// QuerierRoute returns the distribution module's querier route name.

x/distribution/types/msg.go

+38
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,41 @@ func (msg MsgWithdrawValidatorCommission) ValidateBasic() sdk.Error {
116116
}
117117
return nil
118118
}
119+
120+
// msg struct for delegation withdraw from a single validator
121+
type MsgDepositIntoCommunityPool struct {
122+
Amount sdk.Coins `json:"amount" yaml:"amount"`
123+
Depositor sdk.AccAddress `json:"depositor" yaml:"depositor"`
124+
}
125+
126+
func NewMsgDepositIntoCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) MsgDepositIntoCommunityPool {
127+
return MsgDepositIntoCommunityPool{
128+
Amount: amount,
129+
Depositor: depositor,
130+
}
131+
}
132+
133+
func (msg MsgDepositIntoCommunityPool) Route() string { return ModuleName }
134+
func (msg MsgDepositIntoCommunityPool) Type() string { return "deposit_into_community_pool" }
135+
136+
// Return address that must sign over msg.GetSignBytes()
137+
func (msg MsgDepositIntoCommunityPool) GetSigners() []sdk.AccAddress {
138+
return []sdk.AccAddress{msg.Depositor}
139+
}
140+
141+
// get the bytes for the message signer to sign on
142+
func (msg MsgDepositIntoCommunityPool) GetSignBytes() []byte {
143+
bz := ModuleCdc.MustMarshalJSON(msg)
144+
return sdk.MustSortJSON(bz)
145+
}
146+
147+
// quick validity check
148+
func (msg MsgDepositIntoCommunityPool) ValidateBasic() sdk.Error {
149+
if !msg.Amount.IsValid() {
150+
return sdk.ErrInvalidCoins(msg.Amount.String())
151+
}
152+
if msg.Depositor.Empty() {
153+
return sdk.ErrInvalidAddress(msg.Depositor.String())
154+
}
155+
return nil
156+
}

0 commit comments

Comments
 (0)