-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
81 lines (65 loc) · 2.08 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package bank
import (
"fmt"
sdk "github.com/hdac-io/friday/types"
"github.com/hdac-io/friday/x/bank/internal/keeper"
"github.com/hdac-io/friday/x/bank/internal/types"
)
// NewHandler returns a handler for "bank" type 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.MsgSend:
return handleMsgSend(ctx, k, msg)
case types.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.Keeper, msg types.MsgSend) sdk.Result {
if !k.GetSendEnabled(ctx) {
return types.ErrSendDisabled(k.Codespace()).Result()
}
if k.BlacklistedAddr(msg.ToAddress) {
return sdk.ErrUnauthorized(fmt.Sprintf("%s is not allowed to receive transactions", msg.ToAddress)).Result()
}
err := k.SendCoins(ctx, msg.FromAddress, msg.ToAddress, msg.Amount)
if err != nil {
return err.Result()
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
)
return sdk.Result{Events: ctx.EventManager().Events()}
}
// Handle MsgMultiSend.
func handleMsgMultiSend(ctx sdk.Context, k keeper.Keeper, msg types.MsgMultiSend) sdk.Result {
// NOTE: totalIn == totalOut should already have been checked
if !k.GetSendEnabled(ctx) {
return types.ErrSendDisabled(k.Codespace()).Result()
}
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()
}
}
err := k.InputOutputCoins(ctx, msg.Inputs, msg.Outputs)
if err != nil {
return err.Result()
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
)
return sdk.Result{Events: ctx.EventManager().Events()}
}