/
getters_delegation_data.go
71 lines (59 loc) · 2.77 KB
/
getters_delegation_data.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
package keeper
import (
"github.com/KYVENetwork/chain/x/delegation/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// The `DelegationData` stores general aggregated variables for each existing staker
// as well as necessary variables needed for the F1 distribution algorithm
// Look at the proto-file for detailed explanation of the variables.
// Every staker with at least one delegator has this entry.
// AddAmountToDelegationRewards adds the specified amount to the current delegationData object.
// This is needed by the F1-algorithm to calculate to outstanding rewards
func (k Keeper) AddAmountToDelegationRewards(ctx sdk.Context, stakerAddress string, amount uint64) {
delegationData, found := k.GetDelegationData(ctx, stakerAddress)
if found {
delegationData.CurrentRewards += amount
k.SetDelegationData(ctx, delegationData)
}
}
// SetDelegationData set a specific delegationPoolData in the store from its index
func (k Keeper) SetDelegationData(ctx sdk.Context, delegationData types.DelegationData) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DelegationDataKeyPrefix)
b := k.cdc.MustMarshal(&delegationData)
store.Set(types.DelegationDataKey(delegationData.Staker), b)
}
// GetDelegationData returns a delegationData entry for a specific staker
// with `stakerAddress`
func (k Keeper) GetDelegationData(ctx sdk.Context, stakerAddress string) (val types.DelegationData, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DelegationDataKeyPrefix)
b := store.Get(types.DelegationDataKey(stakerAddress))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// DoesDelegationDataExist check if the staker with `stakerAddress` has
// a delegation data entry. This is the case if the staker as at least one delegator.
func (k Keeper) DoesDelegationDataExist(ctx sdk.Context, stakerAddress string) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DelegationDataKeyPrefix)
return store.Has(types.DelegationDataKey(stakerAddress))
}
// RemoveDelegationData removes a delegationData entry from the pool
func (k Keeper) RemoveDelegationData(ctx sdk.Context, stakerAddress string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DelegationDataKeyPrefix)
store.Delete(types.DelegationDataKey(stakerAddress))
}
// GetAllDelegationData returns all delegationData entries
func (k Keeper) GetAllDelegationData(ctx sdk.Context) (list []types.DelegationData) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DelegationDataKeyPrefix)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.DelegationData
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}