Beispiel #1
0
        public async Task <IDataResult <List <OpenOrderList> > > GetAllOrdersAsync(BinanceTrOptions options, string symbol, int limit = 500, CancellationToken ct = default)
        {
            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "symbol", symbol },
                    { "limit", limit.ToString() }
                };

                var result = await SendRequestAsync(HttpMethod.Get, "/open/v1/orders", options, parameters, ct).ConfigureAwait(false);

                var data = CheckResult(result);
                if (!BinanceTrHelper.IsJson(data))
                {
                    return(new ErrorDataResult <List <OpenOrderList> >(data));
                }

                var model = JsonSerializer.Deserialize <AllOrdersModel>(result);
                return(new SuccessDataResult <List <OpenOrderList> >(model.Data.List, model.Msg, model.Code));
            }
            catch (Exception ex)
            {
                return(new ErrorDataResult <List <OpenOrderList> >(ex.Message));
            }
        }
Beispiel #2
0
        public async Task <IDataResult <CancelOrderData> > CancelOrderByIdAsync(BinanceTrOptions options, long orderId, CancellationToken ct = default)
        {
            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "orderId", orderId.ToString() }
                };

                var result = await SendRequestAsync(HttpMethod.Post, "/open/v1/orders/cancel", options, parameters, ct).ConfigureAwait(false);

                var data = CheckResult(result);
                if (!BinanceTrHelper.IsJson(data))
                {
                    return(new ErrorDataResult <CancelOrderData>(data));
                }

                var model = JsonSerializer.Deserialize <CancelOrderModel>(result);
                return(new SuccessDataResult <CancelOrderData>(model.Data, model.Msg, model.Code));
            }
            catch (Exception ex)
            {
                return(new ErrorDataResult <CancelOrderData>(ex.Message));
            }
        }
Beispiel #3
0
        public async Task <IDataResult <LimitOrderData> > PostNewLimitOrderAsync(BinanceTrOptions options, string symbol, OrderSideEnum side, decimal origQuoteQty, decimal price, CancellationToken ct = default)
        {
            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "symbol", symbol },
                    { "side", side.GetDisplayName() },
                    { "type", OrderTypeEnum.LIMIT.GetDisplayName() },
                    { "quantity", origQuoteQty.ToString(CultureInfo.InvariantCulture) },
                    { "price", price.ToString(CultureInfo.InvariantCulture) }
                };

                var result = await SendRequestAsync(HttpMethod.Post, "/open/v1/orders", options, parameters, ct).ConfigureAwait(false);

                var data = CheckResult(result);
                if (!BinanceTrHelper.IsJson(data))
                {
                    return(new ErrorDataResult <LimitOrderData>(data));
                }

                var model = JsonSerializer.Deserialize <LimitOrderModel>(result);
                return(new SuccessDataResult <LimitOrderData>(model.LimitOrderData, model.Msg, model.Code));
            }
            catch (Exception ex)
            {
                return(new ErrorDataResult <LimitOrderData>(ex.Message));
            }
        }
Beispiel #4
0
        private async Task <string> SendRequestAsync(HttpMethod method, string url, BinanceTrOptions options, Dictionary <string, string> parameters = null, CancellationToken ct = default)
        {
            try
            {
                using var httpClient = new HttpClient();
                var requestUri     = BinanceTrHelper.GetRequestUrl(url, true);
                var requestMessage = new HttpRequestMessage(method, requestUri);
                requestMessage.Headers.Add("X-MBX-APIKEY", options.ApiKey);

                if (method == HttpMethod.Get)
                {
                    requestMessage.RequestUri = new Uri(requestMessage.RequestUri.OriginalString + BinanceTrHelper.CreateQueryString(BinanceTrHelper.BuildRequest(options.ApiSecret, parameters)));
                }
                else
                {
                    requestMessage.Content = new FormUrlEncodedContent(BinanceTrHelper.BuildRequest(options.ApiSecret, parameters));
                }

                var response = await httpClient.SendAsync(requestMessage, ct).ConfigureAwait(false);

                return(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Beispiel #5
0
        public async Task <IDataResult <AssetInformationData> > GetAssetIformationAsync(BinanceTrOptions options, string assetName, CancellationToken ct = default)
        {
            try
            {
                var parameters = new Dictionary <string, string>
                {
                    { "asset", assetName }
                };

                var result = await SendRequestAsync(HttpMethod.Get, "/open/v1/account/spot/asset", options, parameters, ct).ConfigureAwait(false);

                var data = CheckResult(result);
                if (!BinanceTrHelper.IsJson(data))
                {
                    return(new ErrorDataResult <AssetInformationData>(data));
                }

                var model = JsonSerializer.Deserialize <AssetInformationModel>(result);
                return(new SuccessDataResult <AssetInformationData>(model.Data, model.Msg, model.Code));
            }
            catch (Exception ex)
            {
                return(new ErrorDataResult <AssetInformationData>(ex.Message));
            }
        }
Beispiel #6
0
        public async Task <IDataResult <List <AccountAsset> > > GetAccountInformationAsync(BinanceTrOptions options, CancellationToken ct = default)
        {
            try
            {
                var result = await SendRequestAsync(HttpMethod.Get, "/open/v1/account/spot", options, ct : ct).ConfigureAwait(false);

                var data = CheckResult(result);
                if (!BinanceTrHelper.IsJson(data))
                {
                    return(new ErrorDataResult <List <AccountAsset> >(data));
                }

                var model = JsonSerializer.Deserialize <AccountInformationModel>(result);
                return(new SuccessDataResult <List <AccountAsset> >(model.AccountData.AccountAssets, model.Msg, model.Code));
            }
            catch (Exception ex)
            {
                return(new ErrorDataResult <List <AccountAsset> >(ex.Message));
            }
        }