BITMEX API - Cancel Order by given Symbol - How to do in Dart Language?

126 views Asked by At

I try to send a cancel order request with formData that only given orders symbol. But unfortunatly all other orders removes from exchange too.. It seems the formData that i added to request does not work.

In Dart Language, Does any one has experience to what is the proper way to send cancel request to server with formData ?

Thank you very much.

1

There are 1 answers

0
ASIAMINOR On BEST ANSWER

I spent almost 2 days to figure out this:) One day, hope to useful to someone need like me..

Here is the my solution to sent cancel order to bitmex... In dart language.

Also you can create a valid signature and authentication with this code..

Cheers

    var verb = "DELETE";
    var path = "/api/v1/order";
    var query = "orderID=$ordID";
    if (query != "") path = path + "?" + query;
    return await bitmexHttpService.authRequest(verb: verb, path: path);

Future<String> authRequest({String verb, String path, String data = ""}) async {
    var _nonce = ((DateTime.now()).microsecondsSinceEpoch).toInt();
    var _signature = signature(apiSecret, verb, path, _nonce);
    var headerWithAuthCredentials = createHeader(verb, _nonce, _signature);

    http.Response response;
    try {
      if (verb == "GET")
        response = await http.get(baseUrl + path, headers: headerWithAuthCredentials);
      else if (verb == "POST")
        response = await http.post(baseUrl + path, body: data, headers: headerWithAuthCredentials);
      else if (verb == "DELETE") {
        response = await http.delete(baseUrl + path, headers: headerWithAuthCredentials);
      }

      if (response.statusCode == 200)
        return response.body;
      else {
        var json = jsonDecode(response.body);
        throw Failure(message: json["error"]["message"]);
      }
    } catch (e) {
      print(e.toString());
    }
  }

  Map<String, String> createHeader(String verb, int nonce, _signature) {
    return {
      "api-key": "$apiKey",
      "api-expires": nonce.toString(),
      "api-signature": "${_signature.toString()}",
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      'accept': 'application/json',
      'x-Requested-With': 'XMLHttpRequest',
    };
  }

  signature(String apiSecret, String verb, String path, int nonce) {
    var message = verb + path + nonce.toString();
    var messageEncoded = utf8.encode(message.trim());
    var apiSecretEncoded = utf8.encode(apiSecret);
    var apiSecretAshMacSha256 = new Hmac(sha256, apiSecretEncoded);
    var sign = apiSecretAshMacSha256.convert(messageEncoded);
    return sign;
  }