From b0c948fc89ba5540ac0705ba912e7b80a9518900 Mon Sep 17 00:00:00 2001 From: jiujiteiro Date: Fri, 24 Mar 2023 10:54:50 -0400 Subject: [PATCH 1/2] modify restriction passing --- app/app.go | 3 ++ x/asset/genesis.go | 1 - x/asset/keeper/restrictions.go | 70 +++++++++++++++------------------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/app/app.go b/app/app.go index 94c8bab4..a8a8d6ad 100644 --- a/app/app.go +++ b/app/app.go @@ -383,6 +383,9 @@ func New( app.BankKeeper, ) + // Add transfer restriction + app.BankKeeper.AppendSendRestriction(app.AssetKeeper.AssetSendRestriction) + // IBC Keeper app.IBCKeeper = ibckeeper.NewKeeper( appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, diff --git a/x/asset/genesis.go b/x/asset/genesis.go index ad6ba63d..35739425 100644 --- a/x/asset/genesis.go +++ b/x/asset/genesis.go @@ -10,7 +10,6 @@ import ( // InitGenesis initializes the assets module's state from a provided genesis // state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { - k.SetTransferRestrictionFn() k.SetParams(ctx, genState.Params) for _, token := range genState.Tokens { k.SetToken(ctx, token) diff --git a/x/asset/keeper/restrictions.go b/x/asset/keeper/restrictions.go index 0e4f9403..ed6517bb 100644 --- a/x/asset/keeper/restrictions.go +++ b/x/asset/keeper/restrictions.go @@ -3,51 +3,43 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/realiotech/realio-network/x/asset/types" ) -func AssetSendRestriction(k Keeper) banktypes.SendRestrictionFn { - return func(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error) { - newToAddr = toAddr - err = nil +func (k Keeper) AssetSendRestriction(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error) { + newToAddr = toAddr + err = nil - for _, coin := range amt { - // Check if the value already exists - // fetch bank metadata to get symbol from denom - symbol := coin.Denom - tokenMetadata, found := k.bankKeeper.GetDenomMetaData(ctx, coin.Denom) - if found { - symbol = tokenMetadata.Symbol - } - token, isFound := k.GetToken( - ctx, - symbol, - ) - if !isFound { - continue - } + for _, coin := range amt { + // Check if the value already exists + // fetch bank metadata to get symbol from denom + symbol := coin.Denom + tokenMetadata, found := k.bankKeeper.GetDenomMetaData(ctx, coin.Denom) + if found { + symbol = tokenMetadata.Symbol + } + token, isFound := k.GetToken( + ctx, + symbol, + ) + if !isFound { + continue + } - var isAuthorizedFrom, isAuthorizedTo bool - if token.AuthorizationRequired { - isAuthorizedFrom = k.IsAddressAuthorizedToSend(ctx, coin.Denom, fromAddr) - isAuthorizedTo = k.IsAddressAuthorizedToSend(ctx, coin.Denom, toAddr) - } else { - continue - } + var isAuthorizedFrom, isAuthorizedTo bool + if token.AuthorizationRequired { + isAuthorizedFrom = k.IsAddressAuthorizedToSend(ctx, coin.Denom, fromAddr) + isAuthorizedTo = k.IsAddressAuthorizedToSend(ctx, coin.Denom, toAddr) + } else { + continue + } - if isAuthorizedFrom && isAuthorizedTo { - continue - } else { //nolint:revive // superfluous else, could fix, but not worth it? - err = sdkerrors.Wrapf(types.ErrNotAuthorized, "%s is not authorized to transact with %s", fromAddr, coin.Denom) - break - } + if isAuthorizedFrom && isAuthorizedTo { + continue + } else { //nolint:revive // superfluous else, could fix, but not worth it? + err = sdkerrors.Wrapf(types.ErrNotAuthorized, "%s is not authorized to transact with %s", fromAddr, coin.Denom) + break } - return } -} - -// SetTransferRestrictionFn Set genereric Authorization Send Transfer Restriction -func (k Keeper) SetTransferRestrictionFn() { - k.bankKeeper.AppendSendRestriction(AssetSendRestriction(k)) + return } From 6608d294f0508cef5e4802d2f22a86b1ec84f9a1 Mon Sep 17 00:00:00 2001 From: jiujiteiro Date: Fri, 24 Mar 2023 11:24:20 -0400 Subject: [PATCH 2/2] lint --- x/asset/keeper/restrictions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/asset/keeper/restrictions.go b/x/asset/keeper/restrictions.go index ed6517bb..d0570ab6 100644 --- a/x/asset/keeper/restrictions.go +++ b/x/asset/keeper/restrictions.go @@ -41,5 +41,5 @@ func (k Keeper) AssetSendRestriction(ctx sdk.Context, fromAddr, toAddr sdk.AccAd break } } - return + return newToAddr, err }