-
Notifications
You must be signed in to change notification settings - Fork 11
/
delegator_unbonding_epoch_entry.go
88 lines (70 loc) · 3.74 KB
/
delegator_unbonding_epoch_entry.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/persistenceOne/pstake-native/x/lscosmos/types"
)
// SetDelegatorUnbondingEpochEntry sets delegator entry for unbondign stkatom for an unbonding epoch
func (k Keeper) SetDelegatorUnbondingEpochEntry(ctx sdk.Context, unbondingEpochEntry types.DelegatorUnbondingEpochEntry) {
store := ctx.KVStore(k.storeKey)
delAddr, err := sdk.AccAddressFromBech32(unbondingEpochEntry.DelegatorAddress)
if err != nil {
panic(err)
}
bz := k.cdc.MustMarshal(&unbondingEpochEntry)
store.Set(types.GetDelegatorUnbondingEpochEntryKey(delAddr, unbondingEpochEntry.EpochNumber), bz)
}
// GetDelegatorUnbondingEpochEntry gets delegator entry for unbondign stkatom for an unbonding epoch
func (k Keeper) GetDelegatorUnbondingEpochEntry(ctx sdk.Context, delegatorAddress sdk.AccAddress, epochNumber int64) types.DelegatorUnbondingEpochEntry {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetDelegatorUnbondingEpochEntryKey(delegatorAddress, epochNumber))
if bz == nil {
return types.DelegatorUnbondingEpochEntry{}
}
var unbondingEpochEntry types.DelegatorUnbondingEpochEntry
k.cdc.MustUnmarshal(bz, &unbondingEpochEntry)
return unbondingEpochEntry
}
// RemoveDelegatorUnbondingEpochEntry removes the delegator unbonding epoch entry for the input
// delegator address and epoch number
func (k Keeper) RemoveDelegatorUnbondingEpochEntry(ctx sdk.Context, delegatorAddress sdk.AccAddress, epochNumber int64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetDelegatorUnbondingEpochEntryKey(delegatorAddress, epochNumber))
}
// IterateDelegatorUnbondingEpochEntry returns a list of types.DelegatorUnbondingEpochEntry by using
// prefix for iterating through the KV store entries using the input delegator address
func (k Keeper) IterateDelegatorUnbondingEpochEntry(ctx sdk.Context, delegatorAddress sdk.AccAddress) []types.DelegatorUnbondingEpochEntry {
store := ctx.KVStore(k.storeKey)
var delegatorUnbondingEntries []types.DelegatorUnbondingEpochEntry
iterator := sdk.KVStorePrefixIterator(store, types.GetPartialDelegatorUnbondingEpochEntryKey(delegatorAddress))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var unbondingEntry types.DelegatorUnbondingEpochEntry
k.cdc.MustUnmarshal(iterator.Value(), &unbondingEntry)
delegatorUnbondingEntries = append(delegatorUnbondingEntries, unbondingEntry)
}
return delegatorUnbondingEntries
}
// IterateAllDelegatorUnbondingEpochEntry returns a list of all epoch entries ever created in the KV store
// by using the prefix iterator
func (k Keeper) IterateAllDelegatorUnbondingEpochEntry(ctx sdk.Context) []types.DelegatorUnbondingEpochEntry {
store := ctx.KVStore(k.storeKey)
var delegatorUnbondingEntries []types.DelegatorUnbondingEpochEntry
iterator := sdk.KVStorePrefixIterator(store, types.DelegatorUnbondingEpochEntryKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var unbondingEntry types.DelegatorUnbondingEpochEntry
k.cdc.MustUnmarshal(iterator.Value(), &unbondingEntry)
delegatorUnbondingEntries = append(delegatorUnbondingEntries, unbondingEntry)
}
return delegatorUnbondingEntries
}
// AddDelegatorUnbondingEpochEntry adds delegator entry for unbondign stkatom for an unbonding epoch
func (k Keeper) AddDelegatorUnbondingEpochEntry(ctx sdk.Context, delegatorAddress sdk.AccAddress, epochNumber int64, amount sdk.Coin) {
unbondingEntry := k.GetDelegatorUnbondingEpochEntry(ctx, delegatorAddress, epochNumber)
if unbondingEntry.Equal(types.DelegatorUnbondingEpochEntry{}) {
unbondingEntry = types.NewDelegatorUnbondingEpochEntry(delegatorAddress.String(), epochNumber, amount)
} else {
unbondingEntry.Amount = unbondingEntry.Amount.Add(amount)
}
k.SetDelegatorUnbondingEpochEntry(ctx, unbondingEntry)
}