-
Notifications
You must be signed in to change notification settings - Fork 7
/
endblocker.go
76 lines (68 loc) · 2.13 KB
/
endblocker.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
package authx
import (
sdk "github.com/cosmos/cosmos-sdk/types"
dex "github.com/coinexchain/cet-sdk/types"
)
func EndBlocker(ctx sdk.Context, aux AccountXKeeper, keeper ExpectedAccountKeeper, tk ExpectedTokenKeeper) {
currentTime := ctx.BlockHeader().Time.Unix()
iterator := aux.UnlockedCoinsQueueIterator(ctx, currentTime)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
addr := iterator.Value()
if addr != nil {
acc, ok := aux.GetAccountX(ctx, addr)
if !ok {
//always account exist
continue
}
withdrawUnlockedCoins(&acc, currentTime, ctx, aux, keeper, tk)
aux.RemoveFromUnlockedCoinsQueueByKey(ctx, iterator.Key())
}
}
}
type NotificationUnlock struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Unlocked sdk.Coins `json:"unlocked"`
LockedCoins LockedCoins `json:"locked_coins"`
FrozenCoins sdk.Coins `json:"frozen_coins"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
Height int64 `json:"height"`
}
func withdrawUnlockedCoins(accx *AccountX, time int64, ctx sdk.Context, kx AccountXKeeper, keeper ExpectedAccountKeeper, tk ExpectedTokenKeeper) {
var unlocked = sdk.Coins{}
var stillLocked LockedCoins
for _, c := range accx.LockedCoins {
if c.UnlockTime <= time {
unlocked = unlocked.Add(sdk.Coins{c.Coin})
} else {
stillLocked = append(stillLocked, c)
}
}
unlocked = unlocked.Sort()
acc := keeper.GetAccount(ctx, accx.Address)
newCoins := acc.GetCoins().Add(unlocked)
_ = acc.SetCoins(newCoins)
keeper.SetAccount(ctx, acc)
for _, coin := range unlocked {
_ = tk.UpdateTokenSendLock(ctx, coin.Denom, coin.Amount, false)
}
accx.LockedCoins = stillLocked
kx.SetAccountX(ctx, *accx)
if len(kx.EventTypeMsgQueue) != 0 {
notifyUnlock := NotificationUnlock{
Address: accx.Address,
Unlocked: unlocked,
LockedCoins: accx.LockedCoins,
FrozenCoins: accx.FrozenCoins,
Coins: newCoins,
Height: ctx.BlockHeight(),
}
bytes := dex.SafeJSONMarshal(notifyUnlock)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
kx.EventTypeMsgQueue,
sdk.NewAttribute("notify_unlock", string(bytes)),
),
)
}
}