Skip to content

Commit

Permalink
Client Configuration (#243)
Browse files Browse the repository at this point in the history
Updated CryptoExchange.Net to version 8.3.0
Added support for loading client settings from IConfiguration
Added DI registration method for configuring Rest and Socket options at the same time
Added DisplayName and ImageUrl properties to BybitExchange class
Updated client constructors to accept IOptions from DI
Removed redundant BybitSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent a4f4b64 commit f9085ed
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 100 deletions.
4 changes: 2 additions & 2 deletions ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.2.0" />
<PackageReference Include="CryptoExchange.Net" Version="8.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
33 changes: 29 additions & 4 deletions ByBit.Net/BybitEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ internal BybitEnvironment(string name,
SocketBaseAddress = socketBaseAddress;
}

/// <summary>
/// ctor for DI, use <see cref="CreateCustom"/> for creating a custom environment
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public BybitEnvironment() : base(TradeEnvironmentNames.Live)
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
{ }

/// <summary>
/// Get the Bybit environment by name
/// </summary>
public static BybitEnvironment? GetEnvironmentByName(string? name)
=> name switch
{
TradeEnvironmentNames.Live => Live,
TradeEnvironmentNames.Testnet => Testnet,
"Netherlands" => Netherlands,
"Hongkong" => HongKong,
"Turkey" => Turkey,
"Demo" => DemoTrading,
"" => Live,
null => Live,
_ => default
};

/// <summary>
/// Live environment
/// </summary>
Expand All @@ -46,31 +71,31 @@ internal BybitEnvironment(string name,
/// Live environment for users from The Netherlands
/// </summary>
public static BybitEnvironment Netherlands { get; }
= new BybitEnvironment(TradeEnvironmentNames.Live,
= new BybitEnvironment("Netherlands",
BybitApiAddresses.Netherlands.RestBaseAddress,
BybitApiAddresses.Netherlands.SocketBaseAddress);

/// <summary>
/// Live environment for users from HongKong
/// </summary>
public static BybitEnvironment HongKong { get; }
= new BybitEnvironment(TradeEnvironmentNames.Live,
= new BybitEnvironment("HongKong",
BybitApiAddresses.HongKong.RestBaseAddress,
BybitApiAddresses.HongKong.SocketBaseAddress);

/// <summary>
/// Live environment for users from Turkey
/// </summary>
public static BybitEnvironment Turkey { get; }
= new BybitEnvironment(TradeEnvironmentNames.Live,
= new BybitEnvironment("Turkey",
BybitApiAddresses.Turkey.RestBaseAddress,
BybitApiAddresses.Turkey.SocketBaseAddress);

/// <summary>
/// Demo trading environment, needs seperate API key. See https://bybit-exchange.github.io/docs/v5/demo
/// </summary>
public static BybitEnvironment DemoTrading { get; }
= new BybitEnvironment(TradeEnvironmentNames.Live,
= new BybitEnvironment("Demo",
BybitApiAddresses.DemoTrading.RestBaseAddress,
BybitApiAddresses.DemoTrading.SocketBaseAddress);

Expand Down
10 changes: 10 additions & 0 deletions ByBit.Net/BybitExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public static class BybitExchange
/// </summary>
public static string ExchangeName => "Bybit";

/// <summary>
/// Exchange name
/// </summary>
public static string DisplayName => "Bybit";

/// <summary>
/// Url to exchange image
/// </summary>
public static string ImageUrl { get; } = "https://raw.githubusercontent.com/JKorf/Bybit.Net/master/ByBit.Net/Icon/icon.png";

/// <summary>
/// Url to the main website
/// </summary>
Expand Down
25 changes: 11 additions & 14 deletions ByBit.Net/Clients/BybitRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System;
using Microsoft.Extensions.Logging;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;

namespace Bybit.Net.Clients
{
Expand All @@ -31,28 +32,26 @@ public class BybitRestClient : BaseRestClient, IBybitRestClient
/// Create a new instance of the BybitRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public BybitRestClient(Action<BybitRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public BybitRestClient(Action<BybitRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

/// <summary>
/// Create a new instance of the BybitRestClient
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
/// <param name="options">Option configuration delegate</param>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="httpClient">Http client for this client</param>
public BybitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Action<BybitRestOptions>? optionsDelegate = null)
public BybitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IOptions<BybitRestOptions> options)
: base(loggerFactory, "Bybit")
{
var options = BybitRestOptions.Default.Copy();
if (optionsDelegate != null)
optionsDelegate(options);
Initialize(options);
Initialize(options.Value);

SpotApiV3 = AddApiClient(new BybitRestClientSpotApiV3(_logger, httpClient, options));
CopyTradingApi = AddApiClient(new BybitRestClientCopyTradingApi(_logger, httpClient, options));
DerivativesApi = AddApiClient(new BybitRestClientDerivativesApi(_logger, httpClient, options));
V5Api = AddApiClient(new V5.BybitRestClientApi(_logger, httpClient, options));
SpotApiV3 = AddApiClient(new BybitRestClientSpotApiV3(_logger, httpClient, options.Value));
CopyTradingApi = AddApiClient(new BybitRestClientCopyTradingApi(_logger, httpClient, options.Value));
DerivativesApi = AddApiClient(new BybitRestClientDerivativesApi(_logger, httpClient, options.Value));
V5Api = AddApiClient(new V5.BybitRestClientApi(_logger, httpClient, options.Value));
}

#endregion
Expand All @@ -63,9 +62,7 @@ public BybitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Ac
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<BybitRestOptions> optionsDelegate)
{
var options = BybitRestOptions.Default.Copy();
optionsDelegate(options);
BybitRestOptions.Default = options;
BybitRestOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
42 changes: 16 additions & 26 deletions ByBit.Net/Clients/BybitSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Bybit.Net.Clients.DerivativesApi.UnifiedMarginApi;
using Bybit.Net.Clients.SpotApi.v3;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;

namespace Bybit.Net.Clients
{
Expand All @@ -39,44 +40,35 @@ public class BybitSocketClient : BaseSocketClient, IBybitSocketClient
/// <inheritdoc />
public IBybitSocketClientPrivateApi V5PrivateApi { get; }

/// <summary>
/// Create a new instance of the BybitSocketClient
/// </summary>
/// <param name="loggerFactory">The logger factory</param>
public BybitSocketClient(ILoggerFactory? loggerFactory = null) : this((x) => { }, loggerFactory)
{
}

/// <summary>
/// Create a new instance of the BybitSocketClient
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public BybitSocketClient(Action<BybitSocketOptions> optionsDelegate) : this(optionsDelegate, null)
public BybitSocketClient(Action<BybitSocketOptions>? optionsDelegate = null)
: this(Options.Create(ApplyOptionsDelegate(optionsDelegate)), null)
{
}

/// <summary>
/// Create a new instance of the BybitSocketClient
/// </summary>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="optionsDelegate">Option configuration delegate</param>
public BybitSocketClient(Action<BybitSocketOptions> optionsDelegate, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "Bybit")
/// <param name="options">Option configuration</param>
public BybitSocketClient(IOptions<BybitSocketOptions> options, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "Bybit")
{
var options = BybitSocketOptions.Default.Copy();
optionsDelegate(options);
Initialize(options);
Initialize(options.Value);

SpotV3Api = AddApiClient(new BybitSocketClientSpotApiV3(_logger, options));
SpotV3Api = AddApiClient(new BybitSocketClientSpotApiV3(_logger, options.Value));

DerivativesApi = AddApiClient(new BybitSocketClientDerivativesPublicApi(_logger, options));
UnifiedMarginApi = AddApiClient(new BybitSocketClientUnifiedMarginApi(_logger, options));
ContractApi = AddApiClient(new BybitSocketClientContractApi(_logger, options));
DerivativesApi = AddApiClient(new BybitSocketClientDerivativesPublicApi(_logger, options.Value));
UnifiedMarginApi = AddApiClient(new BybitSocketClientUnifiedMarginApi(_logger, options.Value));
ContractApi = AddApiClient(new BybitSocketClientContractApi(_logger, options.Value));

V5SpotApi = AddApiClient(new BybitSocketClientSpotApi(_logger, options));
V5InverseApi = AddApiClient(new BybitSocketClientInverseApi(_logger, options));
V5LinearApi = AddApiClient(new BybitSocketClientLinearApi(_logger, options));
V5OptionsApi = AddApiClient(new BybitSocketClientOptionApi(_logger, options));
V5PrivateApi = AddApiClient(new BybitSocketClientPrivateApi(_logger, options));
V5SpotApi = AddApiClient(new BybitSocketClientSpotApi(_logger, options.Value));
V5InverseApi = AddApiClient(new BybitSocketClientInverseApi(_logger, options.Value));
V5LinearApi = AddApiClient(new BybitSocketClientLinearApi(_logger, options.Value));
V5OptionsApi = AddApiClient(new BybitSocketClientOptionApi(_logger, options.Value));
V5PrivateApi = AddApiClient(new BybitSocketClientPrivateApi(_logger, options.Value));
}

/// <summary>
Expand All @@ -85,9 +77,7 @@ public BybitSocketClient(Action<BybitSocketOptions> optionsDelegate, ILoggerFact
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<BybitSocketOptions> optionsDelegate)
{
var options = BybitSocketOptions.Default.Copy();
optionsDelegate(options);
BybitSocketOptions.Default = options;
BybitSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
Loading

0 comments on commit f9085ed

Please sign in to comment.