Skip to content

Commit

Permalink
Add x/community Lend proposals (#1425)
Browse files Browse the repository at this point in the history
* implement & register x/community lend proposals

* register proposals in x/community codec

* allow x/community macc to receive funds

* init lend from genesis in proposal tests

* test CommunityLendWithdrawProposal

* helpful comment on x/community keeper deps

* use RouteKey in module.go
  • Loading branch information
pirtleshell committed Dec 13, 2022
1 parent a7f3b77 commit 7c58fb5
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Output of the go coverage tool
*.out
cover.html

# Exclude build files
vendor
Expand Down
52 changes: 32 additions & 20 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,20 +508,6 @@ func NewApp(

app.evmutilKeeper.SetEvmKeeper(app.evmKeeper)

app.communityKeeper = communitykeeper.NewKeeper(
app.accountKeeper,
app.bankKeeper,
)
app.kavadistKeeper = kavadistkeeper.NewKeeper(
appCodec,
keys[kavadisttypes.StoreKey],
kavadistSubspace,
app.bankKeeper,
app.accountKeeper,
app.communityKeeper,
app.loadBlockedMaccAddrs(),
)

app.transferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
Expand Down Expand Up @@ -620,6 +606,23 @@ func NewApp(
&savingsKeeper,
communitytypes.ModuleAccountName,
)

// x/community's deposit/withdraw to lend proposals depend on hard keeper.
app.communityKeeper = communitykeeper.NewKeeper(
app.accountKeeper,
app.bankKeeper,
hardKeeper,
)
app.kavadistKeeper = kavadistkeeper.NewKeeper(
appCodec,
keys[kavadisttypes.StoreKey],
kavadistSubspace,
app.bankKeeper,
app.accountKeeper,
app.communityKeeper,
app.loadBlockedMaccAddrs(),
)

app.kavamintKeeper = kavamintkeeper.NewKeeper(
appCodec,
keys[kavaminttypes.StoreKey],
Expand Down Expand Up @@ -694,6 +697,7 @@ func NewApp(
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper)).
AddRoute(kavadisttypes.RouterKey, kavadist.NewCommunityPoolMultiSpendProposalHandler(app.kavadistKeeper)).
AddRoute(earntypes.RouterKey, earn.NewCommunityPoolProposalHandler(app.earnKeeper)).
AddRoute(communitytypes.RouterKey, community.NewCommunityPoolProposalHandler(app.communityKeeper)).
AddRoute(committeetypes.RouterKey, committee.NewProposalHandler(app.committeeKeeper))
app.govKeeper = govkeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -1046,14 +1050,22 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
// loadBlockedMaccAddrs returns a map indicating the blocked status of each module account address
func (app *App) loadBlockedMaccAddrs() map[string]bool {
modAccAddrs := app.ModuleAccountAddrs()
kavadistMaccAddr := app.accountKeeper.GetModuleAddress(kavadisttypes.ModuleName)
earnMaccAddr := app.accountKeeper.GetModuleAddress(earntypes.ModuleName)
liquidMaccAddr := app.accountKeeper.GetModuleAddress(liquidtypes.ModuleName)
kavadistFundMaccAddr := app.accountKeeper.GetModuleAddress(kavadisttypes.FundModuleAccount)
allowedMaccs := map[string]bool{
// kavadist
app.accountKeeper.GetModuleAddress(kavadisttypes.ModuleName).String(): true,
// earn
app.accountKeeper.GetModuleAddress(earntypes.ModuleName).String(): true,
// liquid
app.accountKeeper.GetModuleAddress(liquidtypes.ModuleName).String(): true,
// kavadist fund
app.accountKeeper.GetModuleAddress(kavadisttypes.FundModuleAccount).String(): true,
// community
app.accountKeeper.GetModuleAddress(communitytypes.ModuleAccountName).String(): true,
}

for addr := range modAccAddrs {
// Set the kavadist and earn module account address as unblocked
if addr == kavadistMaccAddr.String() || addr == earnMaccAddr.String() || addr == liquidMaccAddr.String() || addr == kavadistFundMaccAddr.String() {
// Set allowed module accounts as unblocked
if allowedMaccs[addr] {
modAccAddrs[addr] = false
}
}
Expand Down
7 changes: 1 addition & 6 deletions x/community/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/kava-labs/kava/x/community"
"github.com/kava-labs/kava/x/community/testutil"
"github.com/kava-labs/kava/x/community/types"
)

type genesisTestSuite struct {
Expand All @@ -32,12 +31,8 @@ func (suite *genesisTestSuite) TestInitGenesis() {
community.InitGenesis(suite.Ctx, suite.Keeper, accountKeeper)
})

communityPoolAddress := accountKeeper.GetModuleAddress(types.ModuleAccountName)
// hello, greppers!
suite.Equal("kava17d2wax0zhjrrecvaszuyxdf5wcu5a0p4qlx3t5", communityPoolAddress.String())

// check for module account this way b/c GetModuleAccount creates if not existing.
acc := accountKeeper.GetAccount(suite.Ctx, communityPoolAddress)
acc := accountKeeper.GetAccount(suite.Ctx, suite.MaccAddress)
suite.NotNil(acc)
_, ok := acc.(authtypes.ModuleAccountI)
suite.True(ok)
Expand Down
24 changes: 24 additions & 0 deletions x/community/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package community

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/kava-labs/kava/x/community/keeper"
"github.com/kava-labs/kava/x/community/types"
)

// NewCommunityPoolProposalHandler handles x/community proposals.
func NewCommunityPoolProposalHandler(k keeper.Keeper) govtypes.Handler {
return func(ctx sdk.Context, content govtypes.Content) error {
switch c := content.(type) {
case *types.CommunityPoolLendDepositProposal:
return keeper.HandleCommunityPoolLendDepositProposal(ctx, k, c)
case *types.CommunityPoolLendWithdrawProposal:
return keeper.HandleCommunityPoolLendWithdrawProposal(ctx, k, c)
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized community proposal content type: %T", c)
}
}
}
4 changes: 3 additions & 1 deletion x/community/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
// Keeper of the community store
type Keeper struct {
bankKeeper types.BankKeeper
hardKeeper types.HardKeeper
moduleAddress sdk.AccAddress
}

// NewKeeper creates a new community Keeper instance
func NewKeeper(ak types.AccountKeeper, bk types.BankKeeper) Keeper {
func NewKeeper(ak types.AccountKeeper, bk types.BankKeeper, hk types.HardKeeper) Keeper {
// ensure community module account is set
addr := ak.GetModuleAddress(types.ModuleAccountName)
if addr == nil {
Expand All @@ -25,6 +26,7 @@ func NewKeeper(ak types.AccountKeeper, bk types.BankKeeper) Keeper {

return Keeper{
bankKeeper: bk,
hardKeeper: hk,
moduleAddress: addr,
}
}
Expand Down
17 changes: 17 additions & 0 deletions x/community/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/kava-labs/kava/x/community/types"
)

// HandleCommunityPoolLendDepositProposal is a handler for executing a passed community pool lend deposit proposal.
func HandleCommunityPoolLendDepositProposal(ctx sdk.Context, k Keeper, p *types.CommunityPoolLendDepositProposal) error {
return k.hardKeeper.Deposit(ctx, k.moduleAddress, p.Amount)
}

// HandleCommunityPoolLendWithdrawProposal is a handler for executing a passed community pool lend withdraw proposal.
func HandleCommunityPoolLendWithdrawProposal(ctx sdk.Context, k Keeper, p *types.CommunityPoolLendWithdrawProposal) error {
return k.hardKeeper.Withdraw(ctx, k.moduleAddress, p.Amount)
}
Loading

0 comments on commit 7c58fb5

Please sign in to comment.