Skip to content

Commit e8368b1

Browse files
authored
Feature/user provider (#13)
Added SetApiCredentials(ExchagneCredentials credentials) to ExchangeRestClient and ExchangeSocketClient Added single exchange subscription methods to ExchangeSocketClient Added (I)ExchangeUserClientProvider allowing for easy client management when handling multiple users Added constructor to ExchangeCredentials for Dictionary<string, ApiCredentials>
1 parent a34a8b0 commit e8368b1

21 files changed

+1077
-38
lines changed

CryptoClients.Net/Clients/ExchangeUserClientProvider.cs

Lines changed: 340 additions & 0 deletions
Large diffs are not rendered by default.

CryptoClients.Net/Clients/Rest/ExchangeRestClient.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,39 @@ public IEnumerable<ISharedClient> GetExchangeSharedClients(string name, TradingM
366366
return result.ToList();
367367
}
368368

369+
/// <inheritdoc />
370+
public void SetApiCredentials(ExchangeCredentials credentials)
371+
{
372+
void SetCredentialsIfNotNull(string exchange, ApiCredentials? credentials)
373+
{
374+
if (credentials == null)
375+
return;
376+
377+
SetApiCredentials(exchange, credentials.Key, credentials.Secret, credentials.Pass);
378+
}
379+
380+
SetCredentialsIfNotNull(Exchange.Binance, credentials.Binance);
381+
SetCredentialsIfNotNull(Exchange.BingX, credentials.BingX);
382+
SetCredentialsIfNotNull(Exchange.Bitfinex, credentials.Bitfinex);
383+
SetCredentialsIfNotNull(Exchange.Bitget, credentials.Bitget);
384+
SetCredentialsIfNotNull(Exchange.BitMart, credentials.BitMart);
385+
SetCredentialsIfNotNull(Exchange.BitMEX, credentials.BitMEX);
386+
SetCredentialsIfNotNull(Exchange.Bybit, credentials.Bybit);
387+
SetCredentialsIfNotNull(Exchange.Coinbase, credentials.Coinbase);
388+
SetCredentialsIfNotNull(Exchange.CoinEx, credentials.CoinEx);
389+
SetCredentialsIfNotNull(Exchange.CryptoCom, credentials.CryptoCom);
390+
SetCredentialsIfNotNull(Exchange.DeepCoin, credentials.DeepCoin);
391+
SetCredentialsIfNotNull(Exchange.GateIo, credentials.GateIo);
392+
SetCredentialsIfNotNull(Exchange.HTX, credentials.HTX);
393+
SetCredentialsIfNotNull(Exchange.HyperLiquid, credentials.HyperLiquid);
394+
SetCredentialsIfNotNull(Exchange.Kraken, credentials.Kraken);
395+
SetCredentialsIfNotNull(Exchange.Kucoin, credentials.Kucoin);
396+
SetCredentialsIfNotNull(Exchange.Mexc, credentials.Mexc);
397+
SetCredentialsIfNotNull(Exchange.OKX, credentials.OKX);
398+
SetCredentialsIfNotNull(Exchange.WhiteBit, credentials.WhiteBit);
399+
SetCredentialsIfNotNull(Exchange.XT, credentials.XT);
400+
}
401+
369402
/// <inheritdoc />
370403
public void SetApiCredentials(string exchange, string apiKey, string apiSecret, string? apiPass = null)
371404
{

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.Balance.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -21,6 +22,18 @@ public partial class ExchangeSocketClient
2122

2223
#region Subscribe Balance
2324

25+
/// <inheritdoc />
26+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToBalanceUpdatesAsync(
27+
string exchange,
28+
SubscribeBalancesRequest request,
29+
Action<ExchangeEvent<SharedBalance[]>> handler,
30+
ExchangeWebResult<string>[]? listenKeyResults = null,
31+
CancellationToken ct = default)
32+
{
33+
var result = await SubscribeToBalanceUpdatesAsync(request, handler, new[] { exchange }, listenKeyResults, ct).ConfigureAwait(false);
34+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
35+
}
36+
2437
/// <inheritdoc />
2538
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToBalanceUpdatesAsync(
2639
SubscribeBalancesRequest request,

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.BookTicker.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -20,6 +21,17 @@ public partial class ExchangeSocketClient
2021

2122
#region Subscribe Book Ticker
2223

24+
/// <inheritdoc />
25+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToBookTickerUpdatesAsync(
26+
string exchange,
27+
SubscribeBookTickerRequest request,
28+
Action<ExchangeEvent<SharedBookTicker>> handler,
29+
CancellationToken ct = default)
30+
{
31+
var result = await SubscribeToBookTickerUpdatesAsync(request, handler, new[] { exchange }, ct).ConfigureAwait(false);
32+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
33+
}
34+
2335
/// <inheritdoc />
2436
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToBookTickerUpdatesAsync(SubscribeBookTickerRequest request, Action<ExchangeEvent<SharedBookTicker>> handler, IEnumerable<string>? exchanges = null, CancellationToken ct = default)
2537
{

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.FuturesOrder.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -20,6 +21,18 @@ public partial class ExchangeSocketClient
2021

2122
#region Subscribe Futures Order
2223

24+
/// <inheritdoc />
25+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToFuturesOrderUpdatesAsync(
26+
string exchange,
27+
SubscribeFuturesOrderRequest request,
28+
Action<ExchangeEvent<SharedFuturesOrder[]>> handler,
29+
ExchangeWebResult<string>[]? listenKeyResults = null,
30+
CancellationToken ct = default)
31+
{
32+
var result = await SubscribeToFuturesOrderUpdatesAsync(request, handler, new[] { exchange }, listenKeyResults, ct).ConfigureAwait(false);
33+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
34+
}
35+
2336
/// <inheritdoc />
2437
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToFuturesOrderUpdatesAsync(
2538
SubscribeFuturesOrderRequest request,

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.Kline.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -21,6 +22,17 @@ public partial class ExchangeSocketClient
2122

2223
#region Subscribe Kline
2324

25+
/// <inheritdoc />
26+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToKlineUpdatesAsync(
27+
string exchange,
28+
SubscribeKlineRequest request,
29+
Action<ExchangeEvent<SharedKline>> handler,
30+
CancellationToken ct = default)
31+
{
32+
var result = await SubscribeToKlineUpdatesAsync(request, handler, new[] { exchange }, ct).ConfigureAwait(false);
33+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
34+
}
35+
2436
/// <inheritdoc />
2537
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToKlineUpdatesAsync(SubscribeKlineRequest request, Action<ExchangeEvent<SharedKline>> handler, IEnumerable<string>? exchanges = null, CancellationToken ct = default)
2638
{

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.OrderBook.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -20,6 +21,17 @@ public partial class ExchangeSocketClient
2021

2122
#region Subscribe Order Book
2223

24+
/// <inheritdoc />
25+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToOrderBookUpdatesAsync(
26+
string exchange,
27+
SubscribeOrderBookRequest request,
28+
Action<ExchangeEvent<SharedOrderBook>> handler,
29+
CancellationToken ct = default)
30+
{
31+
var result = await SubscribeToOrderBookUpdatesAsync(request, handler, new[] { exchange }, ct).ConfigureAwait(false);
32+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
33+
}
34+
2335
/// <inheritdoc />
2436
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToOrderBookUpdatesAsync(SubscribeOrderBookRequest request, Action<ExchangeEvent<SharedOrderBook>> handler, IEnumerable<string>? exchanges = null, CancellationToken ct = default)
2537
{

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.Position.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -20,6 +21,18 @@ public partial class ExchangeSocketClient
2021

2122
#region Subscribe Position
2223

24+
/// <inheritdoc />
25+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToPositionUpdatesAsync(
26+
string exchange,
27+
SubscribePositionRequest request,
28+
Action<ExchangeEvent<SharedPosition[]>> handler,
29+
ExchangeWebResult<string>[]? listenKeyResults = null,
30+
CancellationToken ct = default)
31+
{
32+
var result = await SubscribeToPositionUpdatesAsync(request, handler, new[] { exchange }, listenKeyResults, ct).ConfigureAwait(false);
33+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
34+
}
35+
2336
/// <inheritdoc />
2437
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToPositionUpdatesAsync(
2538
SubscribePositionRequest request,

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.SpotOrder.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -18,6 +19,18 @@ public partial class ExchangeSocketClient
1819

1920
#region Subscribe Spot Order
2021

22+
/// <inheritdoc />
23+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToSpotOrderUpdatesAsync(
24+
string exchange,
25+
SubscribeSpotOrderRequest request,
26+
Action<ExchangeEvent<SharedSpotOrder[]>> handler,
27+
ExchangeWebResult<string>[]? listenKeyResults = null,
28+
CancellationToken ct = default)
29+
{
30+
var result = await SubscribeToSpotOrderUpdatesAsync(request, handler, new[] { exchange }, listenKeyResults, ct).ConfigureAwait(false);
31+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
32+
}
33+
2134
/// <inheritdoc />
2235
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToSpotOrderUpdatesAsync(
2336
SubscribeSpotOrderRequest request,

CryptoClients.Net/Clients/Socket/ExchangeSocketClient.Ticker.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CryptoExchange.Net.Objects.Sockets;
1+
using CryptoExchange.Net.Objects;
2+
using CryptoExchange.Net.Objects.Sockets;
23
using CryptoExchange.Net.SharedApis;
34
using System;
45
using System.Collections.Generic;
@@ -20,6 +21,17 @@ public partial class ExchangeSocketClient
2021

2122
#region Subscribe Ticker
2223

24+
/// <inheritdoc />
25+
public async Task<ExchangeResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(
26+
string exchange,
27+
SubscribeTickerRequest request,
28+
Action<ExchangeEvent<SharedSpotTicker>> handler,
29+
CancellationToken ct = default)
30+
{
31+
var result = await SubscribeToTickerUpdatesAsync(request, handler, new[] { exchange }, ct).ConfigureAwait(false);
32+
return result.SingleOrDefault() ?? new ExchangeResult<UpdateSubscription>(exchange, new InvalidOperationError($"Subscription not supported for {exchange}"));
33+
}
34+
2335
/// <inheritdoc />
2436
public async Task<ExchangeResult<UpdateSubscription>[]> SubscribeToTickerUpdatesAsync(SubscribeTickerRequest request, Action<ExchangeEvent<SharedSpotTicker>> handler, IEnumerable<string>? exchanges = null, CancellationToken ct = default)
2537
{

0 commit comments

Comments
 (0)