-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_utils.go
152 lines (116 loc) · 4.5 KB
/
query_utils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkstaking "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/incubus-network/fanfury-sdk/v2/x/lsnative/staking/types"
)
// Return all validators that a delegator is bonded to. If maxRetrieve is supplied, the respective amount will be returned.
func (k Keeper) GetDelegatorValidators(
ctx sdk.Context, delegatorAddr sdk.AccAddress, maxRetrieve uint32,
) types.Validators {
validators := make([]types.Validator, maxRetrieve)
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetLiquidDelegationsKey(delegatorAddr)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
defer iterator.Close()
i := 0
for ; iterator.Valid() && i < int(maxRetrieve); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
validator, found := k.GetLiquidValidator(ctx, delegation.GetValidatorAddr())
if !found {
panic(sdkstaking.ErrNoValidatorFound)
}
validators[i] = validator
i++
}
return validators[:i] // trim
}
// return a validator that a delegator is bonded to
func (k Keeper) GetDelegatorValidator(
ctx sdk.Context, delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress,
) (validator types.Validator, err error) {
delegation, found := k.GetLiquidDelegation(ctx, delegatorAddr, validatorAddr)
if !found {
return validator, sdkstaking.ErrNoDelegation
}
validator, found = k.GetLiquidValidator(ctx, delegation.GetValidatorAddr())
if !found {
panic(sdkstaking.ErrNoValidatorFound)
}
return validator, nil
}
// return all delegations for a delegator
func (k Keeper) GetAllLiquidDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress) []types.Delegation {
delegations := make([]types.Delegation, 0)
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetLiquidDelegationsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
defer iterator.Close()
i := 0
for ; iterator.Valid(); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
delegations = append(delegations, delegation)
i++
}
return delegations
}
// return all delegations for a delegator
func (k Keeper) GetAllDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress) []sdkstaking.Delegation {
delegations := make([]sdkstaking.Delegation, 0)
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetLiquidDelegationsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
defer iterator.Close()
i := 0
for ; iterator.Valid(); iterator.Next() {
delegation := sdkstaking.MustUnmarshalDelegation(k.cdc, iterator.Value())
delegations = append(delegations, delegation)
i++
}
return delegations
}
// return all unbonding-delegations for a delegator
func (k Keeper) GetAllUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress) []types.UnbondingDelegation {
unbondingDelegations := make([]types.UnbondingDelegation, 0)
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetUBDsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
defer iterator.Close()
for i := 0; iterator.Valid(); iterator.Next() {
unbondingDelegation := types.MustUnmarshalUBD(k.cdc, iterator.Value())
unbondingDelegations = append(unbondingDelegations, unbondingDelegation)
i++
}
return unbondingDelegations
}
// return all redelegations for a delegator
func (k Keeper) GetAllRedelegations(
ctx sdk.Context, delegator sdk.AccAddress, srcValAddress, dstValAddress sdk.ValAddress,
) []types.Redelegation {
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetREDsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey) // smallest to largest
defer iterator.Close()
srcValFilter := !(srcValAddress.Empty())
dstValFilter := !(dstValAddress.Empty())
redelegations := []types.Redelegation{}
for ; iterator.Valid(); iterator.Next() {
redelegation := types.MustUnmarshalRED(k.cdc, iterator.Value())
valSrcAddr, err := sdk.ValAddressFromBech32(redelegation.ValidatorSrcAddress)
if err != nil {
panic(err)
}
valDstAddr, err := sdk.ValAddressFromBech32(redelegation.ValidatorDstAddress)
if err != nil {
panic(err)
}
if srcValFilter && !(srcValAddress.Equals(valSrcAddr)) {
continue
}
if dstValFilter && !(dstValAddress.Equals(valDstAddr)) {
continue
}
redelegations = append(redelegations, redelegation)
}
return redelegations
}