Skip to content

Commit

Permalink
Issues hopefully fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
cranktakular committed Sep 3, 2024
1 parent 0e0c09e commit 9edf0c9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
4 changes: 2 additions & 2 deletions exchanges/coinbasepro/coinbasepro.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,11 @@ func (c *CoinbasePro) GetFills(ctx context.Context, orderID, productID, cursor s
}

// GetOrderByID returns a single order by order id.
func (c *CoinbasePro) GetOrderByID(ctx context.Context, orderID, clientOID, userNativeCurrency string) (*GetOrderResponse, error) {
func (c *CoinbasePro) GetOrderByID(ctx context.Context, orderID, clientOID, userNativeCurrency string) (*SingleOrder, error) {
if orderID == "" {
return nil, errOrderIDEmpty
}
var resp GetOrderResponse
var resp SingleOrder
vals := url.Values{}
if clientOID != "" {
vals.Set("client_order_id", clientOID)
Expand Down
21 changes: 14 additions & 7 deletions exchanges/coinbasepro/coinbasepro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,14 @@ func orderTestHelper(t *testing.T, orderSide string) *GetAllOrdersResp {
if ordIDs == nil || len(ordIDs.Orders) == 0 {
t.Skip(skipInsufficientOrders)
}
return ordIDs
for i := range ordIDs.Orders {
if ordIDs.Orders[i].Status == order.Open.String() {
ordIDs.Orders = ordIDs.Orders[i : i+1]
return ordIDs
}
}
t.Skip(skipInsufficientOrders)
return nil
}

func TestCancelOrders(t *testing.T) {
Expand Down Expand Up @@ -1580,9 +1587,12 @@ func TestWsHandleData(t *testing.T) {
continue
}
}()
_, err := c.wsHandleData(nil, 0)
mockJSON := []byte(`{"type": "error"}`)
_, err := c.wsHandleData(mockJSON, 0)
assert.Error(t, err)
_, err = c.wsHandleData(nil, 0)
assert.ErrorIs(t, err, jsonparser.KeyPathNotFoundError)
mockJSON := []byte(`{"sequence_num": "l"}`)
mockJSON = []byte(`{"sequence_num": "l"}`)
_, err = c.wsHandleData(mockJSON, 0)
assert.ErrorIs(t, err, strconv.ErrSyntax)
mockJSON = []byte(`{"sequence_num": 1, /\\/"""}`)
Expand All @@ -1601,9 +1611,6 @@ func TestWsHandleData(t *testing.T) {
mockJSON = []byte(`{"sequence_num": 0, "channel": "status", "events": [{"type": "moo"}]}`)
_, err = c.wsHandleData(mockJSON, 0)
assert.NoError(t, err)
mockJSON = []byte(`{"sequence_num": 0, "channel": "error", "events": [{"type": "moo"}]}`)
_, err = c.wsHandleData(mockJSON, 0)
assert.NoError(t, err)
mockJSON = []byte(`{"sequence_num": 0, "channel": "ticker", "events": ["type": ""}]}`)
_, err = c.wsHandleData(mockJSON, 0)
assert.ErrorAs(t, err, &targetErr)
Expand Down Expand Up @@ -1717,7 +1724,7 @@ func TestSubscribeUnsubscribe(t *testing.T) {

func TestCheckSubscriptions(t *testing.T) {
t.Parallel()
c := &CoinbasePro{
c := &CoinbasePro{ //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
Base: exchange.Base{
Config: &config.Exchange{
Features: &config.FeaturesConfig{
Expand Down
9 changes: 8 additions & 1 deletion exchanges/coinbasepro/coinbasepro_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ type EditOrderPreviewResp struct {
AverageFilledPrice float64 `json:"average_filled_price,string"`
}

type SingleOrder struct {

Check failure on line 301 in exchanges/coinbasepro/coinbasepro_types.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type SingleOrder should have comment or be unexported (revive)
Order GetOrderResponse `json:"order"`
}

// GetOrderResponse contains information on an order, returned by GetOrderByID
// and IterativeGetAllOrders, and used in GetAllOrdersResp
type GetOrderResponse struct {
Expand All @@ -313,7 +317,7 @@ type GetOrderResponse struct {
CompletionPercentage float64 `json:"completion_percentage,string"`
FilledSize float64 `json:"filled_size,string"`
AverageFilledPrice float64 `json:"average_filled_price,string"`
Fee float64 `json:"fee,string"`
Fee types.Number `json:"fee"`
NumberOfFills int64 `json:"num_fills,string"`
FilledValue float64 `json:"filled_value,string"`
PendingCancel bool `json:"pending_cancel"`
Expand All @@ -337,6 +341,9 @@ type GetOrderResponse struct {
Size float64 `json:"size,string"`
ReplaceAcceptTimestamp time.Time `json:"replace_accept_timestamp"`
} `json:"edit_history"`
Leverage types.Number `json:"leverage"`
MarginType string `json:"margin_type"`
RetailPortfolioID string `json:"retail_portfolio_id"`
}

// FillResponse contains fill information, returned by GetFills
Expand Down
10 changes: 7 additions & 3 deletions exchanges/coinbasepro/coinbasepro_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ var subscriptionNames = map[string]string{

var defaultSubscriptions = subscription.List{
{Enabled: true, Channel: subscription.HeartbeatChannel},
{Enabled: true, Channel: "status"},
// Subscriptions to status return an "authentication failure" error, despite the endpoint not being authenticated
// and other authenticated channels working fine.
{Enabled: false, Channel: "status"},
{Enabled: true, Asset: asset.Spot, Channel: subscription.TickerChannel},
{Enabled: true, Asset: asset.Spot, Channel: subscription.CandlesChannel},
{Enabled: true, Asset: asset.Spot, Channel: subscription.AllTradesChannel},
Expand Down Expand Up @@ -108,6 +110,10 @@ func (c *CoinbasePro) wsReadData() {
// wsHandleData handles all the websocket data coming from the websocket connection
func (c *CoinbasePro) wsHandleData(respRaw []byte, seqCount uint64) (string, error) {
var warnString string
ertype, _, _, err := jsonparser.Get(respRaw, "type")
if err == nil && string(ertype) == "error" {
return warnString, errors.New(string(respRaw))
}
seqData, _, _, err := jsonparser.Get(respRaw, "sequence_num")
if err != nil {
return warnString, err
Expand Down Expand Up @@ -140,8 +146,6 @@ func (c *CoinbasePro) wsHandleData(respRaw []byte, seqCount uint64) (string, err
return warnString, err
}
c.Websocket.DataHandler <- wsStatus
case "error":
c.Websocket.DataHandler <- errors.New(string(respRaw))
case "ticker", "ticker_batch":
wsTicker := []WebsocketTickerHolder{}
err = json.Unmarshal(data, &wsTicker)
Expand Down
16 changes: 12 additions & 4 deletions exchanges/coinbasepro/coinbasepro_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (c *CoinbasePro) SubmitOrder(ctx context.Context, s *order.Submit) (*order.
if err != nil {
return nil, err
}
subResp.Fee = feeResp.TotalFees
subResp.Fee = feeResp.Order.TotalFees
}
return subResp, nil
}
Expand Down Expand Up @@ -651,7 +651,7 @@ func (c *CoinbasePro) GetOrderInfo(ctx context.Context, orderID string, pair cur
if err != nil {
return nil, err
}
response := c.getOrderRespToOrderDetail(genOrderDetail, pair, assetItem)
response := c.getOrderRespToOrderDetail(&genOrderDetail.Order, pair, assetItem)
fillData, err := c.GetFills(ctx, orderID, "", "", time.Time{}, time.Now(), 2<<15-1)
if err != nil {
return nil, err
Expand Down Expand Up @@ -819,7 +819,11 @@ func (c *CoinbasePro) GetActiveOrders(ctx context.Context, req *order.MultiOrder
}
orders := make([]order.Detail, len(respOrders))
for i := range respOrders {
orderRec := c.getOrderRespToOrderDetail(&respOrders[i], req.Pairs[i], asset.Spot)
tempPair, err := currency.NewPairFromString(respOrders[i].ProductID)
if err != nil {
return nil, err
}
orderRec := c.getOrderRespToOrderDetail(&respOrders[i], tempPair, asset.Spot)
orders[i] = *orderRec
}
return req.Filter(c.Name, orders), nil
Expand Down Expand Up @@ -864,7 +868,11 @@ func (c *CoinbasePro) GetOrderHistory(ctx context.Context, req *order.MultiOrder
}
orders := make([]order.Detail, len(ord))
for i := range ord {
singleOrder := c.getOrderRespToOrderDetail(&ord[i], req.Pairs[0], req.AssetType)
tempPair, err := currency.NewPairFromString(ord[i].ProductID)
if err != nil {
return nil, err
}
singleOrder := c.getOrderRespToOrderDetail(&ord[i], tempPair, req.AssetType)
orders[i] = *singleOrder
}
return req.Filter(c.Name, orders), nil
Expand Down

0 comments on commit 9edf0c9

Please sign in to comment.