Exemple #1
0
        public static ListItem GetListItem(ESecurityType type, bool selected)
        {
            var item = new ListItem(GetText(type), GetValue(type));

            if (selected)
            {
                item.Selected = true;
            }
            return(item);
        }
Exemple #2
0
 public static bool Equals(ESecurityType type, string typeStr)
 {
     if (string.IsNullOrEmpty(typeStr))
     {
         return(false);
     }
     if (string.Equals(GetValue(type).ToLower(), typeStr.ToLower()))
     {
         return(true);
     }
     return(false);
 }
Exemple #3
0
 public static string GetText(ESecurityType type)
 {
     if (type == ESecurityType.Public)
     {
         return("所有人");
     }
     if (type == ESecurityType.Friends)
     {
         return("我的好友");
     }
     if (type == ESecurityType.SelfOnly)
     {
         return("只有我自己");
     }
     throw new Exception();
 }
Exemple #4
0
 public static string GetValue(ESecurityType type)
 {
     if (type == ESecurityType.Public)
     {
         return("Public");
     }
     if (type == ESecurityType.Friends)
     {
         return("Friends");
     }
     if (type == ESecurityType.SelfOnly)
     {
         return("SelfOnly");
     }
     throw new Exception();
 }
Exemple #5
0
 public static bool Equals(string typeStr, ESecurityType type)
 {
     return(Equals(type, typeStr));
 }
Exemple #6
0
        /// <summary>Helper for GETs</summary>
        private async Task <JToken> GetData(HttpMethod method, ESecurityType security, string command, CancellationToken?cancel, Params parameters = null, bool timestamp = false, bool log_trace = false)
        {
            // If called from the UI thread, disable the SynchronisationContext
            // to prevent deadlocks when waiting for Async results.
            using (Task_.NoSyncContext())
            {
                // Poloniex requires the 'nonce' values to be strictly increasing.
                // That means all POSTs must be serialised to avoid a race condition
                // when POSTing two messages in quick succession.
                var cancel_token = CancellationTokenSource.CreateLinkedTokenSource(Shutdown, cancel ?? CancellationToken.None).Token;
                using (RequestThrottle.Lock(cancel_token))                 // Limit requests to the required rate
                {
                    await RequestThrottle.Wait(cancel_token);

                    // Add fields to the request based on 'security'
                    parameters = parameters ?? new Params();
                    if (timestamp)
                    {
                        // Needs to be added after waiting on the throttle
                        parameters["timestamp"] = RequestTimestamp;
                    }
                    if (security == ESecurityType.TRADE || security == ESecurityType.USER_DATA)
                    {
                        var query_string = Http_.UrlEncode(parameters).TrimStart('?');
                        var hash         = Hasher.ComputeHash(Encoding.UTF8.GetBytes(query_string));
                        parameters["signature"] = Misc.ToStringHex(hash);
                    }

                    // Create the request
                    var url = $"{UrlRestAddress}{command}{Http_.UrlEncode(parameters)}";
                    var req = new HttpRequestMessage(method, url);
                    if (security == ESecurityType.TRADE || security == ESecurityType.USER_DATA || security == ESecurityType.USER_STREAM || security == ESecurityType.MARKET_DATA)
                    {
                        req.Headers.Add("X-MBX-APIKEY", Key);
                    }

                    if (log_trace)
                    {
                        Log.Write(ELogLevel.Debug, req.ToString());
                    }

                    // Submit the request
                    var sw       = new Stopwatch().StartNow();
                    var response = await Client.SendAsync(req, cancel_token);

                    var reply = await response.Content.ReadAsStringAsync();

                    Log.Write(ELogLevel.Debug, $"Req time: {sw.Elapsed.ToPrettyString(min_unit: TimeSpan_.ETimeUnits.Milliseconds)} - {url}");

                    if (log_trace)
                    {
                        Log.Write(ELogLevel.Debug, reply.ToString());
                    }

                    // Check the API usage weight
                    if (response.Headers.TryGetValues("X-MBX-USED-WEIGHT", out var weights))
                    {
                        RequestThrottle.UsedWeight = long.Parse(weights.First());
                        if (RequestThrottle.UsedWeight > 0.5 * RequestThrottle.WeightLimit)
                        {
                            Debug.Assert(false);
                        }
                    }

                    // Interpret the reply
                    if (!response.IsSuccessStatusCode)
                    {
                        // Check for an error
                        var jobj = reply != null?JObject.Parse(reply) : null;

                        if (jobj != null &&
                            jobj["code"]?.Value <int>() is int code &&
                            jobj["msg"]?.Value <string>() is string msg)
                        {
                            throw new BinanceException((EErrorCode)code, msg);
                        }
                        else
                        {
                            throw new HttpException((int)response.StatusCode, response.ReasonPhrase);
                        }
                    }

                    return(JToken.Parse(reply));
                }
            }