I am trying to query the pending delegator rewards (this will be a very common request when people build staking derivates or other such instruments). But there is no public method to find this.
distKeeper.calculateDelegationRewards provides the info, but cannot be called from another module.
It turns out the distribution.NewQuerier() indirectly exposes this call. My current workaround is:
// Try to get *delegator* reward info!
params := distribution.QueryDelegationRewardsParams{
DelegatorAddress: contractAddr,
ValidatorAddress: valAddr,
}
req := abci.RequestQuery{
Data: mustMarshal(t, params),
}
qres, err := distribution.NewQuerier(distKeeper)(ctx, []string{distribution.QueryDelegationRewards}, req)
require.NoError(t, err)
var rewards sdk.DecCoins
mustParse(t, qres, &rewards)
fmt.Printf("delegator rewards: %#v\n", rewards)
It works, but I feel it is a bit ugly
Make the needed functions public, so I can write this easily.
Actually, looking deeper, the above may have side effects... in the query logic I see:
endingPeriod := k.incrementValidatorPeriod(ctx, val)
rewards := k.calculateDelegationRewards(ctx, val, del, endingPeriod)
incrementValidatorPeriod??
It would be great to have k.GetValidatorPeriod() and then use period +1 or such. That is actually exposed via k.GetValidatorCurrentRewards().Period, but maybe we could make this nicer....
My final proposal would be to add a new function to distribution:
func (k Keeper)CalculateDelegationRewards(ctx sdk.Context, val sdk.ValAddress, del sdk.AccAddress) rewards sdk.DecCoins {
endingPeriod := k.GetValidatorCurrentRewards(ctx, val).Period
return k.calculateDelegationRewards(ctx, val, del, endingPeriod+1)
}
@ethanfrey there is a CalculateDelegationRewards in master:
https://github.com/cosmos/cosmos-sdk/blob/a87d6ea3ab6b198a8bffbcb3e0765575523afe7d/x/distribution/keeper/delegation.go#L56
Great, so making this public should be a backport.
@ethanfrey can we PR into master first and then backport to Launchpad? I think that is the general process we want to follow, right?
The PR #5725 is already in master, so only a backport to Launchpad is needed.
+1 for the process you described.
Just pull in parts of that PR #5725, right? Not all of it.
https://github.com/cosmos/cosmos-sdk/pull/5725/files#diff-de71192adba700b3255b1eab62bef50e in particular
Most helpful comment
Great, so making this public should be a backport.