github.com/Finschia/finschia-sdk@v0.49.1/x/auth/keeper/account.go (about)

     1  package keeper
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	"github.com/Finschia/finschia-sdk/x/auth/types"
     6  )
     7  
     8  // NewAccountWithAddress implements AccountKeeperI.
     9  func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
    10  	acc := ak.proto()
    11  	err := acc.SetAddress(addr)
    12  	if err != nil {
    13  		panic(err)
    14  	}
    15  
    16  	return ak.NewAccount(ctx, acc)
    17  }
    18  
    19  // NewAccount sets the next account number to a given account interface
    20  func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc types.AccountI) types.AccountI {
    21  	if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil {
    22  		panic(err)
    23  	}
    24  
    25  	return acc
    26  }
    27  
    28  // HasAccount implements AccountKeeperI.
    29  func (ak AccountKeeper) HasAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
    30  	store := ctx.KVStore(ak.key)
    31  	return store.Has(types.AddressStoreKey(addr))
    32  }
    33  
    34  // GetAccount implements AccountKeeperI.
    35  func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
    36  	store := ctx.KVStore(ak.key)
    37  	bz := store.Get(types.AddressStoreKey(addr))
    38  	if bz == nil {
    39  		return nil
    40  	}
    41  
    42  	return ak.decodeAccount(bz)
    43  }
    44  
    45  // GetAllAccounts returns all accounts in the accountKeeper.
    46  func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []types.AccountI) {
    47  	ak.IterateAccounts(ctx, func(acc types.AccountI) (stop bool) {
    48  		accounts = append(accounts, acc)
    49  		return false
    50  	})
    51  
    52  	return accounts
    53  }
    54  
    55  // SetAccount implements AccountKeeperI.
    56  func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) {
    57  	addr := acc.GetAddress()
    58  	store := ctx.KVStore(ak.key)
    59  
    60  	bz, err := ak.MarshalAccount(acc)
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  
    65  	store.Set(types.AddressStoreKey(addr), bz)
    66  }
    67  
    68  // RemoveAccount removes an account for the account mapper store.
    69  // NOTE: this will cause supply invariant violation if called
    70  func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc types.AccountI) {
    71  	addr := acc.GetAddress()
    72  	store := ctx.KVStore(ak.key)
    73  	store.Delete(types.AddressStoreKey(addr))
    74  }
    75  
    76  // IterateAccounts iterates over all the stored accounts and performs a callback function.
    77  // Stops iteration when callback returns true.
    78  func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account types.AccountI) (stop bool)) {
    79  	store := ctx.KVStore(ak.key)
    80  	iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
    81  
    82  	defer iterator.Close()
    83  	for ; iterator.Valid(); iterator.Next() {
    84  		account := ak.decodeAccount(iterator.Value())
    85  
    86  		if cb(account) {
    87  			break
    88  		}
    89  	}
    90  }