Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add (re)delegation getters #11596

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,84 @@ func (k Keeper) IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64
}
}

// return the total amount a delegator has unbonding
danburck marked this conversation as resolved.
Show resolved Hide resolved
func (k Keeper) GetDelegatorUnbonding(ctx sdk.Context, delegator sdk.AccAddress) sdk.Int {
unbonding := sdk.ZeroInt()
k.IterateDelegatorUnbondingDelegations(ctx, delegator, func(ubd types.UnbondingDelegation) bool {
for _, entry := range ubd.Entries {
unbonding = unbonding.Add(entry.Balance)
}
return false
})
return unbonding
}

// iterate through a delegator's unbonding delegations
func (k Keeper) IterateDelegatorUnbondingDelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(ubd types.UnbondingDelegation) (stop bool)) {
store := ctx.KVStore(k.storeKey)

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

for ; iterator.Valid(); iterator.Next() {
ubd := types.MustUnmarshalUBD(k.cdc, iterator.Value())
if cb(ubd) {
break
}
}
}

// returm the total amount a delegator has bonded
func (k Keeper) GetDelegatorBonded(ctx sdk.Context, delegator sdk.AccAddress) sdk.Int {
bonded := sdk.ZeroInt()
danburck marked this conversation as resolved.
Show resolved Hide resolved

k.IterateDelegatorDelegations(ctx, delegator, func(delegation types.Delegation) bool {
validatorAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err) // shouldn't happen
}
validator, found := k.GetValidator(ctx, validatorAddr)
if found {
shares := delegation.Shares
tokens := validator.TokensFromSharesTruncated(shares).RoundInt()
danburck marked this conversation as resolved.
Show resolved Hide resolved
bonded = bonded.Add(tokens)
}
return false
})
return bonded
danburck marked this conversation as resolved.
Show resolved Hide resolved
}

// iterate through one delegator's delegations
func (k Keeper) IterateDelegatorDelegations(ctx sdk.Context, delegator sdk.AccAddress, cb func(delegation types.Delegation) (stop bool)) {
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetDelegationsKey(delegator)
iterator := sdk.KVStorePrefixIterator(store, delegatorPrefixKey)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.cdc, iterator.Value())
if cb(delegation) {
break
}
}
}

// iterate through one delegator's redelegations
func (k Keeper) IterateDelegatorRedelegations(ctx sdk.Context, delegator sdk.AccAddress, fn func(red types.Redelegation) (stop bool)) {
danburck marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)
delegatorPrefixKey := types.GetREDsKey(delegator)

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

for ; iterator.Valid(); iterator.Next() {
red := types.MustUnmarshalRED(k.cdc, iterator.Value())
if stop := fn(red); stop {
break
}
danburck marked this conversation as resolved.
Show resolved Hide resolved
}
}

// HasMaxUnbondingDelegationEntries - check if unbonding delegation has maximum number of entries
func (k Keeper) HasMaxUnbondingDelegationEntries(ctx sdk.Context,
delegatorAddr sdk.AccAddress, validatorAddr sdk.ValAddress) bool {
Expand Down
11 changes: 10 additions & 1 deletion x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func TestDelegation(t *testing.T) {
require.Len(t, resDels, 2)
}

// test total bonded for single delegator
expBonded := bond1to1.Shares.Add(bond2to1.Shares).Add(bond1to3.Shares)
resDelBond := app.StakingKeeper.GetDelegatorBonded(ctx, addrDels[0])
require.Equal(t, expBonded, sdk.NewDecFromInt(resDelBond))

// delete a record
app.StakingKeeper.RemoveDelegation(ctx, bond2to3)
_, found = app.StakingKeeper.GetDelegation(ctx, addrDels[1], valAddrs[2])
Expand Down Expand Up @@ -162,7 +167,8 @@ func TestUnbondingDelegation(t *testing.T) {
require.Equal(t, ubd, resUnbond)

// modify a records, save, and retrieve
ubd.Entries[0].Balance = sdk.NewInt(21)
expUnbond := sdk.NewInt(21)
ubd.Entries[0].Balance = expUnbond
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)

resUnbonds := app.StakingKeeper.GetUnbondingDelegations(ctx, delAddrs[0], 5)
Expand All @@ -175,6 +181,9 @@ func TestUnbondingDelegation(t *testing.T) {
require.True(t, found)
require.Equal(t, ubd, resUnbond)

resDelUnbond := app.StakingKeeper.GetDelegatorUnbonding(ctx, delAddrs[0])
require.Equal(t, expUnbond, resDelUnbond)

// delete a record
app.StakingKeeper.RemoveUnbondingDelegation(ctx, ubd)
_, found = app.StakingKeeper.GetUnbondingDelegation(ctx, delAddrs[0], valAddrs[0])
Expand Down