Skip to content

Commit

Permalink
test: refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
queencre committed Dec 4, 2022
1 parent 8613d36 commit 06654e7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 27 deletions.
4 changes: 2 additions & 2 deletions x/liquidfarming/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (s *IntegrationTestSuite) TestNewLiquidFarmCmd() {
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewInt64Coin(s.cfg.BondDenom, 10)).String()),
},
false, &sdk.TxResponse{}, 2,
false, &sdk.TxResponse{}, 18,
},
{
"invalid case: pool id not found",
Expand Down Expand Up @@ -595,7 +595,7 @@ func (s *IntegrationTestSuite) TestNewPlaceBidCmd() {
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewInt64Coin(s.cfg.BondDenom, 10)).String()),
},
false, &sdk.TxResponse{}, 2,
false, &sdk.TxResponse{}, 18,
},
{
"invalid case: invalid bidding coin denom",
Expand Down
24 changes: 11 additions & 13 deletions x/liquidfarming/keeper/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,29 @@ func (k Keeper) PlaceBid(ctx sdk.Context, auctionId uint64, poolId uint64, bidde
}

if auction.Status != types.AuctionStatusStarted {
return types.Bid{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "auction status must be %s", types.AuctionStatusStarted.String())
return types.Bid{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "auction status must be %s", types.AuctionStatusStarted.String())
}

if auction.BiddingCoinDenom != biddingCoin.Denom {
return types.Bid{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected denom %s, but got %s", auction.BiddingCoinDenom, biddingCoin.Denom)
return types.Bid{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "expected denom %s, but got %s", auction.BiddingCoinDenom, biddingCoin.Denom)
}

if biddingCoin.Amount.LT(liquidFarm.MinBidAmount) {
return types.Bid{}, sdkerrors.Wrapf(types.ErrSmallerThanMinimumAmount, "%s is smaller than %s", biddingCoin.Amount, liquidFarm.MinBidAmount)
return types.Bid{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "must be greater than the minimum bid amount %s", liquidFarm.MinBidAmount)
}

winningBid, found := k.GetWinningBid(ctx, auctionId, poolId)
if found {
if winningBid, found := k.GetWinningBid(ctx, auctionId, poolId); found {
if biddingCoin.Amount.LTE(winningBid.Amount.Amount) {
return types.Bid{}, sdkerrors.Wrapf(types.ErrNotBiggerThanWinningBidAmount, "%s is not bigger than %s", biddingCoin.Amount, winningBid.Amount.Amount)
return types.Bid{}, sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "must be greater than the winning bid amount %s", winningBid.Amount.Amount)
}
}

// Refund the previous bid if exists
previousBid, found := k.GetBid(ctx, auctionId, bidder)
if found {
if previousBid, found := k.GetBid(ctx, auctionId, bidder); found {
if err := k.bankKeeper.SendCoins(ctx, auction.GetPayingReserveAddress(), previousBid.GetBidder(), sdk.NewCoins(previousBid.Amount)); err != nil {
return types.Bid{}, err
}
Expand All @@ -56,11 +58,7 @@ func (k Keeper) PlaceBid(ctx sdk.Context, auctionId uint64, poolId uint64, bidde
return types.Bid{}, err
}

bid := types.NewBid(
poolId,
bidder.String(),
biddingCoin,
)
bid := types.NewBid(poolId, bidder.String(), biddingCoin)
k.SetBid(ctx, bid)
k.SetWinningBid(ctx, auction.Id, bid)

Expand Down
4 changes: 2 additions & 2 deletions x/liquidfarming/keeper/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *KeeperTestSuite) TestPlaceBid_Validation() {
sdk.NewInt64Coin(pool.PoolCoinDenom, 100),
),
nil,
"100 is smaller than 1000000: smaller than the minimum amount",
"must be greater than the minimum bid amount 1000000: invalid request",
},
} {
s.Run(tc.name, func() {
Expand Down Expand Up @@ -137,7 +137,7 @@ func (s *KeeperTestSuite) TestPlaceBid() {

// Place a bid with less than the winning bid amount
_, err = s.keeper.PlaceBid(s.ctx, auctionId, pool.Id, bidderAddr2, sdk.NewInt64Coin(pool.PoolCoinDenom, 90_000_000))
s.Require().EqualError(err, "90000000 is not bigger than 150000000: not bigger than the winning bid amount")
s.Require().EqualError(err, "must be greater than the winning bid amount 150000000: invalid request")
}

func (s *KeeperTestSuite) TestPlaceBid_AuctionStatus() {
Expand Down
2 changes: 1 addition & 1 deletion x/liquidfarming/keeper/liquidfarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) LiquidFarm(ctx sdk.Context, poolId uint64, farmer sdk.AccAddress
}

if farmingCoin.Amount.LT(liquidFarm.MinFarmAmount) {
return sdkerrors.Wrapf(types.ErrSmallerThanMinimumAmount, "%s is smaller than %s", farmingCoin.Amount, liquidFarm.MinFarmAmount)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "must be greater than the minimum amount %s", liquidFarm.MinFarmAmount)
}

reserveAddr := types.LiquidFarmReserveAddress(pool.Id)
Expand Down
2 changes: 1 addition & 1 deletion x/liquidfarming/keeper/liquidfarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *KeeperTestSuite) TestLiquidFarm_Validation() {
sdk.NewInt64Coin(pool.PoolCoinDenom, 100),
),
nil,
"100 is smaller than 100000000: smaller than the minimum amount",
"must be greater than the minimum amount 100000000: invalid request",
},
{
"insufficient funds",
Expand Down
9 changes: 1 addition & 8 deletions x/liquidfarming/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@ package types

// DONTCOVER

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// x/liquidfarming module sentinel errors
var (
ErrSmallerThanMinimumAmount = sdkerrors.Register(ModuleName, 2, "smaller than the minimum amount")
ErrNotBiggerThanWinningBidAmount = sdkerrors.Register(ModuleName, 3, "not bigger than the winning bid amount")
)
var ()

0 comments on commit 06654e7

Please sign in to comment.