2019-07-29 08:27:01 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewAccountWithAddress implements sdk.AccountKeeper.
|
|
|
|
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) exported.Account {
|
|
|
|
acc := ak.proto()
|
|
|
|
err := acc.SetAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
return ak.NewAccount(ctx, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccount sets the next account number to a given account interface
|
|
|
|
func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc exported.Account) exported.Account {
|
|
|
|
if err := acc.SetAccountNumber(ak.GetNextAccountNumber(ctx)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount implements sdk.AccountKeeper.
|
|
|
|
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) exported.Account {
|
|
|
|
store := ctx.KVStore(ak.key)
|
|
|
|
bz := store.Get(types.AddressStoreKey(addr))
|
|
|
|
if bz == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
|
|
|
return ak.decodeAccount(bz)
|
2019-07-29 08:27:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllAccounts returns all accounts in the accountKeeper.
|
|
|
|
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []exported.Account) {
|
2020-02-18 04:50:13 -08:00
|
|
|
ak.IterateAccounts(ctx, func(acc exported.Account) (stop bool) {
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
return accounts
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAccount implements sdk.AccountKeeper.
|
|
|
|
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc exported.Account) {
|
|
|
|
addr := acc.GetAddress()
|
|
|
|
store := ctx.KVStore(ak.key)
|
2020-02-18 04:50:13 -08:00
|
|
|
|
|
|
|
bz, err := ak.cdc.MarshalAccount(acc)
|
2019-07-29 08:27:01 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-02-18 04:50:13 -08:00
|
|
|
|
2019-07-29 08:27:01 -07:00
|
|
|
store.Set(types.AddressStoreKey(addr), bz)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAccount removes an account for the account mapper store.
|
|
|
|
// NOTE: this will cause supply invariant violation if called
|
|
|
|
func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc exported.Account) {
|
|
|
|
addr := acc.GetAddress()
|
|
|
|
store := ctx.KVStore(ak.key)
|
|
|
|
store.Delete(types.AddressStoreKey(addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAccounts iterates over all the stored accounts and performs a callback function
|
|
|
|
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account exported.Account) (stop bool)) {
|
|
|
|
store := ctx.KVStore(ak.key)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
|
|
|
|
|
|
|
|
defer iterator.Close()
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
account := ak.decodeAccount(iterator.Value())
|
|
|
|
|
|
|
|
if cb(account) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|