diff --git a/exchanges/binance/binance_test.go b/exchanges/binance/binance_test.go index d4239621..ce97f837 100644 --- a/exchanges/binance/binance_test.go +++ b/exchanges/binance/binance_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "reflect" "sync" "testing" "time" @@ -2269,7 +2270,7 @@ func TestExecutionTypeToOrderStatus(t *testing.T) { } testCases := []TestCases{ {Case: "NEW", Result: order.New}, - {Case: "CANCELLED", Result: order.Cancelled}, + {Case: "CANCELED", Result: order.Cancelled}, {Case: "REJECTED", Result: order.Rejected}, {Case: "TRADE", Result: order.PartiallyFilled}, {Case: "EXPIRED", Result: order.Expired}, @@ -2490,12 +2491,40 @@ func TestSetExchangeOrderExecutionLimits(t *testing.T) { } func TestWsOrderExecutionReport(t *testing.T) { - t.Parallel() + // cannot run in parallel due to inspecting the DataHandler result payload := []byte(`{"stream":"jTfvpakT2yT0hVIo5gYWVihZhdM2PrBgJUZ5PyfZ4EVpCkx4Uoxk5timcrQc","data":{"e":"executionReport","E":1616627567900,"s":"BTCUSDT","c":"c4wyKsIhoAaittTYlIVLqk","S":"BUY","o":"LIMIT","f":"GTC","q":"0.00028400","p":"52789.10000000","P":"0.00000000","F":"0.00000000","g":-1,"C":"","x":"NEW","X":"NEW","r":"NONE","i":5340845958,"l":"0.00000000","z":"0.00000000","L":"0.00000000","n":"0","N":null,"T":1616627567900,"t":-1,"I":11388173160,"w":true,"m":false,"M":false,"O":1616627567900,"Z":"0.00000000","Y":"0.00000000","Q":"0.00000000"}}`) + expRes := order.Detail{ + Price: 52789.1, + Amount: 0.00028400, + Exchange: "Binance", + ID: "5340845958", + ClientID: "c4wyKsIhoAaittTYlIVLqk", + Side: order.Buy, + Type: order.Limit, + Status: order.New, + AssetType: asset.Spot, + Pair: currency.NewPair(currency.BTC, currency.USDT), + RemainingAmount: 0.000284, + Date: time.Unix(0, 1616627567900*int64(time.Millisecond)), + } + // empty the channel. otherwise mock_test will fail + for len(b.Websocket.DataHandler) > 0 { + <-b.Websocket.DataHandler + } + err := b.wsHandleData(payload) if err != nil { t.Fatal(err) } + res := <-b.Websocket.DataHandler + switch r := res.(type) { + case *order.Detail: + if !reflect.DeepEqual(expRes, *r) { + t.Errorf("Results do not match:\nexpected: %v\nreceived: %v", expRes, *r) + } + default: + t.Fatalf("expected type order.Detail, found %T", res) + } payload = []byte(`{"stream":"jTfvpakT2yT0hVIo5gYWVihZhdM2PrBgJUZ5PyfZ4EVpCkx4Uoxk5timcrQc","data":{"e":"executionReport","E":1616633041556,"s":"BTCUSDT","c":"YeULctvPAnHj5HXCQo9Mob","S":"BUY","o":"LIMIT","f":"GTC","q":"0.00028600","p":"52436.85000000","P":"0.00000000","F":"0.00000000","g":-1,"C":"","x":"TRADE","X":"FILLED","r":"NONE","i":5341783271,"l":"0.00028600","z":"0.00028600","L":"52436.85000000","n":"0.00000029","N":"BTC","T":1616633041555,"t":726946523,"I":11390206312,"w":false,"m":false,"M":true,"O":1616633041555,"Z":"14.99693910","Y":"14.99693910","Q":"0.00000000"}}`) err = b.wsHandleData(payload) diff --git a/exchanges/binance/binance_types.go b/exchanges/binance/binance_types.go index 267b031e..4581f28f 100644 --- a/exchanges/binance/binance_types.go +++ b/exchanges/binance/binance_types.go @@ -736,7 +736,7 @@ type wsOrderUpdate struct { // WsOrderUpdateData defines websocket account order update data type WsOrderUpdateData struct { - ClientOrderID string `json:"C"` + ClientOrderID string `json:"c"` EventTime time.Time `json:"E"` IcebergQuantity float64 `json:"F,string"` LastExecutedPrice float64 `json:"L,string"` @@ -749,7 +749,7 @@ type WsOrderUpdateData struct { OrderStatus string `json:"X"` LastQuoteAssetTransactedQuantity float64 `json:"Y,string"` CumulativeQuoteTransactedQuantity float64 `json:"Z,string"` - CancelledClientOrderID string `json:"c"` + CancelledClientOrderID string `json:"C"` EventType string `json:"e"` TimeInForce string `json:"f"` OrderListID int64 `json:"g"` @@ -763,6 +763,7 @@ type WsOrderUpdateData struct { RejectionReason string `json:"r"` Symbol string `json:"s"` TradeID int64 `json:"t"` + Ignored int64 `json:"I"` // must be ignored explicitly, otherwise it overwrites 'i' IsOnOrderBook bool `json:"w"` CurrentExecutionType string `json:"x"` CumulativeFilledQuantity float64 `json:"z,string"` diff --git a/exchanges/binance/binance_websocket.go b/exchanges/binance/binance_websocket.go index 8df87855..7775f3a1 100644 --- a/exchanges/binance/binance_websocket.go +++ b/exchanges/binance/binance_websocket.go @@ -253,6 +253,7 @@ func (b *Binance) wsHandleData(respRaw []byte) error { AssetType: a, Date: data.Data.OrderCreationTime, Pair: p, + ClientID: data.Data.ClientOrderID, } return nil case "listStatus": @@ -421,7 +422,7 @@ func stringToOrderStatus(status string) (order.Status, error) { switch status { case "NEW": return order.New, nil - case "CANCELLED": + case "CANCELED": return order.Cancelled, nil case "REJECTED": return order.Rejected, nil