github.com/lino-network/lino@v0.6.11/x/account/model/storage.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/utils"
     9  	"github.com/lino-network/lino/x/account/types"
    10  )
    11  
    12  var (
    13  	AccountInfoSubstore   = []byte{0x00}
    14  	AccountBankSubstore   = []byte{0x01}
    15  	AccountMetaSubstore   = []byte{0x02}
    16  	AccountPoolSubstore   = []byte{0x04}
    17  	AccountSupplySubstore = []byte{0x05}
    18  )
    19  
    20  // AccountStorage - account storage
    21  type AccountStorage struct {
    22  	// The (unexposed) key used to access the store from the Context.
    23  	key sdk.StoreKey
    24  
    25  	// The wire codec for binary encoding/decoding of accounts
    26  	cdc *wire.Codec
    27  }
    28  
    29  // NewLinoAccountStorage - creates and returns a account manager
    30  func NewAccountStorage(key sdk.StoreKey) AccountStorage {
    31  	cdc := wire.New()
    32  	wire.RegisterCrypto(cdc)
    33  	cdc.Seal()
    34  
    35  	return AccountStorage{
    36  		key: key,
    37  		cdc: cdc,
    38  	}
    39  }
    40  
    41  // DoesAccountExist - returns true when a specific account exist in the KVStore.
    42  func (as AccountStorage) DoesAccountExist(ctx sdk.Context, accKey linotypes.AccountKey) bool {
    43  	store := ctx.KVStore(as.key)
    44  	return store.Has(GetAccountInfoKey(accKey))
    45  }
    46  
    47  // GetInfo - returns general account info of a specific account, returns error otherwise.
    48  func (as AccountStorage) GetInfo(ctx sdk.Context, accKey linotypes.AccountKey) (*AccountInfo, sdk.Error) {
    49  	store := ctx.KVStore(as.key)
    50  	infoByte := store.Get(GetAccountInfoKey(accKey))
    51  	if infoByte == nil {
    52  		return nil, types.ErrAccountNotFound(accKey)
    53  	}
    54  	info := new(AccountInfo)
    55  	as.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, info)
    56  	return info, nil
    57  }
    58  
    59  // SetInfo - sets general account info to a specific account, returns error if any.
    60  func (as AccountStorage) SetInfo(ctx sdk.Context, accInfo *AccountInfo) {
    61  	store := ctx.KVStore(as.key)
    62  	infoByte := as.cdc.MustMarshalBinaryLengthPrefixed(*accInfo)
    63  	store.Set(GetAccountInfoKey(accInfo.Username), infoByte)
    64  }
    65  
    66  // GetBank - returns bank info of a specific address, returns error if any.
    67  func (as AccountStorage) GetBank(ctx sdk.Context, addr sdk.Address) (*AccountBank, sdk.Error) {
    68  	store := ctx.KVStore(as.key)
    69  	bankByte := store.Get(GetAccountBankKey(addr))
    70  	if bankByte == nil {
    71  		return nil, types.ErrAccountBankNotFound(addr)
    72  	}
    73  	bank := new(AccountBank)
    74  	as.cdc.MustUnmarshalBinaryLengthPrefixed(bankByte, bank)
    75  	return bank, nil
    76  }
    77  
    78  // SetBank - sets bank info for a given address, returns error if any.
    79  func (as AccountStorage) SetBank(ctx sdk.Context, addr sdk.Address, accBank *AccountBank) {
    80  	store := ctx.KVStore(as.key)
    81  	bankByte := as.cdc.MustMarshalBinaryLengthPrefixed(*accBank)
    82  	store.Set(GetAccountBankKey(addr), bankByte)
    83  }
    84  
    85  // GetMeta - returns meta of a given account that are tiny and frequently updated fields.
    86  func (as AccountStorage) GetMeta(ctx sdk.Context, accKey linotypes.AccountKey) *AccountMeta {
    87  	store := ctx.KVStore(as.key)
    88  	metaByte := store.Get(GetAccountMetaKey(accKey))
    89  	if metaByte == nil {
    90  		return &AccountMeta{
    91  			JSONMeta: "",
    92  		}
    93  	}
    94  	meta := new(AccountMeta)
    95  	as.cdc.MustUnmarshalBinaryLengthPrefixed(metaByte, meta)
    96  	return meta
    97  }
    98  
    99  // SetMeta - sets meta for a given account, returns error if any.
   100  func (as AccountStorage) SetMeta(ctx sdk.Context, accKey linotypes.AccountKey, accMeta *AccountMeta) {
   101  	store := ctx.KVStore(as.key)
   102  	metaByte := as.cdc.MustMarshalBinaryLengthPrefixed(*accMeta)
   103  	store.Set(GetAccountMetaKey(accKey), metaByte)
   104  }
   105  
   106  func (as AccountStorage) SetPool(ctx sdk.Context, pool *Pool) {
   107  	store := ctx.KVStore(as.key)
   108  	bz := as.cdc.MustMarshalBinaryLengthPrefixed(pool)
   109  	store.Set(GetAccountPoolKey(pool.Name), bz)
   110  }
   111  
   112  func (as AccountStorage) GetPool(ctx sdk.Context, name linotypes.PoolName) (*Pool, sdk.Error) {
   113  	store := ctx.KVStore(as.key)
   114  	bz := store.Get(GetAccountPoolKey(name))
   115  	if bz == nil {
   116  		return nil, types.ErrPoolNotFound(name)
   117  	}
   118  	pool := new(Pool)
   119  	as.cdc.MustUnmarshalBinaryLengthPrefixed(bz, pool)
   120  	return pool, nil
   121  }
   122  
   123  func (as AccountStorage) GetSupply(ctx sdk.Context) *Supply {
   124  	store := ctx.KVStore(as.key)
   125  	bz := store.Get(GetAccountSupplyKey())
   126  	if bz == nil {
   127  		panic("Lino Supply Not Initialized")
   128  	}
   129  	supply := new(Supply)
   130  	as.cdc.MustUnmarshalBinaryLengthPrefixed(bz, supply)
   131  	return supply
   132  }
   133  
   134  func (as AccountStorage) SetSupply(ctx sdk.Context, supply *Supply) {
   135  	store := ctx.KVStore(as.key)
   136  	bz := as.cdc.MustMarshalBinaryLengthPrefixed(supply)
   137  	store.Set(GetAccountSupplyKey(), bz)
   138  }
   139  
   140  func (as AccountStorage) PartialStoreMap(ctx sdk.Context) utils.StoreMap {
   141  	store := ctx.KVStore(as.key)
   142  	stores := []utils.SubStore{
   143  		{
   144  			Store:      store,
   145  			Prefix:     AccountInfoSubstore,
   146  			ValCreator: func() interface{} { return new(AccountInfo) },
   147  			Decoder:    as.cdc.MustUnmarshalBinaryLengthPrefixed,
   148  		},
   149  		{
   150  			Store:      store,
   151  			Prefix:     AccountBankSubstore,
   152  			ValCreator: func() interface{} { return new(AccountBank) },
   153  			Decoder:    as.cdc.MustUnmarshalBinaryLengthPrefixed,
   154  		},
   155  		{
   156  			Store:      store,
   157  			Prefix:     AccountMetaSubstore,
   158  			ValCreator: func() interface{} { return new(AccountMeta) },
   159  			Decoder:    as.cdc.MustUnmarshalBinaryLengthPrefixed,
   160  		},
   161  		{
   162  			Store:      store,
   163  			Prefix:     AccountPoolSubstore,
   164  			ValCreator: func() interface{} { return new(Pool) },
   165  			Decoder:    as.cdc.MustUnmarshalBinaryLengthPrefixed,
   166  		},
   167  	}
   168  	return utils.NewStoreMap(stores)
   169  }
   170  
   171  // GetAccountInfoPrefix - "account info substore"
   172  func GetAccountInfoPrefix() []byte {
   173  	return AccountInfoSubstore
   174  }
   175  
   176  // GetAccountInfoKey - "account info substore" + "username"
   177  func GetAccountInfoKey(accKey linotypes.AccountKey) []byte {
   178  	return append(GetAccountInfoPrefix(), accKey...)
   179  }
   180  
   181  // GetAccountBankKey - "account bank substore" + "username"
   182  func GetAccountBankKey(addr sdk.Address) []byte {
   183  	return append(AccountBankSubstore, addr.Bytes()...)
   184  }
   185  
   186  // GetAccountMetaKey - "account meta substore" + "username"
   187  func GetAccountMetaKey(accKey linotypes.AccountKey) []byte {
   188  	return append(AccountMetaSubstore, accKey...)
   189  }
   190  
   191  // GetAccountPoolKey - "AccountPoolSubstore" + "pool name"
   192  func GetAccountPoolKey(poolname linotypes.PoolName) []byte {
   193  	return append(AccountPoolSubstore, poolname...)
   194  }
   195  
   196  // GetAccountSupplyKey - AccountSupplySubstore
   197  func GetAccountSupplyKey() []byte {
   198  	return AccountSupplySubstore
   199  }