Skip to content

Commit

Permalink
Merge pull request #49 from realiotech/jiujiteiro/restriction-update
Browse files Browse the repository at this point in the history
modify restriction passing
  • Loading branch information
jiujiteiro authored Mar 24, 2023
2 parents 528abd5 + 6608d29 commit 4f1a74a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion x/asset/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 31 additions & 39 deletions x/asset/keeper/restrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 newToAddr, err
}

0 comments on commit 4f1a74a

Please sign in to comment.