Skip to content

Commit

Permalink
Merge pull request #203 from crescent-network/fix/integer-paid-received
Browse files Browse the repository at this point in the history
fix: force paid/received/deposit to be integers for user orders
  • Loading branch information
kingcre authored Sep 11, 2023
2 parents 325c028 + 3edbb72 commit f1c7203
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions x/exchange/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func OrderStateInvariant(k Keeper) sdk.Invariant {
msg += fmt.Sprintf("\torder %d should have been deleted since it has no executable quantity\n", order.Id)
cnt++
}
if !order.RemainingDeposit.TruncateDec().Equal(order.RemainingDeposit) {
msg += fmt.Sprintf("\torder %d should have integer remaining deposit but has %s\n",
order.Id, order.RemainingDeposit)
cnt++
}
return false
})
broken := cnt != 0
Expand Down
11 changes: 7 additions & 4 deletions x/exchange/keeper/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func (k Keeper) finalizeMatching(ctx sdk.Context, market types.Market, orders []
ordererAddr := memOrder.OrdererAddress()
if memOrder.IsMatched() {
receivedCoin := sdk.NewDecCoinFromDec(receiveDenom, memOrder.Received())
escrow.Unlock(ordererAddr, receivedCoin)
if memOrder.Type() == types.UserMemOrder {
paid := memOrder.Paid().Ceil()
receivedCoin.Amount = receivedCoin.Amount.TruncateDec()
order := memOrder.Order()
order.OpenQuantity = order.OpenQuantity.Sub(memOrder.ExecutedQuantity())
order.RemainingDeposit = memOrder.RemainingDeposit()
order.RemainingDeposit = order.RemainingDeposit.Sub(paid)
if err := ctx.EventManager().EmitTypedEvent(&types.EventOrderFilled{
MarketId: market.Id,
OrderId: order.Id,
Expand All @@ -108,7 +109,7 @@ func (k Keeper) finalizeMatching(ctx sdk.Context, market types.Market, orders []
Quantity: order.Quantity,
OpenQuantity: order.OpenQuantity,
ExecutedQuantity: memOrder.ExecutedQuantity(),
Paid: sdk.NewDecCoinFromDec(payDenom, memOrder.Paid()),
Paid: sdk.NewDecCoinFromDec(payDenom, paid),
Received: receivedCoin,
}); err != nil {
return err
Expand All @@ -129,6 +130,7 @@ func (k Keeper) finalizeMatching(ctx sdk.Context, market types.Market, orders []
k.SetOrder(ctx, order)
}
}
escrow.Unlock(ordererAddr, receivedCoin)
}
// Should refund deposit
if memOrder.Type() == types.OrderSourceMemOrder && memOrder.RemainingDeposit().IsPositive() {
Expand Down Expand Up @@ -176,7 +178,8 @@ func (k Keeper) finalizeMatching(ctx sdk.Context, market types.Market, orders []
if totalFee.IsNegative() {
paid.Amount = paid.Amount.Add(totalFee)
}
received := sdk.NewDecCoinFromDec(receiveDenom, totalReceived)
paid.Amount = paid.Amount.Ceil()
received := sdk.NewDecCoinFromDec(receiveDenom, totalReceived.TruncateDec())
if err := ctx.EventManager().EmitTypedEvent(&types.EventOrderSourceOrdersFilled{
MarketId: market.Id,
SourceName: sourceName,
Expand Down
5 changes: 4 additions & 1 deletion x/exchange/keeper/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,11 @@ func (s *KeeperTestSuite) TestDecQuantity() {
s.PlaceMarketOrder(market.Id, ordererAddr3, false, sdk.NewDec(10000))
orderer3BalancesAfter := s.GetAllBalances(ordererAddr3)

// order2 remaining deposit = 380uusd
// order2 executable quantity ~= 2699.6305768
// order2 executable quantity * 0.14076 ~= 379.9999999
diff, _ := orderer3BalancesAfter.SafeSub(orderer3BalancesBefore)
s.AssertEqual(sdk.NewInt(380), diff.AmountOf("uusd")) // 2699.7*0.14076=380.009722
s.AssertEqual(sdk.NewInt(379), diff.AmountOf("uusd"))
}

func (s *KeeperTestSuite) TestNumMMOrdersEdgecase() {
Expand Down
2 changes: 1 addition & 1 deletion x/exchange/types/mem_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (order *MemOrder) ExecutableQuantity() sdk.Dec {
if order.isBuy {
return sdk.MinDec(executableQty, order.remainingDeposit.QuoTruncate(order.price))
}
return executableQty
return sdk.MinDec(executableQty, order.remainingDeposit)
}

func (order *MemOrder) HasPriorityOver(other *MemOrder) bool {
Expand Down

0 comments on commit f1c7203

Please sign in to comment.