Skip to content
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

chore: backport withdraw emissions update #2143

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions x/emissions/keeper/msg_server_withdraw_emissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

}
4 changes: 2 additions & 2 deletions x/emissions/keeper/withdrawable_emissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions x/emissions/keeper/withdrawable_emissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading