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.
Currently we call
WithdrawRewardsfor each staking coin denom, which sends rewards for that denom to the farmer:farming/x/farming/keeper/staking.go
Lines 280 to 301 in debb2e6
We can optimize this to send total rewards only once at the end of the function.
To do this, we should change
WithdrawRewardsto not send coins, instead return justsdk.Coins.But we need to discuss more about this optimization. It just reduces gas cost of
MsgUnstake.