forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 27
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(dist): Add WithdrawDelegationWithoutSend function to avoid hitting the wallet #48
Closed
Vvaradinov
wants to merge
1
commit into
ledger/v0.47.x
from
Vvaradinov/withdraw-delegation-without-send
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,3 +212,69 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali | |
|
||
return finalRewards, nil | ||
} | ||
|
||
func (k Keeper) WithdrawDelegationRewardsWithoutSending(ctx sdk.Context, val stakingtypes.ValidatorI, del stakingtypes.DelegationI) (sdk.Coins, error) { | ||
// check existence of delegator starting info | ||
if !k.HasDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr()) { | ||
return nil, types.ErrEmptyDelegationDistInfo | ||
} | ||
|
||
// end current period and calculate rewards | ||
endingPeriod := k.IncrementValidatorPeriod(ctx, val) | ||
rewardsRaw := k.CalculateDelegationRewards(ctx, val, del, endingPeriod) | ||
outstanding := k.GetValidatorOutstandingRewardsCoins(ctx, del.GetValidatorAddr()) | ||
|
||
// defensive edge case may happen on the very final digits | ||
// of the decCoins due to operation order of the distribution mechanism. | ||
rewards := rewardsRaw.Intersect(outstanding) | ||
if !rewards.IsEqual(rewardsRaw) { | ||
logger := k.Logger(ctx) | ||
logger.Info( | ||
"rounding error withdrawing rewards from validator", | ||
"delegator", del.GetDelegatorAddr().String(), | ||
"validator", val.GetOperator().String(), | ||
"got", rewards.String(), | ||
"expected", rewardsRaw.String(), | ||
) | ||
} | ||
|
||
// truncate reward dec coins, return remainder to community pool | ||
finalRewards, remainder := rewards.TruncateDecimal() | ||
|
||
// update the outstanding rewards and the community pool only if the | ||
// transaction was successful | ||
k.SetValidatorOutstandingRewards(ctx, del.GetValidatorAddr(), types.ValidatorOutstandingRewards{Rewards: outstanding.Sub(rewards)}) | ||
feePool := k.GetFeePool(ctx) | ||
feePool.CommunityPool = feePool.CommunityPool.Add(remainder...) | ||
k.SetFeePool(ctx, feePool) | ||
|
||
// decrement reference count of starting period | ||
startingInfo := k.GetDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr()) | ||
startingPeriod := startingInfo.PreviousPeriod | ||
k.decrementReferenceCount(ctx, del.GetValidatorAddr(), startingPeriod) | ||
|
||
// remove delegator starting info | ||
k.DeleteDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr()) | ||
|
||
if finalRewards.IsZero() { | ||
baseDenom, _ := sdk.GetBaseDenom() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be an issue since it's hardcoded |
||
if baseDenom == "" { | ||
baseDenom = sdk.DefaultBondDenom | ||
} | ||
|
||
// Note, we do not call the NewCoins constructor as we do not want the zero | ||
// coin removed. | ||
finalRewards = sdk.Coins{sdk.NewCoin(baseDenom, math.ZeroInt())} | ||
} | ||
Comment on lines
+259
to
+268
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also would be good to add tests for this 👍 |
||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
types.EventTypeWithdrawRewards, | ||
sdk.NewAttribute(sdk.AttributeKeyAmount, finalRewards.String()), | ||
sdk.NewAttribute(types.AttributeKeyValidator, val.GetOperator().String()), | ||
sdk.NewAttribute(types.AttributeKeyDelegator, del.GetDelegatorAddr().String()), | ||
), | ||
) | ||
|
||
return finalRewards, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be good to refactor the shared code bits with the
withdrawDelegationRewards
method