-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
74 lines (59 loc) · 2 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
package tokenlock
import (
"fmt"
"github.com/sunnya97/cosmos-sdk-modules/x/tokenlock/tags"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewHandler creates a new handler for tokenlock module
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgLockCoins:
return handleMsgLockCoins(ctx, keeper, msg)
case MsgUnlockCoins:
return handleMsgUnlockCoins(ctx, keeper, msg)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", RouterKey, msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
func handleMsgLockCoins(ctx sdk.Context, keeper Keeper, msg MsgLockCoins) sdk.Result {
err := keeper.LockCoins(ctx, msg.Owner, msg.UnlockTime, msg.Amount)
if err != nil {
return err.Result()
}
return sdk.Result{}
}
func handleMsgUnlockCoins(ctx sdk.Context, keeper Keeper, msg MsgUnlockCoins) sdk.Result {
err := keeper.BeginUnlock(ctx, msg.Owner, msg.UnlockTime, msg.Amount)
if err != nil {
return err.Result()
}
return sdk.Result{
Tags: sdk.NewTags(tags.Sender, msg.Owner, tags.Category, tags.TxCategory, tags.Action, tags.ActionTokenUnlockStarted),
}
}
// Called every block, process inflation, update validator set
func EndBlocker(ctx sdk.Context, keeper Keeper) sdk.Tags {
logger := keeper.Logger(ctx)
resTags := sdk.NewTags()
iterator := keeper.UnlockQueueIterator(ctx, ctx.BlockHeader().Time)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var unlock TokenUnlock
keeper.cdc.MustUnmarshalBinaryBare(iterator.Value(), &unlock)
keeper.FinishUnlock(ctx, unlock)
store := prefix.NewStore(ctx.KVStore(keeper.storeKey), PrefixUnlockQueue)
store.Delete(iterator.Key())
resTags = resTags.AppendTag(tags.Action, tags.ActionTokenUnlockCompleted)
resTags = resTags.AppendTag(tags.Sender, unlock.Owner.String())
logger.Info(
fmt.Sprintf("unlocked %d to %s",
unlock.Amount, unlock.Owner,
),
)
}
return resTags
}