From 1afc59aa1d902ca94a5e2994ebfe92ade4c928f6 Mon Sep 17 00:00:00 2001 From: dimiandre Date: Wed, 17 Apr 2024 16:47:09 +0200 Subject: [PATCH] tokenfactory updates --- .gitignore | 2 ++ app/keepers/keepers.go | 1 + x/tokenfactory/keeper/bankactions.go | 26 ++++++++++++++++++++++++ x/tokenfactory/keeper/keeper.go | 12 +++++++++-- x/tokenfactory/types/expected_keepers.go | 1 + 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d67943a7f..4501d3cf3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ heighliner* # emacs editor config \#*\# .\#* + +.DS_Store \ No newline at end of file diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index d9dfce18e..e06228610 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -468,6 +468,7 @@ func NewAppKeepers( appKeepers.TokenFactoryKeeper = tokenfactorykeeper.NewKeeper( appCodec, appKeepers.keys[tokenfactorytypes.StoreKey], + maccPerms, appKeepers.AccountKeeper, appKeepers.BankKeeper, appKeepers.DistrKeeper, diff --git a/x/tokenfactory/keeper/bankactions.go b/x/tokenfactory/keeper/bankactions.go index af93771a5..d347b56ee 100644 --- a/x/tokenfactory/keeper/bankactions.go +++ b/x/tokenfactory/keeper/bankactions.go @@ -2,6 +2,10 @@ package keeper import ( "fmt" + "sort" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" sdk "github.com/cosmos/cosmos-sdk/types" @@ -68,6 +72,28 @@ func (k Keeper) forceTransfer(ctx sdk.Context, amount sdk.Coin, fromAddr string, return err } + fromAcc, err := sdk.AccAddressFromBech32(fromAddr) + if err != nil { + return err + } + + sortedPermAddrs := make([]string, 0, len(k.permAddrs)) + for moduleName := range k.permAddrs { + sortedPermAddrs = append(sortedPermAddrs, moduleName) + } + sort.Strings(sortedPermAddrs) + + for _, moduleName := range sortedPermAddrs { + account := k.accountKeeper.GetModuleAccount(ctx, moduleName) + if account == nil { + return status.Errorf(codes.NotFound, "account %s not found", moduleName) + } + + if account.GetAddress().Equals(fromAcc) { + return status.Errorf(codes.Internal, "send from module acc not available") + } + } + fromSdkAddr, err := sdk.AccAddressFromBech32(fromAddr) if err != nil { return err diff --git a/x/tokenfactory/keeper/keeper.go b/x/tokenfactory/keeper/keeper.go index 71128f1e7..1cf03a906 100644 --- a/x/tokenfactory/keeper/keeper.go +++ b/x/tokenfactory/keeper/keeper.go @@ -16,8 +16,9 @@ import ( type ( Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + permAddrs map[string]authtypes.PermissionsForAddress accountKeeper types.AccountKeeper bankKeeper types.BankKeeper @@ -35,16 +36,23 @@ type ( func NewKeeper( cdc codec.BinaryCodec, storeKey storetypes.StoreKey, + maccPerms map[string][]string, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, communityPoolKeeper types.CommunityPoolKeeper, enabledCapabilities []string, authority string, ) Keeper { + permAddrs := make(map[string]authtypes.PermissionsForAddress) + for name, perms := range maccPerms { + permAddrs[name] = authtypes.NewPermissionsForAddress(name, perms) + } + return Keeper{ cdc: cdc, storeKey: storeKey, + permAddrs: permAddrs, accountKeeper: accountKeeper, bankKeeper: bankKeeper, communityPoolKeeper: communityPoolKeeper, diff --git a/x/tokenfactory/types/expected_keepers.go b/x/tokenfactory/types/expected_keepers.go index ff3842327..07e19ff6b 100644 --- a/x/tokenfactory/types/expected_keepers.go +++ b/x/tokenfactory/types/expected_keepers.go @@ -31,6 +31,7 @@ type BankKeeper interface { type AccountKeeper interface { SetModuleAccount(ctx sdk.Context, macc authtypes.ModuleAccountI) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI + GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI } // CommunityPoolKeeper defines the contract needed to be fulfilled for community pool interactions.