-
Notifications
You must be signed in to change notification settings - Fork 50
/
handler.go
62 lines (53 loc) · 2.25 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
package auth
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/certikfoundation/shentu/common"
"github.com/certikfoundation/shentu/x/auth/types"
"github.com/certikfoundation/shentu/x/auth/vesting"
)
// NewHandler returns a handler for "auth" type messages.
func NewHandler(ak AccountKeeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
switch msg := msg.(type) {
case types.MsgUnlock:
return handleMsgUnlock(ctx, ak, msg)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "Unrecognized cert Msg type: %v", msg.Type())
}
}
}
func handleMsgUnlock(ctx sdk.Context, ak AccountKeeper, msg types.MsgUnlock) (*sdk.Result, error) {
// preliminary checks
acc := ak.GetAccount(ctx, msg.Account)
if acc == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", msg.Account)
}
mvacc, ok := acc.(*vesting.ManualVestingAccount)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "receiver account is not a manual vesting account")
}
if !msg.Issuer.Equals(mvacc.Unlocker) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "sender of this transaction is not the designated unlocker")
}
if mvacc.VestedCoins.Add(msg.UnlockAmount...).IsAnyGT(mvacc.OriginalVesting) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "cannot unlock more than the original vesting amount")
}
// update vested coins
mvacc.VestedCoins = mvacc.VestedCoins.Add(msg.UnlockAmount...)
if ctx.BlockHeight() >= common.Update1Height && mvacc.DelegatedVesting.IsAllGT(mvacc.OriginalVesting.Sub(mvacc.VestedCoins)) {
unlockedDelegated := mvacc.DelegatedVesting.Sub(mvacc.OriginalVesting.Sub(mvacc.VestedCoins))
mvacc.DelegatedVesting = mvacc.DelegatedVesting.Sub(unlockedDelegated)
mvacc.DelegatedFree = mvacc.DelegatedFree.Add(unlockedDelegated...)
}
ak.SetAccount(ctx, mvacc)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeUnlock,
sdk.NewAttribute("unlocker", msg.Issuer.String()),
sdk.NewAttribute("account", msg.Account.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.UnlockAmount.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}