-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
account.go
108 lines (89 loc) · 3.13 KB
/
account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
// NewAccountWithAddress implements AccountKeeperI.
func (ak AccountKeeper) NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
acc := ak.proto()
err := acc.SetAddress(addr)
if err != nil {
panic(err)
}
return ak.NewAccount(ctx, acc)
}
// NewAccount sets the next account number to a given account interface
func (ak AccountKeeper) NewAccount(ctx sdk.Context, acc types.AccountI) types.AccountI {
if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil {
panic(err)
}
return acc
}
// HasAccount implements AccountKeeperI.
func (ak AccountKeeper) HasAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
store := ctx.KVStore(ak.storeKey)
return store.Has(types.AddressStoreKey(addr))
}
// HasAccountAddressByID checks account address exists by id.
func (ak AccountKeeper) HasAccountAddressByID(ctx sdk.Context, id uint64) bool {
store := ctx.KVStore(ak.storeKey)
return store.Has(types.AccountNumberStoreKey(id))
}
// GetAccount implements AccountKeeperI.
func (ak AccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.AddressStoreKey(addr))
if bz == nil {
return nil
}
return ak.decodeAccount(bz)
}
// GetAccountAddressById returns account address by id.
func (ak AccountKeeper) GetAccountAddressByID(ctx sdk.Context, id uint64) string {
store := ctx.KVStore(ak.storeKey)
bz := store.Get(types.AccountNumberStoreKey(id))
if bz == nil {
return ""
}
return sdk.AccAddress(bz).String()
}
// GetAllAccounts returns all accounts in the accountKeeper.
func (ak AccountKeeper) GetAllAccounts(ctx sdk.Context) (accounts []types.AccountI) {
ak.IterateAccounts(ctx, func(acc types.AccountI) (stop bool) {
accounts = append(accounts, acc)
return false
})
return accounts
}
// SetAccount implements AccountKeeperI.
func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)
bz, err := ak.MarshalAccount(acc)
if err != nil {
panic(err)
}
store.Set(types.AddressStoreKey(addr), bz)
store.Set(types.AccountNumberStoreKey(acc.GetAccountNumber()), addr.Bytes())
}
// 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 types.AccountI) {
addr := acc.GetAddress()
store := ctx.KVStore(ak.storeKey)
store.Delete(types.AddressStoreKey(addr))
store.Delete(types.AccountNumberStoreKey(acc.GetAccountNumber()))
}
// IterateAccounts iterates over all the stored accounts and performs a callback function.
// Stops iteration when callback returns true.
func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account types.AccountI) (stop bool)) {
store := ctx.KVStore(ak.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
account := ak.decodeAccount(iterator.Value())
if cb(account) {
break
}
}
}