Skip to content

Commit

Permalink
Refactor tokenfactory address checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiandre committed Jun 11, 2024
1 parent b0faafb commit 600795c
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions x/tokenfactory/keeper/bankactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@ import (
"fmt"
"sort"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/CosmosContracts/juno/v22/x/tokenfactory/types"
)

// BlockedAddr checks if a given address is restricted
func (k Keeper) BlockedAddr(ctx sdk.Context, addr sdk.AccAddress) bool {

Check failure on line 14 in x/tokenfactory/keeper/bankactions.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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 true
}

if account.GetAddress().Equals(addr) {
return true
}
}
return false
}

func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string) error {
// verify that denom is an x/tokenfactory denom
_, _, err := types.DeconstructDenom(amount.Denom)
Expand All @@ -29,7 +48,7 @@ func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string) error {
return err
}

if k.bankKeeper.BlockedAddr(addr) {
if k.BlockedAddr(ctx, addr) {
return fmt.Errorf("failed to mint to blocked address: %s", addr)
}

Expand All @@ -50,7 +69,7 @@ func (k Keeper) burnFrom(ctx sdk.Context, amount sdk.Coin, burnFrom string) erro
return err
}

if k.bankKeeper.BlockedAddr(addr) {
if k.BlockedAddr(ctx, addr) {
return fmt.Errorf("failed to burn from blocked address: %s", addr)
}

Expand All @@ -72,28 +91,6 @@ 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
Expand All @@ -104,8 +101,8 @@ func (k Keeper) forceTransfer(ctx sdk.Context, amount sdk.Coin, fromAddr string,
return err
}

if k.bankKeeper.BlockedAddr(toSdkAddr) {
return fmt.Errorf("failed to force transfer to blocked address: %s", toSdkAddr)
if k.BlockedAddr(ctx, fromSdkAddr) || k.BlockedAddr(ctx, toSdkAddr) {
return fmt.Errorf("failed to transfer from blocked address: %s", fromSdkAddr)
}

return k.bankKeeper.SendCoins(ctx, fromSdkAddr, toSdkAddr, sdk.NewCoins(amount))
Expand Down

0 comments on commit 600795c

Please sign in to comment.