github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/supply/internal/keeper/account.go (about)

     1  package keeper
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/exported"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/internal/types"
     8  )
     9  
    10  // GetModuleAddress returns an address based on the module name
    11  func (k Keeper) GetModuleAddress(moduleName string) sdk.AccAddress {
    12  	permAddr, ok := k.permAddrs[moduleName]
    13  	if !ok {
    14  		return nil
    15  	}
    16  	return permAddr.GetAddress()
    17  }
    18  
    19  // GetModuleAddressAndPermissions returns an address and permissions based on the module name
    20  func (k Keeper) GetModuleAddressAndPermissions(moduleName string) (addr sdk.AccAddress, permissions []string) {
    21  	permAddr, ok := k.permAddrs[moduleName]
    22  	if !ok {
    23  		return addr, permissions
    24  	}
    25  	return permAddr.GetAddress(), permAddr.GetPermissions()
    26  }
    27  
    28  // GetModuleAccountAndPermissions gets the module account from the auth account store and its
    29  // registered permissions
    30  func (k Keeper) GetModuleAccountAndPermissions(ctx sdk.Context, moduleName string) (exported.ModuleAccountI, []string) {
    31  	addr, perms := k.GetModuleAddressAndPermissions(moduleName)
    32  	if addr == nil {
    33  		return nil, []string{}
    34  	}
    35  
    36  	acc := k.ak.GetAccount(ctx, addr)
    37  	if acc != nil {
    38  		macc, ok := acc.(exported.ModuleAccountI)
    39  		if !ok {
    40  			panic("account is not a module account")
    41  		}
    42  		return macc, perms
    43  	}
    44  
    45  	// create a new module account
    46  	macc := types.NewEmptyModuleAccount(moduleName, perms...)
    47  	maccI := (k.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI) // set the account number
    48  	k.SetModuleAccount(ctx, maccI)
    49  
    50  	return maccI, perms
    51  }
    52  
    53  // GetModuleAccount gets the module account from the auth account store
    54  func (k Keeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
    55  	acc, _ := k.GetModuleAccountAndPermissions(ctx, moduleName)
    56  	return acc
    57  }
    58  
    59  // SetModuleAccount sets the module account to the auth account store
    60  func (k Keeper) SetModuleAccount(ctx sdk.Context, macc exported.ModuleAccountI) { //nolint:interfacer
    61  	k.ak.SetAccount(ctx, macc)
    62  }
    63  
    64  func (k Keeper) GetAccount(ctx sdk.Context, acc sdk.AccAddress) authtypes.Account {
    65  	return k.ak.GetAccount(ctx, acc)
    66  }