From e44c61a31d1cca26b06b4202377cf09487f0b949 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 16 Apr 2024 18:05:10 +0900 Subject: [PATCH] feat: add initGenesis Signed-off-by: 170210 --- x/ERRORS.md | 2 +- x/fswap/genesis.go | 24 ------------- x/fswap/genesis_test.go | 1 - x/fswap/keeper/genesis.go | 40 +++++++++++++++++++++ x/fswap/keeper/genesis_test.go | 1 + x/fswap/module.go | 4 +-- x/fswap/simulation/{simap.go => helpers.go} | 0 x/fswap/types/errors.go | 2 +- x/fswap/types/expected_keepers.go | 1 + x/fswap/types/genesis.go | 20 +++++------ 10 files changed, 56 insertions(+), 39 deletions(-) delete mode 100644 x/fswap/genesis.go delete mode 100644 x/fswap/genesis_test.go create mode 100644 x/fswap/keeper/genesis.go create mode 100644 x/fswap/keeper/genesis_test.go rename x/fswap/simulation/{simap.go => helpers.go} (100%) diff --git a/x/ERRORS.md b/x/ERRORS.md index c324c8315c..c9cf32ec4c 100644 --- a/x/ERRORS.md +++ b/x/ERRORS.md @@ -177,7 +177,7 @@ |Error Name|Codespace|Code|Description| |:-|:-|:-|:-| -|ErrSample|fswap|1100|sample error| +|ErrParamsNotFound|fswap|1100|params does not exist| >You can also find detailed information in the following Errors.go files: * [fswap/types/errors.go](fswap/types/errors.go) diff --git a/x/fswap/genesis.go b/x/fswap/genesis.go deleted file mode 100644 index af232aad36..0000000000 --- a/x/fswap/genesis.go +++ /dev/null @@ -1,24 +0,0 @@ -package fswap - -import ( - sdk "github.com/Finschia/finschia-sdk/types" - "github.com/Finschia/finschia-sdk/x/fswap/keeper" - "github.com/Finschia/finschia-sdk/x/fswap/types" -) - -// InitGenesis initializes the capability module's state from a provided genesis -// state. -func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { - // this line is used by starport scaffolding # genesis/module/init - k.SetParams(ctx, genState.Params) -} - -// ExportGenesis returns the capability module's exported genesis. -func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { - genesis := types.DefaultGenesis() - genesis.Params = k.GetParams(ctx) - - // this line is used by starport scaffolding # genesis/module/export - - return genesis -} diff --git a/x/fswap/genesis_test.go b/x/fswap/genesis_test.go deleted file mode 100644 index 43bc7afc7c..0000000000 --- a/x/fswap/genesis_test.go +++ /dev/null @@ -1 +0,0 @@ -package fswap_test diff --git a/x/fswap/keeper/genesis.go b/x/fswap/keeper/genesis.go new file mode 100644 index 0000000000..6fd7619da0 --- /dev/null +++ b/x/fswap/keeper/genesis.go @@ -0,0 +1,40 @@ +package keeper + +import ( + sdk "github.com/Finschia/finschia-sdk/types" + "github.com/Finschia/finschia-sdk/x/fswap/types" +) + +const ( + DefaultOldCoins string = "cony" +) + +var ( + DefaultSwapRate = sdk.NewDecWithPrec(148079656, 6) +) + +// InitGenesis initializes the capability module's state from a provided genesis +// state. +func (k Keeper) InitGenesis(ctx sdk.Context, bk types.BankKeeper, genState types.GenesisState) error { + if err := k.SetParams(ctx, genState.Params); err != nil { + return err + } + if err := k.SetSwapped(ctx, genState.Swapped); err != nil { + return err + } + totalOldCoinsSupply := bk.GetSupply(ctx, DefaultOldCoins).Amount + totalNewCoinsSupply := DefaultSwapRate.MulInt(totalOldCoinsSupply) + totalNewCoins := sdk.NewDecCoinFromDec(genState.Params.NewCoinDenom, totalNewCoinsSupply) + if err := k.SetTotalSupply(ctx, totalNewCoins); err != nil { + return err + } + return nil +} + +// ExportGenesis returns the capability module's exported genesis. +func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + return &types.GenesisState{ + Params: k.GetParams(ctx), + Swapped: k.GetSwapped(ctx), + } +} diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go new file mode 100644 index 0000000000..9429264902 --- /dev/null +++ b/x/fswap/keeper/genesis_test.go @@ -0,0 +1 @@ +package keeper_test diff --git a/x/fswap/module.go b/x/fswap/module.go index 4992b043fe..c47665a60d 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -149,14 +149,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra // Initialize global index to index in genesis state cdc.MustUnmarshalJSON(gs, &genState) - InitGenesis(ctx, am.keeper, genState) + am.keeper.InitGenesis(ctx, am.bankKeeper, genState) return []abci.ValidatorUpdate{} } // ExportGenesis returns the capability module's exported genesis state as raw JSON bytes. func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { - genState := ExportGenesis(ctx, am.keeper) + genState := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(genState) } diff --git a/x/fswap/simulation/simap.go b/x/fswap/simulation/helpers.go similarity index 100% rename from x/fswap/simulation/simap.go rename to x/fswap/simulation/helpers.go diff --git a/x/fswap/types/errors.go b/x/fswap/types/errors.go index c0b627de88..4f0d76d856 100644 --- a/x/fswap/types/errors.go +++ b/x/fswap/types/errors.go @@ -8,5 +8,5 @@ import ( // x/fswap module sentinel errors var ( - ErrSample = sdkerrors.Register(ModuleName, 1100, "sample error") + ErrParamsNotFound = sdkerrors.Register(ModuleName, 1100, "params does not exist") ) diff --git a/x/fswap/types/expected_keepers.go b/x/fswap/types/expected_keepers.go index 6134a117d7..195cd89bdc 100644 --- a/x/fswap/types/expected_keepers.go +++ b/x/fswap/types/expected_keepers.go @@ -15,4 +15,5 @@ type AccountKeeper interface { type BankKeeper interface { SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins // Methods imported from bank should be defined here + GetSupply(ctx sdk.Context, denom string) sdk.Coin } diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index 8df94bae23..08a19f7f40 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -1,17 +1,10 @@ package types -import ( -// this line is used by starport scaffolding # genesis/types/import -) - -// DefaultIndex is the default capability global index -const DefaultIndex uint64 = 1 - // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - // this line is used by starport scaffolding # genesis/types/default - Params: DefaultParams(), + Params: DefaultParams(), + Swapped: DefaultSwapped(), } } @@ -19,6 +12,13 @@ func DefaultGenesis() *GenesisState { // failure. func (gs GenesisState) Validate() error { // this line is used by starport scaffolding # genesis/types/validate + if err := gs.Params.Validate(); err != nil { + return err + } + + if err := gs.Swapped.Validate(); err != nil { + return err + } - return gs.Params.Validate() + return nil }