|
| 1 | +package futures |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/adshao/go-binance/v2/common" |
| 8 | + "github.com/adshao/go-binance/v2/common/websocket" |
| 9 | +) |
| 10 | + |
| 11 | +// OrderStatusWsService query order |
| 12 | +type OrderStatusWsService struct { |
| 13 | + c websocket.Client |
| 14 | + ApiKey string |
| 15 | + SecretKey string |
| 16 | + KeyType string |
| 17 | + TimeOffset int64 |
| 18 | +} |
| 19 | + |
| 20 | +// NewOrderStatusWsService init OrderStatusWsService |
| 21 | +func NewOrderStatusWsService(apiKey, secretKey string) (*OrderStatusWsService, error) { |
| 22 | + conn, err := websocket.NewConnection(WsApiInitReadWriteConn, WebsocketKeepalive, WebsocketTimeoutReadWriteConnection) |
| 23 | + if err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + |
| 27 | + client, err := websocket.NewClient(conn) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + return &OrderStatusWsService{ |
| 33 | + c: client, |
| 34 | + ApiKey: apiKey, |
| 35 | + SecretKey: secretKey, |
| 36 | + KeyType: common.KeyTypeHmac, |
| 37 | + }, nil |
| 38 | +} |
| 39 | + |
| 40 | +// OrderStatusWsRequest parameters for 'order.status' websocket API |
| 41 | +type OrderStatusWsRequest struct { |
| 42 | + symbol string |
| 43 | + orderID int64 |
| 44 | + origClientOrderID string |
| 45 | + timestamp int64 |
| 46 | +} |
| 47 | + |
| 48 | +// NewOrderStatusWsRequest init OrderStatusWsRequest |
| 49 | +func NewOrderStatusWsRequest() *OrderStatusWsRequest { |
| 50 | + return &OrderStatusWsRequest{} |
| 51 | +} |
| 52 | + |
| 53 | +// Symbol set symbol |
| 54 | +func (s *OrderStatusWsRequest) Symbol(symbol string) *OrderStatusWsRequest { |
| 55 | + s.symbol = symbol |
| 56 | + return s |
| 57 | +} |
| 58 | + |
| 59 | +// OrderID set orderID |
| 60 | +func (s *OrderStatusWsRequest) OrderID(orderID int64) *OrderStatusWsRequest { |
| 61 | + s.orderID = orderID |
| 62 | + return s |
| 63 | +} |
| 64 | + |
| 65 | +// OrigClientOrderID set origClientOrderID |
| 66 | +func (s *OrderStatusWsRequest) OrigClientOrderID(origClientOrderID string) *OrderStatusWsRequest { |
| 67 | + s.origClientOrderID = origClientOrderID |
| 68 | + return s |
| 69 | +} |
| 70 | + |
| 71 | +// QueryOrderResponse define query order response |
| 72 | +type QueryOrderResponse struct { |
| 73 | + AvgPrice string `json:"avgPrice"` |
| 74 | + ClientOrderID string `json:"clientOrderId"` |
| 75 | + CumQuote string `json:"cumQuote"` |
| 76 | + ExecutedQty string `json:"executedQty"` |
| 77 | + OrderID int64 `json:"orderId"` |
| 78 | + OrigQty string `json:"origQty"` |
| 79 | + OrigType string `json:"origType"` |
| 80 | + Price string `json:"price"` |
| 81 | + ReduceOnly bool `json:"reduceOnly"` |
| 82 | + Side string `json:"side"` |
| 83 | + PositionSide string `json:"positionSide"` |
| 84 | + Status string `json:"status"` |
| 85 | + Symbol string `json:"symbol"` |
| 86 | + Time int64 `json:"time"` |
| 87 | + TimeInForce string `json:"timeInForce"` |
| 88 | + Type string `json:"type"` |
| 89 | + UpdateTime int64 `json:"updateTime"` |
| 90 | + ClosePosition bool `json:"closePosition"` |
| 91 | + PriceProtect bool `json:"priceProtect"` |
| 92 | + StopPrice string `json:"stopPrice"` |
| 93 | + ActivatePrice string `json:"activatePrice"` |
| 94 | + PriceRate string `json:"priceRate"` |
| 95 | + WorkingType string `json:"workingType"` |
| 96 | +} |
| 97 | + |
| 98 | +// QueryOrderResult define order creation result |
| 99 | +type QueryOrderResult struct { |
| 100 | + QueryOrderResponse |
| 101 | +} |
| 102 | + |
| 103 | +// QueryOrderWsResponse define 'order.status' websocket API response |
| 104 | +type QueryOrderWsResponse struct { |
| 105 | + Id string `json:"id"` |
| 106 | + Status int `json:"status"` |
| 107 | + Result QueryOrderResult `json:"result"` |
| 108 | + |
| 109 | + // error response |
| 110 | + Error *common.APIError `json:"error,omitempty"` |
| 111 | +} |
| 112 | + |
| 113 | +func (r *OrderStatusWsRequest) GetParams() map[string]interface{} { |
| 114 | + return r.buildParams() |
| 115 | +} |
| 116 | + |
| 117 | +// buildParams builds params |
| 118 | +func (s *OrderStatusWsRequest) buildParams() params { |
| 119 | + m := params{ |
| 120 | + "symbol": s.symbol, |
| 121 | + "orderId": s.orderID, |
| 122 | + } |
| 123 | + if s.origClientOrderID != "" { |
| 124 | + m["origClientOrderId"] = s.origClientOrderID |
| 125 | + } |
| 126 | + |
| 127 | + return m |
| 128 | +} |
| 129 | + |
| 130 | +// Do - sends 'order.status' request |
| 131 | +func (s *OrderStatusWsService) Do(requestID string, request *OrderStatusWsRequest) error { |
| 132 | + rawData, err := websocket.CreateRequest( |
| 133 | + websocket.NewRequestData( |
| 134 | + requestID, |
| 135 | + s.ApiKey, |
| 136 | + s.SecretKey, |
| 137 | + s.TimeOffset, |
| 138 | + s.KeyType, |
| 139 | + ), |
| 140 | + websocket.OrderStatusFuturesWsApiMethod, |
| 141 | + request.buildParams(), |
| 142 | + ) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + if err := s.c.Write(requestID, rawData); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// SyncDo - sends 'order.status' request and receives response |
| 155 | +func (s *OrderStatusWsService) SyncDo(requestID string, request *OrderStatusWsRequest) (*QueryOrderWsResponse, error) { |
| 156 | + rawData, err := websocket.CreateRequest( |
| 157 | + websocket.NewRequestData( |
| 158 | + requestID, |
| 159 | + s.ApiKey, |
| 160 | + s.SecretKey, |
| 161 | + s.TimeOffset, |
| 162 | + s.KeyType, |
| 163 | + ), |
| 164 | + websocket.OrderStatusFuturesWsApiMethod, |
| 165 | + request.buildParams(), |
| 166 | + ) |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + |
| 171 | + response, err := s.c.WriteSync(requestID, rawData, websocket.WriteSyncWsTimeout) |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + |
| 176 | + queryOrderWsResponse := &QueryOrderWsResponse{} |
| 177 | + if err := json.Unmarshal(response, queryOrderWsResponse); err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + |
| 181 | + return queryOrderWsResponse, nil |
| 182 | +} |
| 183 | + |
| 184 | +// ReceiveAllDataBeforeStop waits until all responses will be received from websocket until timeout expired |
| 185 | +func (s *OrderStatusWsService) ReceiveAllDataBeforeStop(timeout time.Duration) { |
| 186 | + s.c.Wait(timeout) |
| 187 | +} |
| 188 | + |
| 189 | +// GetReadChannel returns channel with API response data (including API errors) |
| 190 | +func (s *OrderStatusWsService) GetReadChannel() <-chan []byte { |
| 191 | + return s.c.GetReadChannel() |
| 192 | +} |
| 193 | + |
| 194 | +// GetReadErrorChannel returns channel with errors which are occurred while reading websocket connection |
| 195 | +func (s *OrderStatusWsService) GetReadErrorChannel() <-chan error { |
| 196 | + return s.c.GetReadErrorChannel() |
| 197 | +} |
| 198 | + |
| 199 | +// GetReconnectCount returns count of reconnect attempts by client |
| 200 | +func (s *OrderStatusWsService) GetReconnectCount() int64 { |
| 201 | + return s.c.GetReconnectCount() |
| 202 | +} |
0 commit comments