Minor best practices edits in Hard module (#877)

* put err return inside conditional

* check error type instead of error msg string
This commit is contained in:
Denali Marsh 2021-03-15 16:15:19 +01:00 committed by GitHub
parent 20b3fa53e3
commit 72bfee6523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 7 deletions

View File

@ -1,7 +1,7 @@
package keeper package keeper
import ( import (
"strings" "errors"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@ -44,7 +44,7 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
// Sends coins from Hard module account to user // Sends coins from Hard module account to user
err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins) err = k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, borrower, coins)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "insufficient account funds") { if errors.Is(err, sdkerrors.ErrInsufficientFunds) {
modAccCoins := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName).GetCoins() modAccCoins := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName).GetCoins()
for _, coin := range coins { for _, coin := range coins {
_, isNegative := modAccCoins.SafeSub(sdk.NewCoins(coin)) _, isNegative := modAccCoins.SafeSub(sdk.NewCoins(coin))
@ -56,8 +56,6 @@ func (k Keeper) Borrow(ctx sdk.Context, borrower sdk.AccAddress, coins sdk.Coins
} }
} }
} }
}
if err != nil {
return err return err
} }

View File

@ -1,7 +1,7 @@
package keeper package keeper
import ( import (
"strings" "errors"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@ -44,7 +44,7 @@ func (k Keeper) Deposit(ctx sdk.Context, depositor sdk.AccAddress, coins sdk.Coi
err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins) err = k.supplyKeeper.SendCoinsFromAccountToModule(ctx, depositor, types.ModuleAccountName, coins)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "insufficient account funds") { if errors.Is(err, sdkerrors.ErrInsufficientFunds) {
accCoins := k.accountKeeper.GetAccount(ctx, depositor).SpendableCoins(ctx.BlockTime()) accCoins := k.accountKeeper.GetAccount(ctx, depositor).SpendableCoins(ctx.BlockTime())
for _, coin := range coins { for _, coin := range coins {
_, isNegative := accCoins.SafeSub(sdk.NewCoins(coin)) _, isNegative := accCoins.SafeSub(sdk.NewCoins(coin))

View File

@ -91,7 +91,7 @@ func (suite *KeeperTestSuite) TestDeposit() {
}, },
errArgs{ errArgs{
expectPass: false, expectPass: false,
contains: "insufficient funds", contains: "insufficient funds: the requested deposit amount",
}, },
}, },
} }