cosmos-sdk/x/bank/handler.go

64 lines
1.5 KiB
Go

package bank
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/tags"
)
// NewHandler returns a handler for "bank" type messages.
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgSend:
return handleMsgSend(ctx, k, msg)
case MsgMultiSend:
return handleMsgMultiSend(ctx, k, msg)
default:
errMsg := fmt.Sprintf("unrecognized bank message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
// Handle MsgSend.
func handleMsgSend(ctx sdk.Context, k Keeper, msg MsgSend) sdk.Result {
if !k.GetSendEnabled(ctx) {
return ErrSendDisabled(k.Codespace()).Result()
}
err := k.SendCoins(ctx, msg.FromAddress, msg.ToAddress, msg.Amount)
if err != nil {
return err.Result()
}
resTags := sdk.NewTags(
tags.Category, tags.TxCategory,
tags.Sender, msg.FromAddress.String(),
tags.Recipient, msg.ToAddress.String(),
)
return sdk.Result{
Tags: resTags,
}
}
// Handle MsgMultiSend.
func handleMsgMultiSend(ctx sdk.Context, k Keeper, msg MsgMultiSend) sdk.Result {
// NOTE: totalIn == totalOut should already have been checked
if !k.GetSendEnabled(ctx) {
return ErrSendDisabled(k.Codespace()).Result()
}
resTags, err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs)
if err != nil {
return err.Result()
}
resTags = resTags.AppendTag(tags.Category, tags.TxCategory)
return sdk.Result{
Tags: resTags,
}
}