-
Notifications
You must be signed in to change notification settings - Fork 44
/
keeper_shares.go
59 lines (49 loc) · 1.54 KB
/
keeper_shares.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
package keeper
import (
gomath "math"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Calculate the delegated amount
func (k Keeper) CalcDelegationAmount(ctx sdk.Context, delegator string) math.Int {
// Derivate bech32 based delegator address
delAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
// This could be validator address
return sdk.ZeroInt()
}
// Get elys delegation
delAmount := sdk.ZeroDec()
delegations := k.Keeper.GetDelegatorDelegations(ctx, delAddr, gomath.MaxUint16)
for _, del := range delegations {
valAddr := del.GetValidatorAddr()
val := k.Keeper.Validator(ctx, valAddr)
shares := del.GetShares()
tokens := val.TokensFromSharesTruncated(shares)
delAmount = delAmount.Add(tokens)
}
return delAmount.TruncateInt()
}
// Calculate delegation to bonded validators
func (k Keeper) CalcBondedDelegationAmount(ctx sdk.Context, delegator string) math.Int {
// Derivate bech32 based delegator address
delAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
// This could be validator address
return sdk.ZeroInt()
}
// Get elys delegation for creator address
delAmount := sdk.ZeroDec()
delegations := k.Keeper.GetDelegatorDelegations(ctx, delAddr, gomath.MaxUint16)
for _, del := range delegations {
valAddr := del.GetValidatorAddr()
val := k.Keeper.Validator(ctx, valAddr)
if !val.IsBonded() {
continue
}
shares := del.GetShares()
tokens := val.TokensFromSharesTruncated(shares)
delAmount = delAmount.Add(tokens)
}
return delAmount.TruncateInt()
}