-
Notifications
You must be signed in to change notification settings - Fork 130
/
handler.go
141 lines (108 loc) · 5.45 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package v174
import (
"fmt"
"time"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
liquidvestingkeeper "github.com/haqq-network/haqq/x/liquidvesting/keeper"
liquidvestingtypes "github.com/haqq-network/haqq/x/liquidvesting/types"
vestingtypes "github.com/haqq-network/haqq/x/vesting/types"
)
func StretchLockupScheduleForAccounts(ctx sdk.Context, ak authkeeper.AccountKeeper, stretchLength int64, lockupLengthThreshold time.Time) error {
logger := ctx.Logger()
logger.Info("Stretching lockup schedules for accounts: ")
// Iterate all accounts
ak.IterateAccounts(ctx, func(acc authtypes.AccountI) (stop bool) {
// Check if acc is vesting account
vacc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
if !ok {
return false
}
// if end time for unlock account is after 2026-01-01 modify schedule
if time.Unix(vacc.GetEndTime(), 0).After(lockupLengthThreshold) {
upcomingPeriods := liquidvestingtypes.ExtractUpcomingPeriods(vacc.GetStartTime(), vacc.GetEndTime(), vacc.LockupPeriods, ctx.BlockTime().Unix())
pastPeriods := liquidvestingtypes.ExtractPastPeriods(vacc.GetStartTime(), vacc.GetEndTime(), vacc.LockupPeriods, ctx.BlockTime().Unix())
// stretched upcoming periods
stretchedUpcomingPeriods := stretchPeriods(ctx, upcomingPeriods, stretchLength, "account", vacc.GetAddress().String())
// add 1095 days (three years to the end time)
oldEndTime := vacc.GetEndTime()
newEndTime := oldEndTime + OneDayInSeconds*stretchLength
vacc.EndTime = newEndTime
// set stretched lockup periods
newLookupPeriods := make(sdkvesting.Periods, 0, len(pastPeriods)+len(stretchedUpcomingPeriods))
newLookupPeriods = append(newLookupPeriods, pastPeriods...)
newLookupPeriods = append(newLookupPeriods, stretchedUpcomingPeriods...)
vacc.LockupPeriods = newLookupPeriods
ak.SetAccount(ctx, vacc)
logger.Info(fmt.Sprintf(" > account: %s — from %d to %d", vacc.GetAddress().String(), oldEndTime, newEndTime))
}
return false
})
return nil
}
func StretchLockupScheduleForLiquidVestingTokens(ctx sdk.Context, lk liquidvestingkeeper.Keeper, stretchLength int64, lockupLengthThreshold time.Time) error {
logger := ctx.Logger()
logger.Info("Stretching lockup schedules for liquid vesting tokens: ")
// Iterate all denoms
lk.IterateDenoms(ctx, func(denom liquidvestingtypes.Denom) (stop bool) {
// if end time for liquid denom is after 2026-01-01 modify schedule
if denom.EndTime.After(lockupLengthThreshold) {
upcomingPeriods := liquidvestingtypes.ExtractUpcomingPeriods(denom.StartTime.Unix(), denom.EndTime.Unix(), denom.LockupPeriods, ctx.BlockTime().Unix())
pastPeriods := liquidvestingtypes.ExtractPastPeriods(denom.StartTime.Unix(), denom.EndTime.Unix(), denom.LockupPeriods, ctx.BlockTime().Unix())
// stretched upcoming periods
stretchedUpcomingPeriods := stretchPeriods(ctx, upcomingPeriods, stretchLength, "liquid", denom.DisplayDenom)
// add 1095 days (three years to the end time)
oldEndTime := denom.EndTime.Unix()
newEndTime := oldEndTime + OneDayInSeconds*stretchLength
denom.EndTime = time.Unix(newEndTime, 0)
// set stretched lockup periods
newLookupPeriods := make(sdkvesting.Periods, 0, len(pastPeriods)+len(stretchedUpcomingPeriods))
newLookupPeriods = append(newLookupPeriods, pastPeriods...)
newLookupPeriods = append(newLookupPeriods, stretchedUpcomingPeriods...)
denom.LockupPeriods = newLookupPeriods
lk.SetDenom(ctx, denom)
logger.Info(fmt.Sprintf(" > denom: %s — from %d to %d", denom.DisplayDenom, oldEndTime, newEndTime))
}
return false
})
return nil
}
func stretchPeriods(ctx sdk.Context, periods sdkvesting.Periods, stretchDays int64, entityType string, entity string) sdkvesting.Periods {
const Denom = "aISLM"
logger := ctx.Logger()
periodsLengthInDays := periods.TotalLength() / OneDayInSeconds
totalAmount := periods.TotalAmount().AmountOf(Denom)
logger.Info(fmt.Sprintf(" > type: %s entity: %s — amount: %d", entityType, entity, totalAmount))
stretchedPerDayLockupAmount := totalAmount.Quo(sdkmath.NewInt(periodsLengthInDays + stretchDays))
extraLengthAmount := stretchedPerDayLockupAmount.Mul(sdkmath.NewInt(stretchDays))
// update amount of existing periods
updatedPeriods := make(sdkvesting.Periods, len(periods))
copy(updatedPeriods, periods)
totalUpdatedAmount := totalAmount.Sub(extraLengthAmount)
for _, period := range updatedPeriods {
periodAmount := period.Amount.AmountOf(Denom)
newAmount := periodAmount.Mul(totalUpdatedAmount).Quo(totalAmount)
for i, coin := range period.Amount {
if coin.Denom == Denom {
period.Amount[i] = sdk.NewCoin(Denom, newAmount)
}
}
}
// allocate extra periods
extraPeriods := make(sdkvesting.Periods, stretchDays)
for i := range extraPeriods {
extraPeriods[i] = sdkvesting.Period{
Length: OneDayInSeconds,
Amount: sdk.NewCoins(sdk.NewCoin(Denom, stretchedPerDayLockupAmount)),
}
}
// calculate total remainder and add it to the last period of the stretched periods
updatedPeriodsAmount := updatedPeriods.TotalAmount().AmountOf(Denom)
extraPeriodsAmount := extraPeriods.TotalAmount().AmountOf(Denom)
calculationDiff := totalAmount.Sub(updatedPeriodsAmount.Add(extraPeriodsAmount))
extraPeriods[stretchDays-1].Amount.Add(sdk.NewCoin(Denom, calculationDiff))
return append(updatedPeriods, extraPeriods...)
}