1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-12 08:53:01 +00:00

Compare commits

...

6 Commits

Author SHA1 Message Date
Jkorf d6b680d42e Updated to version 11.0.2 2026-03-26 13:01:38 +01:00
JKorf 24123261e5 Fixed example 2026-03-25 21:40:40 +01:00
JKorf aab30e05f0 Updated SetOptions logic to allow calling on client without credentials 2026-03-25 21:06:54 +01:00
Jkorf 54b15b75d3 Updated examples 2026-03-24 17:25:58 +01:00
Jkorf d715c7df59 Updated to version 11.0.1 2026-03-24 14:38:39 +01:00
Jkorf e1d33f252f Fixed CreateParamString method for arrays of value types 2026-03-24 14:28:46 +01:00
18 changed files with 221 additions and 85 deletions
@@ -1,4 +1,5 @@
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Options;
using CryptoExchange.Net.UnitTests.TestImplementations;
using NUnit.Framework;
@@ -54,6 +55,63 @@ namespace CryptoExchange.Net.UnitTests
Assert.That(options.ApiCredentials.Secret == "456");
}
[Test]
public void TestSetOptionsRest()
{
var client = new TestRestClient();
client.SetOptions(new UpdateOptions
{
RequestTimeout = TimeSpan.FromSeconds(2),
Proxy = new ApiProxy("http://testproxy", 1234)
});
Assert.That(client.Api1.ClientOptions.Proxy, Is.Not.Null);
Assert.That(client.Api1.ClientOptions.Proxy.Host, Is.EqualTo("http://testproxy"));
Assert.That(client.Api1.ClientOptions.Proxy.Port, Is.EqualTo(1234));
Assert.That(client.Api1.ClientOptions.RequestTimeout, Is.EqualTo(TimeSpan.FromSeconds(2)));
}
[Test]
public void TestSetOptionsRestWithCredentials()
{
var client = new TestRestClient();
client.SetOptions(new UpdateOptions<HMACCredential>
{
ApiCredentials = new HMACCredential("123", "456"),
RequestTimeout = TimeSpan.FromSeconds(2),
Proxy = new ApiProxy("http://testproxy", 1234)
});
Assert.That(client.Api1.ApiCredentials, Is.Not.Null);
Assert.That(client.Api1.ApiCredentials.Key, Is.EqualTo("123"));
Assert.That(client.Api1.ClientOptions.Proxy, Is.Not.Null);
Assert.That(client.Api1.ClientOptions.Proxy.Host, Is.EqualTo("http://testproxy"));
Assert.That(client.Api1.ClientOptions.Proxy.Port, Is.EqualTo(1234));
Assert.That(client.Api1.ClientOptions.RequestTimeout, Is.EqualTo(TimeSpan.FromSeconds(2)));
}
[Test]
public void TestWhenUpdatingSettingsExistingClientsAreNotAffected()
{
TestClientOptions.Default = new TestClientOptions
{
ApiCredentials = new HMACCredential("111", "222"),
RequestTimeout = TimeSpan.FromSeconds(1),
};
var client1 = new TestRestClient();
Assert.That(client1.ClientOptions.RequestTimeout, Is.EqualTo(TimeSpan.FromSeconds(1)));
Assert.That(client1.ClientOptions.ApiCredentials.Key, Is.EqualTo("111"));
TestClientOptions.Default.ApiCredentials = new HMACCredential("333", "444");
TestClientOptions.Default.RequestTimeout = TimeSpan.FromSeconds(2);
var client2 = new TestRestClient();
Assert.That(client2.ClientOptions.RequestTimeout, Is.EqualTo(TimeSpan.FromSeconds(2)));
Assert.That(client2.ClientOptions.ApiCredentials.Key, Is.EqualTo("333"));
}
}
public class TestClientOptions: RestExchangeOptions<TestEnvironment, HMACCredential>
@@ -23,7 +23,7 @@ using CryptoExchange.Net.Converters.MessageParsing.DynamicConverters;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestRestClient: BaseRestClient
public class TestRestClient: BaseRestClient<TestEnvironment, HMACCredential>
{
public TestRestApi1Client Api1 { get; }
public TestRestApi2Client Api2 { get; }
@@ -37,8 +37,8 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
{
Initialize(options.Value);
Api1 = new TestRestApi1Client(options.Value);
Api2 = new TestRestApi2Client(options.Value);
Api1 = AddApiClient(new TestRestApi1Client(options.Value));
Api2 = AddApiClient(new TestRestApi2Client(options.Value));
}
public void SetResponse(string responseData, out IRequest requestObj)
@@ -14,6 +14,11 @@ namespace CryptoExchange.Net.Clients
/// </summary>
public abstract class BaseRestClient : BaseClient, IRestClient
{
/// <summary>
/// Api clients in this client
/// </summary>
internal new List<RestApiClient> ApiClients => base.ApiClients.OfType<RestApiClient>().ToList();
/// <inheritdoc />
public int TotalRequestsMade => ApiClients.OfType<RestApiClient>().Sum(s => s.TotalRequestsMade);
@@ -29,6 +34,14 @@ namespace CryptoExchange.Net.Clients
LibraryHelpers.StaticLogger = loggerFactory?.CreateLogger("CryptoExchange");
}
/// <summary>
/// Update options
/// </summary>
public virtual void SetOptions(UpdateOptions options)
{
foreach (var apiClient in ApiClients)
apiClient.SetOptions(options);
}
}
/// <inheritdoc />
@@ -41,6 +54,11 @@ namespace CryptoExchange.Net.Clients
/// </summary>
internal new List<RestApiClient<TEnvironment, TApiCredentials>> ApiClients => base.ApiClients.OfType<RestApiClient<TEnvironment, TApiCredentials>>().ToList();
/// <summary>
/// Provided client options
/// </summary>
public new RestExchangeOptions<TEnvironment, TApiCredentials> ClientOptions => (RestExchangeOptions<TEnvironment, TApiCredentials>)base.ClientOptions;
/// <summary>
/// ctor
/// </summary>
@@ -21,6 +21,11 @@ namespace CryptoExchange.Net.Clients
{
#region fields
/// <summary>
/// Api clients in this client
/// </summary>
internal new List<SocketApiClient> ApiClients => base.ApiClients.OfType<SocketApiClient>().ToList();
/// <summary>
/// If client is disposing
/// </summary>
@@ -135,6 +140,15 @@ namespace CryptoExchange.Net.Clients
return result;
}
/// <summary>
/// Update options
/// </summary>
public virtual void SetOptions(UpdateOptions options)
{
foreach (var apiClient in ApiClients)
apiClient.SetOptions(options);
}
}
/// <inheritdoc />
@@ -147,6 +161,11 @@ namespace CryptoExchange.Net.Clients
/// </summary>
internal new List<SocketApiClient<TEnvironment, TApiCredentials>> ApiClients => base.ApiClients.OfType<SocketApiClient<TEnvironment, TApiCredentials>>().ToList();
/// <summary>
/// Provided client options
/// </summary>
public new SocketExchangeOptions<TEnvironment, TApiCredentials> ClientOptions => (SocketExchangeOptions<TEnvironment, TApiCredentials>)base.ClientOptions;
/// <summary>
/// ctor
/// </summary>
+11 -4
View File
@@ -819,6 +819,16 @@ namespace CryptoExchange.Net.Clients
&& definition.Method == HttpMethod.Get
&& !definition.PreventCaching;
/// <inheritdoc />
public virtual void SetOptions(UpdateOptions options)
{
_proxyConfigured = options.Proxy != null;
ClientOptions.Proxy = options.Proxy;
ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout;
RequestFactory.UpdateSettings(options.Proxy, options.RequestTimeout ?? ClientOptions.RequestTimeout, ClientOptions.HttpKeepAliveInterval);
}
}
/// <inheritdoc />
@@ -890,12 +900,9 @@ namespace CryptoExchange.Net.Clients
/// <inheritdoc />
public virtual void SetOptions(UpdateOptions<TApiCredentials> options)
{
_proxyConfigured = options.Proxy != null;
ClientOptions.Proxy = options.Proxy;
ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout;
base.SetOptions(options);
ApiCredentials = (TApiCredentials?)options.ApiCredentials?.Copy() ?? ApiCredentials;
RequestFactory.UpdateSettings(options.Proxy, options.RequestTimeout ?? ClientOptions.RequestTimeout, ClientOptions.HttpKeepAliveInterval);
}
}
+23 -19
View File
@@ -1035,6 +1035,28 @@ namespace CryptoExchange.Net.Clients
/// </summary>
/// <returns></returns>
public abstract ISocketMessageHandler CreateMessageConverter(WebSocketMessageType messageType);
/// <inheritdoc />
public virtual void SetOptions(UpdateOptions options)
{
var previousProxyIsSet = _proxyConfigured;
ClientOptions.Proxy = options.Proxy;
ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout;
_proxyConfigured = options.Proxy != null;
if ((!previousProxyIsSet && options.Proxy == null)
|| _socketConnections.IsEmpty)
{
return;
}
_logger.LogInformation("Reconnecting websockets to apply proxy");
// Update proxy, also triggers reconnect
foreach (var connection in _socketConnections)
_ = connection.Value.UpdateProxy(options.Proxy);
}
}
/// <inheritdoc />
@@ -1102,25 +1124,7 @@ namespace CryptoExchange.Net.Clients
/// <inheritdoc />
public virtual void SetOptions(UpdateOptions<TApiCredentials> options)
{
var previousProxyIsSet = _proxyConfigured;
ClientOptions.Proxy = options.Proxy;
ClientOptions.RequestTimeout = options.RequestTimeout ?? ClientOptions.RequestTimeout;
ApiCredentials = (TApiCredentials?)options.ApiCredentials?.Copy() ?? ApiCredentials;
_proxyConfigured = options.Proxy != null;
if ((!previousProxyIsSet && options.Proxy == null)
|| _socketConnections.IsEmpty)
{
return;
}
_logger.LogInformation("Reconnecting websockets to apply proxy");
// Update proxy, also triggers reconnect
foreach (var connection in _socketConnections)
_ = connection.Value.UpdateProxy(options.Proxy);
base.SetOptions(options);
}
}
+3 -3
View File
@@ -6,9 +6,9 @@
<PackageId>CryptoExchange.Net</PackageId>
<Authors>JKorf</Authors>
<Description>CryptoExchange.Net is a base library which is used to implement different cryptocurrency (exchange) API's. It provides a standardized way of implementing different API's, which results in a very similar experience for users of the API implementations.</Description>
<PackageVersion>11.0.0</PackageVersion>
<AssemblyVersion>11.0.0</AssemblyVersion>
<FileVersion>11.0.0</FileVersion>
<PackageVersion>11.0.2</PackageVersion>
<AssemblyVersion>11.0.2</AssemblyVersion>
<FileVersion>11.0.2</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>OKX;OKX.Net;Mexc;Mexc.Net;Kucoin;Kucoin.Net;Kraken;Kraken.Net;Huobi;Huobi.Net;CoinEx;CoinEx.Net;Bybit;Bybit.Net;Bitget;Bitget.Net;Bitfinex;Bitfinex.Net;Binance;Binance.Net;CryptoCurrency;CryptoCurrency Exchange;CryptoExchange.Net</PackageTags>
<RepositoryType>git</RepositoryType>
+3 -3
View File
@@ -75,7 +75,7 @@ namespace CryptoExchange.Net
if (serializationType == ArrayParametersSerialization.Array)
{
bool firstArrayValue = true;
foreach (var entry in (object[])parameter.Value)
foreach (var entry in (Array)parameter.Value)
{
if (!firstArrayValue)
uriString.Append('&');
@@ -92,7 +92,7 @@ namespace CryptoExchange.Net
else if (serializationType == ArrayParametersSerialization.MultipleValues)
{
bool firstArrayValue = true;
foreach (var entry in (object[])parameter.Value)
foreach (var entry in (Array)parameter.Value)
{
if (!firstArrayValue)
uriString.Append('&');
@@ -109,7 +109,7 @@ namespace CryptoExchange.Net
{
uriString.Append('[');
var firstArrayEntry = true;
foreach (var entry in (object[])parameter.Value)
foreach (var entry in (Array)parameter.Value)
{
if (!firstArrayEntry)
uriString.Append(',');
@@ -28,6 +28,12 @@ namespace CryptoExchange.Net.Interfaces.Clients
/// Whether client is disposed
/// </summary>
bool Disposed { get; }
/// <summary>
/// Update specific options
/// </summary>
/// <param name="options">Options to update. Only specific options are changeable after the client has been created</param>
void SetOptions(UpdateOptions options);
}
/// <inheritdoc />
@@ -60,6 +60,12 @@ namespace CryptoExchange.Net.Interfaces.Clients
/// </summary>
/// <returns></returns>
Task UnsubscribeAllAsync();
/// <summary>
/// Update specific options
/// </summary>
/// <param name="options">Options to update. Only specific options are changeable after the client has been created</param>
void SetOptions(UpdateOptions options);
}
/// <inheritdoc />
@@ -6,19 +6,27 @@ namespace CryptoExchange.Net.Objects.Options
/// <summary>
/// Options to update
/// </summary>
public class UpdateOptions<TApiCredentials> where TApiCredentials : ApiCredentials
public class UpdateOptions
{
/// <summary>
/// Proxy setting. Note that if this is not provided any previously set proxy will be reset
/// </summary>
public ApiProxy? Proxy { get; set; }
/// <summary>
/// Api credentials
/// </summary>
public TApiCredentials? ApiCredentials { get; set; }
/// <summary>
/// Request timeout
/// </summary>
public TimeSpan? RequestTimeout { get; set; }
}
/// <summary>
/// Options to update
/// </summary>
public class UpdateOptions<TApiCredentials>: UpdateOptions
where TApiCredentials : ApiCredentials
{
/// <summary>
/// Api credentials
/// </summary>
public TApiCredentials? ApiCredentials { get; set; }
}
}
+25 -25
View File
@@ -5,32 +5,32 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Binance.Net" Version="12.5.0" />
<PackageReference Include="Bitfinex.Net" Version="10.6.0" />
<PackageReference Include="BitMart.Net" Version="3.5.0" />
<PackageReference Include="BloFin.Net" Version="2.5.0" />
<PackageReference Include="Bybit.Net" Version="6.5.0" />
<PackageReference Include="CoinEx.Net" Version="10.5.0" />
<PackageReference Include="CoinW.Net" Version="2.5.0" />
<PackageReference Include="CryptoCom.Net" Version="3.5.0" />
<PackageReference Include="DeepCoin.Net" Version="3.5.0" />
<PackageReference Include="GateIo.Net" Version="3.5.0" />
<PackageReference Include="HyperLiquid.Net" Version="3.7.0" />
<PackageReference Include="JK.BingX.Net" Version="3.5.0" />
<PackageReference Include="JK.Bitget.Net" Version="3.5.0" />
<PackageReference Include="JK.Mexc.Net" Version="4.5.0" />
<PackageReference Include="JK.OKX.Net" Version="4.5.0" />
<PackageReference Include="Jkorf.Aster.Net" Version="2.5.0" />
<PackageReference Include="JKorf.BitMEX.Net" Version="3.5.0" />
<PackageReference Include="JKorf.Coinbase.Net" Version="3.5.1" />
<PackageReference Include="JKorf.HTX.Net" Version="8.5.0" />
<PackageReference Include="JKorf.Upbit.Net" Version="2.5.0" />
<PackageReference Include="KrakenExchange.Net" Version="7.5.0" />
<PackageReference Include="Kucoin.Net" Version="8.5.0" />
<PackageReference Include="Binance.Net" Version="12.11.0" />
<PackageReference Include="Bitfinex.Net" Version="10.10.1" />
<PackageReference Include="BitMart.Net" Version="3.9.1" />
<PackageReference Include="BloFin.Net" Version="2.10.1" />
<PackageReference Include="Bybit.Net" Version="6.10.0" />
<PackageReference Include="CoinEx.Net" Version="10.9.1" />
<PackageReference Include="CoinW.Net" Version="2.9.1" />
<PackageReference Include="CryptoCom.Net" Version="3.9.1" />
<PackageReference Include="DeepCoin.Net" Version="3.9.1" />
<PackageReference Include="GateIo.Net" Version="3.10.1" />
<PackageReference Include="HyperLiquid.Net" Version="4.0.1" />
<PackageReference Include="JK.BingX.Net" Version="3.9.1" />
<PackageReference Include="JK.Bitget.Net" Version="3.9.0" />
<PackageReference Include="JK.Mexc.Net" Version="4.9.0" />
<PackageReference Include="JK.OKX.Net" Version="4.10.1" />
<PackageReference Include="Jkorf.Aster.Net" Version="3.0.0" />
<PackageReference Include="JKorf.BitMEX.Net" Version="3.9.1" />
<PackageReference Include="JKorf.Coinbase.Net" Version="3.9.1" />
<PackageReference Include="JKorf.HTX.Net" Version="8.9.0" />
<PackageReference Include="JKorf.Upbit.Net" Version="2.9.0" />
<PackageReference Include="KrakenExchange.Net" Version="7.9.0" />
<PackageReference Include="Kucoin.Net" Version="8.10.1" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Toobit.Net" Version="3.5.0" />
<PackageReference Include="WhiteBit.Net" Version="3.5.0" />
<PackageReference Include="XT.Net" Version="3.5.0" />
<PackageReference Include="Toobit.Net" Version="3.9.1" />
<PackageReference Include="WhiteBit.Net" Version="3.9.1" />
<PackageReference Include="XT.Net" Version="3.9.1" />
</ItemGroup>
</Project>
+2 -1
View File
@@ -21,6 +21,7 @@
@using HTX.Net.Interfaces
@using HyperLiquid.Net.Interfaces
@using Kraken.Net.Interfaces
@using Kucoin.Net
@using Kucoin.Net.Clients
@using Kucoin.Net.Interfaces
@using Mexc.Net.Interfaces
@@ -83,7 +84,7 @@
// Since the Kucoin order book stream needs authentication we will need to provide API credentials beforehand
KucoinRestClient.SetDefaultOptions(options =>
{
options.ApiCredentials = new ApiCredentials("KEY", "SECRET", "PASSPHRASE");
options.ApiCredentials = new KucoinCredentials("KEY", "SECRET", "PASSPHRASE");
});
_books = new Dictionary<string, ISymbolOrderBook>
+2 -1
View File
@@ -1,3 +1,4 @@
using Binance.Net;
using CryptoExchange.Net.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@@ -30,7 +31,7 @@ namespace BlazorClient
// Register the clients, options can be provided in the callback parameter
services.AddBinance(restOptions =>
{
restOptions.ApiCredentials = new ApiCredentials("KEY", "SECRET");
restOptions.ApiCredentials = new BinanceCredentials("KEY", "SECRET");
});
services.AddAster();
+14 -14
View File
@@ -6,20 +6,20 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Binance.Net" Version="12.5.0" />
<PackageReference Include="Bitfinex.Net" Version="10.6.0" />
<PackageReference Include="BitMart.Net" Version="3.5.0" />
<PackageReference Include="Bybit.Net" Version="6.5.0" />
<PackageReference Include="CoinEx.Net" Version="10.5.0" />
<PackageReference Include="CryptoCom.Net" Version="3.5.0" />
<PackageReference Include="GateIo.Net" Version="3.5.0" />
<PackageReference Include="JK.Bitget.Net" Version="3.5.0" />
<PackageReference Include="JK.Mexc.Net" Version="4.5.0" />
<PackageReference Include="JK.OKX.Net" Version="4.5.0" />
<PackageReference Include="JKorf.Coinbase.Net" Version="3.5.1" />
<PackageReference Include="JKorf.HTX.Net" Version="8.5.0" />
<PackageReference Include="KrakenExchange.Net" Version="7.5.0" />
<PackageReference Include="Kucoin.Net" Version="8.5.0" />
<PackageReference Include="Binance.Net" Version="12.11.0" />
<PackageReference Include="Bitfinex.Net" Version="10.10.1" />
<PackageReference Include="BitMart.Net" Version="3.9.1" />
<PackageReference Include="Bybit.Net" Version="6.10.0" />
<PackageReference Include="CoinEx.Net" Version="10.9.1" />
<PackageReference Include="CryptoCom.Net" Version="3.9.1" />
<PackageReference Include="GateIo.Net" Version="3.10.1" />
<PackageReference Include="JK.Bitget.Net" Version="3.9.0" />
<PackageReference Include="JK.Mexc.Net" Version="4.9.0" />
<PackageReference Include="JK.OKX.Net" Version="4.10.1" />
<PackageReference Include="JKorf.Coinbase.Net" Version="3.9.1" />
<PackageReference Include="JKorf.HTX.Net" Version="8.9.0" />
<PackageReference Include="KrakenExchange.Net" Version="7.9.0" />
<PackageReference Include="Kucoin.Net" Version="8.10.1" />
</ItemGroup>
</Project>
+6 -4
View File
@@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Binance.Net;
using Binance.Net.Clients;
using Bybit.Net;
using Bybit.Net.Clients;
using ConsoleClient.Exchanges;
using CryptoExchange.Net.Authentication;
@@ -15,19 +17,19 @@ namespace ConsoleClient
{
static Dictionary<string, IExchange> _exchanges = new Dictionary<string, IExchange>
{
{ "Binance", new BinanceExchange() },
{ "Bybit", new BybitExchange() }
{ "Binance", new Exchanges.BinanceExchange() },
{ "Bybit", new Exchanges.BybitExchange() }
};
static async Task Main(string[] args)
{
BinanceRestClient.SetDefaultOptions(options =>
{
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
options.ApiCredentials = new BinanceCredentials("APIKEY", "APISECRET");
});
BybitRestClient.SetDefaultOptions(options =>
{
options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
options.ApiCredentials = new BybitCredentials("APIKEY", "APISECRET");
});
while (true)
+3 -3
View File
@@ -8,9 +8,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Binance.Net" Version="12.5.0" />
<PackageReference Include="BitMart.Net" Version="3.5.0" />
<PackageReference Include="JK.OKX.Net" Version="4.5.0" />
<PackageReference Include="Binance.Net" Version="12.11.0" />
<PackageReference Include="BitMart.Net" Version="3.9.1" />
<PackageReference Include="JK.OKX.Net" Version="4.10.1" />
</ItemGroup>
</Project>
+6
View File
@@ -68,6 +68,12 @@ Make a one time donation in a crypto currency of your choice. If you prefer to d
Alternatively, sponsor me on Github using [Github Sponsors](https://github.com/sponsors/JKorf).
## Release notes
* Version 11.0.2 - 26 Mar 2026
* Updated SetOptions logic to allow calling on client without credentials
* Version 11.0.1 - 24 Mar 2026
* Fixed CreateParamString method for arrays of value types
* Version 11.0.0 - 23 Mar 2026
* Updated API credential logic, exchange implementation are expected to provide their own credentials implementation with ApiCredentials as base class
* Removed ApiCredentials implementation used by most exchanges