/
hooks.go
228 lines (190 loc) · 8.78 KB
/
hooks.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package keeper
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/v11/utils"
epochstypes "github.com/Stride-Labs/stride/v11/x/epochs/types"
recordstypes "github.com/Stride-Labs/stride/v11/x/records/types"
"github.com/Stride-Labs/stride/v11/x/stakeibc/types"
)
func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
// Update the stakeibc epoch tracker
epochNumber, err := k.UpdateEpochTracker(ctx, epochInfo)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Unable to update epoch tracker, err: %s", err.Error()))
return
}
// Day Epoch - Process Unbondings
if epochInfo.Identifier == epochstypes.DAY_EPOCH {
// Initiate unbondings from any hostZone where it's appropriate
k.InitiateAllHostZoneUnbondings(ctx, epochNumber)
// Check previous epochs to see if unbondings finished, and sweep the tokens if so
k.SweepAllUnbondedTokens(ctx)
// Cleanup any records that are no longer needed
k.CleanupEpochUnbondingRecords(ctx, epochNumber)
// Create an empty unbonding record for this epoch
k.CreateEpochUnbondingRecord(ctx, epochNumber)
}
// Stride Epoch - Process Deposits and Delegations
if epochInfo.Identifier == epochstypes.STRIDE_EPOCH {
// Get cadence intervals
redemptionRateInterval := k.GetParam(ctx, types.KeyRedemptionRateInterval)
depositInterval := k.GetParam(ctx, types.KeyDepositInterval)
delegationInterval := k.GetParam(ctx, types.KeyDelegateInterval)
reinvestInterval := k.GetParam(ctx, types.KeyReinvestInterval)
// Create a new deposit record for each host zone and the grab all deposit records
k.CreateDepositRecordsForEpoch(ctx, epochNumber)
depositRecords := k.RecordsKeeper.GetAllDepositRecord(ctx)
// TODO: move this to an external function that anyone can call, so that we don't have to call it every epoch
k.SetWithdrawalAddress(ctx)
// Update the redemption rate
if epochNumber%redemptionRateInterval == 0 {
k.UpdateRedemptionRates(ctx, depositRecords)
}
// Transfer deposited funds from the controller account to the delegation account on the host zone
if epochNumber%depositInterval == 0 {
k.TransferExistingDepositsToHostZones(ctx, epochNumber, depositRecords)
}
// Delegate tokens from the delegation account
if epochNumber%delegationInterval == 0 {
k.StakeExistingDepositsOnHostZones(ctx, epochNumber, depositRecords)
}
// Reinvest staking rewards
if epochNumber%reinvestInterval == 0 { // allow a few blocks from UpdateUndelegatedBal to avoid conflicts
k.ReinvestRewards(ctx)
}
}
if epochInfo.Identifier == epochstypes.MINT_EPOCH {
k.AllocateHostZoneReward(ctx)
}
}
func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {}
// Hooks wrapper struct for incentives keeper
type Hooks struct {
k Keeper
}
var _ epochstypes.EpochHooks = Hooks{}
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// epochs hooks
func (h Hooks) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
h.k.BeforeEpochStart(ctx, epochInfo)
}
func (h Hooks) AfterEpochEnd(ctx sdk.Context, epochInfo epochstypes.EpochInfo) {
h.k.AfterEpochEnd(ctx, epochInfo)
}
// Update the epoch information in the stakeibc epoch tracker
func (k Keeper) UpdateEpochTracker(ctx sdk.Context, epochInfo epochstypes.EpochInfo) (epochNumber uint64, err error) {
epochNumber, err = cast.ToUint64E(epochInfo.CurrentEpoch)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch number to uint64: %v", err))
return 0, err
}
epochDurationNano, err := cast.ToUint64E(epochInfo.Duration.Nanoseconds())
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch duration to uint64: %v", err))
return 0, err
}
nextEpochStartTime, err := cast.ToUint64E(epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration).UnixNano())
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch duration to uint64: %v", err))
return 0, err
}
epochTracker := types.EpochTracker{
EpochIdentifier: epochInfo.Identifier,
EpochNumber: epochNumber,
Duration: epochDurationNano,
NextEpochStartTime: nextEpochStartTime,
}
k.SetEpochTracker(ctx, epochTracker)
return epochNumber, nil
}
// Set the withdrawal account address for each host zone
func (k Keeper) SetWithdrawalAddress(ctx sdk.Context) {
k.Logger(ctx).Info("Setting Withdrawal Addresses...")
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
err := k.SetWithdrawalAddressOnHost(ctx, hostZone)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Unable to set withdrawal address on %s, err: %s", hostZone.ChainId, err))
}
}
}
// Updates the redemption rate for each host zone
// The redemption rate equation is:
//
// (Unbonded Balance + Staked Balance + Module Account Balance) / (stToken Supply)
func (k Keeper) UpdateRedemptionRates(ctx sdk.Context, depositRecords []recordstypes.DepositRecord) {
k.Logger(ctx).Info("Updating Redemption Rates...")
// Update the redemption rate for each host zone
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
// Gather redemption rate components
stSupply := k.bankKeeper.GetSupply(ctx, types.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)).Amount
if stSupply.IsZero() {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "No st%s in circulation - redemption rate is unchanged", hostZone.HostDenom))
continue
}
undelegatedBalance := k.GetUndelegatedBalance(hostZone, depositRecords)
stakedBalance := hostZone.StakedBal
moduleAcctBalance := k.GetModuleAccountBalance(hostZone, depositRecords)
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId,
"Redemption Rate Components - Undelegated Balance: %v, Staked Balance: %v, Module Account Balance: %v, stToken Supply: %v",
undelegatedBalance, stakedBalance, moduleAcctBalance, stSupply))
// Calculate the redemption rate
redemptionRate := (sdk.NewDecFromInt(undelegatedBalance).Add(sdk.NewDecFromInt(stakedBalance)).Add(sdk.NewDecFromInt(moduleAcctBalance))).Quo(sdk.NewDecFromInt(stSupply))
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "New Redemption Rate: %v (vs Prev Rate: %v)", redemptionRate, hostZone.RedemptionRate))
// Update the host zone
hostZone.LastRedemptionRate = hostZone.RedemptionRate
hostZone.RedemptionRate = redemptionRate
k.SetHostZone(ctx, hostZone)
}
}
func (k Keeper) GetUndelegatedBalance(hostZone types.HostZone, depositRecords []recordstypes.DepositRecord) sdkmath.Int {
// filter to only the deposit records for the host zone with status DELEGATION_QUEUE
UndelegatedDepositRecords := utils.FilterDepositRecords(depositRecords, func(record recordstypes.DepositRecord) (condition bool) {
return ((record.Status == recordstypes.DepositRecord_DELEGATION_QUEUE || record.Status == recordstypes.DepositRecord_DELEGATION_IN_PROGRESS) && record.HostZoneId == hostZone.ChainId)
})
// sum the amounts of the deposit records
totalAmount := sdkmath.ZeroInt()
for _, depositRecord := range UndelegatedDepositRecords {
totalAmount = totalAmount.Add(depositRecord.Amount)
}
return totalAmount
}
func (k Keeper) GetModuleAccountBalance(hostZone types.HostZone, depositRecords []recordstypes.DepositRecord) sdkmath.Int {
// filter to only the deposit records for the host zone with status DELEGATION
ModuleAccountRecords := utils.FilterDepositRecords(depositRecords, func(record recordstypes.DepositRecord) (condition bool) {
return (record.Status == recordstypes.DepositRecord_TRANSFER_QUEUE || record.Status == recordstypes.DepositRecord_TRANSFER_IN_PROGRESS) && record.HostZoneId == hostZone.ChainId
})
// sum the amounts of the deposit records
totalAmount := sdkmath.ZeroInt()
for _, depositRecord := range ModuleAccountRecords {
totalAmount = totalAmount.Add(depositRecord.Amount)
}
return totalAmount
}
func (k Keeper) ReinvestRewards(ctx sdk.Context) {
k.Logger(ctx).Info("Reinvesting tokens...")
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
// only process host zones once withdrawal accounts are registered
withdrawalAccount := hostZone.WithdrawalAccount
if withdrawalAccount == nil || withdrawalAccount.Address == "" {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Withdrawal account not registered for host zone"))
continue
}
// read clock time on host zone
blockTime, err := k.GetLightClientTimeSafely(ctx, hostZone.ConnectionId)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not find blockTime for host zone %s, err: %s", hostZone.ConnectionId, err.Error()))
continue
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "BlockTime for host zone: %d", blockTime))
err = k.UpdateWithdrawalBalance(ctx, hostZone)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error updating withdrawal balance for host zone %s: %s", hostZone.ConnectionId, err.Error()))
continue
}
}
}