Skip to content

Commit

Permalink
Merge pull request #177 from crescent-network/fix-num-mm-orders
Browse files Browse the repository at this point in the history
fix: fix bug in NumMMOrders calculation
  • Loading branch information
kingcre authored Aug 10, 2023
2 parents 0709514 + 24de37d commit c275d58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions x/exchange/keeper/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (k Keeper) placeLimitOrder(
k.SetOrdersByOrdererIndex(ctx, order)

if typ == types.OrderTypeMM {
// NOTE: NumMMOrders might have been changed in executeOrder if the
// orderer completed own orders.
numMMOrders, _ = k.GetNumMMOrders(ctx, ordererAddr, marketId)
k.SetNumMMOrders(ctx, ordererAddr, marketId, numMMOrders+1)
}
}
Expand Down
17 changes: 17 additions & 0 deletions x/exchange/keeper/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,20 @@ func (s *KeeperTestSuite) TestDecQuantity() {
diff, _ := orderer3BalancesAfter.SafeSub(orderer3BalancesBefore)
s.AssertEqual(sdk.NewInt(380), diff.AmountOf("uusd")) // 2699.7*0.14076=380.009722
}

func (s *KeeperTestSuite) TestNumMMOrdersEdgecase() {
market := s.CreateMarket("ucre", "uusd")

ordererAddr := s.FundedAccount(1, enoughCoins)
// Place 2 MM orders
s.PlaceMMLimitOrder(market.Id, ordererAddr, true, utils.ParseDec("4.9"), sdk.NewDec(10_000000), time.Hour)
s.PlaceMMLimitOrder(market.Id, ordererAddr, true, utils.ParseDec("4.85"), sdk.NewDec(10_000000), time.Hour)

// Match against own orders
s.PlaceMMLimitOrder(market.Id, ordererAddr, false, utils.ParseDec("4.5"), sdk.NewDec(30_000000), time.Hour)

numMMOrders, _ := s.keeper.GetNumMMOrders(s.Ctx, ordererAddr, market.Id)
// The number of MM orders should be 1, since previous order are fully matched
// and deleted.
s.Require().Equal(uint32(1), numMMOrders)
}

0 comments on commit c275d58

Please sign in to comment.