2020-03-04 19:16:27 +00:00
|
|
|
package committee
|
|
|
|
|
|
|
|
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-04 19:16:27 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/committee/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewHandler creates an sdk.Handler for committee messages
|
|
|
|
func NewHandler(k keeper.Keeper) sdk.Handler {
|
2020-04-27 18:19:05 +00:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
2020-03-12 00:47:25 +00:00
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
|
|
|
2020-03-04 19:16:27 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case types.MsgSubmitProposal:
|
2020-03-12 00:47:25 +00:00
|
|
|
return handleMsgSubmitProposal(ctx, k, msg)
|
2020-03-04 19:16:27 +00:00
|
|
|
case types.MsgVote:
|
2020-03-12 00:47:25 +00:00
|
|
|
return handleMsgVote(ctx, k, msg)
|
2020-03-04 19:16:27 +00:00
|
|
|
default:
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", ModuleName, msg)
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitProposal) (*sdk.Result, error) {
|
2020-03-12 00:47:25 +00:00
|
|
|
proposalID, err := k.SubmitProposal(ctx, msg.Proposer, msg.CommitteeID, msg.PubProposal)
|
2020-03-04 19:16:27 +00:00
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, err
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 00:47:25 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
2020-03-27 20:28:51 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
2020-03-12 00:47:25 +00:00
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Proposer.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
return &sdk.Result{
|
2020-03-12 00:47:25 +00:00
|
|
|
Data: GetKeyFromID(proposalID),
|
|
|
|
Events: ctx.EventManager().Events(),
|
2020-04-27 18:19:05 +00:00
|
|
|
}, nil
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) (*sdk.Result, error) {
|
2020-03-12 00:47:25 +00:00
|
|
|
err := k.AddVote(ctx, msg.ProposalID, msg.Voter)
|
|
|
|
if err != nil {
|
2020-04-27 18:19:05 +00:00
|
|
|
return nil, err
|
2020-03-12 00:47:25 +00:00
|
|
|
}
|
2020-05-15 19:25:49 +00:00
|
|
|
|
2020-04-28 00:51:53 +00:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter.String()),
|
|
|
|
),
|
|
|
|
)
|
2020-03-11 19:27:36 +00:00
|
|
|
|
2020-04-27 18:19:05 +00:00
|
|
|
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
|
2020-03-04 19:16:27 +00:00
|
|
|
}
|