예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="user"></param>
        /// <param name="exchange"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="limit"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetOrdersHistoryAsync(this ICryptocoreHttpClient client,
                                                                ICryptocoreApiUser user, string exchange, DateTime start, DateTime end = default, int limit = 100,
                                                                CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(user, nameof(user));
            Throw.IfNullOrWhiteSpace(exchange, nameof(exchange));

            var request =
                new CryptocoreHttpRequest($"/v1/orders/{exchange}/history")
            {
                ApiKey = user.ApiKey
            };

            request.AddParameter("time_start", start.ToTimestamp());

            if (end != default)
            {
                request.AddParameter("time_end", end.ToTimestamp());
            }

            if (limit > 0)
            {
                request.AddParameter("limit", limit);
            }

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="user"></param>
        /// <param name="exchange"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetLatestBalancesAsync(this ICryptocoreHttpClient client,
                                                                 ICryptocoreApiUser user, string exchange, CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(user, nameof(user));

            var request = new CryptocoreHttpRequest($"/v1/account/balances/{exchange}/latest")
            {
                ApiKey = user.ApiKey
            };

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #3
0
        private async Task <string> RequestAsync(HttpMethod method, CryptocoreHttpRequest request, CancellationToken token = default)
        {
            Throw.IfNull(request, nameof(request));

            token.ThrowIfCancellationRequested();

            var requestMessage = request.CreateMessage(method);

            Logger?.LogDebug($"{nameof(CryptocoreHttpClient)}.{nameof(RequestAsync)}: [{method.Method}] \"{requestMessage.RequestUri}\"");

            using (var response = await _httpClient.SendAsync(requestMessage, token).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync()
                               .ConfigureAwait(false);

                    return(json);
                }

                if (response.StatusCode == HttpStatusCode.GatewayTimeout)
                {
                    throw new CryptocoreUnknownStatusException();
                }

                var error = await response.Content.ReadAsStringAsync()
                            .ConfigureAwait(false);

                var    errorCode    = 0;
                string errorMessage = null;

                // ReSharper disable once InvertIf
                if (!string.IsNullOrWhiteSpace(error) && error.IsJsonObject())
                {
                    try // to parse server error response.
                    {
                        var jObject = JObject.Parse(error);

                        errorCode    = jObject["code"]?.Value <int>() ?? 0;
                        errorMessage = jObject["message"]?.Value <string>();
                    }
                    catch (Exception e)
                    {
                        Logger?.LogError(e, $"{nameof(CryptocoreHttpClient)}.{nameof(RequestAsync)} failed to parse server error response: \"{error}\"");
                    }
                }

                throw new CryptocoreHttpException(response.StatusCode, response.ReasonPhrase, errorCode, errorMessage);
            }
        }
예제 #4
0
        /// <summary>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="exchange"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetSymbolsAsync(this ICryptocoreHttpClient client, string exchange = null,
                                                          CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));

            var request = new CryptocoreHttpRequest("/v1/data/symbols");

            if (!string.IsNullOrEmpty(exchange))
            {
                request.AddParameter("exchange_id", exchange);
            }

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="user"></param>
        /// <param name="exchange"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetActiveOrdersAsync(this ICryptocoreHttpClient client,
                                                               ICryptocoreApiUser user, string exchange, CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(user, nameof(user));
            Throw.IfNullOrWhiteSpace(exchange, nameof(exchange));

            var request = new CryptocoreHttpRequest($"/v1/orders/{exchange}/active")
            {
                ApiKey = user.ApiKey
            };

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="user"></param>
        /// <param name="symbol"></param>
        /// <param name="orderId"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> CancelAsync(this ICryptocoreHttpClient client, ICryptocoreApiUser user, Symbol symbol, string orderId,
                                                      CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(user, nameof(user));
            Throw.IfNull(symbol, nameof(symbol));
            Throw.IfNullOrWhiteSpace(orderId, nameof(orderId));

            var request = new CryptocoreHttpRequest($"/v1/orders/{symbol.Exchange}/{symbol}/active/{orderId}")
            {
                ApiKey = user.ApiKey
            };

            return(await client.DeleteAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="symbol"></param>
        /// <param name="limit"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetLatestTradesAsync(this ICryptocoreHttpClient client, Symbol symbol,
                                                               int limit = 100, CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(symbol, nameof(symbol));

            var request = new CryptocoreHttpRequest($"/v1/data/trades/{symbol}/latest");

            if (limit > 0)
            {
                request.AddParameter("limit", limit);
            }

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="symbol"></param>
        /// <param name="interval"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> GetHistoricalCandlesAsync(this ICryptocoreHttpClient client, Symbol symbol,
                                                                    CandleInterval interval, DateTime start, DateTime end = default,
                                                                    CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(symbol, nameof(symbol));

            var request = new CryptocoreHttpRequest($"/v1/data/ohlcv/{symbol}/history");

            request.AddParameter("period_id", interval.ConvertToString());
            request.AddParameter("time_start", start.ToTimestamp());

            if (end != default)
            {
                request.AddParameter("time_end", end.ToTimestamp());
            }

            return(await client.GetAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="user"></param>
        /// <param name="symbol"></param>
        /// <param name="type"></param>
        /// <param name="side"></param>
        /// <param name="price"></param>
        /// <param name="quantity"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <string> PlaceAsync(this ICryptocoreHttpClient client, ICryptocoreApiUser user,
                                                     Symbol symbol, OrderType type, OrderSide side, decimal price, decimal quantity,
                                                     CancellationToken token = default)
        {
            Throw.IfNull(client, nameof(client));
            Throw.IfNull(user, nameof(user));
            Throw.IfNull(symbol, nameof(symbol));

            var request = new CryptocoreHttpRequest($"/v1/orders/{symbol.Exchange}/{symbol}")
            {
                ApiKey = user.ApiKey
            };

            request.AddParameter("type", type.ConvertToString());
            request.AddParameter("side", side.ConvertToString());
            request.AddParameter("price", price);
            request.AddParameter("quantity", quantity);

            return(await client.PostAsync(request, token)
                   .ConfigureAwait(false));
        }
예제 #10
0
 public Task <string> PostAsync(CryptocoreHttpRequest request, CancellationToken token = default)
 {
     return(RequestAsync(HttpMethod.Post, request, token));
 }