github.com/lino-network/lino@v0.6.11/x/price/model/store.go (about)

     1  package model
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  
     7  	linotypes "github.com/lino-network/lino/types"
     8  	"github.com/lino-network/lino/x/price/types"
     9  )
    10  
    11  var (
    12  	fedPriceSubStore       = []byte{0x00} // validator's latest price.
    13  	priceHistorySubStore   = []byte{0x01} // hourly prices
    14  	currentPriceSubStore   = []byte{0x02} // current price
    15  	lastValidatorsSubStore = []byte{0x03} // validators in last update time.
    16  	feedHistorySubStore    = []byte{0x04} // fed history.
    17  )
    18  
    19  // GetFedPriceKey - price key.
    20  func GetFedPriceKey(u linotypes.AccountKey) []byte {
    21  	return append(fedPriceSubStore, u...)
    22  }
    23  
    24  // GetPriceHistoryKey - hourly price.
    25  func GetPriceHistoryKey() []byte {
    26  	return priceHistorySubStore
    27  }
    28  
    29  // GetCurrentPriceKey - get current price.
    30  func GetCurrentPriceKey() []byte {
    31  	return currentPriceSubStore
    32  }
    33  
    34  // GetLastValidatorsKey - get last validators.
    35  func GetLastValidatorsKey() []byte {
    36  	return lastValidatorsSubStore
    37  }
    38  
    39  // GetLastValidatorsKey - get last validators.
    40  func GetFeedHistoryKey() []byte {
    41  	return feedHistorySubStore
    42  }
    43  
    44  // PriceStorage - price storage
    45  type PriceStorage struct {
    46  	key sdk.StoreKey
    47  	cdc *wire.Codec
    48  }
    49  
    50  // NewPriceStorage - returns a new PriceStorage, binary encoded.
    51  func NewPriceStorage(key sdk.StoreKey) PriceStorage {
    52  	cdc := wire.New()
    53  	wire.RegisterCrypto(cdc)
    54  
    55  	return PriceStorage{
    56  		key: key,
    57  		cdc: cdc,
    58  	}
    59  }
    60  
    61  // GetFedPrice - get fed price of validator from KVStore
    62  func (ps PriceStorage) GetFedPrice(ctx sdk.Context, val linotypes.AccountKey) (*FedPrice, sdk.Error) {
    63  	store := ctx.KVStore(ps.key)
    64  	key := GetFedPriceKey(val)
    65  	infoByte := store.Get(key)
    66  	if infoByte == nil {
    67  		return nil, types.ErrFedPriceNotFound(val)
    68  	}
    69  	price := new(FedPrice)
    70  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, price)
    71  	return price, nil
    72  }
    73  
    74  // SetFedPrice - set fed price to KVStore
    75  func (ps PriceStorage) SetFedPrice(ctx sdk.Context, price *FedPrice) {
    76  	store := ctx.KVStore(ps.key)
    77  	bytes := ps.cdc.MustMarshalBinaryLengthPrefixed(*price)
    78  	store.Set(GetFedPriceKey(price.Validator), bytes)
    79  }
    80  
    81  // GetPriceHistory - return price history.
    82  func (ps PriceStorage) GetPriceHistory(ctx sdk.Context) []TimePrice {
    83  	store := ctx.KVStore(ps.key)
    84  	key := GetPriceHistoryKey()
    85  	bytes := store.Get(key)
    86  	if bytes == nil {
    87  		return nil
    88  	}
    89  	price := make([]TimePrice, 0)
    90  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(bytes, &price)
    91  	return price
    92  }
    93  
    94  // SetPriceHistory - set price history.
    95  func (ps PriceStorage) SetPriceHistory(ctx sdk.Context, prices []TimePrice) {
    96  	store := ctx.KVStore(ps.key)
    97  	bytes := ps.cdc.MustMarshalBinaryLengthPrefixed(prices)
    98  	store.Set(GetPriceHistoryKey(), bytes)
    99  }
   100  
   101  // GetCurrentPrice - return current price
   102  func (ps PriceStorage) GetCurrentPrice(ctx sdk.Context) (*TimePrice, sdk.Error) {
   103  	store := ctx.KVStore(ps.key)
   104  	key := GetCurrentPriceKey()
   105  	bytes := store.Get(key)
   106  	if bytes == nil {
   107  		return nil, types.ErrCurrentPriceNotFound()
   108  	}
   109  	price := new(TimePrice)
   110  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(bytes, price)
   111  	return price, nil
   112  }
   113  
   114  func (ps PriceStorage) SetCurrentPrice(ctx sdk.Context, price *TimePrice) {
   115  	store := ctx.KVStore(ps.key)
   116  	bytes := ps.cdc.MustMarshalBinaryLengthPrefixed(price)
   117  	store.Set(GetCurrentPriceKey(), bytes)
   118  }
   119  
   120  func (ps PriceStorage) GetLastValidators(ctx sdk.Context) []linotypes.AccountKey {
   121  	store := ctx.KVStore(ps.key)
   122  	key := GetLastValidatorsKey()
   123  	bytes := store.Get(key)
   124  	if bytes == nil {
   125  		return nil
   126  	}
   127  	vals := make([]linotypes.AccountKey, 0)
   128  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(bytes, &vals)
   129  	return vals
   130  }
   131  
   132  func (ps PriceStorage) SetLastValidators(ctx sdk.Context, last []linotypes.AccountKey) {
   133  	store := ctx.KVStore(ps.key)
   134  	bytes := ps.cdc.MustMarshalBinaryLengthPrefixed(last)
   135  	store.Set(GetLastValidatorsKey(), bytes)
   136  }
   137  
   138  func (ps PriceStorage) GetFeedHistory(ctx sdk.Context) []FeedHistory {
   139  	store := ctx.KVStore(ps.key)
   140  	key := GetFeedHistoryKey()
   141  	bytes := store.Get(key)
   142  	if bytes == nil {
   143  		return nil
   144  	}
   145  	history := make([]FeedHistory, 0)
   146  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(bytes, &history)
   147  	return history
   148  }
   149  
   150  func (ps PriceStorage) SetFeedHistory(ctx sdk.Context, history []FeedHistory) {
   151  	store := ctx.KVStore(ps.key)
   152  	bytes := ps.cdc.MustMarshalBinaryLengthPrefixed(history)
   153  	store.Set(GetFeedHistoryKey(), bytes)
   154  }