Skip to content

Commit

Permalink
feat: add migrations to TotalLiquidStakedTokens and missing param.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max authored and xlab committed Apr 19, 2023
1 parent 256a845 commit 40474ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
7 changes: 3 additions & 4 deletions x/lsnative/staking/keeper/liquid_stake.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/persistenceOne/persistence-sdk/v2/x/lsnative/staking/types"
Expand All @@ -25,10 +27,7 @@ func (k Keeper) GetTotalLiquidStakedTokens(ctx sdk.Context) sdk.Int {
tokensBz := store.Get(types.TotalLiquidStakedTokensKey)

if tokensBz == nil {
// QUESTION: Should we panic here instead?
// This is basically protecting against the case where we failed
// to bootstrap the total liquid staked in the upgrade handler
return sdk.ZeroInt()
panic(errors.New("no total liquid staked tokens in store"))
}

var tokens sdk.Int
Expand Down
19 changes: 18 additions & 1 deletion x/lsnative/staking/migrations/v046/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ import (
// MigrateStore performs in-place store migrations from v0.43/v0.44/v0.45 to v0.46.
// The migration includes:
//
// - Setting the MinCommissionRate & ValidatorBondFactor params in the paramstore
// - Setting the MinCommissionRate, ValidatorBondFactor and GlobalLiquidStakingCap params in the paramstore
// - Initializing TotalLiquidStakedTokens to 0
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, paramstore paramtypes.Subspace) error {
migrateParamsStore(ctx, paramstore)

store := ctx.KVStore(storeKey)
migrateTotalLiquidStakedTokens(store, sdk.ZeroInt())

return nil
}

// migrateParamsStore migrates the param store values
func migrateParamsStore(ctx sdk.Context, paramstore paramtypes.Subspace) {
if paramstore.HasKeyTable() {
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
paramstore.Set(ctx, types.KeyValidatorBondFactor, types.DefaultValidatorBondFactor)
paramstore.Set(ctx, types.KeyGlobalLiquidStakingCap, types.DefaultGlobalLiquidStakingCap)
} else {
paramstore.WithKeyTable(types.ParamKeyTable())
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
paramstore.Set(ctx, types.KeyValidatorBondFactor, types.DefaultValidatorBondFactor)
paramstore.Set(ctx, types.KeyGlobalLiquidStakingCap, types.DefaultGlobalLiquidStakingCap)
}
}

// migrateTotalLiquidStakedTokens migrates the total outstanding tokens owned by a liquid staking provider
func migrateTotalLiquidStakedTokens(store sdk.KVStore, tokens sdk.Int) {
tokensBz, err := tokens.Marshal()
if err != nil {
panic(err)
}

store.Set(types.TotalLiquidStakedTokensKey, tokensBz)
}

0 comments on commit 40474ca

Please sign in to comment.