Skip to content

Commit

Permalink
fix: migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Dec 2, 2023
1 parent 4d56f27 commit 99d6624
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions x/margin/keeper/reset_store.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions x/margin/migrations/v4_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params)
Expand Down
10 changes: 6 additions & 4 deletions x/parameter/migrations/v2_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion x/parameter/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
84 changes: 83 additions & 1 deletion x/parameter/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

Expand All @@ -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
}

0 comments on commit 99d6624

Please sign in to comment.