diff --git a/x/emissions/keeper/msg_server_withdraw_emissions_test.go b/x/emissions/keeper/msg_server_withdraw_emissions_test.go index e93c79620a..c47fc52309 100644 --- a/x/emissions/keeper/msg_server_withdraw_emissions_test.go +++ b/x/emissions/keeper/msg_server_withdraw_emissions_test.go @@ -113,5 +113,24 @@ func TestMsgServer_WithdrawEmission(t *testing.T) { balance := sk.BankKeeper.GetBalance(ctx, sdk.MustAccAddressFromBech32(withdrawableEmission.Address), config.BaseDenom).Amount.String() require.Equal(t, sdk.ZeroInt().String(), balance) }) + t.Run("unable to withdraw emissions if amount requested is more that available", func(t *testing.T) { + k, ctx, sk, _ := keepertest.EmissionsKeeper(t) + + msgServer := keeper.NewMsgServerImpl(*k) + withdrawableEmission := sample.WithdrawableEmissions(t) + k.SetWithdrawableEmission(ctx, withdrawableEmission) + withdrawAmount := withdrawableEmission.Amount.Add(sdkmath.OneInt()) + err := sk.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawAmount))) + require.NoError(t, err) + err = sk.BankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.UndistributedObserverRewardsPool, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawAmount))) + require.NoError(t, err) + + _, err = msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ + Creator: withdrawableEmission.Address, + Amount: withdrawAmount, + }) + require.ErrorIs(t, err, types.ErrUnableToWithdrawEmissions) + require.ErrorContains(t, err, "amount to be removed is greater than the available withdrawable emission") + }) } diff --git a/x/emissions/keeper/withdrawable_emissions.go b/x/emissions/keeper/withdrawable_emissions.go index 6e9655915d..40b627f654 100644 --- a/x/emissions/keeper/withdrawable_emissions.go +++ b/x/emissions/keeper/withdrawable_emissions.go @@ -57,10 +57,10 @@ func (k Keeper) RemoveWithdrawableEmission(ctx sdk.Context, address string, amou return types.ErrEmissionsNotFound } if amount.IsNegative() || amount.IsZero() { - return types.ErrInvalidAmount + return types.ErrInvalidAmount.Wrap("amount to be removed is negative or zero") } if amount.GT(we.Amount) { - amount = we.Amount + return types.ErrInvalidAmount.Wrap("amount to be removed is greater than the available withdrawable emission") } we.Amount = we.Amount.Sub(amount) k.SetWithdrawableEmission(ctx, we) diff --git a/x/emissions/keeper/withdrawable_emissions_test.go b/x/emissions/keeper/withdrawable_emissions_test.go index f81cb1517f..b65c277b40 100644 --- a/x/emissions/keeper/withdrawable_emissions_test.go +++ b/x/emissions/keeper/withdrawable_emissions_test.go @@ -130,17 +130,14 @@ func TestKeeper_RemoveObserverEmission(t *testing.T) { require.Equal(t, sdkmath.ZeroInt(), we2.Amount) }) - t.Run("remove all observer emission successfully using amount higher that available", func(t *testing.T) { + t.Run("unable to remove observer emission if requested amount is higher than available", func(t *testing.T) { k, ctx, _, _ := keepertest.EmissionsKeeper(t) we := sample.WithdrawableEmissions(t) k.SetWithdrawableEmission(ctx, we) err := k.RemoveWithdrawableEmission(ctx, we.Address, we.Amount.Add(sdkmath.OneInt())) - require.NoError(t, err) - we2, found := k.GetWithdrawableEmission(ctx, we.Address) - require.True(t, found) - require.Equal(t, sdkmath.ZeroInt(), we2.Amount) + require.ErrorIs(t, err, emissionstypes.ErrInvalidAmount) + require.ErrorContains(t, err, "amount to be removed is greater than the available withdrawable emission") }) - t.Run("unable to remove non-existent emission ", func(t *testing.T) { k, ctx, _, _ := keepertest.EmissionsKeeper(t) address := sample.AccAddress()