Exemplo n.º 1
0
// GetOrderStatus gets the status of an order on the exchange
func (client *Client) GetOrderStatus(id int64) (exchange.Order, error) {
	// Create request struct
	request := struct {
		URL     string `json:"request"`
		Nonce   string `json:"nonce"`
		OrderID int64  `json:"order_id"`
	}{
		"/v1/order/status",
		strconv.FormatInt(time.Now().UnixNano(), 10),
		id,
	}

	// Create order to be returned
	var order exchange.Order

	// Send POST request
	data, err := client.post(client.baseURL+request.URL, request)
	if err != nil {
		return order, fmt.Errorf("%s GetOrderStatus error: %s", client, err.Error())
	}

	// Unmarshal response
	var response struct {
		Message        string  `json:"message"`
		IsLive         bool    `json:"is_live,bool"`
		ExecutedAmount float64 `json:"executed_amount,string"`
	}
	err = json.Unmarshal(data, &response)
	if err != nil {
		return order, fmt.Errorf("%s GetOrderStatus error: %s", client, err.Error())
	}
	if response.Message != "" {
		return order, fmt.Errorf("%s GetOrderStatus error: %s", client, response.Message)
	}

	if response.IsLive {
		order.Status = "live"
	} else {
		order.Status = "dead"
	}
	order.FilledAmount = math.Abs(response.ExecutedAmount)
	return order, nil
}
Exemplo n.º 2
0
// GetOrderStatus gets the status of an order on the exchange
func (client *Client) GetOrderStatus(id int64) (exchange.Order, error) {
	// Construct parameters
	params := make(map[string]string)
	params["api_key"] = client.key
	params["symbol"] = fmt.Sprintf("%s_%s", client.symbol, client.currency)
	params["order_id"] = fmt.Sprintf("%d", id)
	params["sign"] = client.constructSign(params)

	// Construct request
	channel := fmt.Sprintf("ok_spot%s_order_info", client.currency)
	req := request{Event: "addChannel", Channel: channel, Parameters: params}

	// Write to WebSocket
	client.writeOrderMsg <- req

	// Create order to be returned
	var order exchange.Order

	// Read response
	var resp response
	select {
	case resp = <-client.readOrderMsg:
	case <-time.After(3 * time.Second):
		return order, fmt.Errorf("%s GetOrderStatus read timeout", client)
	}

	if len(resp) == 0 {
		return order, fmt.Errorf("%s GetOrderStatus bad message", client)
	}

	if resp[0].ErrorCode != 0 {
		return order, fmt.Errorf("%s GetOrderStatus error code: %d", client, resp[0].ErrorCode)
	}

	// Unmarshal
	var orderData struct {
		Orders []struct {
			Status     int     `json:"status"`
			DealAmount float64 `json:"deal_amount"`
		} `json:"orders"`
	}
	if err := json.Unmarshal(resp[0].Data, &orderData); err != nil {
		return order, fmt.Errorf("%s GetOrderStatus error: %s", client, err)
	}

	if len(orderData.Orders) == 0 {
		return order, fmt.Errorf("%s GetOrderStatus no orders, received: %+v", client, resp)
	}

	// Determine order status
	if orderData.Orders[0].Status == -1 || orderData.Orders[0].Status == 2 {
		order.Status = "dead"
	} else if orderData.Orders[0].Status == 4 || orderData.Orders[0].Status == 5 {
		order.Status = ""
	} else {
		order.Status = "live"
	}
	order.FilledAmount = math.Abs(orderData.Orders[0].DealAmount)

	return order, nil

}