-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
90 lines (74 loc) · 3.02 KB
/
keeper.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
package keeper
import (
"fmt"
"math"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/percosis-labs/percosis/v16/x/valset-pref/types"
)
type Keeper struct {
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace
stakingKeeper types.StakingInterface
distirbutionKeeper types.DistributionKeeper
lockupKeeper types.LockupKeeper
}
func NewKeeper(storeKey sdk.StoreKey,
paramSpace paramtypes.Subspace,
stakingKeeper types.StakingInterface,
distirbutionKeeper types.DistributionKeeper,
lockupKeeper types.LockupKeeper,
) Keeper {
return Keeper{
storeKey: storeKey,
paramSpace: paramSpace,
stakingKeeper: stakingKeeper,
distirbutionKeeper: distirbutionKeeper,
lockupKeeper: lockupKeeper,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetDelegationPreferences checks if valset position exists, if it does return that
// else return existing delegation that's not valset.
func (k Keeper) GetDelegationPreferences(ctx sdk.Context, delegator string) (types.ValidatorSetPreferences, error) {
valSet, exists := k.GetValidatorSetPreference(ctx, delegator)
if !exists {
delAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
return types.ValidatorSetPreferences{}, err
}
existingDelsValSetFormatted, err := k.GetExistingStakingDelegations(ctx, delAddr)
if err != nil {
return types.ValidatorSetPreferences{}, err
}
return types.ValidatorSetPreferences{Preferences: existingDelsValSetFormatted}, nil
}
return valSet, nil
}
// GetExistingStakingDelegations returns the existing delegation that's not valset.
// This function also formats the output into ValidatorSetPreference struct {valAddr, weight}.
// The weight is calculated based on (valDelegation / totalDelegations) for each validator.
// This method erros when given address does not have any existing delegations.
func (k Keeper) GetExistingStakingDelegations(ctx sdk.Context, delAddr sdk.AccAddress) ([]types.ValidatorPreference, error) {
var existingDelsValSetFormatted []types.ValidatorPreference
existingDelegations := k.stakingKeeper.GetDelegatorDelegations(ctx, delAddr, math.MaxUint16)
if len(existingDelegations) == 0 {
return nil, fmt.Errorf("No Existing delegation")
}
existingTotalShares := sdk.NewDec(0)
// calculate total shares that currently exists
for _, existingDelegation := range existingDelegations {
existingTotalShares = existingTotalShares.Add(existingDelegation.Shares)
}
// for each delegation format it in types.ValidatorSetPreferences format
for _, existingDelegation := range existingDelegations {
existingDelsValSetFormatted = append(existingDelsValSetFormatted, types.ValidatorPreference{
ValOperAddress: existingDelegation.ValidatorAddress,
Weight: existingDelegation.Shares.Quo(existingTotalShares),
})
}
return existingDelsValSetFormatted, nil
}