Skip to content

Commit

Permalink
Allow to close bid when bid state is open (#676)
Browse files Browse the repository at this point in the history
* Allow to close bid when bid state is open

* Fix handler tests

* Add test for close bid with state open

Co-authored-by: Adam Bozanich <adam.boz@gmail.com>
  • Loading branch information
akhilkumarpilli and boz committed Jun 19, 2020
1 parent 1f291c4 commit a4bcbbc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
15 changes: 11 additions & 4 deletions x/market/handler/handler.go
Expand Up @@ -66,14 +66,21 @@ func handleMsgCloseBid(ctx sdk.Context, keepers Keepers, msg types.MsgCloseBid)
return nil, types.ErrUnknownBid
}

lease, found := keepers.Market.GetLease(ctx, types.LeaseID(msg.BidID))
order, found := keepers.Market.GetOrder(ctx, msg.OrderID())
if !found {
return nil, types.ErrUnknownLeaseForBid
return nil, types.ErrUnknownOrderForBid
}

order, found := keepers.Market.GetOrder(ctx, msg.OrderID())
if bid.State == types.BidOpen {
keepers.Market.OnBidClosed(ctx, bid)
return &sdk.Result{
Events: ctx.EventManager().Events(),
}, nil
}

lease, found := keepers.Market.GetLease(ctx, types.LeaseID(msg.BidID))
if !found {
return nil, types.ErrUnknownOrderForBid
return nil, types.ErrUnknownLeaseForBid
}

if lease.State != types.LeaseActive {
Expand Down
25 changes: 25 additions & 0 deletions x/market/handler/handler_test.go
Expand Up @@ -312,6 +312,8 @@ func TestCloseBidUnknownLease(t *testing.T) {

bid, _ := suite.createBid()

suite.mkeeper.OnBidMatched(suite.ctx, bid)

msg := types.MsgCloseBid{
BidID: bid.ID(),
}
Expand Down Expand Up @@ -344,6 +346,29 @@ func TestCloseBidValid(t *testing.T) {
})
}

func TestCloseBidWithStateOpen(t *testing.T) {
suite := setupTestSuite(t)

bid, _ := suite.createBid()

msg := types.MsgCloseBid{
BidID: bid.ID(),
}

res, err := suite.handler(suite.ctx, msg)
require.NotNil(t, res)
require.NoError(t, err)

t.Run("ensure event created", func(t *testing.T) {
iev := testutil.ParseMarketEvent(t, res.Events[2:])
require.IsType(t, types.EventBidClosed{}, iev)

dev := iev.(types.EventBidClosed)

require.Equal(t, msg.BidID, dev.ID)
})
}

func TestCloseBidNotActiveLease(t *testing.T) {
suite := setupTestSuite(t)

Expand Down

0 comments on commit a4bcbbc

Please sign in to comment.