Skip to content

Commit

Permalink
iterate delegation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RustNinja committed Dec 19, 2023
1 parent 38c7444 commit 0a40503
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions x/stakingmiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,39 @@ func (k Keeper) SetDelegation(ctx sdk.Context, DelegatorAddress string, Validato
store.Set(types.GetDelegationKey(delegatorAddress, GetValidatorAddr(delegation)), b)
}

func (k Keeper) IterateDelegations(ctx sdk.Context, fn func(index int64, ubd types.Delegation) (stop bool)) {
store := ctx.KVStore(k.storeKey)

iterator := sdk.KVStorePrefixIterator(store, types.DelegationKey)
defer iterator.Close()

for i := int64(0); iterator.Valid(); iterator.Next() {
ubd := MustUnmarshalUBD(k.cdc, iterator.Value())
if stop := fn(i, ubd); stop {
break
}
i++
}
}

func GetValidatorAddr(d types.Delegation) sdk.ValAddress {
addr, err := sdk.ValAddressFromBech32(d.ValidatorAddress)
if err != nil {
panic(err)
}
return addr
}

func UnmarshalBD(cdc codec.BinaryCodec, value []byte) (ubd types.Delegation, err error) {
err = cdc.Unmarshal(value, &ubd)
return ubd, err
}

func MustUnmarshalUBD(cdc codec.BinaryCodec, value []byte) types.Delegation {
ubd, err := UnmarshalBD(cdc, value)
if err != nil {
panic(err)
}

return ubd
}

0 comments on commit 0a40503

Please sign in to comment.