2020-03-04 19:16:27 +00:00
|
|
|
package committee
|
2020-03-04 16:55:12 +00:00
|
|
|
|
2020-03-21 18:06:58 +00:00
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-27 18:19:05 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-03-21 18:06:58 +00:00
|
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
2021-06-07 16:08:03 +00:00
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
2020-03-21 18:06:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewProposalHandler(k Keeper) govtypes.Handler {
|
2020-04-27 18:19:05 +00:00
|
|
|
return func(ctx sdk.Context, content govtypes.Content) error {
|
2020-03-21 18:06:58 +00:00
|
|
|
switch c := content.(type) {
|
|
|
|
case CommitteeChangeProposal:
|
|
|
|
return handleCommitteeChangeProposal(ctx, k, c)
|
|
|
|
case CommitteeDeleteProposal:
|
|
|
|
return handleCommitteeDeleteProposal(ctx, k, c)
|
|
|
|
|
|
|
|
default:
|
2020-04-27 18:19:05 +00:00
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s proposal content type: %T", ModuleName, c)
|
2020-03-21 18:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleCommitteeChangeProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeChangeProposal) error {
|
2020-03-21 18:06:58 +00:00
|
|
|
if err := committeeProposal.ValidateBasic(); err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return sdkerrors.Wrap(ErrInvalidPubProposal, err.Error())
|
2020-03-21 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all committee's ongoing proposals
|
2021-06-07 16:08:03 +00:00
|
|
|
proposals := k.GetProposalsByCommittee(ctx, committeeProposal.NewCommittee.GetID())
|
2020-05-15 19:25:49 +00:00
|
|
|
for _, p := range proposals {
|
2021-06-07 16:08:03 +00:00
|
|
|
k.CloseProposal(ctx, p, types.Failed)
|
2020-05-15 19:25:49 +00:00
|
|
|
}
|
2020-03-21 18:06:58 +00:00
|
|
|
|
|
|
|
// update/create the committee
|
|
|
|
k.SetCommittee(ctx, committeeProposal.NewCommittee)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleCommitteeDeleteProposal(ctx sdk.Context, k Keeper, committeeProposal CommitteeDeleteProposal) error {
|
2020-03-21 18:06:58 +00:00
|
|
|
if err := committeeProposal.ValidateBasic(); err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return sdkerrors.Wrap(ErrInvalidPubProposal, err.Error())
|
2020-03-21 18:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all committee's ongoing proposals
|
2020-05-15 19:25:49 +00:00
|
|
|
proposals := k.GetProposalsByCommittee(ctx, committeeProposal.CommitteeID)
|
|
|
|
for _, p := range proposals {
|
2021-06-07 16:08:03 +00:00
|
|
|
k.CloseProposal(ctx, p, types.Failed)
|
2020-05-15 19:25:49 +00:00
|
|
|
}
|
2020-03-21 18:06:58 +00:00
|
|
|
|
|
|
|
k.DeleteCommittee(ctx, committeeProposal.CommitteeID)
|
|
|
|
return nil
|
|
|
|
}
|