github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/keeper/account.go (about) 1 package keeper 2 3 import ( 4 "context" 5 "errors" 6 7 "cosmossdk.io/collections" 8 9 sdk "github.com/cosmos/cosmos-sdk/types" 10 ) 11 12 // NewAccountWithAddress implements AccountKeeperI. 13 func (ak AccountKeeper) NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI { 14 acc := ak.proto() 15 err := acc.SetAddress(addr) 16 if err != nil { 17 panic(err) 18 } 19 20 return ak.NewAccount(ctx, acc) 21 } 22 23 // NewAccount sets the next account number to a given account interface 24 func (ak AccountKeeper) NewAccount(ctx context.Context, acc sdk.AccountI) sdk.AccountI { 25 if err := acc.SetAccountNumber(ak.NextAccountNumber(ctx)); err != nil { 26 panic(err) 27 } 28 29 return acc 30 } 31 32 // HasAccount implements AccountKeeperI. 33 func (ak AccountKeeper) HasAccount(ctx context.Context, addr sdk.AccAddress) bool { 34 has, _ := ak.Accounts.Has(ctx, addr) 35 return has 36 } 37 38 // GetAccount implements AccountKeeperI. 39 func (ak AccountKeeper) GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI { 40 acc, err := ak.Accounts.Get(ctx, addr) 41 if err != nil && !errors.Is(err, collections.ErrNotFound) { 42 panic(err) 43 } 44 return acc 45 } 46 47 // GetAllAccounts returns all accounts in the accountKeeper. 48 func (ak AccountKeeper) GetAllAccounts(ctx context.Context) (accounts []sdk.AccountI) { 49 ak.IterateAccounts(ctx, func(acc sdk.AccountI) (stop bool) { 50 accounts = append(accounts, acc) 51 return false 52 }) 53 54 return accounts 55 } 56 57 // SetAccount implements AccountKeeperI. 58 func (ak AccountKeeper) SetAccount(ctx context.Context, acc sdk.AccountI) { 59 err := ak.Accounts.Set(ctx, acc.GetAddress(), acc) 60 if err != nil { 61 panic(err) 62 } 63 } 64 65 // RemoveAccount removes an account for the account mapper store. 66 // NOTE: this will cause supply invariant violation if called 67 func (ak AccountKeeper) RemoveAccount(ctx context.Context, acc sdk.AccountI) { 68 err := ak.Accounts.Remove(ctx, acc.GetAddress()) 69 if err != nil { 70 panic(err) 71 } 72 } 73 74 // IterateAccounts iterates over all the stored accounts and performs a callback function. 75 // Stops iteration when callback returns true. 76 func (ak AccountKeeper) IterateAccounts(ctx context.Context, cb func(account sdk.AccountI) (stop bool)) { 77 err := ak.Accounts.Walk(ctx, nil, func(_ sdk.AccAddress, value sdk.AccountI) (bool, error) { 78 return cb(value), nil 79 }) 80 if err != nil { 81 panic(err) 82 } 83 }