Skip to content

Commit

Permalink
Feature/cryptoclients update (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf authored Apr 28, 2024
1 parent 17e4a51 commit e8cfbb2
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.3.3" />
<PackageReference Include="CryptoExchange.Net" Version="7.4.0" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions ByBit.Net/BybitExchange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Bybit.Net
{
/// <summary>
/// Bybit exchange information and configuration
/// </summary>
public static class BybitExchange
{
/// <summary>
/// Exchange name
/// </summary>
public static string ExchangeName => "Bybit";

/// <summary>
/// Url to the main website
/// </summary>
public static string Url { get; } = "https://www.bybit.com";

/// <summary>
/// Urls to the API documentation
/// </summary>
public static string[] ApiDocsUrl { get; } = new[] {
"https://bybit-exchange.github.io/docs/v3/intro",
"https://bybit-exchange.github.io/docs/v5/intro"
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ internal BybitRestClientCopyTradingApi(ILogger logger, HttpClient? httpClient, B
}
#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ internal BybitRestClientDerivativesApi(ILogger logger, HttpClient? httpClient, B
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <summary>
/// Get url for an endpoint
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ internal BybitSocketClientDerivativesPublicApi(ILogger log, BybitSocketOptions o
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
}

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
public override string? GetListenerIdentifier(IMessageAccessor message)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ internal BybitSocketClientContractApi(ILogger log, BybitSocketOptions options)
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { });
}

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ internal BybitSocketClientUnifiedMarginApi(ILogger log, BybitSocketOptions optio
RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitPingQuery(), x => { });
}

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);
Expand Down
3 changes: 3 additions & 0 deletions ByBit.Net/Clients/SpotApi/v3/BybitRestClientSpotApiV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ internal BybitRestClientSpotApiV3(ILogger logger, HttpClient? httpClient, BybitR
}
#endregion

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

#region Common interface

