diff --git a/x/exchange/keeper/order.go b/x/exchange/keeper/order.go index 12bb2ad8..72acd13c 100644 --- a/x/exchange/keeper/order.go +++ b/x/exchange/keeper/order.go @@ -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) } } diff --git a/x/exchange/keeper/order_test.go b/x/exchange/keeper/order_test.go index 9fcff7a7..db4fc692 100644 --- a/x/exchange/keeper/order_test.go +++ b/x/exchange/keeper/order_test.go @@ -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) +}