binance / binance-connector-dotnet

Lightweight connector for integration with Binance API
MIT License
207 stars 69 forks source link

Hi, with your hint in the last question, everything went smoothly. I wanted to ask if your code has a method that can get the data you need from * OrderResult *. #20

Closed Polotenec closed 2 years ago

Polotenec commented 2 years ago

Description

using System;
using System.Threading;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Binance.Common;
using Binance.Spot;
using Binance.Spot.Models;
using Microsoft.Extensions.Logging;

public class NewOrder_Example
{
    public static async Task Main(string[] args)
    {
        HttpClient httpClient = new HttpClient();

        string apiKey = "api-key";
        string apiSecret = "api-secret";
        var spotAccountTrade = new SpotAccountTrade(httpClient, apiKey: apiKey, apiSecret: apiSecret);

        string symbol = USDTcrypt;
        Side side = BUY;
        OrderType type = "MARKET";
        TimeInForce? timeInForce = GTC;
        double? quantity = firstValue; 
        double? quoteOrderQty = null;
        double? price = null;
        string newClientOrderId;
        decimal? stopPrice;
        decimal? trailingDelta;
        decimal? icebergQty;
        NewOrderResponseType? newOrderRespType;
        long? recvWindow;
            var OrderResult = await spotAccountTrade.NewOrder(symbol, side, type, timeInForce, quantity); 

        // What code do I use to get the order ID from OrderResult

        var result_Id = OrderResult.Split(':', ';'); // ... orderId: 246456435734; ...
        long Order_id = long.Parse(result_Id[n]);
        Console.WriteLine(Order_id); // 246456435734

        // What code do I want to use

        dynamic order_result = OrderResult.Result;
        result_Id = order_result.orderId; // <---- Get only order id, no extra characters
        long Order_id = long.Parse(result_Id);
        Console.WriteLine(Order_id); // 246456435734

    }
}   
tantialex commented 2 years ago

You must first deserialise the json response using System.Text.Json or Newtonsoft.Json.

Here is an example using System.Text.Json

namespace Binance.Spot.SpotAccountTradeExamples
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Binance.Common;
    using Binance.Spot;
    using Binance.Spot.Models;
    using Microsoft.Extensions.Logging;
    using System.Text.Json.Nodes; // Import package

    public class QueryOrder_Example
    {
        public static async Task Main(string[] args)
        {
            using var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
            });
            ILogger logger = loggerFactory.CreateLogger<QueryOrder_Example>();

            HttpMessageHandler loggingHandler = new BinanceLoggingHandler(logger: logger);
            HttpClient httpClient = new HttpClient(handler: loggingHandler);

            string apiKey = "api-key";
            string apiSecret = "api-secret";

            string symbol = "BTCUSDT";
            string orderId = 123;

            var spotAccountTrade = new SpotAccountTrade(httpClient, apiKey: apiKey, apiSecret: apiSecret);

            var result = await spotAccountTrade.QueryOrder(symbol, orderId);

            JsonNode orderResult = JsonNode.Parse(result); // Parse json string

            Console.WriteLine(orderResult["orderId"]); // Output deserialized object's property
        }
    }
}
Polotenec commented 2 years ago

Сначала вы должны десериализовать ответ json, используя System.Text.Jsonили Newtonsoft.Json.

Вот пример использованияSystem.Text.Json

namespace Binance.Spot.SpotAccountTradeExamples
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Binance.Common;
    using Binance.Spot;
    using Binance.Spot.Models;
    using Microsoft.Extensions.Logging;
    using System.Text.Json.Nodes; // Import package

    public class QueryOrder_Example
    {
        public static async Task Main(string[] args)
        {
            using var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
            });
            ILogger logger = loggerFactory.CreateLogger<QueryOrder_Example>();

            HttpMessageHandler loggingHandler = new BinanceLoggingHandler(logger: logger);
            HttpClient httpClient = new HttpClient(handler: loggingHandler);

            string apiKey = "api-key";
            string apiSecret = "api-secret";

            string symbol = "BTCUSDT";
            string orderId = 123;

            var spotAccountTrade = new SpotAccountTrade(httpClient, apiKey: apiKey, apiSecret: apiSecret);

            var result = await spotAccountTrade.QueryOrder(symbol, orderId);

            JsonNode orderResult = JsonNode.Parse(result); // Parse json string

            Console.WriteLine(orderResult["orderId"]); // Output deserialized object's property
        }
    }
}

Thanks a lot! Everything worked out, my code was reduced at times.

flibustier7seas commented 1 year ago

Why doesn't the client deserialize the response itself?