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

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     6  )
     7  
     8  // used for import / export via genesis json
     9  type PoolHistoricalRewardsRecord struct {
    10  	PoolName string                `json:"pool_name" yaml:"pool_name"`
    11  	Period   uint64                `json:"period" yaml:"period"`
    12  	Rewards  PoolHistoricalRewards `json:"rewards" yaml:"rewards"`
    13  }
    14  
    15  // used for import / export via genesis json
    16  type PoolCurrentRewardsRecord struct {
    17  	PoolName string             `json:"pool_name" yaml:"pool_name"`
    18  	Rewards  PoolCurrentRewards `json:"rewards" yaml:"rewards"`
    19  }
    20  
    21  // used for import / export via genesis json
    22  type LockInfoRecord struct {
    23  	PoolName    string         `json:"pool_name" yaml:"pool_name"`
    24  	LockAddress sdk.AccAddress `json:"lock_address" yaml:"lock_address"`
    25  	LockInfo    LockInfo       `json:"lock_info" yaml:"lock_info"`
    26  }
    27  
    28  // GenesisState - all farm state that must be provided at genesis
    29  type GenesisState struct {
    30  	Pools                 FarmPools                     `json:"pools" yaml:"pools"`
    31  	LockInfos             []LockInfo                    `json:"lock_infos" yaml:"lock_infos"`
    32  	PoolHistoricalRewards []PoolHistoricalRewardsRecord `json:"historical_rewards" yaml:"historical_rewards"`
    33  	PoolCurrentRewards    []PoolCurrentRewardsRecord    `json:"current_rewards" yaml:"current_rewards"`
    34  	WhiteList             PoolNameList                  `json:"pools_yield_native_token" yaml:"pools_yield_native_token"`
    35  	Params                Params                        `json:"params" yaml:"params"`
    36  }
    37  
    38  // NewGenesisState creates a new GenesisState object
    39  func NewGenesisState(pools FarmPools, lockInfos []LockInfo, histories []PoolHistoricalRewardsRecord,
    40  	currents []PoolCurrentRewardsRecord, whiteList PoolNameList, params Params,
    41  ) GenesisState {
    42  	return GenesisState{
    43  		Pools:                 pools,
    44  		LockInfos:             lockInfos,
    45  		PoolHistoricalRewards: histories,
    46  		PoolCurrentRewards:    currents,
    47  		WhiteList:             whiteList,
    48  		Params:                params,
    49  	}
    50  }
    51  
    52  // DefaultGenesisState - default GenesisState used by Cosmos Hub
    53  func DefaultGenesisState() GenesisState {
    54  	return GenesisState{
    55  		Pools:                 FarmPools{},
    56  		LockInfos:             []LockInfo{},
    57  		PoolHistoricalRewards: []PoolHistoricalRewardsRecord{},
    58  		PoolCurrentRewards:    []PoolCurrentRewardsRecord{},
    59  		Params:                DefaultParams(),
    60  	}
    61  }
    62  
    63  // ValidateGenesis validates the farm genesis parameters
    64  func ValidateGenesis(data GenesisState) error {
    65  	if len(data.Pools) != len(data.PoolCurrentRewards) {
    66  		return fmt.Errorf("count of pools(%d) is not equal to that of current rewards(%d)",
    67  			len(data.Pools), len(data.PoolCurrentRewards))
    68  	}
    69  
    70  	var expectedReferenceCount uint16
    71  	for _, h := range data.PoolHistoricalRewards {
    72  		expectedReferenceCount += h.Rewards.ReferenceCount
    73  	}
    74  
    75  	actualReferenceCount := len(data.LockInfos) + len(data.PoolCurrentRewards)
    76  	if actualReferenceCount != int(expectedReferenceCount) {
    77  		return fmt.Errorf("actual reference count(%d) is not equal to expected reference count(%d)",
    78  			actualReferenceCount, expectedReferenceCount)
    79  	}
    80  	return nil
    81  }