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

     1  package order
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/x/order/keeper"
     8  	"github.com/fibonacci-chain/fbc/x/order/types"
     9  )
    10  
    11  // GenesisState - all order state that must be provided at genesis
    12  type GenesisState struct {
    13  	Params     types.Params   `json:"params"`
    14  	OpenOrders []*types.Order `json:"open_orders"`
    15  }
    16  
    17  // DefaultGenesisState - default GenesisState used by Cosmos Hub
    18  func DefaultGenesisState() GenesisState {
    19  	return GenesisState{
    20  		Params:     types.DefaultParams(),
    21  		OpenOrders: nil,
    22  	}
    23  }
    24  
    25  // ValidateGenesis validates the slashing genesis parameters
    26  func ValidateGenesis(data GenesisState) error {
    27  	return nil
    28  }
    29  
    30  // InitGenesis initialize default parameters
    31  // and the keeper's address to pubkey map
    32  func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data GenesisState) {
    33  	keeper.SetParams(ctx, &data.Params)
    34  
    35  	// reset open order& depth book
    36  	for _, order := range data.OpenOrders {
    37  		if order == nil {
    38  			panic("the nil pointer is not expected")
    39  		}
    40  		height := types.GetBlockHeightFromOrderID(order.OrderID)
    41  
    42  		futureHeight := height + data.Params.OrderExpireBlocks
    43  		futureExpireHeightList := keeper.GetExpireBlockHeight(ctx, futureHeight)
    44  		futureExpireHeightList = append(futureExpireHeightList, height)
    45  		keeper.SetExpireBlockHeight(ctx, futureHeight, futureExpireHeightList)
    46  
    47  		orderNum := keeper.GetBlockOrderNum(ctx, height)
    48  		keeper.SetBlockOrderNum(ctx, height, orderNum+1)
    49  		keeper.SetOrder(ctx, order.OrderID, order)
    50  
    51  		// update depth book and orderIDsMap in cache
    52  		keeper.InsertOrderIntoDepthBook(order)
    53  	}
    54  	if len(data.OpenOrders) > 0 {
    55  		keeper.Cache2Disk(ctx)
    56  	}
    57  }
    58  
    59  // ExportGenesis writes the current store values
    60  // to a genesis file, which can be imported again
    61  // with InitGenesis
    62  func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) (data GenesisState) {
    63  	params := keeper.GetParams(ctx)
    64  	tokenPairs := keeper.GetDexKeeper().GetTokenPairs(ctx)
    65  
    66  	var openOrders []*types.Order
    67  	var num int64 = 1
    68  	for _, pair := range tokenPairs {
    69  		if pair == nil {
    70  			panic("the nil pointer is not expected")
    71  		}
    72  		product := fmt.Sprintf("%s_%s", pair.BaseAssetSymbol, pair.QuoteAssetSymbol)
    73  		// update token pairs price
    74  		pair.InitPrice = keeper.GetLastPrice(ctx, product)
    75  		keeper.GetDexKeeper().UpdateTokenPair(ctx, product, pair)
    76  
    77  		// get open orders
    78  		depthBook := keeper.GetDepthBookFromDB(ctx, product)
    79  		var openIDs []string
    80  		for _, item := range depthBook.Items {
    81  			if item.SellQuantity.IsPositive() {
    82  				key := types.FormatOrderIDsKey(product, item.Price, types.SellOrder)
    83  				ids := keeper.GetProductPriceOrderIDsFromDB(ctx, key)
    84  
    85  				openIDs = append(openIDs, ids...)
    86  			}
    87  			if item.BuyQuantity.IsPositive() {
    88  				key := types.FormatOrderIDsKey(product, item.Price, types.BuyOrder)
    89  				ids := keeper.GetProductPriceOrderIDsFromDB(ctx, key)
    90  				openIDs = append(openIDs, ids...)
    91  			}
    92  		}
    93  
    94  		for _, orderID := range openIDs {
    95  			order := keeper.GetOrder(ctx, orderID)
    96  			if order.Status == types.OrderStatusFilled ||
    97  				order.Status == types.OrderStatusCancelled ||
    98  				order.Status == types.OrderStatusExpired {
    99  				continue
   100  			}
   101  			// change orderID for order expire and order id on new chain
   102  			// ID+genesisBlockHeight+1~n
   103  			//order.OrderID = common.FormatOrderID(1, num)
   104  			//order.Quantity = order.RemainQuantity
   105  			//order.Status = types.OrderStatusOpen
   106  			//order.TxHash=??
   107  			openOrders = append(openOrders, order)
   108  			num++
   109  		}
   110  	}
   111  
   112  	return GenesisState{
   113  		Params:     *params,
   114  		OpenOrders: openOrders,
   115  	}
   116  }