/
records.go
84 lines (69 loc) · 3.19 KB
/
records.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
package keeper
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/v19/utils"
recordstypes "github.com/Stride-Labs/stride/v19/x/records/types"
)
// Create a new deposit record for each host zone for the given epoch
func (k Keeper) CreateDepositRecordsForEpoch(ctx sdk.Context, epochNumber uint64) {
k.Logger(ctx).Info(fmt.Sprintf("Creating Deposit Records for Epoch %d", epochNumber))
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Creating Deposit Record"))
depositRecord := recordstypes.DepositRecord{
Amount: sdkmath.ZeroInt(),
Denom: hostZone.HostDenom,
HostZoneId: hostZone.ChainId,
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: epochNumber,
}
k.RecordsKeeper.AppendDepositRecord(ctx, depositRecord)
}
}
// Creates a new epoch unbonding record for the epoch
func (k Keeper) CreateEpochUnbondingRecord(ctx sdk.Context, epochNumber uint64) bool {
k.Logger(ctx).Info(fmt.Sprintf("Creating Epoch Unbonding Records for Epoch %d", epochNumber))
hostZoneUnbondings := []*recordstypes.HostZoneUnbonding{}
for _, hostZone := range k.GetAllActiveHostZone(ctx) {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Creating Epoch Unbonding Record"))
hostZoneUnbonding := recordstypes.HostZoneUnbonding{
NativeTokenAmount: sdkmath.ZeroInt(),
StTokenAmount: sdkmath.ZeroInt(),
Denom: hostZone.HostDenom,
HostZoneId: hostZone.ChainId,
Status: recordstypes.HostZoneUnbonding_UNBONDING_QUEUE,
}
hostZoneUnbondings = append(hostZoneUnbondings, &hostZoneUnbonding)
}
epochUnbondingRecord := recordstypes.EpochUnbondingRecord{
EpochNumber: cast.ToUint64(epochNumber),
HostZoneUnbondings: hostZoneUnbondings,
}
k.RecordsKeeper.SetEpochUnbondingRecord(ctx, epochUnbondingRecord)
return true
}
// Deletes any epoch unbonding records that have had all unbondings claimed
func (k Keeper) CleanupEpochUnbondingRecords(ctx sdk.Context, epochNumber uint64) bool {
k.Logger(ctx).Info("Cleaning Claimed Epoch Unbonding Records...")
for _, epochUnbondingRecord := range k.RecordsKeeper.GetAllEpochUnbondingRecord(ctx) {
shouldDeleteEpochUnbondingRecord := true
hostZoneUnbondings := epochUnbondingRecord.HostZoneUnbondings
for _, hostZoneUnbonding := range hostZoneUnbondings {
// if an EpochUnbondingRecord has any HostZoneUnbonding with non-zero balances, we don't delete the EpochUnbondingRecord
// because it has outstanding tokens that need to be claimed
if !hostZoneUnbonding.NativeTokenAmount.Equal(sdkmath.ZeroInt()) {
shouldDeleteEpochUnbondingRecord = false
break
}
}
if shouldDeleteEpochUnbondingRecord {
k.Logger(ctx).Info(fmt.Sprintf(" EpochUnbondingRecord %d - All unbondings claimed, removing record", epochUnbondingRecord.EpochNumber))
k.RecordsKeeper.RemoveEpochUnbondingRecord(ctx, epochUnbondingRecord.EpochNumber)
} else {
k.Logger(ctx).Info(fmt.Sprintf(" EpochUnbondingRecord %d - Has unclaimed unbondings", epochUnbondingRecord.EpochNumber))
}
}
return true
}