github.com/lino-network/lino@v0.6.11/x/developer/model/storage.go (about) 1 package model 2 3 import ( 4 "strings" 5 6 wire "github.com/cosmos/cosmos-sdk/codec" 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 9 linotypes "github.com/lino-network/lino/types" 10 "github.com/lino-network/lino/utils" 11 "github.com/lino-network/lino/x/developer/types" 12 ) 13 14 const ( 15 trueStr = "t" 16 ) 17 18 var ( 19 DeveloperSubstore = []byte{0x00} 20 IdaSubstore = []byte{0x01} 21 IdaBalanceSubstore = []byte{0x02} 22 ReservePoolSubstore = []byte{0x03} 23 AffiliatedAccSubstore = []byte{0x04} // (app, user) 24 UserRoleSubstore = []byte{0x05} // user role. 25 IdaStatsSubstore = []byte{0x06} 26 ) 27 28 // GetDeveloperKey - "developer substore" + "developer" 29 func GetDeveloperKey(accKey linotypes.AccountKey) []byte { 30 return append(DeveloperSubstore, accKey...) 31 } 32 33 // GetIDAKey - "ida substore" + "developer" 34 func GetIDAKey(accKey linotypes.AccountKey) []byte { 35 return append(IdaSubstore, accKey...) 36 } 37 38 // GetIDAStatsKey - "ida stats substore" + "developer" 39 func GetIDAStatsKey(accKey linotypes.AccountKey) []byte { 40 return append(IdaStatsSubstore, accKey...) 41 } 42 43 // GetIDABalanceKey - "ida balance substore" + "app" + "/" + "user" 44 func GetIDABalanceKey(app linotypes.AccountKey, user linotypes.AccountKey) []byte { 45 prefix := append(IdaBalanceSubstore, app...) 46 return append(append(prefix, []byte(linotypes.KeySeparator)...), user...) 47 } 48 49 // ParseIDABalanceKey parse balance key. 50 func ParseIDABalanceKey(key []byte) (app linotypes.AccountKey, user linotypes.AccountKey) { 51 parsed := strings.Split(string(key), linotypes.KeySeparator) 52 return linotypes.AccountKey(parsed[0]), linotypes.AccountKey(parsed[1]) 53 } 54 55 // GetAffiliatedAccAppPrefix - "affiliated account substore" + "app" + "/" 56 func GetAffiliatedAccAppPrefix(app linotypes.AccountKey) []byte { 57 prefix := append(AffiliatedAccSubstore, app...) 58 return append(prefix, []byte(linotypes.KeySeparator)...) 59 } 60 61 // GetAffiliatedAccKey - "affiliated app prefix" + "user" 62 func GetAffiliatedAccKey(app linotypes.AccountKey, user linotypes.AccountKey) []byte { 63 return append(GetAffiliatedAccAppPrefix(app), user...) 64 } 65 66 // ParseAffiliatedAccKey parse affiliated account key. 67 func ParseAffiliatedAccKey(key []byte) (app linotypes.AccountKey, user linotypes.AccountKey) { 68 parsed := strings.Split(string(key), linotypes.KeySeparator) 69 return linotypes.AccountKey(parsed[0]), linotypes.AccountKey(parsed[1]) 70 } 71 72 // GetUserRoleKey - "user" -> role 73 func GetUserRoleKey(user linotypes.AccountKey) []byte { 74 return append(UserRoleSubstore, user...) 75 } 76 77 // GetReservePoolKey - reserve pool key 78 func GetReservePoolKey() []byte { 79 return ReservePoolSubstore 80 } 81 82 // DeveloperStorage - developer storage 83 type DeveloperStorage struct { 84 key sdk.StoreKey 85 cdc *wire.Codec 86 } 87 88 // DeveloperStorage - new developer storage 89 func NewDeveloperStorage(key sdk.StoreKey) DeveloperStorage { 90 cdc := wire.New() 91 wire.RegisterCrypto(cdc) 92 return DeveloperStorage{ 93 key: key, 94 cdc: cdc, 95 } 96 } 97 98 // HasDeveloper - check if developer in KVStore or not 99 func (ds DeveloperStorage) HasDeveloper(ctx sdk.Context, accKey linotypes.AccountKey) bool { 100 store := ctx.KVStore(ds.key) 101 return store.Has(GetDeveloperKey(accKey)) 102 } 103 104 // GetDeveloper - get developer from KVStore 105 func (ds DeveloperStorage) GetDeveloper(ctx sdk.Context, accKey linotypes.AccountKey) (*Developer, sdk.Error) { 106 store := ctx.KVStore(ds.key) 107 bz := store.Get(GetDeveloperKey(accKey)) 108 if bz == nil { 109 return nil, types.ErrDeveloperNotFound() 110 } 111 dev := new(Developer) 112 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, dev) 113 return dev, nil 114 } 115 116 // SetDeveloper - set developer to KVStore 117 func (ds DeveloperStorage) SetDeveloper(ctx sdk.Context, developer Developer) { 118 store := ctx.KVStore(ds.key) 119 developerByte := ds.cdc.MustMarshalBinaryLengthPrefixed(developer) 120 store.Set(GetDeveloperKey(developer.Username), developerByte) 121 } 122 123 // GetAllDevelopers - get developer list from KVStore. 124 // NOTE, the result includes the all developers even if it's marked in value as deleted. 125 func (ds DeveloperStorage) GetAllDevelopers(ctx sdk.Context) []Developer { 126 store := ctx.KVStore(ds.key) 127 rst := make([]Developer, 0) 128 itr := sdk.KVStorePrefixIterator(store, DeveloperSubstore) 129 defer itr.Close() 130 for ; itr.Valid(); itr.Next() { 131 dev := new(Developer) 132 ds.cdc.MustUnmarshalBinaryLengthPrefixed(itr.Value(), dev) 133 rst = append(rst, *dev) 134 } 135 return rst 136 } 137 138 // HasIDA - check if developer has a IDA in KVStore or not 139 func (ds DeveloperStorage) HasIDA(ctx sdk.Context, accKey linotypes.AccountKey) bool { 140 return ctx.KVStore(ds.key).Has(GetIDAKey(accKey)) 141 } 142 143 // GetIDA - get ida of a developer KVStore 144 func (ds DeveloperStorage) GetIDA(ctx sdk.Context, accKey linotypes.AccountKey) (*AppIDA, sdk.Error) { 145 store := ctx.KVStore(ds.key) 146 bz := store.Get(GetIDAKey(accKey)) 147 if bz == nil { 148 return nil, types.ErrIDANotFound() 149 } 150 ida := new(AppIDA) 151 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, ida) 152 return ida, nil 153 } 154 155 // SetIDA - set IDA to KVStore 156 func (ds DeveloperStorage) SetIDA(ctx sdk.Context, ida AppIDA) { 157 store := ctx.KVStore(ds.key) 158 bz := ds.cdc.MustMarshalBinaryLengthPrefixed(ida) 159 store.Set(GetIDAKey(ida.App), bz) 160 } 161 162 // GetIDABalance - get ida balance of (app, user) 163 func (ds DeveloperStorage) GetIDABank(ctx sdk.Context, app linotypes.AccountKey, user linotypes.AccountKey) *IDABank { 164 store := ctx.KVStore(ds.key) 165 bz := store.Get(GetIDABalanceKey(app, user)) 166 if bz == nil { 167 return &IDABank{ 168 Balance: linotypes.NewMiniDollar(0), 169 Unauthed: false, 170 } 171 } 172 rst := &IDABank{} 173 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, rst) 174 return rst 175 } 176 177 // SetIDABalance - set (app, user)'s ida balance to amount 178 func (ds DeveloperStorage) SetIDABank(ctx sdk.Context, app linotypes.AccountKey, user linotypes.AccountKey, bank *IDABank) { 179 store := ctx.KVStore(ds.key) 180 bz := ds.cdc.MustMarshalBinaryLengthPrefixed(bank) 181 store.Set(GetIDABalanceKey(app, user), bz) 182 } 183 184 // GetReservePool - get IDA's lino reserve pool. 185 func (ds DeveloperStorage) GetReservePool(ctx sdk.Context) *ReservePool { 186 store := ctx.KVStore(ds.key) 187 bz := store.Get(GetReservePoolKey()) 188 if bz == nil { 189 panic("Developer IDA reserve pool MUST be initialized.") 190 } 191 rst := new(ReservePool) 192 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, rst) 193 return rst 194 } 195 196 // SetReservePool - get IDA's lino reserve pool. 197 func (ds DeveloperStorage) SetReservePool(ctx sdk.Context, pool *ReservePool) { 198 store := ctx.KVStore(ds.key) 199 bz := ds.cdc.MustMarshalBinaryLengthPrefixed(pool) 200 store.Set(GetReservePoolKey(), bz) 201 } 202 203 // SetAffiliatedAcc - set affiliated account. 204 func (ds DeveloperStorage) SetAffiliatedAcc(ctx sdk.Context, app, user linotypes.AccountKey) { 205 store := ctx.KVStore(ds.key) 206 store.Set(GetAffiliatedAccKey(app, user), []byte(trueStr)) 207 } 208 209 // HasAffiliateAcc - has this affiliated account.. 210 func (ds DeveloperStorage) HasAffiliatedAcc(ctx sdk.Context, app, user linotypes.AccountKey) bool { 211 store := ctx.KVStore(ds.key) 212 return store.Has(GetAffiliatedAccKey(app, user)) 213 } 214 215 // DelAffiliatedAcc - remove this affiliated acc. 216 func (ds DeveloperStorage) DelAffiliatedAcc(ctx sdk.Context, app, user linotypes.AccountKey) { 217 store := ctx.KVStore(ds.key) 218 store.Delete(GetAffiliatedAccKey(app, user)) 219 } 220 221 func (ds DeveloperStorage) GetAllAffiliatedAcc(ctx sdk.Context, app linotypes.AccountKey) []linotypes.AccountKey { 222 store := ctx.KVStore(ds.key) 223 rst := make([]linotypes.AccountKey, 0) 224 appPrefix := GetAffiliatedAccAppPrefix(app) 225 itr := sdk.KVStorePrefixIterator(store, appPrefix) 226 defer itr.Close() 227 for ; itr.Valid(); itr.Next() { 228 acc := itr.Key()[len(appPrefix):] 229 rst = append(rst, linotypes.AccountKey(acc)) 230 } 231 return rst 232 } 233 234 func (ds DeveloperStorage) SetUserRole(ctx sdk.Context, user linotypes.AccountKey, role *Role) { 235 store := ctx.KVStore(ds.key) 236 bz := ds.cdc.MustMarshalBinaryLengthPrefixed(role) 237 store.Set(GetUserRoleKey(user), bz) 238 } 239 240 func (ds DeveloperStorage) DelUserRole(ctx sdk.Context, user linotypes.AccountKey) { 241 ctx.KVStore(ds.key).Delete(GetUserRoleKey(user)) 242 } 243 244 func (ds DeveloperStorage) GetUserRole(ctx sdk.Context, user linotypes.AccountKey) (*Role, sdk.Error) { 245 bz := ctx.KVStore(ds.key).Get(GetUserRoleKey(user)) 246 if bz == nil { 247 return nil, types.ErrInvalidUserRole() 248 } 249 rst := new(Role) 250 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, rst) 251 return rst, nil 252 } 253 254 func (ds DeveloperStorage) HasUserRole(ctx sdk.Context, user linotypes.AccountKey) bool { 255 return ctx.KVStore(ds.key).Has(GetUserRoleKey(user)) 256 } 257 258 // SetIDAStats set ida stats of a app. 259 func (ds DeveloperStorage) SetIDAStats(ctx sdk.Context, app linotypes.AccountKey, stats AppIDAStats) { 260 store := ctx.KVStore(ds.key) 261 bz := ds.cdc.MustMarshalBinaryLengthPrefixed(stats) 262 store.Set(GetIDAStatsKey(app), bz) 263 } 264 265 // GetIDAStats returns the stats of the IDA. 266 func (ds DeveloperStorage) GetIDAStats(ctx sdk.Context, app linotypes.AccountKey) *AppIDAStats { 267 bz := ctx.KVStore(ds.key).Get(GetIDAStatsKey(app)) 268 if bz == nil { 269 return &AppIDAStats{ 270 Total: linotypes.NewMiniDollar(0), 271 } 272 } 273 stats := new(AppIDAStats) 274 ds.cdc.MustUnmarshalBinaryLengthPrefixed(bz, stats) 275 return stats 276 } 277 278 func (ds DeveloperStorage) StoreMap(ctx sdk.Context) utils.StoreMap { 279 store := ctx.KVStore(ds.key) 280 substores := []utils.SubStore{ 281 { 282 Store: store, 283 Prefix: DeveloperSubstore, 284 ValCreator: func() interface{} { return new(Developer) }, 285 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 286 }, 287 { 288 Store: store, 289 Prefix: IdaSubstore, 290 ValCreator: func() interface{} { return new(AppIDA) }, 291 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 292 }, 293 { 294 Store: store, 295 Prefix: UserRoleSubstore, 296 ValCreator: func() interface{} { return new(Role) }, 297 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 298 }, 299 { 300 Store: store, 301 Prefix: IdaBalanceSubstore, 302 ValCreator: func() interface{} { return new(IDABank) }, 303 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 304 }, 305 { 306 Store: store, 307 Prefix: ReservePoolSubstore, 308 ValCreator: func() interface{} { return new(ReservePool) }, 309 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 310 }, 311 { 312 Store: store, 313 Prefix: IdaStatsSubstore, 314 ValCreator: func() interface{} { return new(AppIDAStats) }, 315 Decoder: ds.cdc.MustUnmarshalBinaryLengthPrefixed, 316 }, 317 { 318 Store: store, 319 Prefix: AffiliatedAccSubstore, 320 NoValue: true, 321 }, 322 } 323 return utils.NewStoreMap(substores) 324 }