-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
308 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Messages | ||
|
||
## MsgWithdrawEmission | ||
|
||
WithdrawEmission create a withdraw emission object , which is then process at endblock | ||
The withdraw emission object is created and stored | ||
using the address of the creator as the index key ,therefore, if more that one withdraw requests are created in a block on thr last one would be processed. | ||
Creating a withdraw does not guarantee that the emission will be processed | ||
All withdraws for a block are deleted at the end of the block irrespective of whether they were processed or not. | ||
|
||
```proto | ||
message MsgWithdrawEmission { | ||
string creator = 1; | ||
string amount = 2; | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// @generated by protoc-gen-es v1.3.0 with parameter "target=dts" | ||
// @generated from file emissions/tx.proto (package zetachain.zetacore.emissions, syntax proto3) | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
|
||
import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf"; | ||
import { Message, proto3 } from "@bufbuild/protobuf"; | ||
|
||
/** | ||
* @generated from message zetachain.zetacore.emissions.MsgWithdrawEmission | ||
*/ | ||
export declare class MsgWithdrawEmission extends Message<MsgWithdrawEmission> { | ||
/** | ||
* @generated from field: string creator = 1; | ||
*/ | ||
creator: string; | ||
|
||
/** | ||
* @generated from field: string amount = 2; | ||
*/ | ||
amount: string; | ||
|
||
constructor(data?: PartialMessage<MsgWithdrawEmission>); | ||
|
||
static readonly runtime: typeof proto3; | ||
static readonly typeName = "zetachain.zetacore.emissions.MsgWithdrawEmission"; | ||
static readonly fields: FieldList; | ||
|
||
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): MsgWithdrawEmission; | ||
|
||
static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): MsgWithdrawEmission; | ||
|
||
static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): MsgWithdrawEmission; | ||
|
||
static equals(a: MsgWithdrawEmission | PlainMessage<MsgWithdrawEmission> | undefined, b: MsgWithdrawEmission | PlainMessage<MsgWithdrawEmission> | undefined): boolean; | ||
} | ||
|
||
/** | ||
* @generated from message zetachain.zetacore.emissions.MsgWithdrawEmissionResponse | ||
*/ | ||
export declare class MsgWithdrawEmissionResponse extends Message<MsgWithdrawEmissionResponse> { | ||
constructor(data?: PartialMessage<MsgWithdrawEmissionResponse>); | ||
|
||
static readonly runtime: typeof proto3; | ||
static readonly typeName = "zetachain.zetacore.emissions.MsgWithdrawEmissionResponse"; | ||
static readonly fields: FieldList; | ||
|
||
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): MsgWithdrawEmissionResponse; | ||
|
||
static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): MsgWithdrawEmissionResponse; | ||
|
||
static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): MsgWithdrawEmissionResponse; | ||
|
||
static equals(a: MsgWithdrawEmissionResponse | PlainMessage<MsgWithdrawEmissionResponse> | undefined, b: MsgWithdrawEmissionResponse | PlainMessage<MsgWithdrawEmissionResponse> | undefined): boolean; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
x/emissions/keeper/msg_server_withdraw_emissions_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zeta-chain/zetacore/cmd/zetacored/config" | ||
keepertest "github.com/zeta-chain/zetacore/testutil/keeper" | ||
"github.com/zeta-chain/zetacore/testutil/sample" | ||
"github.com/zeta-chain/zetacore/x/emissions" | ||
"github.com/zeta-chain/zetacore/x/emissions/keeper" | ||
"github.com/zeta-chain/zetacore/x/emissions/types" | ||
) | ||
|
||
func TestMsgServer_WithdrawEmission(t *testing.T) { | ||
t.Run("successfully withdraw emissions at endblock", func(t *testing.T) { | ||
k, ctx, sk, _ := keepertest.EmissionsKeeper(t) | ||
|
||
msgServer := keeper.NewMsgServerImpl(*k) | ||
withdrawableEmission := sample.WithdrawableEmissions(t) | ||
k.SetWithdrawableEmission(ctx, withdrawableEmission) | ||
err := sk.BankKeeper.MintCoins(ctx, types.UndistributedObserverRewardsPool, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawableEmission.Amount))) | ||
require.NoError(t, err) | ||
|
||
_, err = msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ | ||
Creator: withdrawableEmission.Address, | ||
Amount: withdrawableEmission.Amount, | ||
}) | ||
require.NoError(t, err) | ||
|
||
we, found := k.GetWithdrawEmissions(ctx, withdrawableEmission.Address) | ||
require.True(t, found) | ||
require.Equal(t, withdrawableEmission.Amount, we.Amount) | ||
|
||
balance := k.GetBankKeeper().GetBalance(ctx, sdk.MustAccAddressFromBech32(withdrawableEmission.Address), config.BaseDenom).Amount.String() | ||
require.Equal(t, sdk.ZeroInt().String(), balance) | ||
|
||
emissions.EndBlocker(ctx, *k) | ||
balance = k.GetBankKeeper().GetBalance(ctx, sdk.MustAccAddressFromBech32(withdrawableEmission.Address), config.BaseDenom).Amount.String() | ||
require.Equal(t, withdrawableEmission.Amount.String(), balance) | ||
}) | ||
|
||
t.Run("unable to create withdraw emissions with invalid address", func(t *testing.T) { | ||
k, ctx, sk, _ := keepertest.EmissionsKeeper(t) | ||
|
||
msgServer := keeper.NewMsgServerImpl(*k) | ||
withdrawableEmission := sample.WithdrawableEmissions(t) | ||
k.SetWithdrawableEmission(ctx, withdrawableEmission) | ||
err := sk.BankKeeper.MintCoins(ctx, types.UndistributedObserverRewardsPool, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawableEmission.Amount))) | ||
require.NoError(t, err) | ||
|
||
_, err = msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ | ||
Creator: "invalid_address", | ||
Amount: withdrawableEmission.Amount, | ||
}) | ||
require.ErrorIs(t, err, types.ErrInvalidAddress) | ||
}) | ||
|
||
t.Run("unable to create withdraw emissions if undistributed bbserver rewards pool does not have enough balance", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.EmissionsKeeper(t) | ||
|
||
msgServer := keeper.NewMsgServerImpl(*k) | ||
withdrawableEmission := sample.WithdrawableEmissions(t) | ||
k.SetWithdrawableEmission(ctx, withdrawableEmission) | ||
|
||
_, err := msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ | ||
Creator: withdrawableEmission.Address, | ||
Amount: withdrawableEmission.Amount, | ||
}) | ||
require.ErrorIs(t, err, types.ErrRewardsPoolDoesNotHaveEnoughBalance) | ||
}) | ||
|
||
t.Run("unable to create withdraw emissions with invalid amount", func(t *testing.T) { | ||
k, ctx, _, _ := keepertest.EmissionsKeeper(t) | ||
msgServer := keeper.NewMsgServerImpl(*k) | ||
withdrawableEmission := sample.WithdrawableEmissions(t) | ||
k.SetWithdrawableEmission(ctx, withdrawableEmission) | ||
_, err := msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ | ||
Creator: withdrawableEmission.Address, | ||
Amount: sdkmath.NewInt(-1), | ||
}) | ||
require.ErrorIs(t, err, types.ErrUnableToCreateWithdrawEmissions) | ||
}) | ||
|
||
t.Run("successfully create withdraw emissions but unable to process it", func(t *testing.T) { | ||
k, ctx, sk, _ := keepertest.EmissionsKeeper(t) | ||
msgServer := keeper.NewMsgServerImpl(*k) | ||
withdrawablEmission := sample.WithdrawableEmissions(t) | ||
k.SetWithdrawableEmission(ctx, withdrawablEmission) | ||
err := sk.BankKeeper.MintCoins(ctx, types.UndistributedObserverRewardsPool, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawablEmission.Amount))) | ||
require.NoError(t, err) | ||
|
||
_, err = msgServer.WithdrawEmission(ctx, &types.MsgWithdrawEmission{ | ||
Creator: withdrawablEmission.Address, | ||
Amount: withdrawablEmission.Amount, | ||
}) | ||
require.NoError(t, err) | ||
|
||
we, found := k.GetWithdrawEmissions(ctx, withdrawablEmission.Address) | ||
require.True(t, found) | ||
require.Equal(t, withdrawablEmission.Amount, we.Amount) | ||
|
||
balance := k.GetBankKeeper().GetBalance(ctx, sdk.MustAccAddressFromBech32(withdrawablEmission.Address), config.BaseDenom).Amount.String() | ||
require.Equal(t, sdk.ZeroInt().String(), balance) | ||
|
||
// Undistributed pool balance gets affected after the withdraw has been created | ||
err = sk.BankKeeper.BurnCoins(ctx, types.UndistributedObserverRewardsPool, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, withdrawablEmission.Amount))) | ||
require.NoError(t, err) | ||
|
||
emissions.EndBlocker(ctx, *k) | ||
// Undistributed pool does not have a balance so no rewards are distributed | ||
balance = k.GetBankKeeper().GetBalance(ctx, sdk.MustAccAddressFromBech32(withdrawablEmission.Address), config.BaseDenom).Amount.String() | ||
require.Equal(t, sdk.ZeroInt().String(), balance) | ||
|
||
// Withdraw gets deleted after end-blocker | ||
_, found = k.GetWithdrawEmissions(ctx, withdrawablEmission.Address) | ||
require.False(t, found) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.