Skip to content

Commit

Permalink
🔧 fix(coinbase): fix query trades and add corresponding test
Browse files Browse the repository at this point in the history
  • Loading branch information
dboy committed Feb 25, 2025
1 parent bc49378 commit 549958d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
5 changes: 3 additions & 2 deletions pkg/exchange/coinbase/api/v1/get_order_trades_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ type Trade struct {

type TradeSnapshot []Trade

//go:generate requestgen -method GET -url "/orders/fills" -type GetOrderTradesRequest -responseType .TradeSnapshot
//go:generate requestgen -method GET -url "/fills" -type GetOrderTradesRequest -responseType .TradeSnapshot
type GetOrderTradesRequest struct {
client requestgen.AuthenticatedAPIClient

orderID string `param:"order_id,required"`
orderID string `param:"order_id"`
productID string `param:"product_id"` // one of order_id or product_id is required
limit int `param:"limit"`
before *int `param:"before"` // pagination id, which is the trade_id (exclusive)
after *int `param:"after"` // pagination id, which is the trade_id (exclusive)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/exchange/coinbase/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func toGlobalOrder(cbOrder *api.Order) types.Order {
UUID: cbOrder.ID,
OrderID: FNV64a(cbOrder.ID),
OriginalStatus: string(cbOrder.Status),
CreationTime: types.Time(cbOrder.CreatedAt),
CreationTime: cbOrder.CreatedAt,
}
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/exchange/coinbase/exchage.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.O
}

func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]types.Trade, error) {
cbTrades, err := e.queryOrderTradesByPagination(ctx, q.OrderID)
cbTrades, err := e.queryOrderTradesByPagination(ctx, q)
if err != nil {
return nil, errors.Wrapf(err, "failed to get order trades: %v", q.OrderID)
}
Expand All @@ -366,8 +366,15 @@ func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]
return trades, nil
}

func (e *Exchange) queryOrderTradesByPagination(ctx context.Context, orderID string) (api.TradeSnapshot, error) {
req := e.client.NewGetOrderTradesRequest().OrderID(orderID).Limit(PaginationLimit)
func (e *Exchange) queryOrderTradesByPagination(ctx context.Context, q types.OrderQuery) (api.TradeSnapshot, error) {
paginationLimit := 100
req := e.client.NewGetOrderTradesRequest().Limit(paginationLimit)
if len(q.OrderID) > 0 {
req.OrderID(q.OrderID)
}
if len(q.Symbol) > 0 {
req.ProductID(toLocalSymbol(q.Symbol))
}
cbTrades, err := req.Do(ctx)
if err != nil {
return nil, err
Expand Down
53 changes: 51 additions & 2 deletions pkg/exchange/coinbase/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_new(t *testing.T) {
_ = ex.PlatformFeeCurrency()
}

func Test_SubmitOrder(t *testing.T) {
func Test_OrdersAPI(t *testing.T) {
ex := getExchangeOrSkip(t)
ctx := context.Background()

Expand All @@ -37,7 +37,40 @@ func Test_SubmitOrder(t *testing.T) {
})
assert.Error(t, err)
assert.Empty(t, order)
// TODO: test with real symbol
// should succeed
order, err = ex.SubmitOrder(
ctx,
types.SubmitOrder{
Symbol: "ETHUSD",
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Price: fixedpoint.MustNewFromString("0.01"),
Quantity: fixedpoint.MustNewFromString("100"), // min funds is $1
})
assert.NoError(t, err)
assert.NotEmpty(t, order)

// test query open orders
order, err = ex.QueryOrder(ctx, types.OrderQuery{Symbol: "ETHUSD", OrderID: order.UUID, ClientOrderID: order.UUID})
assert.NoError(t, err)

orders, err := ex.QueryOpenOrders(ctx, "ETHUSD")
assert.NoError(t, err)
found := false
for _, o := range orders {
if o.UUID == order.UUID {
found = true
break
}
}
assert.True(t, found)

// test cancel order
err = ex.CancelOrders(ctx, types.Order{
Exchange: types.ExchangeCoinBase,
UUID: order.UUID,
})
assert.NoError(t, err)
}

func Test_QueryAccount(t *testing.T) {
Expand All @@ -47,6 +80,13 @@ func Test_QueryAccount(t *testing.T) {
assert.NoError(t, err)
}

func Test_QueryAccountBalances(t *testing.T) {
ex := getExchangeOrSkip(t)
ctx := context.Background()
_, err := ex.QueryAccountBalances(ctx)
assert.NoError(t, err)
}

func Test_QueryOpenOrders(t *testing.T) {
ex := getExchangeOrSkip(t)
ctx := context.Background()
Expand Down Expand Up @@ -94,6 +134,15 @@ func Test_QueryKLines(t *testing.T) {
assert.NotNil(t, klines)
}

func Test_QueryOrderTrades(t *testing.T) {
ex := getExchangeOrSkip(t)
ctx := context.Background()

trades, err := ex.QueryOrderTrades(ctx, types.OrderQuery{Symbol: "ETHUSD"})
assert.NoError(t, err)
assert.NotNil(t, trades)
}

func getExchangeOrSkip(t *testing.T) *Exchange {
if b, _ := strconv.ParseBool(os.Getenv("CI")); b {
t.Skip("skip test for CI")
Expand Down

0 comments on commit 549958d

Please sign in to comment.