Skip to content

Commit

Permalink
SQS-417 | Fix orderbook order Quantity parsing (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
deividaspetraitis authored Sep 23, 2024
1 parent 65036e2 commit 0c663b7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
5 changes: 2 additions & 3 deletions domain/orderbook/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package orderbookdomain

import (
"fmt"
"strconv"

"github.com/osmosis-labs/osmosis/osmomath"
)
Expand Down Expand Up @@ -34,12 +33,12 @@ type Order struct {

// Status returns the status of the order based on the percent filled.
func (o Order) Status(percentFilled float64) (OrderStatus, error) {
quantity, err := strconv.Atoi(o.Quantity)
quantity, err := osmomath.NewDecFromStr(o.Quantity)
if err != nil {
return "", fmt.Errorf("error parsing quantity: %w", err)
}

if quantity == 0 || percentFilled == 1 {
if quantity.IsZero() || percentFilled == 1 {
return StatusFilled, nil
}

Expand Down
77 changes: 77 additions & 0 deletions domain/orderbook/order_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package orderbookdomain_test

import (
"testing"

orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"

"github.com/stretchr/testify/assert"
)

func TestOrderStatus(t *testing.T) {
tests := []struct {
name string
order orderbookdomain.Order
percentFilled float64
expected orderbookdomain.OrderStatus
expectError bool
}{
{
name: "Valid quantity, percentFilled = 0",
order: orderbookdomain.Order{Quantity: "100.0"},
percentFilled: 0,
expected: orderbookdomain.StatusOpen,
expectError: false,
},
{
name: "Valid quantity, percentFilled = 1",
order: orderbookdomain.Order{Quantity: "100.0"},
percentFilled: 1,
expected: orderbookdomain.StatusFilled,
expectError: false,
},
{
name: "Valid quantity, percentFilled < 1",
order: orderbookdomain.Order{Quantity: "100.0"},
percentFilled: 0.5,
expected: orderbookdomain.StatusPartiallyFilled,
expectError: false,
},
{
name: "Zero quantity",
order: orderbookdomain.Order{Quantity: "0"},
percentFilled: 1,
expected: orderbookdomain.StatusFilled,
expectError: false,
},
{
name: "Invalid quantity string",
order: orderbookdomain.Order{Quantity: "invalid"},
percentFilled: 1,
expectError: true,
},
{
name: "Empty quantity string",
order: orderbookdomain.Order{Quantity: ""},
percentFilled: 1,
expectError: true,
},
{
name: "Out of range quantity string",
order: orderbookdomain.Order{Quantity: "101960000000000000000"},
expected: orderbookdomain.StatusFilled,
percentFilled: 1,
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status, err := tt.order.Status(tt.percentFilled)
if tt.expectError {
assert.Error(t, err)
}
assert.Equal(t, tt.expected, status)
})
}
}

0 comments on commit 0c663b7

Please sign in to comment.