Skip to content

Commit

Permalink
limit order qty and rate to max int64 for db purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Feb 7, 2025
1 parent a2556d1 commit da7a07c
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions dex/order/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"sync"
"time"

Expand Down Expand Up @@ -732,6 +733,16 @@ func ValidateOrder(ord Order, status OrderStatus, lotSize uint64) error {
return fmt.Errorf("same asset specified for base and quote")
}

validateTrade := func(t *Trade) error {
if t.Quantity%lotSize != 0 || t.Remaining()%lotSize != 0 {
return fmt.Errorf("market sell order fails lot size requirement %d %% %d = %d", t.Quantity, lotSize, t.Quantity%lotSize)
}
if t.Quantity > math.MaxInt64 {
return fmt.Errorf("order quantity %d is greater than max allowed %d", t.Quantity, math.MaxInt64)
}
return nil
}

// Each order type has different rules about status and lot size.
switch ot := ord.(type) {
case *MarketOrder:
Expand All @@ -750,8 +761,10 @@ func ValidateOrder(ord Order, status OrderStatus, lotSize uint64) error {
// Market sell orders must respect lot size. Market buy orders must be
// of an amount sufficiently buffered beyond the minimum standing sell
// order's lot cost, but that is enforced by the order router.
if ot.Sell && (ot.Quantity%lotSize != 0 || ot.Remaining()%lotSize != 0) {
return fmt.Errorf("market sell order fails lot size requirement %d %% %d = %d", ot.Quantity, lotSize, ot.Quantity%lotSize)
if ot.Sell {
if err := validateTrade(&ot.T); err != nil {
return err
}
}

case *CancelOrder:
Expand Down Expand Up @@ -788,9 +801,14 @@ func ValidateOrder(ord Order, status OrderStatus, lotSize uint64) error {
}

// All limit orders must respect lot size.
if ot.Quantity%lotSize != 0 || ot.Remaining()%lotSize != 0 {
return fmt.Errorf("limit order fails lot size requirement %d %% %d = %d", ot.Quantity, lotSize, ot.Quantity%lotSize)
if err := validateTrade(&ot.T); err != nil {
return err
}

if ot.Rate > math.MaxInt64 {
return fmt.Errorf("order rate %d is greater than max allowed %d", ot.Rate, math.MaxInt64)
}

default:
// cannot validate an unknown order type
return fmt.Errorf("unknown order type")
Expand Down

0 comments on commit da7a07c

Please sign in to comment.