Skip to content

Commit

Permalink
feat: add initGenesis
Browse files Browse the repository at this point in the history
Signed-off-by: 170210 <j170210@icloud.com>
  • Loading branch information
170210 committed Apr 16, 2024
1 parent 9fc8271 commit e44c61a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 39 deletions.
2 changes: 1 addition & 1 deletion x/ERRORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 0 additions & 24 deletions x/fswap/genesis.go

This file was deleted.

1 change: 0 additions & 1 deletion x/fswap/genesis_test.go

This file was deleted.

40 changes: 40 additions & 0 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -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 (

Check failure on line 12 in x/fswap/keeper/genesis.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
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),
}
}
1 change: 1 addition & 0 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package keeper_test
4 changes: 2 additions & 2 deletions x/fswap/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 152 in x/fswap/module.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `am.keeper.InitGenesis` is not checked (errcheck)

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)
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion x/fswap/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
1 change: 1 addition & 0 deletions x/fswap/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 10 additions & 10 deletions x/fswap/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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(),
}
}

// Validate performs basic genesis state validation returning an error upon any
// 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
}

0 comments on commit e44c61a

Please sign in to comment.