Skip to content

Commit

Permalink
genesis added for common
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsingh33 committed Dec 15, 2023
1 parent 2d96f73 commit 44678fc
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 84 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.0
cosmossdk.io/math v1.1.2
github.com/CosmWasm/wasmd v0.41.0
github.com/CosmWasm/wasmvm v1.3.0
Expand Down Expand Up @@ -49,7 +50,6 @@ require (
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/core v0.6.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/log v1.2.1 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
Expand Down
10 changes: 8 additions & 2 deletions proto/comdex/common/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ option go_package = "github.com/comdex-official/comdex/x/common/types";

// GenesisState defines the common module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated WhitelistedContract whitelisted_contracts = 2 [ (gogoproto.moretags) = "yaml:\"whitelisted_contracts\"" ];
Params params = 1 [
(gogoproto.moretags) = "yaml:\"params\"",
(gogoproto.nullable) = false
];
repeated WhitelistedContract whitelisted_contracts = 2 [
(gogoproto.moretags) = "yaml:\"whitelisted_contracts\"",
(gogoproto.nullable) = false
];
}
26 changes: 17 additions & 9 deletions x/common/genesis.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package common

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/comdex-official/comdex/x/common/keeper"
"github.com/comdex-official/comdex/x/common/types"
sdk "github.com/cosmos/cosmos-sdk/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
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState *types.GenesisState) {
var (
GameID uint64
)
// this line is used by starport scaffolding # genesis/module/init
for _, item := range genState.WhitelistedContracts {
if item.GameId > GameID {
GameID = item.GameId
}
k.SetContract(ctx, item)
}
k.SetGameID(ctx, GameID)
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
return types.NewGenesisState(
k.GetAllContract(ctx),
k.GetParams(ctx),
)
}
6 changes: 3 additions & 3 deletions x/common/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {

// DefaultGenesis returns the capability module's default genesis state.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesis())
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}

// ValidateGenesis performs genesis state validation for the capability module.
Expand All @@ -66,7 +66,7 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
return genState.Validate()
return genState.ValidateGenesis()
}

// RegisterRESTRoutes registers the capability module's REST service handlers.
Expand Down Expand Up @@ -144,7 +144,7 @@ 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)
InitGenesis(ctx, am.keeper, &genState)

return []abci.ValidatorUpdate{}
}
Expand Down
27 changes: 11 additions & 16 deletions x/common/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
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 {
func NewGenesisState(whitelistedContracts []WhitelistedContract, params Params) *GenesisState {
return &GenesisState{
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
WhitelistedContracts: whitelistedContracts,
Params: params,
}
}

// 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
func DefaultGenesisState() *GenesisState {
return NewGenesisState(
[]WhitelistedContract{},
DefaultParams(),
)
}

return gs.Params.Validate()
func (m *GenesisState) ValidateGenesis() error {
return nil
}
36 changes: 18 additions & 18 deletions x/common/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 35 additions & 35 deletions x/common/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package types_test

import (
"testing"
// import (
// "testing"

"github.com/stretchr/testify/require"
"github.com/comdex-official/comdex/x/common/types"
)
// "github.com/stretchr/testify/require"
// "github.com/comdex-official/comdex/x/common/types"
// )

func TestGenesisState_Validate(t *testing.T) {
for _, tc := range []struct {
desc string
genState *types.GenesisState
valid bool
} {
{
desc: "default is valid",
genState: types.DefaultGenesis(),
valid: true,
},
{
desc: "valid genesis state",
genState: &types.GenesisState{
// func TestGenesisState_Validate(t *testing.T) {
// for _, tc := range []struct {
// desc string
// genState *types.GenesisState
// valid bool
// } {
// {
// desc: "default is valid",
// genState: types.DefaultGenesis(),
// valid: true,
// },
// {
// desc: "valid genesis state",
// genState: &types.GenesisState{

// this line is used by starport scaffolding # types/genesis/validField
},
valid: true,
},
// this line is used by starport scaffolding # types/genesis/testcase
} {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
// // this line is used by starport scaffolding # types/genesis/validField
// },
// valid: true,
// },
// // this line is used by starport scaffolding # types/genesis/testcase
// } {
// t.Run(tc.desc, func(t *testing.T) {
// err := tc.genState.Validate()
// if tc.valid {
// require.NoError(t, err)
// } else {
// require.Error(t, err)
// }
// })
// }
// }

0 comments on commit 44678fc

Please sign in to comment.