-
Notifications
You must be signed in to change notification settings - Fork 47
/
vest.go
68 lines (54 loc) · 2.08 KB
/
vest.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/commitment/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)
func (k Keeper) VestTokens(ctx sdk.Context, epochIdentifier string) error {
// Future Improvement: get all VestingTokens by denom and iterate
k.IterateCommitments(ctx, func(commitments types.Commitments) (stop bool) {
logger := k.Logger(ctx)
newVestingTokens := make([]*types.VestingTokens, 0, len(commitments.VestingTokens))
for index := len(commitments.VestingTokens) - 1; index >= 0; index-- {
vesting := commitments.VestingTokens[index]
vesting.CurrentEpoch = vesting.CurrentEpoch + 1
if vesting.NumEpochs == 0 || vesting.CurrentEpoch > vesting.NumEpochs || vesting.UnvestedAmount.IsZero() {
continue
}
epochAmount := vesting.TotalAmount.Quo(sdk.NewInt(vesting.NumEpochs))
withdrawCoins := sdk.NewCoins(sdk.NewCoin(vesting.Denom, epochAmount))
// mint coins if vesting token is ELYS
if vesting.Denom == ptypes.Elys {
err := k.bankKeeper.MintCoins(ctx, types.ModuleName, withdrawCoins)
if err != nil {
logger.Debug(
"unable to mint vested tokens for ELYS token",
"vestingtokens", vesting, commitments.Creator,
)
}
}
// Send the coins to the user's account
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.MustAccAddressFromBech32(commitments.Creator), withdrawCoins)
if err != nil {
logger.Debug(
"unable to send vested tokens",
"vestingtokens", vesting, commitments.Creator,
)
}
vesting.UnvestedAmount = vesting.UnvestedAmount.Sub(epochAmount)
}
// Remove completed vesting items.
for index := 0; index < len(commitments.VestingTokens); index++ {
vesting := commitments.VestingTokens[index]
if vesting.CurrentEpoch >= vesting.NumEpochs || vesting.UnvestedAmount.IsZero() {
continue
}
newVestingTokens = append(newVestingTokens, vesting)
}
commitments.VestingTokens = newVestingTokens
// update commitments
k.SetCommitments(ctx, commitments)
return false
})
return nil
}