Skip to content

Commit

Permalink
Update CryptoExchange.Net version to 8.5.0, added SetOptions on clien…
Browse files Browse the repository at this point in the history
…ts, added setting of DefaultProxyCredentials to CredentialCache.DefaultCredentials on the DI http client, improved websocket disconnect detection, updated dotnet versions to 9.0
  • Loading branch information
JKorf committed Dec 23, 2024
1 parent 8090ace commit 7763d1a
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Set GitHub package source
run: dotnet nuget add source --username JKorf --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/JKorf/index.json"
- name: Restore dependencies
Expand Down
2 changes: 1 addition & 1 deletion ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.4.3" />
<PackageReference Include="CryptoExchange.Net" Version="8.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
10 changes: 10 additions & 0 deletions ByBit.Net/Clients/BybitRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;
using CryptoExchange.Net.Objects.Options;

namespace Bybit.Net.Clients
{
Expand Down Expand Up @@ -56,6 +57,15 @@ public BybitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IO

#endregion

/// <inheritdoc />
public void SetOptions(UpdateOptions options)
{
SpotApiV3.SetOptions(options);
CopyTradingApi.SetOptions(options);
DerivativesApi.SetOptions(options);
V5Api.SetOptions(options);
}

/// <summary>
/// Set the default options to be used when creating new clients
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions ByBit.Net/Clients/BybitSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Bybit.Net.Clients.SpotApi.v3;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;
using CryptoExchange.Net.Objects.Options;

namespace Bybit.Net.Clients
{
Expand Down Expand Up @@ -71,6 +72,20 @@ public BybitSocketClient(IOptions<BybitSocketOptions> options, ILoggerFactory? l
V5PrivateApi = AddApiClient(new BybitSocketClientPrivateApi(_logger, options.Value));
}

/// <inheritdoc />
public void SetOptions(UpdateOptions options)
{
SpotV3Api.SetOptions(options);
DerivativesApi.SetOptions(options);
UnifiedMarginApi.SetOptions(options);
ContractApi.SetOptions(options);
V5SpotApi.SetOptions(options);
V5InverseApi.SetOptions(options);
V5LinearApi.SetOptions(options);
V5OptionsApi.SetOptions(options);
V5PrivateApi.SetOptions(options);
}

/// <summary>
/// Set the default options to be used when creating new clients
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ internal BybitSocketClientDerivativesPublicApi(ILogger log, BybitSocketOptions o
UnhandledMessageExpected = true;
KeepAliveInterval = TimeSpan.Zero;

RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ internal BybitSocketClientContractApi(ILogger log, BybitSocketOptions options)
UnhandledMessageExpected = true;
KeepAliveInterval = TimeSpan.Zero;

RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ internal BybitSocketClientUnifiedMarginApi(ILogger log, BybitSocketOptions optio
{
KeepAliveInterval = TimeSpan.Zero;

RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitPingQuery(), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitPingQuery(),
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
14 changes: 13 additions & 1 deletion ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ internal BybitSocketClientSpotApiV3(ILogger logger, BybitSocketOptions options)
{
KeepAliveInterval = TimeSpan.Zero;

RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
14 changes: 13 additions & 1 deletion ByBit.Net/Clients/V5/BybitSocketClientInverseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ internal partial class BybitSocketClientInverseApi : BybitSocketClientBaseApi, I
internal BybitSocketClientInverseApi(ILogger log, BybitSocketOptions options)
: base(log, options, "/v5/public/inverse")
{
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}
public IBybitSocketClientInverseApiShared SharedClient => this;

Expand Down
14 changes: 13 additions & 1 deletion ByBit.Net/Clients/V5/BybitSocketClientLinearApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ internal partial class BybitSocketClientLinearApi : BybitSocketClientBaseApi, IB
internal BybitSocketClientLinearApi(ILogger log, BybitSocketOptions options)
: base(log, options, "/v5/public/linear")
{
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

public IBybitSocketClientLinearApiShared SharedClient => this;
Expand Down
14 changes: 13 additions & 1 deletion ByBit.Net/Clients/V5/BybitSocketClientOptionApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@ internal class BybitSocketClientOptionApi : BybitSocketClientBaseApi, IBybitSock
internal BybitSocketClientOptionApi(ILogger log, BybitSocketOptions options)
: base(log, options, "/v5/public/option")
{
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitPingQuery(), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitPingQuery(),
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
16 changes: 14 additions & 2 deletions ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ internal BybitSocketClientPrivateApi(ILogger logger, BybitSocketOptions options)

_referer = !string.IsNullOrEmpty(options.Referer) ? options.Referer! : "Zx000356";

RegisterPeriodicQuery("Heartbeat", options.V5Options.PingInterval, GetPingQuery, x => { });
RegisterPeriodicQuery(
"Heartbeat",
options.V5Options.PingInterval,
GetPingQuery,
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});

SetDedicatedConnection(BaseAddress.AppendPath("/v5/trade"), true);
}
Expand All @@ -49,7 +61,7 @@ private Query GetPingQuery(SocketConnection connection)
{
if (connection.ConnectionUri.AbsolutePath.EndsWith("private"))
{
return new BybitQuery("ping", null);
return new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) };
}
else
{
Expand Down
14 changes: 13 additions & 1 deletion ByBit.Net/Clients/V5/BybitSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ internal partial class BybitSocketClientSpotApi : BybitSocketClientBaseApi, IByb
internal BybitSocketClientSpotApi(ILogger logger, BybitSocketOptions options)
: base(logger, options, "/v5/public/spot")
{
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
RegisterPeriodicQuery(
"Heartbeat",
TimeSpan.FromSeconds(20),
x => new BybitQuery("ping", null) { RequestTimeout = TimeSpan.FromSeconds(5) },
(connection, result) =>
{
if (result.Error?.Message.Equals("Query timeout") == true)
{
// Ping timeout, reconnect
_logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId);
_ = connection.TriggerReconnectAsync();
}
});
}

/// <inheritdoc />
Expand Down
1 change: 1 addition & 0 deletions ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private static IServiceCollection AddBybitCore(
try
{
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
}
catch (PlatformNotSupportedException)
{ }
Expand Down
7 changes: 7 additions & 0 deletions ByBit.Net/Interfaces/Clients/IBybitRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bybit.Net.Interfaces.Clients.SpotApi.v3;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Options;

namespace Bybit.Net.Interfaces.Clients
{
Expand Down Expand Up @@ -30,6 +31,12 @@ public interface IBybitRestClient: IRestClient
/// </summary>
V5.IBybitRestClientApi V5Api { get; }

/// <summary>
/// Update specific options
/// </summary>
/// <param name="options">Options to update. Only specific options are changable after the client has been created</param>
void SetOptions(UpdateOptions options);

/// <summary>
/// Set the API credentials for this client. All Api clients in this client will use the new credentials, regardless of earlier set options.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions ByBit.Net/Interfaces/Clients/IBybitSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Bybit.Net.Interfaces.Clients.V5;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects.Options;

namespace Bybit.Net.Interfaces.Clients
{
Expand Down Expand Up @@ -50,6 +51,12 @@ public interface IBybitSocketClient : ISocketClient
/// </summary>
public IBybitSocketClientPrivateApi V5PrivateApi { get; }

/// <summary>
/// Update specific options
/// </summary>
/// <param name="options">Options to update. Only specific options are changable after the client has been created</param>
void SetOptions(UpdateOptions options);

/// <summary>
/// Set the API credentials for this client. All Api clients in this client will use the new credentials, regardless of earlier set options.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions ByBit.Net/Objects/Sockets/Queries/BybitPingQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CryptoExchange.Net;
using CryptoExchange.Net.Sockets;
using System;
using System.Collections.Generic;

namespace Bybit.Net.Objects.Sockets.Queries
Expand All @@ -10,6 +11,7 @@ internal class BybitPingQuery : Query<BybitPong>

public BybitPingQuery() : base(new BybitRequestMessage { RequestId = ExchangeHelpers.NextId().ToString(), Operation = "ping", Args = null }, false, 1)
{
RequestTimeout = TimeSpan.FromSeconds(5);
ListenerIdentifiers = new HashSet<string>() { "pong" };
}
}
Expand Down
2 changes: 1 addition & 1 deletion Bybit.UnitTests/Bybit.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit 7763d1a

Please sign in to comment.