diff --git a/app/setup_handlers.go b/app/setup_handlers.go index d5f2bbf35..84c76cc9f 100644 --- a/app/setup_handlers.go +++ b/app/setup_handlers.go @@ -26,6 +26,7 @@ import ( leveragelptypes "github.com/elys-network/elys/x/leveragelp/types" margintypes "github.com/elys-network/elys/x/margin/types" oracletypes "github.com/elys-network/elys/x/oracle/types" + parametertypes "github.com/elys-network/elys/x/parameter/types" stablestaketypes "github.com/elys-network/elys/x/stablestake/types" tokenomicstypes "github.com/elys-network/elys/x/tokenomics/types" transferhooktypes "github.com/elys-network/elys/x/transferhook/types" @@ -86,6 +87,8 @@ func setUpgradeHandler(app *ElysApp) { keyTable = transferhooktypes.ParamKeyTable() //nolint:staticcheck case stablestaketypes.ModuleName: keyTable = stablestaketypes.ParamKeyTable() //nolint:staticcheck + case parametertypes.ModuleName: + keyTable = parametertypes.ParamKeyTable() //nolint:staticcheck } if !subspace.HasKeyTable() { diff --git a/config.yml b/config.yml index 5ba07336e..8db47a0d8 100644 --- a/config.yml +++ b/config.yml @@ -436,7 +436,7 @@ genesis: min_commission_rate: "0.05" max_voting_power: "0.66" min_self_delegation: "1" - broker_address: "" + broker_address: "elys1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrec2l" margin: params: leverage_max: "10" diff --git a/x/margin/keeper/reset_store.go b/x/margin/keeper/reset_store.go new file mode 100644 index 000000000..ed748abd8 --- /dev/null +++ b/x/margin/keeper/reset_store.go @@ -0,0 +1,31 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/margin/types" +) + +// ResetStore resets all keys in the margin module store +func (k Keeper) ResetStore(ctx sdk.Context) error { + store := ctx.KVStore(k.storeKey) + + // List of prefixes to clear + prefixes := [][]byte{ + types.MTPPrefix, + types.MTPCountPrefix, + types.OpenMTPCountPrefix, + types.WhitelistPrefix, + types.SQBeginBlockPrefix, + } + + for _, prefix := range prefixes { + iter := sdk.KVStorePrefixIterator(store, prefix) + defer iter.Close() + + for ; iter.Valid(); iter.Next() { + store.Delete(iter.Key()) + } + } + + return nil +} diff --git a/x/margin/migrations/v4_migration.go b/x/margin/migrations/v4_migration.go index 22b9f43ec..ffb471071 100644 --- a/x/margin/migrations/v4_migration.go +++ b/x/margin/migrations/v4_migration.go @@ -6,6 +6,9 @@ import ( ) func (m Migrator) V4Migration(ctx sdk.Context) error { + // reset store + m.keeper.ResetStore(ctx) + // reset params params := types.NewParams() m.keeper.SetParams(ctx, ¶ms) diff --git a/x/parameter/migrations/v2_migration.go b/x/parameter/migrations/v2_migration.go index e0b83fff5..246effd3a 100644 --- a/x/parameter/migrations/v2_migration.go +++ b/x/parameter/migrations/v2_migration.go @@ -6,12 +6,14 @@ import ( ) func (m Migrator) V2Migration(ctx sdk.Context) error { + // reset params params := types.NewParams( - sdk.NewDecWithPrec(5, 2), // min commission 0.05 - sdk.NewDecWithPrec(66, 1), // max voting power - sdk.NewInt(1), // min self delegation - "elys1mx32w9tnfxv0z5j000750h8ver7qf3xpj09w3uzvsr3hq68f4hxqte4gam", // broker address + sdk.NewDecWithPrec(5, 2), // min commission 0.05 + sdk.NewDecWithPrec(66, 1), // max voting power + sdk.NewInt(1), // min self delegation + "elys1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrec2l", // broker address ) m.keeper.SetParams(ctx, params) + return nil } diff --git a/x/parameter/types/errors.go b/x/parameter/types/errors.go index 8501b36b9..8ed0ecb78 100644 --- a/x/parameter/types/errors.go +++ b/x/parameter/types/errors.go @@ -8,5 +8,8 @@ import ( // x/parameter module sentinel errors var ( - ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") + ErrInvalidMinCommissionRate = sdkerrors.Register(ModuleName, 1101, "invalid min commission rate") + ErrInvalidMaxVotingPower = sdkerrors.Register(ModuleName, 1102, "invalid max voting power") + ErrInvalidMinSelfDelegation = sdkerrors.Register(ModuleName, 1103, "invalid min self delegation") + ErrInvalidBrokerAddress = sdkerrors.Register(ModuleName, 1104, "invalid broker address") ) diff --git a/x/parameter/types/params.go b/x/parameter/types/params.go index d0b1e0891..ed92a18d1 100644 --- a/x/parameter/types/params.go +++ b/x/parameter/types/params.go @@ -2,9 +2,25 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "gopkg.in/yaml.v2" ) +var _ paramtypes.ParamSet = (*Params)(nil) + +// Parameter keys +var ( + KeyMinCommissionRate = []byte("MinCommissionRate") + KeyMaxVotingPower = []byte("MaxVotingPower") + KeyMinSelfDelegation = []byte("MinSelfDelegation") + KeyBrokerAddress = []byte("BrokerAddress") +) + +// ParamKeyTable the param key table for launch module +func ParamKeyTable() paramtypes.KeyTable { + return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) +} + // NewParams creates a new Params instance func NewParams(minCommissionRate sdk.Dec, maxVotingPower sdk.Dec, minSelfDelegation sdk.Int, brokerAddress string) Params { return Params{ @@ -21,12 +37,34 @@ func DefaultParams() Params { sdk.NewDecWithPrec(5, 2), sdk.NewDec(100), sdk.OneInt(), - "", + "elys1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrec2l", ) } +// ParamSetPairs get the params.ParamSet +func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { + return paramtypes.ParamSetPairs{ + paramtypes.NewParamSetPair(KeyMinCommissionRate, &p.MinCommissionRate, validateMinCommissionRate), + paramtypes.NewParamSetPair(KeyMaxVotingPower, &p.MaxVotingPower, validateMaxVotingPower), + paramtypes.NewParamSetPair(KeyMinSelfDelegation, &p.MinSelfDelegation, validateMinSelfDelegation), + paramtypes.NewParamSetPair(KeyBrokerAddress, &p.BrokerAddress, validateBrokerAddress), + } +} + // Validate validates the set of params func (p Params) Validate() error { + if err := validateMinCommissionRate(p.MinCommissionRate); err != nil { + return err + } + if err := validateMaxVotingPower(p.MaxVotingPower); err != nil { + return err + } + if err := validateMinSelfDelegation(p.MinSelfDelegation); err != nil { + return err + } + if err := validateBrokerAddress(p.BrokerAddress); err != nil { + return err + } return nil } @@ -35,3 +73,47 @@ func (p Params) String() string { out, _ := yaml.Marshal(p) return string(out) } + +func validateMinCommissionRate(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return ErrInvalidMinCommissionRate + } + if v.IsNegative() { + return ErrInvalidMinCommissionRate + } + return nil +} + +func validateMaxVotingPower(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return ErrInvalidMaxVotingPower + } + if v.IsNegative() { + return ErrInvalidMaxVotingPower + } + return nil +} + +func validateMinSelfDelegation(i interface{}) error { + v, ok := i.(sdk.Int) + if !ok { + return ErrInvalidMinSelfDelegation + } + if v.IsNegative() { + return ErrInvalidMinSelfDelegation + } + return nil +} + +func validateBrokerAddress(i interface{}) error { + v, ok := i.(string) + if !ok { + return ErrInvalidBrokerAddress + } + if v == "" { + return ErrInvalidBrokerAddress + } + return nil +}