2018-01-12 11:49:53 -08:00
|
|
|
package bank
|
2018-01-03 17:20:21 -08:00
|
|
|
|
|
|
|
import (
|
2019-04-15 09:25:04 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-14 07:10:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
|
2018-01-03 17:20:21 -08:00
|
|
|
)
|
|
|
|
|
2018-03-27 10:42:09 -07:00
|
|
|
// NewHandler returns a handler for "bank" type messages.
|
2019-06-14 07:10:37 -07:00
|
|
|
func NewHandler(k keeper.Keeper) sdk.Handler {
|
2018-03-27 10:42:09 -07:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
2019-06-26 09:03:25 -07:00
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
|
|
|
2018-03-27 10:42:09 -07:00
|
|
|
switch msg := msg.(type) {
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.MsgSend:
|
2018-04-18 21:49:24 -07:00
|
|
|
return handleMsgSend(ctx, k, msg)
|
2019-04-15 09:25:04 -07:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
case types.MsgMultiSend:
|
2019-02-04 15:58:02 -08:00
|
|
|
return handleMsgMultiSend(ctx, k, msg)
|
2019-04-15 09:25:04 -07:00
|
|
|
|
2018-03-27 10:42:09 -07:00
|
|
|
default:
|
2019-04-15 09:25:04 -07:00
|
|
|
errMsg := fmt.Sprintf("unrecognized bank message type: %T", msg)
|
2018-03-27 10:42:09 -07:00
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
|
|
|
}
|
2018-01-17 16:59:40 -08:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 17:20:21 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// Handle MsgSend.
|
2019-06-14 07:10:37 -07:00
|
|
|
func handleMsgSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgSend) sdk.Result {
|
2019-02-04 15:58:02 -08:00
|
|
|
if !k.GetSendEnabled(ctx) {
|
2019-06-05 16:26:17 -07:00
|
|
|
return types.ErrSendDisabled(k.Codespace()).Result()
|
2019-02-04 15:58:02 -08:00
|
|
|
}
|
2019-06-26 09:03:25 -07:00
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
if k.BlacklistedAddr(msg.ToAddress) {
|
|
|
|
return sdk.ErrUnauthorized(fmt.Sprintf("%s is not allowed to receive transactions", msg.ToAddress)).Result()
|
|
|
|
}
|
|
|
|
|
2019-04-09 05:55:01 -07:00
|
|
|
err := k.SendCoins(ctx, msg.FromAddress, msg.ToAddress, msg.Amount)
|
2019-02-04 15:58:02 -08:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
),
|
2019-04-09 05:55:01 -07:00
|
|
|
)
|
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
return sdk.Result{Events: ctx.EventManager().Events()}
|
2019-02-04 15:58:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle MsgMultiSend.
|
2019-06-14 07:10:37 -07:00
|
|
|
func handleMsgMultiSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgMultiSend) sdk.Result {
|
2018-01-17 16:59:40 -08:00
|
|
|
// NOTE: totalIn == totalOut should already have been checked
|
2019-01-28 19:06:48 -08:00
|
|
|
if !k.GetSendEnabled(ctx) {
|
2019-06-05 16:26:17 -07:00
|
|
|
return types.ErrSendDisabled(k.Codespace()).Result()
|
2019-01-28 19:06:48 -08:00
|
|
|
}
|
2019-06-26 09:03:25 -07:00
|
|
|
|
2019-07-31 08:39:02 -07:00
|
|
|
for _, out := range msg.Outputs {
|
|
|
|
if k.BlacklistedAddr(out.Address) {
|
|
|
|
return sdk.ErrUnauthorized(fmt.Sprintf("%s is not allowed to receive transactions", out.Address)).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs)
|
2018-03-27 10:42:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|
2018-01-17 16:59:40 -08:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
ctx.EventManager().EmitEvent(
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeMessage,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return sdk.Result{Events: ctx.EventManager().Events()}
|
2018-01-17 16:59:40 -08:00
|
|
|
}
|