github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/dex/genesis.go (about) 1 package dex 2 3 import ( 4 "fmt" 5 6 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 7 "github.com/fibonacci-chain/fbc/x/dex/types" 8 ordertypes "github.com/fibonacci-chain/fbc/x/order/types" 9 ) 10 11 // GenesisState - all dex state that must be provided at genesis 12 type GenesisState struct { 13 Params Params `json:"params"` 14 TokenPairs []*TokenPair `json:"token_pairs"` 15 WithdrawInfos WithdrawInfos `json:"withdraw_infos"` 16 ProductLocks ordertypes.ProductLockMap `json:"product_locks"` 17 Operators DEXOperators `json:"operators"` 18 MaxTokenPairID uint64 `json:"max_token_pair_id" yaml:"max_token_pair_id"` 19 } 20 21 // DefaultGenesisState - default GenesisState used by Cosmos Hub 22 // TODO: check how the added params' influence export facility 23 func DefaultGenesisState() GenesisState { 24 return GenesisState{ 25 Params: *DefaultParams(), 26 TokenPairs: nil, 27 WithdrawInfos: nil, 28 ProductLocks: *ordertypes.NewProductLockMap(), 29 Operators: nil, 30 MaxTokenPairID: 0, 31 } 32 } 33 34 // ValidateGenesis validates the slashing genesis parameters 35 func ValidateGenesis(data GenesisState) error { 36 for _, pair := range data.TokenPairs { 37 if pair.ID <= 0 { 38 return fmt.Errorf("invalid tx tokenPair ID: %d", pair.ID) 39 } 40 } 41 return nil 42 } 43 44 // InitGenesis initialize default parameters 45 // and the keeper's address to pubkey map 46 func InitGenesis(ctx sdk.Context, keeper IKeeper, data GenesisState) { 47 // if module account dosen't exist, it will create automatically 48 moduleAcc := keeper.GetSupplyKeeper().GetModuleAccount(ctx, types.ModuleName) 49 if moduleAcc == nil { 50 panic(fmt.Sprintf("%s module account has not been set", types.ModuleName)) 51 } 52 53 // set params 54 keeper.SetParams(ctx, data.Params) 55 56 // reset operators 57 for _, operator := range data.Operators { 58 keeper.SetOperator(ctx, operator) 59 } 60 // set maxID 61 keeper.SetMaxTokenPairID(ctx, data.MaxTokenPairID) 62 63 // reset token pair 64 for _, pair := range data.TokenPairs { 65 err := keeper.SaveTokenPair(ctx, pair) 66 if err != nil { 67 panic(err) 68 } 69 } 70 71 // reset delay withdraw queue 72 for _, withdrawInfo := range data.WithdrawInfos { 73 keeper.SetWithdrawInfo(ctx, withdrawInfo) 74 keeper.SetWithdrawCompleteTimeAddress(ctx, withdrawInfo.CompleteTime, withdrawInfo.Owner) 75 } 76 77 for k, v := range data.ProductLocks.Data { 78 keeper.LockTokenPair(ctx, k, v) 79 } 80 } 81 82 // ExportGenesis writes the current store values 83 // to a genesis file, which can be imported again 84 // with InitGenesis 85 func ExportGenesis(ctx sdk.Context, keeper IKeeper) (data GenesisState) { 86 params := keeper.GetParams(ctx) 87 88 var operators types.DEXOperators 89 keeper.IterateOperators(ctx, func(operator types.DEXOperator) bool { 90 operators = append(operators, operator) 91 return false 92 }) 93 94 tokenPairs := keeper.GetTokenPairs(ctx) 95 96 var withdrawInfos WithdrawInfos 97 keeper.IterateWithdrawInfo(ctx, func(_ int64, withdrawInfo WithdrawInfo) (stop bool) { 98 withdrawInfos = append(withdrawInfos, withdrawInfo) 99 return false 100 }) 101 return GenesisState{ 102 Params: params, 103 TokenPairs: tokenPairs, 104 WithdrawInfos: withdrawInfos, 105 ProductLocks: *keeper.LoadProductLocks(ctx), 106 Operators: operators, 107 MaxTokenPairID: keeper.GetMaxTokenPairID(ctx), 108 } 109 }