github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/token/genesis.go (about)

     1  package token
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/x/common"
     9  	"github.com/fibonacci-chain/fbc/x/token/types"
    10  )
    11  
    12  // default owner of fibo
    13  const DefaultTokenOwner = "fb18rrc500xu2haw7vyksqlj2lfp9xex2hczv3jkx"
    14  
    15  // all state that must be provided in genesis file
    16  type GenesisState struct {
    17  	Params       types.Params     `json:"params"`
    18  	Tokens       []types.Token    `json:"tokens"`
    19  	LockedAssets []types.AccCoins `json:"locked_assets"`
    20  	LockedFees   []types.AccCoins `json:"locked_fees"`
    21  }
    22  
    23  // default GenesisState used by Cosmos Hub
    24  func defaultGenesisState() GenesisState {
    25  	return GenesisState{
    26  		Params:       types.DefaultParams(),
    27  		Tokens:       []types.Token{defaultGenesisStatefibo()},
    28  		LockedAssets: nil,
    29  		LockedFees:   nil,
    30  	}
    31  }
    32  
    33  // default fibo information
    34  func defaultGenesisStatefibo() types.Token {
    35  	addr, err := sdk.AccAddressFromBech32(DefaultTokenOwner)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  
    40  	totalSupply := sdk.NewDec(1000000000)
    41  	return types.Token{
    42  		Description:         "FB Group Global Utility Token",
    43  		Symbol:              common.NativeToken,
    44  		OriginalSymbol:      common.NativeToken,
    45  		WholeName:           "FIBO",
    46  		OriginalTotalSupply: totalSupply,
    47  		Owner:               addr,
    48  		Mintable:            true,
    49  	}
    50  }
    51  
    52  func validateGenesis(data GenesisState) error {
    53  	for _, token := range data.Tokens {
    54  		msg := types.NewMsgTokenIssue(token.Description,
    55  			token.Symbol,
    56  			token.OriginalSymbol,
    57  			token.WholeName,
    58  			token.OriginalTotalSupply.String(),
    59  			token.Owner,
    60  			token.Mintable)
    61  
    62  		err := msg.ValidateBasic()
    63  		if err != nil {
    64  			return errors.New(err.Error())
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  // initGenesis initialize default parameters
    71  // and the keeper's address to pubkey map
    72  func initGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) {
    73  	// if module account dosen't exist, it will create automatically
    74  	moduleAcc := keeper.supplyKeeper.GetModuleAccount(ctx, types.ModuleName)
    75  	if moduleAcc == nil {
    76  		panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
    77  	}
    78  
    79  	// set params
    80  	keeper.SetParams(ctx, data.Params)
    81  
    82  	for _, token := range data.Tokens {
    83  		keeper.NewToken(ctx, token)
    84  	}
    85  
    86  	for _, lock := range data.LockedAssets {
    87  		if err := keeper.updateLockedCoins(ctx, lock.Acc, lock.Coins, true, types.LockCoinsTypeQuantity); err != nil {
    88  			panic(err)
    89  		}
    90  	}
    91  	for _, lock := range data.LockedFees {
    92  		if err := keeper.updateLockedCoins(ctx, lock.Acc, lock.Coins, true, types.LockCoinsTypeFee); err != nil {
    93  			panic(err)
    94  		}
    95  	}
    96  }
    97  
    98  // ExportGenesis writes the current store values
    99  // to a genesis file, which can be imported again
   100  // with initGenesis
   101  func ExportGenesis(ctx sdk.Context, keeper Keeper) (data GenesisState) {
   102  	params := keeper.GetParams(ctx)
   103  	tokens := keeper.GetTokensInfo(ctx)
   104  	lockedAsset := keeper.GetAllLockedCoins(ctx)
   105  
   106  	var lockedFees []types.AccCoins
   107  	keeper.IterateLockedFees(ctx, func(acc sdk.AccAddress, coins sdk.SysCoins) bool {
   108  		lockedFees = append(lockedFees,
   109  			types.AccCoins{
   110  				Acc:   acc,
   111  				Coins: coins,
   112  			})
   113  		return false
   114  	})
   115  
   116  	return GenesisState{
   117  		Params:       params,
   118  		Tokens:       tokens,
   119  		LockedAssets: lockedAsset,
   120  		LockedFees:   lockedFees,
   121  	}
   122  }