From 04c136cb18a9da85e0a8b7705b3ab91b2da5257b Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:14:28 -0500 Subject: [PATCH] feat(inflation): add inflation tx cmds (#1795) * feat(inflation): add toggle-inflation cli command * feat(inflation): add edit-params command and update validate-basic * chore: update changelog --- CHANGELOG.md | 27 ++--- x/inflation/client/cli/tx.go | 172 +++++++++++++++++++++++++++++++ x/inflation/keeper/msg_server.go | 2 +- x/inflation/module.go | 2 +- x/inflation/types/msgs.go | 29 ++++++ 5 files changed, 218 insertions(+), 14 deletions(-) create mode 100644 x/inflation/client/cli/tx.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 25e917d45..27931c05b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1774](https://github.com/NibiruChain/nibiru/pull/1774) - chore: update cosmos sdk v0.47.7 - [#1776](https://github.com/NibiruChain/nibiru/pull/1776) - feat(inflation): make inflation params a collection and add commands to update them - [#1786](https://github.com/NibiruChain/nibiru/pull/1786) - fix(inflation): fix inflation off-by 2 error +- [#1795](https://github.com/NibiruChain/nibiru/pull/1795) - feat(inflation): add inflation tx cmds #### Dapp modules: perp, spot, etc @@ -94,7 +95,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests - [#1736](https://github.com/NibiruChain/nibiru/pull/1736) - test(sim): add sim genesis state for all cusom modules - [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs + * [#1769](https://github.com/NibiruChain/nibiru/pull/1769) - feat: cherrypick GetBuildWasmMsg() to main branch + - [#1778](https://github.com/NibiruChain/nibiru/pull/1778) - chore: bump librocksdb to v8.9.1 ### Dependencies @@ -109,18 +112,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump `codecov/codecov-action` from 3 to 4 ([#1784](https://github.com/NibiruChain/nibiru/pull/1784)) - Bump `golangci/golangci-lint-action` from 3 to 4 ([#1791](https://github.com/NibiruChain/nibiru/pull/1791)) -* Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750)) -* Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756)) -* Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720)) -* Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724)) -* Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730)) -* Bump `github.com/dvsekhvalnov/jose2go` from 1.5.0 to 1.6.0 ([#1733](https://github.com/NibiruChain/nibiru/pull/1733)) -* Bump `github.com/spf13/cast` from 1.5.1 to 1.6.0 ([#1689](https://github.com/NibiruChain/nibiru/pull/1689)) -* Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676)) -* Bump `github.com/grpc-ecosystem/grpc-gateway/v2` from 2.18.0 to 2.18.1 ([#1675](https://github.com/NibiruChain/nibiru/pull/1675)) -* Bump `actions/setup-go` from 4 to 5 ([#1696](https://github.com/NibiruChain/nibiru/pull/1696)) -* Bump `golang` from 1.19 to 1.21 ([#1698](https://github.com/NibiruChain/nibiru/pull/1698)) -* [#1678](https://github.com/NibiruChain/nibiru/pull/1678) - chore(deps): collections to v0.4.0 for math.Int value encoder +- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750)) +- Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756)) +- Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720)) +- Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724)) +- Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730)) +- Bump `github.com/dvsekhvalnov/jose2go` from 1.5.0 to 1.6.0 ([#1733](https://github.com/NibiruChain/nibiru/pull/1733)) +- Bump `github.com/spf13/cast` from 1.5.1 to 1.6.0 ([#1689](https://github.com/NibiruChain/nibiru/pull/1689)) +- Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676)) +- Bump `github.com/grpc-ecosystem/grpc-gateway/v2` from 2.18.0 to 2.18.1 ([#1675](https://github.com/NibiruChain/nibiru/pull/1675)) +- Bump `actions/setup-go` from 4 to 5 ([#1696](https://github.com/NibiruChain/nibiru/pull/1696)) +- Bump `golang` from 1.19 to 1.21 ([#1698](https://github.com/NibiruChain/nibiru/pull/1698)) +- [#1678](https://github.com/NibiruChain/nibiru/pull/1678) - chore(deps): collections to v0.4.0 for math.Int value encoder ## [v1.1.0] - 2023-11-20 diff --git a/x/inflation/client/cli/tx.go b/x/inflation/client/cli/tx.go new file mode 100644 index 000000000..5c3f1253e --- /dev/null +++ b/x/inflation/client/cli/tx.go @@ -0,0 +1,172 @@ +package cli + +import ( + "fmt" + "strings" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/spf13/cobra" + + "github.com/NibiruChain/nibiru/x/inflation/types" +) + +// GetTxCmd returns the transaction commands for this module +func GetTxCmd() *cobra.Command { + inflationTxCmd := &cobra.Command{ + Use: types.ModuleName, + Short: "Inflation module subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + + inflationTxCmd.AddCommand( + CmdToggleInflation(), + CmdEditInflationParams(), + ) + + return inflationTxCmd +} + +func CmdToggleInflation() *cobra.Command { + cmd := &cobra.Command{ + Use: "toggle-inflation [true | false]", + Args: cobra.ExactArgs(1), + Short: "Toggle inflation on or off", + Long: strings.TrimSpace(` +Toggle inflation on or off. + +Requires sudo permissions. + +$ nibid tx inflation toggle-inflation true +`), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := &types.MsgToggleInflation{ + Sender: clientCtx.GetFromAddress().String(), + Enable: args[0] == "true", + } + + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} + +func CmdEditInflationParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "edit-params --staking-proportion [staking-proportion] --community-pool-proportion [community-pool-proportion] --strategic-reserves-proportion [strategic-reserves-proportion] --polynomial-factors [polynomial-factors] --epochs-per-period [epochs-per-period] --periods-per-year [periods-per-year] --max-period [max-period]", + Args: cobra.ExactArgs(0), + Short: "Edit the inflation module parameters", + Long: strings.TrimSpace(` +Edit the inflation module parameters. + +Requires sudo permissions. + +--staking-proportion: the proportion of minted tokens to be distributed to stakers +--community-pool-proportion: the proportion of minted tokens to be distributed to the community pool +--strategic-reserves-proportion: the proportion of minted tokens to be distributed to validators + +--polynomial-factors: the polynomial factors of the inflation distribution curve +--epochs-per-period: the number of epochs per period +--periods-per-year: the number of periods per year +--max-period: the maximum number of periods + +$ nibid tx oracle edit-params --staking-proportion 0.6 --community-pool-proportion 0.2 --strategic-reserves-proportion 0.2 --polynomial-factors 0.1,0.2,0.3,0.4,0.5,0.6 --epochs-per-period 100 --periods-per-year 100 --max-period 100 +`), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := &types.MsgEditInflationParams{ + Sender: clientCtx.GetFromAddress().String(), + } + + var stakingProportionDec sdk.Dec + if stakingProportion, _ := cmd.Flags().GetString("staking-proportion"); stakingProportion != "" { + stakingProportionDec = sdk.MustNewDecFromStr(stakingProportion) + msg.InflationDistribution.StakingRewards = stakingProportionDec + } + + var communityPoolProportionDec sdk.Dec + if communityPoolProportion, _ := cmd.Flags().GetString("community-pool-proportion"); communityPoolProportion != "" { + communityPoolProportionDec = sdk.MustNewDecFromStr(communityPoolProportion) + msg.InflationDistribution.CommunityPool = communityPoolProportionDec + } + + var strategicReservesProportionDec sdk.Dec + if strategicReservesProportion, _ := cmd.Flags().GetString("strategic-reserves-proportion"); strategicReservesProportion != "" { + strategicReservesProportionDec = sdk.MustNewDecFromStr(strategicReservesProportion) + msg.InflationDistribution.StrategicReserves = strategicReservesProportionDec + } + + if !stakingProportionDec.IsNil() && !communityPoolProportionDec.IsNil() && !strategicReservesProportionDec.IsNil() { + msg.InflationDistribution = &types.InflationDistribution{ + StakingRewards: stakingProportionDec, + CommunityPool: communityPoolProportionDec, + StrategicReserves: strategicReservesProportionDec, + } + } + + if polynomialFactors, _ := cmd.Flags().GetString("polynomial-factors"); polynomialFactors != "" { + polynomialFactorsArr := strings.Split(polynomialFactors, ",") + fmt.Println(polynomialFactorsArr) + realPolynomialFactors := make([]sdk.Dec, len(polynomialFactorsArr)) + for i, factor := range polynomialFactorsArr { + factorDec := sdk.MustNewDecFromStr(factor) + realPolynomialFactors[i] = factorDec + } + msg.PolynomialFactors = realPolynomialFactors + } + + if epochsPerPeriod, _ := cmd.Flags().GetUint64("epochs-per-period"); epochsPerPeriod != 0 { + epochsPerPeriodInt := sdk.NewIntFromUint64(epochsPerPeriod) + msg.EpochsPerPeriod = &epochsPerPeriodInt + } + + if periodsPerYear, _ := cmd.Flags().GetUint64("periods-per-year"); periodsPerYear != 0 { + periodsPerYearInt := sdk.NewIntFromUint64(periodsPerYear) + msg.PeriodsPerYear = &periodsPerYearInt + } + + if maxPeriod, _ := cmd.Flags().GetUint64("max-period"); maxPeriod != 0 { + maxPeriodInt := sdk.NewIntFromUint64(maxPeriod) + msg.MaxPeriod = &maxPeriodInt + } + + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + cmd.Flags().String("staking-proportion", "", "the proportion of minted tokens to be distributed to stakers") + cmd.Flags().String("community-pool-proportion", "", "the proportion of minted tokens to be distributed to the community pool") + cmd.Flags().String("strategic-reserves-proportion", "", "the proportion of minted tokens to be distributed to validators") + cmd.Flags().String("polynomial-factors", "", "the polynomial factors of the inflation distribution curve") + cmd.Flags().Uint64("epochs-per-period", 0, "the number of epochs per period") + cmd.Flags().Uint64("periods-per-year", 0, "the number of periods per year") + cmd.Flags().Uint64("max-period", 0, "the maximum number of periods") + + return cmd +} diff --git a/x/inflation/keeper/msg_server.go b/x/inflation/keeper/msg_server.go index 7b1b20624..21a67dc38 100644 --- a/x/inflation/keeper/msg_server.go +++ b/x/inflation/keeper/msg_server.go @@ -18,7 +18,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer { return &msgServer{Keeper: keeper} } -// EditOracleParams: gRPC tx msg for editing the inflation module params. +// EditInflationParams: gRPC tx msg for editing the inflation module params. // [SUDO] Only callable by sudoers. func (ms msgServer) EditInflationParams( goCtx context.Context, msg *types.MsgEditInflationParams, diff --git a/x/inflation/module.go b/x/inflation/module.go index 023392be9..b1abe73b5 100644 --- a/x/inflation/module.go +++ b/x/inflation/module.go @@ -77,7 +77,7 @@ func (b AppModuleBasic) RegisterGRPCGatewayRoutes(c client.Context, serveMux *ru } // GetTxCmd returns the root tx command for the inflation module. -func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } +func (AppModuleBasic) GetTxCmd() *cobra.Command { return cli.GetTxCmd() } // GetQueryCmd returns no root query command for the inflation module. func (AppModuleBasic) GetQueryCmd() *cobra.Command { diff --git a/x/inflation/types/msgs.go b/x/inflation/types/msgs.go index fa9c8da82..18496542e 100644 --- a/x/inflation/types/msgs.go +++ b/x/inflation/types/msgs.go @@ -1,6 +1,8 @@ package types import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -41,6 +43,33 @@ func (m MsgEditInflationParams) ValidateBasic() error { if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { return err } + + if m.InflationDistribution != nil { + if m.InflationDistribution.CommunityPool.IsNil() { + return fmt.Errorf("inflation distribution community pool should not be nil") + } + if m.InflationDistribution.StakingRewards.IsNil() { + return fmt.Errorf("inflation distribution staking rewards should not be nil") + } + if m.InflationDistribution.StrategicReserves.IsNil() { + return fmt.Errorf("inflation distribution strategic reserves should not be nil") + } + + sum := sdk.NewDec(0) + sum = sum.Add(m.InflationDistribution.CommunityPool) + sum = sum.Add(m.InflationDistribution.StakingRewards) + sum = sum.Add(m.InflationDistribution.StrategicReserves) + if !sum.Equal(sdk.OneDec()) { + return fmt.Errorf("inflation distribution sum should be 1, got %s", sum) + } + } + + if m.PolynomialFactors != nil { + if len(m.PolynomialFactors) != 6 { + return fmt.Errorf("polynomial factors should have 6 elements, got %d", len(m.PolynomialFactors)) + } + } + return nil }