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

feat: message withdraw emissions #1825

Merged
merged 29 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f86c782
add a message to withdraw emission rewards
kingpinXD Feb 27, 2024
7599d9b
add more tests for end blocker
kingpinXD Feb 27, 2024
7d4c3cb
add unit tests for withdrawable emissions
kingpinXD Feb 27, 2024
da7f1a6
fix unit tests for withdrawable emissions
kingpinXD Feb 27, 2024
dca649e
generate files
kingpinXD Feb 28, 2024
ea2c810
Merge branch 'develop' into withdraw-emissions
kingpinXD Feb 28, 2024
6dff9a3
fix typo
kingpinXD Feb 28, 2024
d2e3fd9
Merge branch 'withdraw-emissions' of github.com:zeta-chain/zeta-node …
kingpinXD Feb 28, 2024
c06b9eb
simply to withdrawing emission amount directly in a message
kingpinXD Mar 1, 2024
2d7ab90
generate docs
kingpinXD Mar 1, 2024
40aa9a5
reject zero amount withdraws
kingpinXD Mar 1, 2024
bba6042
reject zero amount withdraws
kingpinXD Mar 1, 2024
25bed84
increase coverage for msg withdraw emissions
kingpinXD Mar 2, 2024
e9f4276
Merge branch 'develop' into withdraw-emissions
kingpinXD Mar 2, 2024
f2d4d84
fixed to error messages
kingpinXD Mar 4, 2024
01c9e45
Update x/emissions/keeper/msg_server_withdraw_emissions.go
kingpinXD Mar 4, 2024
a17d678
Update x/emissions/keeper/msg_server_withdraw_emissions.go
kingpinXD Mar 4, 2024
8f7d96a
Update x/emissions/keeper/msg_server_withdraw_emissions.go
kingpinXD Mar 4, 2024
f29adf2
remove minter for UndistributedObserverRewardsPool
kingpinXD Mar 4, 2024
037e3ec
Merge remote-tracking branch 'origin/withdraw-emissions' into withdra…
kingpinXD Mar 4, 2024
22b423e
generate files
kingpinXD Mar 4, 2024
f239219
Merge branch 'develop' into withdraw-emissions
kingpinXD Mar 5, 2024
867fd83
Merge branch 'withdraw-emissions' of github.com:zeta-chain/zeta-node …
kingpinXD Mar 5, 2024
c3173de
Update x/emissions/types/keys.go
lumtis Mar 5, 2024
3cc3181
change comments
kingpinXD Mar 5, 2024
e0560ee
Merge remote-tracking branch 'origin/withdraw-emissions' into withdra…
kingpinXD Mar 5, 2024
4960fea
Merge branch 'develop' into withdraw-emissions
kingpinXD Mar 5, 2024
1ae19df
add description for test
kingpinXD Mar 5, 2024
f35ffd7
Merge remote-tracking branch 'origin/withdraw-emissions' into withdra…
kingpinXD Mar 5, 2024
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
### Features

* [1698](https://github.com/zeta-chain/node/issues/1698) - bitcoin dynamic depositor fee
* [1811](https://github.com/zeta-chain/node/pull/1811) - add a message to withdraw emission rewards

### Docs

Expand Down
1 change: 1 addition & 0 deletions docs/cli/zetacored/zetacored_tx_emissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ zetacored tx emissions [flags]
### SEE ALSO

* [zetacored tx](zetacored_tx.md) - Transactions subcommands
* [zetacored tx emissions create-withdraw-emission](zetacored_tx_emissions_create-withdraw-emission.md) - create a new withdrawEmission

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# tx emissions create-withdraw-emission

create a new withdrawEmission

```
zetacored tx emissions create-withdraw-emission [amount] [flags]
```

### Options

```
-a, --account-number uint The account number of the signing account (offline mode only)
--aux Generate aux signer data instead of sending a tx
-b, --broadcast-mode string Transaction broadcasting mode (sync|async|block)
--dry-run ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it (when enabled, the local Keybase is not accessible)
--fee-granter string Fee granter grants fees for the transaction
--fee-payer string Fee payer pays fees for the transaction instead of deducting from the signer
--fees string Fees to pay along with transaction; eg: 10uatom
--from string Name or address of private key with which to sign
--gas string gas limit to set per-transaction; set to "auto" to calculate sufficient gas automatically. Note: "auto" option doesn't always report accurate results. Set a valid coin value to adjust the result. Can be used instead of "fees". (default 200000)
--gas-adjustment float adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored (default 1)
--gas-prices string Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)
--generate-only Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase only accessed when providing a key name)
-h, --help help for create-withdraw-emission
--keyring-backend string Select keyring's backend (os|file|kwallet|pass|test|memory)
--keyring-dir string The client Keyring directory; if omitted, the default 'home' directory will be used
--ledger Use a connected Ledger device
--node string [host]:[port] to tendermint rpc interface for this chain
--note string Note to add a description to the transaction (previously --memo)
--offline Offline mode (does not allow any online functionality)
-o, --output string Output format (text|json)
-s, --sequence uint The sequence number of the signing account (offline mode only)
--sign-mode string Choose sign mode (direct|amino-json|direct-aux), this is an advanced feature
--timeout-height uint Set a block timeout height to prevent the tx from being committed past a certain height
--tip string Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux, and is ignored if the target chain didn't enable the TipDecorator
-y, --yes Skip tx broadcasting prompt confirmation
```

### Options inherited from parent commands

```
--chain-id string The network chain ID
--home string directory for config and data
--log_format string The logging format (json|plain)
--log_level string The logging level (trace|debug|info|warn|error|fatal|panic)
--trace print out full stack trace on errors
```

### SEE ALSO

* [zetacored tx emissions](zetacored_tx_emissions.md) - emissions transactions subcommands

2 changes: 2 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53964,6 +53964,8 @@ definitions:
type: string
proved:
type: boolean
emissionsMsgWithdrawEmissionResponse:
type: object
emissionsQueryGetEmissionsFactorsResponse:
type: object
properties:
Expand Down
17 changes: 17 additions & 0 deletions docs/spec/emissions/messages.md
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
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
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;
}
```

14 changes: 13 additions & 1 deletion proto/emissions/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ import "gogoproto/gogo.proto";
option go_package = "github.com/zeta-chain/zetacore/x/emissions/types";

// Msg defines the Msg service.
service Msg {}
service Msg {
rpc WithdrawEmission(MsgWithdrawEmission) returns (MsgWithdrawEmissionResponse);
}

message MsgWithdrawEmission {
string creator = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message MsgWithdrawEmissionResponse {}
6 changes: 6 additions & 0 deletions testutil/keeper/emissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ func EmissionKeeperWithMockOptions(

return k, ctx, sdkKeepers, zetaKeepers
}

func GetEmissionsBankMock(t testing.TB, keeper *keeper.Keeper) *emissionsmocks.EmissionBankKeeper {
cbk, ok := keeper.GetBankKeeper().(*emissionsmocks.EmissionBankKeeper)
require.True(t, ok)
return cbk
}
2 changes: 1 addition & 1 deletion testutil/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var moduleAccountPerms = map[string][]string{
crosschaintypes.ModuleName: {authtypes.Minter, authtypes.Burner},
fungibletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
emissionstypes.ModuleName: {authtypes.Minter},
emissionstypes.UndistributedObserverRewardsPool: nil,
emissionstypes.UndistributedObserverRewardsPool: {authtypes.Minter, authtypes.Burner},
lumtis marked this conversation as resolved.
Show resolved Hide resolved
emissionstypes.UndistributedTssRewardsPool: nil,
}

Expand Down
18 changes: 18 additions & 0 deletions testutil/keeper/mocks/emissions/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions testutil/keeper/mocks/emissions/emission.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions typescript/emissions/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./events_pb";
export * from "./genesis_pb";
export * from "./params_pb";
export * from "./query_pb";
export * from "./tx_pb";
export * from "./withdrawable_emissions_pb";
56 changes: 56 additions & 0 deletions typescript/emissions/tx_pb.d.ts
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;
}

2 changes: 1 addition & 1 deletion x/emissions/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func GetTxCmd() *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdCreateWithdrawEmission())
return cmd
}
37 changes: 37 additions & 0 deletions x/emissions/client/cli/tx_create_withdraw_emssions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cli

import (
"errors"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/x/emissions/types"
)

func CmdCreateWithdrawEmission() *cobra.Command {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
cmd := &cobra.Command{
Use: "create-withdraw-emission [amount]",
Short: "create a new withdrawEmission",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
argsAmount, ok := sdkmath.NewIntFromString(args[0])
if !ok {
return errors.New("invalid amount")
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.NewMsgWithdrawEmissions(clientCtx.GetFromAddress().String(), argsAmount)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
43 changes: 43 additions & 0 deletions x/emissions/keeper/msg_server_withdraw_emissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zeta-chain/zetacore/cmd/zetacored/config"
"github.com/zeta-chain/zetacore/x/emissions/types"
)

// 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.
func (k msgServer) WithdrawEmission(goCtx context.Context, msg *types.MsgWithdrawEmission) (*types.MsgWithdrawEmissionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// check if the creator address is valid
address, err := sdk.AccAddressFromBech32(msg.Creator)
lumtis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errorsmod.Wrap(types.ErrInvalidAddress, err.Error())
}
// check if the undistributed rewards pool has enough balance to process this request.
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
// This is just a preliminary check, the actual processing at endblock might still fail if the pool balance gets affected.
undistributedRewardsBalance := k.GetBankKeeper().GetBalance(ctx, types.UndistributedObserverRewardsPoolAddress, config.BaseDenom)
if undistributedRewardsBalance.Amount.LT(msg.Amount) {
return nil, errorsmod.Wrap(types.ErrRewardsPoolDoesNotHaveEnoughBalance, " rewards pool does not have enough balance to process this request")
}

err = k.RemoveWithdrawableEmission(ctx, msg.Creator, msg.Amount)
if err != nil {
return nil, errorsmod.Wrap(types.ErrUnableToWithdrawEmissions, err.Error())
lumtis marked this conversation as resolved.
Show resolved Hide resolved
}
err = k.GetBankKeeper().SendCoinsFromModuleToAccount(ctx, types.UndistributedObserverRewardsPool, address, sdk.NewCoins(sdk.NewCoin(config.BaseDenom, msg.Amount)))
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Error while processing withdraw of emission to adresss %s for amount %s : err %s", address, msg.Amount, err))
return nil, errorsmod.Wrap(types.ErrUnableToWithdrawEmissions, err.Error())
}
return &types.MsgWithdrawEmissionResponse{}, nil
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading