Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQS-417 | Fix orderbook order Quantity parsing #519

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}
Loading