Skip to content

fix: send rewards in Unstake at once #208

@hallazzang

Description

@hallazzang

Currently we call WithdrawRewards for each staking coin denom, which sends rewards for that denom to the farmer:

for _, coin := range amount {
staking, found := k.GetStaking(ctx, coin.Denom, farmerAcc)
if !found {
staking.Amount = sdk.ZeroInt()
}
queuedStaking, found := k.GetQueuedStaking(ctx, coin.Denom, farmerAcc)
if !found {
queuedStaking.Amount = sdk.ZeroInt()
}
availableAmt := staking.Amount.Add(queuedStaking.Amount)
if availableAmt.LT(coin.Amount) {
return sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFunds, "%s%s is smaller than %s%s", availableAmt, coin.Denom, coin.Amount, coin.Denom)
}
queuedStaking.Amount = queuedStaking.Amount.Sub(coin.Amount)
if queuedStaking.Amount.IsNegative() {
if _, err := k.WithdrawRewards(ctx, farmerAcc, coin.Denom); err != nil {
return err
}

We can optimize this to send total rewards only once at the end of the function.
To do this, we should change WithdrawRewards to not send coins, instead return just sdk.Coins.

But we need to discuss more about this optimization. It just reduces gas cost of MsgUnstake.

Metadata

Metadata

Assignees

Labels

help wantedExtra attention is neededwontfixThis will not be worked on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions