Skip to content

Commit

Permalink
refactor: improve send restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed May 10, 2024
1 parent 0cbc029 commit 0bf1ada
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
30 changes: 16 additions & 14 deletions x/aura/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,22 @@ func NewKeeper(
return keeper
}

// SendRestrictionFn executes the following checks against all USDY transfers:
// - Is the module currently paused?
// - If we're not minting, check the sender against the blocklist.
// - If we're not burning, check the recipient against the blocklist.
// SendRestrictionFn executes necessary checks against all USDY transfers.
func (k *Keeper) SendRestrictionFn(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error) {
if amount := amt.AmountOf(k.Denom); !amount.IsZero() {
paused, _ := k.Paused.Get(ctx)
if paused {
return toAddr, fmt.Errorf("%s transfers are paused", k.Denom)
}

if !fromAddr.Equals(types.ModuleAddress) {
burning := !fromAddr.Equals(types.ModuleAddress) && toAddr.Equals(types.ModuleAddress)
minting := fromAddr.Equals(types.ModuleAddress) && !toAddr.Equals(types.ModuleAddress)

if burning {
return toAddr, nil
}

if !minting {
has, err := k.BlockedAddresses.Has(ctx, fromAddr)
if err != nil {
return toAddr, errors.Wrap(err, "unable to retrieve blocked address")
Expand All @@ -107,15 +111,13 @@ func (k *Keeper) SendRestrictionFn(ctx context.Context, fromAddr, toAddr sdk.Acc
}
}

if !toAddr.Equals(types.ModuleAddress) {
has, err := k.BlockedAddresses.Has(ctx, toAddr)
if err != nil {
return toAddr, errors.Wrap(err, "unable to retrieve blocked address")
}
if has {
address, _ := k.accountKeeper.AddressCodec().BytesToString(toAddr)
return toAddr, fmt.Errorf("%s is blocked from receiving %s", address, k.Denom)
}
has, err := k.BlockedAddresses.Has(ctx, toAddr)
if err != nil {
return toAddr, errors.Wrap(err, "unable to retrieve blocked address")
}
if has {
address, _ := k.accountKeeper.AddressCodec().BytesToString(toAddr)
return toAddr, fmt.Errorf("%s is blocked from receiving %s", address, k.Denom)
}
}

Expand Down
71 changes: 45 additions & 26 deletions x/aura/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,77 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/noble-assets/aura/utils"
"github.com/noble-assets/aura/utils/mocks"
"github.com/noble-assets/aura/x/aura/types"
"github.com/stretchr/testify/require"
)

func TestSendRestriction(t *testing.T) {
keeper, ctx := mocks.AuraKeeper(t)
from, to := utils.TestAccount(), utils.TestAccount()
coins := sdk.NewCoins(sdk.NewCoin(
keeper.Denom, ONE,
))

// ACT: Attempt to send 1 USDC.
_, err := keeper.SendRestrictionFn(ctx, from.Bytes, to.Bytes, sdk.NewCoins(sdk.NewCoin(
// ACT: Attempt to send different token.
_, err := keeper.SendRestrictionFn(ctx, utils.TestAccount().Bytes, utils.TestAccount().Bytes, sdk.NewCoins(sdk.NewCoin(
"uusdc", math.NewInt(1_000_000),
)))
// ASSERT: The action should've succeeded due to different denom.
require.NoError(t, err)

// ACT: Attempt to send 1 USDY.
_, err = keeper.SendRestrictionFn(ctx, from.Bytes, to.Bytes, sdk.NewCoins(sdk.NewCoin(
keeper.Denom, ONE,
)))
// ACT: Attempt to send.
_, err = keeper.SendRestrictionFn(ctx, utils.TestAccount().Bytes, utils.TestAccount().Bytes, coins)
// ASSERT: The action should've succeeded.
require.NoError(t, err)

// ARRANGE: Set paused state to true.
require.NoError(t, keeper.Paused.Set(ctx, true))

// ACT: Attempt to send 1 USDY when paused.
_, err = keeper.SendRestrictionFn(ctx, from.Bytes, to.Bytes, sdk.NewCoins(sdk.NewCoin(
keeper.Denom, ONE,
)))
// ACT: Attempt to send when paused.
_, err = keeper.SendRestrictionFn(ctx, utils.TestAccount().Bytes, utils.TestAccount().Bytes, coins)
// ASSERT: The action should've failed due to module being paused.
require.ErrorContains(t, err, "ausdy transfers are paused")

// ARRANGE: Set paused state to false.
require.NoError(t, keeper.Paused.Set(ctx, false))
// ARRANGE: Block from address.
require.NoError(t, keeper.BlockedAddresses.Set(ctx, from.Bytes, true))
// ARRANGE: Generate a user account.
user := utils.TestAccount()

// ACT: Attempt to send 1 USDY from blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, from.Bytes, to.Bytes, sdk.NewCoins(sdk.NewCoin(
keeper.Denom, ONE,
)))
// ACT: Attempt to burn from non-blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, user.Bytes, types.ModuleAddress, coins)
// ASSERT: The action should've succeeded.
require.NoError(t, err)

// ARRANGE: Block user address.
require.NoError(t, keeper.BlockedAddresses.Set(ctx, user.Bytes, true))

// ACT: Attempt to burn from blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, user.Bytes, types.ModuleAddress, coins)
// ASSERT: The action should've succeeded.
require.NoError(t, err)

// ACT: Attempt to mint to blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, types.ModuleAddress, user.Bytes, coins)
// ASSERT: The action shoudl've failed due to blocked recipient.
require.ErrorContains(t, err, "blocked from receiving")

// ARRANGE: Unblock user address.
require.NoError(t, keeper.BlockedAddresses.Remove(ctx, user.Bytes))

// ACT: Attempt to mint to non-blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, types.ModuleAddress, user.Bytes, coins)
// ASSERT: The action should've succeeded.
require.NoError(t, err)

// ARRANGE: Block user address.
require.NoError(t, keeper.BlockedAddresses.Set(ctx, user.Bytes, true))

// ACT: Attempt to send from blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, user.Bytes, utils.TestAccount().Bytes, coins)
// ASSERT: The action should've failed due to blocked sender.
require.ErrorContains(t, err, "blocked from sending")

// ARRANGE: Unblock from address.
require.NoError(t, keeper.BlockedAddresses.Remove(ctx, from.Bytes))
// ARRANGE: Block to address.
require.NoError(t, keeper.BlockedAddresses.Set(ctx, to.Bytes, true))

// ACT: Attempt to send 1 USDY to blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, from.Bytes, to.Bytes, sdk.NewCoins(sdk.NewCoin(
keeper.Denom, ONE,
)))
// ACT: Attempt to send to blocklisted address.
_, err = keeper.SendRestrictionFn(ctx, utils.TestAccount().Bytes, user.Bytes, coins)
// ASSERT: The action should've failed due to blocked recipient.
require.ErrorContains(t, err, "blocked from receiving")
}

0 comments on commit 0bf1ada

Please sign in to comment.