diff --git a/x/auction/keeper/genesis.go b/x/auction/keeper/genesis.go index 417631b18..686f31a96 100644 --- a/x/auction/keeper/genesis.go +++ b/x/auction/keeper/genesis.go @@ -8,7 +8,11 @@ import ( // Loads module state from genesis func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { - k.SetParams(ctx, genState.Params) + err := k.SetParams(ctx, genState.Params) + if err != nil { + panic(err) + } + for _, auction := range genState.Auctions { if err := k.SetAuction(ctx, &auction); err != nil { panic(err) @@ -18,8 +22,13 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { // Export's module state into genesis file func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + params, err := k.GetParams(ctx) + if err != nil { + panic(err) + } + genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) + genesis.Params = params genesis.Auctions = k.GetAllAuctions(ctx) return genesis } diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 91f62951c..a95de8616 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -13,7 +13,7 @@ import ( ) type Keeper struct { - cdc codec.BinaryCodec + cdc codec.Codec storeKey storetypes.StoreKey accountKeeper types.AccountKeeper bankKeeper types.BankKeeper @@ -21,7 +21,7 @@ type Keeper struct { } func NewKeeper( - cdc codec.BinaryCodec, + cdc codec.Codec, storeKey storetypes.StoreKey, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, diff --git a/x/auction/keeper/params.go b/x/auction/keeper/params.go index cdcf0b467..9675b0911 100644 --- a/x/auction/keeper/params.go +++ b/x/auction/keeper/params.go @@ -6,20 +6,22 @@ import ( "github.com/Stride-Labs/stride/v24/x/auction/types" ) -// Writes params to the store -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { +// GetParams get params +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { store := ctx.KVStore(k.storeKey) - paramsBz := k.cdc.MustMarshal(¶ms) - store.Set([]byte(types.ParamsPrefix), paramsBz) + bz := store.Get([]byte(types.ParamsKey)) + params := types.Params{} + err := k.cdc.UnmarshalJSON(bz, ¶ms) + return params, err } -// Retrieves the module parameters -func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { +// SetParams set params +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { store := ctx.KVStore(k.storeKey) - paramsBz := store.Get([]byte(types.ParamsPrefix)) - if len(paramsBz) == 0 { - panic("module parameters not set") + bz, err := k.cdc.MarshalJSON(¶ms) + if err != nil { + return err } - k.cdc.MustUnmarshal(paramsBz, ¶ms) - return params + store.Set([]byte(types.ParamsKey), bz) + return nil } diff --git a/x/auction/types/keys.go b/x/auction/types/keys.go index c9d169fbf..d71eee1ce 100644 --- a/x/auction/types/keys.go +++ b/x/auction/types/keys.go @@ -11,7 +11,7 @@ const ( // RouterKey defines the routing key RouterKey = ModuleName - ParamsPrefix = "params" + ParamsKey = "params" KeyAuctionPrefix = "auction" ) diff --git a/x/icqoracle/keeper/abci.go b/x/icqoracle/keeper/abci.go index 05639548e..8f6dd6eec 100644 --- a/x/icqoracle/keeper/abci.go +++ b/x/icqoracle/keeper/abci.go @@ -8,7 +8,13 @@ import ( func (k Keeper) BeginBlocker(ctx sdk.Context) { // Get all token prices - params := k.GetParams(ctx) + params, err := k.GetParams(ctx) + if err != nil { + // Can't really do anything but log + // A panic would halt the chain + ctx.Logger().Error("failed to get icqoracle params: %w", err) + return + } currentTime := ctx.BlockTime() diff --git a/x/icqoracle/keeper/genesis.go b/x/icqoracle/keeper/genesis.go index 6e0961aa0..cb31e53f7 100644 --- a/x/icqoracle/keeper/genesis.go +++ b/x/icqoracle/keeper/genesis.go @@ -11,7 +11,11 @@ import ( // Loads module state from genesis func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { - k.SetParams(ctx, genState.Params) + err := k.SetParams(ctx, genState.Params) + if err != nil { + panic(err) + } + for _, tokenPrice := range genState.TokenPrices { tokenPrice.SpotPrice = math.LegacyZeroDec() tokenPrice.UpdatedAt = time.Time{} @@ -25,8 +29,13 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { // Export's module state into genesis file func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + params, err := k.GetParams(ctx) + if err != nil { + panic(err) + } + genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) + genesis.Params = params genesis.TokenPrices = k.GetAllTokenPrices(ctx) return genesis } diff --git a/x/icqoracle/keeper/icq.go b/x/icqoracle/keeper/icq.go index e194c6c4f..a710949f8 100644 --- a/x/icqoracle/keeper/icq.go +++ b/x/icqoracle/keeper/icq.go @@ -61,7 +61,11 @@ func (k Keeper) SubmitOsmosisClPoolICQ( ) error { k.Logger(ctx).Info(utils.LogWithTokenPriceQuery(tokenPrice.BaseDenom, tokenPrice.QuoteDenom, tokenPrice.OsmosisPoolId, "Submitting OsmosisClPool ICQ")) - params := k.GetParams(ctx) + params, err := k.GetParams(ctx) + if err != nil { + k.Logger(ctx).Error(utils.LogWithTokenPriceQuery(tokenPrice.BaseDenom, tokenPrice.QuoteDenom, tokenPrice.OsmosisPoolId, "Error getting module params: %s", err.Error())) + return err + } osmosisPoolId, err := strconv.ParseUint(tokenPrice.OsmosisPoolId, 10, 64) if err != nil { diff --git a/x/icqoracle/keeper/keeper.go b/x/icqoracle/keeper/keeper.go index 23c613516..6bea267d0 100644 --- a/x/icqoracle/keeper/keeper.go +++ b/x/icqoracle/keeper/keeper.go @@ -16,13 +16,13 @@ import ( ) type Keeper struct { - cdc codec.BinaryCodec + cdc codec.Codec storeKey storetypes.StoreKey icqKeeper interchainquerykeeper.Keeper } func NewKeeper( - cdc codec.BinaryCodec, + cdc codec.Codec, storeKey storetypes.StoreKey, ) *Keeper { return &Keeper{ @@ -154,7 +154,11 @@ func (k Keeper) GetTokenPriceForQuoteDenom(ctx sdk.Context, baseDenom string, qu } // Get price expiration timeout - priceExpirationTimeoutSec := int64(k.GetParams(ctx).PriceExpirationTimeoutSec) + params, err := k.GetParams(ctx) + if err != nil { + return math.LegacyDec{}, fmt.Errorf("error getting params: %w", err) + } + priceExpirationTimeoutSec := int64(params.PriceExpirationTimeoutSec) // Init price price = math.LegacyZeroDec() diff --git a/x/icqoracle/keeper/params.go b/x/icqoracle/keeper/params.go index 62d90a49b..fbd5c9168 100644 --- a/x/icqoracle/keeper/params.go +++ b/x/icqoracle/keeper/params.go @@ -6,20 +6,22 @@ import ( "github.com/Stride-Labs/stride/v24/x/icqoracle/types" ) -// Writes params to the store -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { +// GetParams get params +func (k Keeper) GetParams(ctx sdk.Context) (types.Params, error) { store := ctx.KVStore(k.storeKey) - paramsBz := k.cdc.MustMarshal(¶ms) - store.Set([]byte(types.ParamsPrefix), paramsBz) + bz := store.Get([]byte(types.ParamsKey)) + params := types.Params{} + err := k.cdc.UnmarshalJSON(bz, ¶ms) + return params, err } -// Retrieves the module parameters -func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { +// SetParams set params +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error { store := ctx.KVStore(k.storeKey) - paramsBz := store.Get([]byte(types.ParamsPrefix)) - if len(paramsBz) == 0 { - panic("module parameters not set") + bz, err := k.cdc.MarshalJSON(¶ms) + if err != nil { + return err } - k.cdc.MustUnmarshal(paramsBz, ¶ms) - return params + store.Set([]byte(types.ParamsKey), bz) + return nil } diff --git a/x/icqoracle/keeper/query.go b/x/icqoracle/keeper/query.go index d15560d60..44606380f 100644 --- a/x/icqoracle/keeper/query.go +++ b/x/icqoracle/keeper/query.go @@ -60,7 +60,10 @@ func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*t ctx := sdk.UnwrapSDKContext(goCtx) - params := k.GetParams(ctx) + params, err := k.GetParams(ctx) + if err != nil { + return nil, err + } return &types.QueryParamsResponse{ Params: params, diff --git a/x/icqoracle/types/keys.go b/x/icqoracle/types/keys.go index a3fa62e5c..1008cab15 100644 --- a/x/icqoracle/types/keys.go +++ b/x/icqoracle/types/keys.go @@ -11,7 +11,7 @@ const ( // RouterKey defines the routing key RouterKey = ModuleName - ParamsPrefix = "params" + ParamsKey = "params" KeyPricePrefix = "price" )