Skip to content

Commit

Permalink
fix: skip reward distribution when there are no delegators
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Jul 5, 2023
1 parent 1c71bbb commit d808ed7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 7 additions & 2 deletions x/alliance/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,15 @@ func accumulateRewards(latestRewardHistories types.RewardHistories, rewardHistor
// To calculate the number of rewards claimable, take reward_history * alliance_token_amount * reward_weight
func (k Keeper) AddAssetsToRewardPool(ctx sdk.Context, from sdk.AccAddress, val types.AllianceValidator, coins sdk.Coins) error {
rewardHistories := types.NewRewardHistories(val.GlobalRewardHistory)
// We need some delegations before we can split rewards. Else rewards belong to no one and do nothing
if len(val.TotalDelegatorShares) == 0 {
return nil
}

totalAssetWeight := k.totalAssetWeight(ctx, val)
// We need some delegations before we can split rewards. Else rewards belong to no one
if totalAssetWeight.IsZero() {
return types.ErrZeroDelegations
// Do nothing since there are no assets to distribute rewards to
return nil
}

for _, c := range coins {
Expand Down
4 changes: 0 additions & 4 deletions x/alliance/keeper/tests/reward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ func TestRewardPoolAndGlobalIndex(t *testing.T) {
coin := app.BankKeeper.GetBalance(ctx, mintPoolAddr, "stake")
require.Equal(t, sdk.NewCoin("stake", sdk.NewInt(4000_000)), coin)

// Transfer to reward pool without delegations will fail
err = app.AllianceKeeper.AddAssetsToRewardPool(ctx, mintPoolAddr, val1, sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(2000_000))))
require.Error(t, err)

_, err = app.AllianceKeeper.Delegate(ctx, user1, val1, sdk.NewCoin(AllianceDenom, sdk.NewInt(1000_000)))
require.NoError(t, err)
assets := app.AllianceKeeper.GetAllAssets(ctx)
Expand Down

0 comments on commit d808ed7

Please sign in to comment.