gpt4 book ai didi

c# - 无法使包装器 binance.net 正常工作

转载 作者:行者123 更新时间:2023-12-02 16:16:56 25 4
gpt4 key购买 nike

我已经安装了来自 https://github.com/JKorf/Binance.Net 的包装器

使用他们的示例代码,我的应用程序中有以下内容:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Binance.Net;
using Binance.Net.Enums;
using Binance.Net.Objects;
using Binance.Net.Objects.Spot;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;

namespace binance_stream_user_data_updates
{
class Program
{
static void Main(string[] args)
{
var client = new BinanceClient(new BinanceClientOptions{
ApiCredentials = new ApiCredentials("key","secret"),
BaseAddress = "https://testnet.binance.vision",
LogVerbosity = LogVerbosity.Debug,
LogWriters = new List<TextWriter> { Console.Out }
});

var startResult = client.Spot.UserStream.StartUserStream();

if(!startResult.Success)
throw new Exception($"Failed to start user stream: {startResult.Error}");

var socketClient = new BinanceSocketClient();

socketClient.Spot.SubscribeToUserDataUpdates(startResult.Data,
null,
data => {
Console.WriteLine(data.Orders);
},
null,
null);

Console.ReadLine();

socketClient.UnsubscribeAll();


}
}
}

然后我运行上面的应用程序,它似乎已连接并开始等待,如终端中所示:

dotnet run
2021/02/26 20:36:51:863 | Binance | Debug | Client configuration: LogVerbosity: Debug, Writers: 1, Credentials: Set, BaseAddress: https://testnet.binance.vision/, Proxy: -, RateLimiters: 0, RateLimitBehaviour: Wait, RequestTimeout: 00:00:30
2021/02/26 20:36:51:903 | Binance | Debug | [1] Creating request for https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:51:911 | Binance | Debug | [1] Sending GET request to https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:53:242 | Binance | Debug | [1] Response received in 1320ms: {"serverTime":1614371813101}
2021/02/26 20:36:53:345 | Binance | Debug | [2] Creating request for https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:53:346 | Binance | Debug | [2] Sending GET request to https://testnet.binance.vision/api/v3/time
2021/02/26 20:36:54:028 | Binance | Debug | [2] Response received in 681ms: {"serverTime":1614371813881}
2021/02/26 20:36:54:029 | Binance | Info | Time offset set to 535.073ms
2021/02/26 20:36:54:031 | Binance | Debug | [3] Creating request for https://testnet.binance.vision/api/v1/userDataStream
2021/02/26 20:36:54:037 | Binance | Debug | [3] Sending POST request to https://testnet.binance.vision/api/v1/userDataStream with request body
2021/02/26 20:36:54:732 | Binance | Debug | [3] Response received in 694ms: {"listenKey":"key"}

然后我发布订单

发布https://testnet.binance.vision/api/v3/order?symbol=BNBUSDT&side=SELL&type=MARKET&quantity=0.1&newClientOrderId=my_order_id_201&newOrderRespType=FULL&timestamp=1614370483356&signature=58cfd86cffc626703eac32f14bf0fa2e9af4850fb33974a03d1eee3f666df15f

当我这样做时,从上面的代码运行的 websocket 什么都不输出。知道我做错了什么吗?

最佳答案

有些事情必须完成才能获得正确的结果:

  1. 您必须为 BinanceSocketClient 指定选项
  2. 您必须等待一些事件才能取消订阅
  3. 在等待期间,您必须使用 Postman 应用程序(或其他类似应用程序)和您的凭据添加新订单(或新的 OCO 订单)。 (这不在下面的代码中)

请看下面的代码:

async static Task Main(string[] args)
{
var client = new BinanceClient(new BinanceClientOptions
{
ApiCredentials = new ApiCredentials("key", "secret"),
BaseAddress = "https://testnet.binance.vision",
LogVerbosity = LogVerbosity.Debug,
LogWriters = new List<TextWriter> { Console.Out }
});

var startResult = client.Spot.UserStream.StartUserStream();

if (!startResult.Success)
throw new Exception($"Failed to start user stream: {startResult.Error}");

var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions
{
ApiCredentials = new ApiCredentials("key", "secret"),
BaseAddress = "wss://testnet.binance.vision",
LogVerbosity = LogVerbosity.Debug,
LogWriters = new List<TextWriter> { Console.Out }
});

var stop = false;
var subscribeResponse = socketClient.Spot.SubscribeToUserDataUpdates(
startResult.Data,
data =>
{
stop = true;
Console.WriteLine($"Order updated: {data}");
},
data =>
{
stop = true;
Console.WriteLine($"OCO Orders updated: {data.Orders}");
},
null,
data =>
{
stop = true;
Console.WriteLine($"Balance updated: ${data}");
});

if (subscribeResponse.Success)
{
while (!stop)
{
await Task.Delay(100);
}
}

await socketClient.UnsubscribeAll();
}

关于c# - 无法使包装器 binance.net 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66392643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com