0g-chain/x/committee/handler.go
rhuairahrighairigh 20bcfec407 add msgs
2020-03-12 00:47:25 +00:00

74 lines
2.0 KiB
Go

package committee
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"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 {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgSubmitProposal:
return handleMsgSubmitProposal(ctx, k, msg)
case types.MsgVote:
return handleMsgVote(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s msg type: %T", types.ModuleName, msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgSubmitProposal(ctx sdk.Context, k keeper.Keeper, msg types.MsgSubmitProposal) sdk.Result {
proposalID, err := k.SubmitProposal(ctx, msg.Proposer, msg.CommitteeID, msg.PubProposal)
if err != nil {
return err.Result()
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
// TODO sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Proposer.String()),
),
)
return sdk.Result{
Data: GetKeyFromID(proposalID),
Events: ctx.EventManager().Events(),
}
}
func handleMsgVote(ctx sdk.Context, k keeper.Keeper, msg types.MsgVote) sdk.Result {
err := k.AddVote(ctx, msg.ProposalID, msg.Voter)
if err != nil {
return err.Result()
}
// Try closing proposal in case enough votes have been cast
_ = k.CloseOutProposal(ctx, msg.ProposalID)
// if err.Error() == "note enough votes to close proposal" { // TODO
// return nil // This is not a reason to error
// }
// if err != nil {
// return err
// }
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
// TODO sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Voter.String()),
),
)
return sdk.Result{Events: ctx.EventManager().Events()}
}