/// <inheritdoc />
Expand Down
3 changes: 3 additions & 0 deletions ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ internal BybitSocketClientSpotApiV3(ILogger logger, BybitSocketOptions options)
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
public override string? GetListenerIdentifier(IMessageAccessor message)
{
Expand Down
15 changes: 15 additions & 0 deletions ByBit.Net/Clients/V5/BybitRestClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ internal BybitRestClientApi(ILogger logger, HttpClient? httpClient, BybitRestOpt
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials)
=> new BybitAuthenticationProvider(credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <summary>
/// Get url for an endpoint
/// </summary>
Expand Down Expand Up @@ -103,6 +106,18 @@ protected override async Task<WebCallResult<DateTime>> GetServerTimestampAsync()
public override TimeSpan? GetTimeOffset()
=> _timeSyncState.TimeOffset;

internal async Task<WebCallResult> SendAsync(RequestDefinition definition, ParameterCollection? parameters, CancellationToken cancellationToken, int? weight = null)
{
var result = await base.SendAsync<BybitResult<object>>(BaseAddress, definition, parameters, cancellationToken, null, weight).ConfigureAwait(false);
if (!result)
return result.AsDataless();

if (result.Data.ReturnCode != 0)
return result.AsDatalessError(new ServerError(result.Data.ReturnCode, result.Data.ReturnMessage));

return result.AsDataless();
}

internal async Task<WebCallResult<BybitExtResult<T, U>>> SendRequestFullResponseAsync<T,U>(
Uri uri,
HttpMethod method,
Expand Down
17 changes: 17 additions & 0 deletions ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
using System.Globalization;
using Bybit.Net.Interfaces.Clients.V5;
using Bybit.Net.Objects.Internal;
using System.Linq;

namespace Bybit.Net.Clients.V5
{
/// <inheritdoc />
public class BybitRestClientApiAccount : IBybitRestClientApiAccount
{
private static readonly RequestDefinitionCache _definitions = new RequestDefinitionCache();

private BybitRestClientApi _baseClient;

internal BybitRestClientApiAccount(BybitRestClientApi baseClient)
Expand Down Expand Up @@ -1005,5 +1008,19 @@ public async Task<WebCallResult<IEnumerable<BybitLiabilityRepayment>>> RepayLiab
}

#endregion

#region Request Demo Funds

/// <inheritdoc />
public async Task<WebCallResult> RequestDemoFundsAsync(Dictionary<string, decimal> funds, CancellationToken ct = default)
{
var parameters = new ParameterCollection();
parameters.AddOptionalParameter("utaDemoApplyMoney", funds.Select(f => new { coin = f.Key, amountStr = f.Value.ToString(CultureInfo.InvariantCulture) }));

var request = _definitions.GetOrCreate(HttpMethod.Post, "v5/account/demo-apply-money", true);
return await _baseClient.SendAsync(request, parameters, ct).ConfigureAwait(false);
}

#endregion
}
}
3 changes: 3 additions & 0 deletions ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ internal BybitSocketClientBaseApi(ILogger log, BybitSocketOptions options, strin
HandleMessageBeforeConfirmation = true;
}

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
public virtual Task<CallResult<UpdateSubscription>> SubscribeToOrderbookUpdatesAsync(string symbol, int depth, Action<DataEvent<BybitOrderbook>> updateHandler, CancellationToken ct = default)
=> SubscribeToOrderbookUpdatesAsync(new string[] { symbol }, depth, updateHandler, ct);
Expand Down
3 changes: 3 additions & 0 deletions ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ internal BybitSocketClientPrivateApi(ILogger logger, BybitSocketOptions options)
/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials);

/// <inheritdoc />
public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant();

/// <inheritdoc />
protected override Query? GetAuthenticationRequest()
{
Expand Down
33 changes: 33 additions & 0 deletions ByBit.Net/Enums/TransactionLogType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public enum TransactionLogType
[Map("LIQUIDATION")]
Liquidation,
/// <summary>
/// Auto deleveraging
/// </summary>
[Map("ADL")]
Adl,
/// <summary>
/// Airdrop
/// </summary>
[Map("AIRDROP")]
Expand Down Expand Up @@ -168,5 +173,33 @@ public enum TransactionLogType
/// </summary>
[Map("PREMARKET_ROLLBACK_PLEDGE_PENALTY_TO_BUYER")]
PreMarketRollbackPledgePenaltyToBuyer,
/// <summary>
/// </summary>
[Map("CUSTODY_NETWORK_FEE")]
CustodyNetworkFee,
/// <summary>
/// </summary>
[Map("CUSTODY_SETTLE_FEE")]
CustodySettleFee,
/// <summary>
/// </summary>
[Map("CUSTODY_LOCK")]
CustodyLock,
/// <summary>
/// </summary>
[Map("CUSTODY_UNLOCK")]
CustodyUnlock,
/// <summary>
/// </summary>
[Map("CUSTODY_UNLOCK_REFUND")]
CustodyUnlockRefund,
/// <summary>
/// </summary>
[Map("LOANS_BORROW_FUNDS")]
LoansBorrowFunds,
/// <summary>
/// </summary>
[Map("LOANS_ASSET_REDEMPTION")]
LoansAssetRedemption,
}
}
2 changes: 1 addition & 1 deletion ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IServiceCollection AddBybit(

services.AddTransient<ICryptoRestClient, CryptoRestClient>();
services.AddTransient<ICryptoSocketClient, CryptoSocketClient>();
services.AddSingleton<IBybitOrderBookFactory, BybitOrderBookFactory>();
services.AddTransient<IBybitOrderBookFactory, BybitOrderBookFactory>();
services.AddTransient(x => x.GetRequiredService<IBybitRestClient>().V5Api.CommonSpotClient);
if (socketClientLifeTime == null)
services.AddSingleton<IBybitSocketClient, BybitSocketClient>();
Expand Down
9 changes: 9 additions & 0 deletions ByBit.Net/Interfaces/Clients/V5/IBybitRestClientApiAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,5 +575,14 @@ Task<WebCallResult<BybitPosition>> AddOrReduceMarginAsync(
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult<IEnumerable<BybitLiabilityRepayment>>> RepayLiabilitiesAsync(string? asset = null, CancellationToken ct = default);

/// <summary>
/// Request funds for demo trading
/// <para><a href="https://bybit-exchange.github.io/docs/v5/demo#request-demo-trading-funds" /></para>
/// </summary>
/// <param name="funds">Dictionary of the asset and amount you want to receive. Only BTC, ETH, USDT or USDC supported</param>
/// <param name="ct">Cancellation token</param>
/// <returns></returns>
Task<WebCallResult> RequestDemoFundsAsync(Dictionary<string, decimal> funds, CancellationToken ct = default);
}
}
15 changes: 15 additions & 0 deletions ByBit.Net/Interfaces/IBybitOrderBookFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ namespace Bybit.Net.Interfaces
/// </summary>
public interface IBybitOrderBookFactory
{
/// <summary>
/// Spot order book factory methods
/// </summary>
public IOrderBookFactory<BybitOrderBookOptions> Spot { get; }

/// <summary>
/// Options order book factory methods
/// </summary>
public IOrderBookFactory<BybitOrderBookOptions> Options { get; }

/// <summary>
/// Linear/Inverse order book factory methods
/// </summary>
public IOrderBookFactory<BybitOrderBookOptions> LinearInverse { get; }

/// <summary>
/// Create a SymbolOrderBook specifying the category
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions ByBit.Net/Objects/Models/V5/BybitInsurance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class BybitInsurance
[JsonProperty("coin")]
public string Asset { get; set; } = string.Empty;
/// <summary>
/// Symbols
/// </summary>
[JsonProperty("symbols")]
public string Symbols { get; set; } = string.Empty;
/// <summary>
/// Balance
/// </summary>
public decimal Balance { get; set; }
Expand Down
14 changes: 14 additions & 0 deletions ByBit.Net/SymbolOrderBooks/BybitOrderBookFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bybit.Net.Interfaces.Clients;
using Bybit.Net.Objects.Options;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.OrderBook;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -14,13 +15,26 @@ public class BybitOrderBookFactory : IBybitOrderBookFactory
{
private readonly IServiceProvider _serviceProvider;

/// <inheritdoc />
public IOrderBookFactory<BybitOrderBookOptions> Spot { get; }

/// <inheritdoc />
public IOrderBookFactory<BybitOrderBookOptions> Options { get; }

/// <inheritdoc />
public IOrderBookFactory<BybitOrderBookOptions> LinearInverse { get; }

/// <summary>
/// ctor
/// </summary>
/// <param name="serviceProvider">Service provider for resolving logging and clients</param>
public BybitOrderBookFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;

Spot = new OrderBookFactory<BybitOrderBookOptions>((symbol, options) => CreateSpot(symbol, options), (baseAsset, quoteAsset, options) => CreateSpot(baseAsset + quoteAsset, options));
Options = new OrderBookFactory<BybitOrderBookOptions>((symbol, options) => CreateOption(symbol, options), (baseAsset, quoteAsset, options) => CreateOption(baseAsset + quoteAsset, options));
LinearInverse = new OrderBookFactory<BybitOrderBookOptions>((symbol, options) => CreateLinearInverse(symbol, options), (baseAsset, quoteAsset, options) => CreateLinearInverse(baseAsset + quoteAsset, options));
}

/// <inheritdoc />
Expand Down

0 comments on commit e8cfbb2

Please sign in to comment.