From c9988f923af192360a9b1d6dc95fee3c87e733f0 Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 25 Apr 2024 21:43:59 +0200 Subject: [PATCH 1/6] wip --- ByBit.Net/Bybit.Net.csproj | 4 +++- .../BybitRestClientCopyTradingApi.cs | 3 +++ .../BybitRestClientDerivativesApi.cs | 3 +++ .../BybitSocketClientDerivativesPublicApi.cs | 3 +++ .../ContractApi/BybitSocketClientContractApi.cs | 3 +++ .../BybitSocketClientUnifiedMarginApi.cs | 3 +++ .../SpotApi/v3/BybitRestClientSpotApiV3.cs | 3 +++ .../SpotApi/v3/BybitSocketClientSpotApiV3.cs | 3 +++ ByBit.Net/Clients/V5/BybitRestClientApi.cs | 15 +++++++++++++++ ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs | 3 +++ .../Clients/V5/BybitSocketClientPrivateApi.cs | 3 +++ ByBit.Net/Interfaces/IBybitOrderBookFactory.cs | 15 +++++++++++++++ .../SymbolOrderBooks/BybitOrderBookFactory.cs | 14 ++++++++++++++ 13 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ByBit.Net/Bybit.Net.csproj b/ByBit.Net/Bybit.Net.csproj index 2cafe6af..91634618 100644 --- a/ByBit.Net/Bybit.Net.csproj +++ b/ByBit.Net/Bybit.Net.csproj @@ -52,6 +52,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + \ No newline at end of file diff --git a/ByBit.Net/Clients/CopyTradingApi/BybitRestClientCopyTradingApi.cs b/ByBit.Net/Clients/CopyTradingApi/BybitRestClientCopyTradingApi.cs index 4386746c..e4f55421 100644 --- a/ByBit.Net/Clients/CopyTradingApi/BybitRestClientCopyTradingApi.cs +++ b/ByBit.Net/Clients/CopyTradingApi/BybitRestClientCopyTradingApi.cs @@ -57,6 +57,9 @@ internal BybitRestClientCopyTradingApi(ILogger logger, HttpClient? httpClient, B } #endregion + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); diff --git a/ByBit.Net/Clients/DerivativesApi/BybitRestClientDerivativesApi.cs b/ByBit.Net/Clients/DerivativesApi/BybitRestClientDerivativesApi.cs index 84d6f0a5..2734f52f 100644 --- a/ByBit.Net/Clients/DerivativesApi/BybitRestClientDerivativesApi.cs +++ b/ByBit.Net/Clients/DerivativesApi/BybitRestClientDerivativesApi.cs @@ -68,6 +68,9 @@ internal BybitRestClientDerivativesApi(ILogger logger, HttpClient? httpClient, B protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// /// Get url for an endpoint /// diff --git a/ByBit.Net/Clients/DerivativesApi/BybitSocketClientDerivativesPublicApi.cs b/ByBit.Net/Clients/DerivativesApi/BybitSocketClientDerivativesPublicApi.cs index db982e0e..ebabbdc8 100644 --- a/ByBit.Net/Clients/DerivativesApi/BybitSocketClientDerivativesPublicApi.cs +++ b/ByBit.Net/Clients/DerivativesApi/BybitSocketClientDerivativesPublicApi.cs @@ -43,6 +43,9 @@ internal BybitSocketClientDerivativesPublicApi(ILogger log, BybitSocketOptions o RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { }); } + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// public override string? GetListenerIdentifier(IMessageAccessor message) { diff --git a/ByBit.Net/Clients/DerivativesApi/ContractApi/BybitSocketClientContractApi.cs b/ByBit.Net/Clients/DerivativesApi/ContractApi/BybitSocketClientContractApi.cs index bab2e8f9..bd22952b 100644 --- a/ByBit.Net/Clients/DerivativesApi/ContractApi/BybitSocketClientContractApi.cs +++ b/ByBit.Net/Clients/DerivativesApi/ContractApi/BybitSocketClientContractApi.cs @@ -38,6 +38,9 @@ internal BybitSocketClientContractApi(ILogger log, BybitSocketOptions options) RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitQuery("ping", null), x => { }); } + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); diff --git a/ByBit.Net/Clients/DerivativesApi/UnifiedMarginApi/BybitSocketClientUnifiedMarginApi.cs b/ByBit.Net/Clients/DerivativesApi/UnifiedMarginApi/BybitSocketClientUnifiedMarginApi.cs index de3267d1..e7db8f1b 100644 --- a/ByBit.Net/Clients/DerivativesApi/UnifiedMarginApi/BybitSocketClientUnifiedMarginApi.cs +++ b/ByBit.Net/Clients/DerivativesApi/UnifiedMarginApi/BybitSocketClientUnifiedMarginApi.cs @@ -40,6 +40,9 @@ internal BybitSocketClientUnifiedMarginApi(ILogger log, BybitSocketOptions optio RegisterPeriodicQuery("Heartbeat", TimeSpan.FromSeconds(20), x => new BybitPingQuery(), x => { }); } + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); diff --git a/ByBit.Net/Clients/SpotApi/v3/BybitRestClientSpotApiV3.cs b/ByBit.Net/Clients/SpotApi/v3/BybitRestClientSpotApiV3.cs index ae259a9e..7091a98d 100644 --- a/ByBit.Net/Clients/SpotApi/v3/BybitRestClientSpotApiV3.cs +++ b/ByBit.Net/Clients/SpotApi/v3/BybitRestClientSpotApiV3.cs @@ -43,6 +43,9 @@ internal BybitRestClientSpotApiV3(ILogger logger, HttpClient? httpClient, BybitR } #endregion + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + #region Common interface /// diff --git a/ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs b/ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs index dfaa1be0..1801d1dc 100644 --- a/ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs +++ b/ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs @@ -40,6 +40,9 @@ internal BybitSocketClientSpotApiV3(ILogger logger, BybitSocketOptions options) protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// public override string? GetListenerIdentifier(IMessageAccessor message) { diff --git a/ByBit.Net/Clients/V5/BybitRestClientApi.cs b/ByBit.Net/Clients/V5/BybitRestClientApi.cs index 7901cf68..85ec4e8d 100644 --- a/ByBit.Net/Clients/V5/BybitRestClientApi.cs +++ b/ByBit.Net/Clients/V5/BybitRestClientApi.cs @@ -75,6 +75,9 @@ internal BybitRestClientApi(ILogger logger, HttpClient? httpClient, BybitRestOpt protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// /// Get url for an endpoint /// @@ -103,6 +106,18 @@ protected override async Task> GetServerTimestampAsync() public override TimeSpan? GetTimeOffset() => _timeSyncState.TimeOffset; + internal async Task SendAsync(RequestDefinition definition, ParameterCollection? parameters, CancellationToken cancellationToken, int? weight = null) + { + var result = await base.SendAsync>(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>> SendRequestFullResponseAsync( Uri uri, HttpMethod method, diff --git a/ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs b/ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs index 91b9a9e4..b3dcac6e 100644 --- a/ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs +++ b/ByBit.Net/Clients/V5/BybitSocketClientBaseApi.cs @@ -37,6 +37,9 @@ internal BybitSocketClientBaseApi(ILogger log, BybitSocketOptions options, strin HandleMessageBeforeConfirmation = true; } + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// public virtual Task> SubscribeToOrderbookUpdatesAsync(string symbol, int depth, Action> updateHandler, CancellationToken ct = default) => SubscribeToOrderbookUpdatesAsync(new string[] { symbol }, depth, updateHandler, ct); diff --git a/ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs b/ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs index 46f990cd..fe5258f0 100644 --- a/ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs +++ b/ByBit.Net/Clients/V5/BybitSocketClientPrivateApi.cs @@ -38,6 +38,9 @@ internal BybitSocketClientPrivateApi(ILogger logger, BybitSocketOptions options) /// protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new BybitAuthenticationProvider(credentials); + /// + public override string FormatSymbol(string baseAsset, string quoteAsset) => baseAsset.ToUpperInvariant() + quoteAsset.ToUpperInvariant(); + /// protected override Query? GetAuthenticationRequest() { diff --git a/ByBit.Net/Interfaces/IBybitOrderBookFactory.cs b/ByBit.Net/Interfaces/IBybitOrderBookFactory.cs index 742951de..32e54685 100644 --- a/ByBit.Net/Interfaces/IBybitOrderBookFactory.cs +++ b/ByBit.Net/Interfaces/IBybitOrderBookFactory.cs @@ -10,6 +10,21 @@ namespace Bybit.Net.Interfaces /// public interface IBybitOrderBookFactory { + /// + /// Spot order book factory methods + /// + public IOrderBookFactory Spot { get; } + + /// + /// Options order book factory methods + /// + public IOrderBookFactory Options { get; } + + /// + /// Linear/Inverse order book factory methods + /// + public IOrderBookFactory LinearInverse { get; } + /// /// Create a SymbolOrderBook specifying the category /// diff --git a/ByBit.Net/SymbolOrderBooks/BybitOrderBookFactory.cs b/ByBit.Net/SymbolOrderBooks/BybitOrderBookFactory.cs index 6e60bc60..04480a4a 100644 --- a/ByBit.Net/SymbolOrderBooks/BybitOrderBookFactory.cs +++ b/ByBit.Net/SymbolOrderBooks/BybitOrderBookFactory.cs @@ -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; @@ -14,6 +15,15 @@ public class BybitOrderBookFactory : IBybitOrderBookFactory { private readonly IServiceProvider _serviceProvider; + /// + public IOrderBookFactory Spot { get; } + + /// + public IOrderBookFactory Options { get; } + + /// + public IOrderBookFactory LinearInverse { get; } + /// /// ctor /// @@ -21,6 +31,10 @@ public class BybitOrderBookFactory : IBybitOrderBookFactory public BybitOrderBookFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; + + Spot = new OrderBookFactory((symbol, options) => CreateSpot(symbol, options), (baseAsset, quoteAsset, options) => CreateSpot(baseAsset + quoteAsset, options)); + Options = new OrderBookFactory((symbol, options) => CreateOption(symbol, options), (baseAsset, quoteAsset, options) => CreateOption(baseAsset + quoteAsset, options)); + LinearInverse = new OrderBookFactory((symbol, options) => CreateLinearInverse(symbol, options), (baseAsset, quoteAsset, options) => CreateLinearInverse(baseAsset + quoteAsset, options)); } /// From 03c827eec68c0c24d854a1122a0bea581c467499 Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 25 Apr 2024 21:44:09 +0200 Subject: [PATCH 2/6] Added V5Api.Account.RequestDemoFundsAsync --- .../Clients/V5/BybitRestClientApiAccount.cs | 17 +++++++++++++++++ .../Clients/V5/IBybitRestClientApiAccount.cs | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs b/ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs index 45cdac27..efd9a0c6 100644 --- a/ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs +++ b/ByBit.Net/Clients/V5/BybitRestClientApiAccount.cs @@ -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 { /// public class BybitRestClientApiAccount : IBybitRestClientApiAccount { + private static readonly RequestDefinitionCache _definitions = new RequestDefinitionCache(); + private BybitRestClientApi _baseClient; internal BybitRestClientApiAccount(BybitRestClientApi baseClient) @@ -1005,5 +1008,19 @@ public async Task>> RepayLiab } #endregion + + #region Request Demo Funds + + /// + public async Task RequestDemoFundsAsync(Dictionary 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 } } diff --git a/ByBit.Net/Interfaces/Clients/V5/IBybitRestClientApiAccount.cs b/ByBit.Net/Interfaces/Clients/V5/IBybitRestClientApiAccount.cs index be578714..22c7e44b 100644 --- a/ByBit.Net/Interfaces/Clients/V5/IBybitRestClientApiAccount.cs +++ b/ByBit.Net/Interfaces/Clients/V5/IBybitRestClientApiAccount.cs @@ -575,5 +575,14 @@ Task> AddOrReduceMarginAsync( /// Cancellation token /// Task>> RepayLiabilitiesAsync(string? asset = null, CancellationToken ct = default); + + /// + /// Request funds for demo trading + /// + /// + /// Dictionary of the asset and amount you want to receive. Only BTC, ETH, USDT or USDC supported + /// Cancellation token + /// + Task RequestDemoFundsAsync(Dictionary funds, CancellationToken ct = default); } } \ No newline at end of file From cf8072a3007c0d54520eb3495a1ea2c0bb96be34 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 26 Apr 2024 14:39:23 +0200 Subject: [PATCH 3/6] Fix for order book factory injection --- ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs b/ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs index 74da1bbf..36018ab0 100644 --- a/ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs +++ b/ByBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs @@ -59,7 +59,7 @@ public static IServiceCollection AddBybit( services.AddTransient(); services.AddTransient(); - services.AddSingleton(); + services.AddTransient(); services.AddTransient(x => x.GetRequiredService().V5Api.CommonSpotClient); if (socketClientLifeTime == null) services.AddSingleton(); From 6c683afa77f61087a1dce7694563f7c754678ebb Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 26 Apr 2024 15:40:31 +0200 Subject: [PATCH 4/6] wip --- ByBit.Net/BybitExchange.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ByBit.Net/BybitExchange.cs diff --git a/ByBit.Net/BybitExchange.cs b/ByBit.Net/BybitExchange.cs new file mode 100644 index 00000000..3a3c4880 --- /dev/null +++ b/ByBit.Net/BybitExchange.cs @@ -0,0 +1,26 @@ +namespace Bybit.Net +{ + /// + /// Bybit exchange information and configuration + /// + public static class BybitExchange + { + /// + /// Exchange name + /// + public static string ExchangeName => "Bybit"; + + /// + /// Url to the main website + /// + public static string Url { get; } = "https://www.bybit.com"; + + /// + /// Urls to the API documentation + /// + public static string[] ApiDocsUrl { get; } = new[] { + "https://bybit-exchange.github.io/docs/v3/intro", + "https://bybit-exchange.github.io/docs/v5/intro" + }; + } +} From 47bde120d4c8b191c67c5f87affb615d4bb6dcf9 Mon Sep 17 00:00:00 2001 From: JKorf Date: Sat, 27 Apr 2024 09:10:59 +0200 Subject: [PATCH 5/6] Added missing TransactionLogType enum values, added Symbols to V5Api.ExchangeData.GetInsuranceAsync response model --- ByBit.Net/Enums/TransactionLogType.cs | 33 +++++++++++++++++++ ByBit.Net/Objects/Models/V5/BybitInsurance.cs | 5 +++ 2 files changed, 38 insertions(+) diff --git a/ByBit.Net/Enums/TransactionLogType.cs b/ByBit.Net/Enums/TransactionLogType.cs index e0742a1c..2211a9c6 100644 --- a/ByBit.Net/Enums/TransactionLogType.cs +++ b/ByBit.Net/Enums/TransactionLogType.cs @@ -38,6 +38,11 @@ public enum TransactionLogType [Map("LIQUIDATION")] Liquidation, /// + /// Auto deleveraging + /// + [Map("ADL")] + Adl, + /// /// Airdrop /// [Map("AIRDROP")] @@ -168,5 +173,33 @@ public enum TransactionLogType /// [Map("PREMARKET_ROLLBACK_PLEDGE_PENALTY_TO_BUYER")] PreMarketRollbackPledgePenaltyToBuyer, + /// + /// + [Map("CUSTODY_NETWORK_FEE")] + CustodyNetworkFee, + /// + /// + [Map("CUSTODY_SETTLE_FEE")] + CustodySettleFee, + /// + /// + [Map("CUSTODY_LOCK")] + CustodyLock, + /// + /// + [Map("CUSTODY_UNLOCK")] + CustodyUnlock, + /// + /// + [Map("CUSTODY_UNLOCK_REFUND")] + CustodyUnlockRefund, + /// + /// + [Map("LOANS_BORROW_FUNDS")] + LoansBorrowFunds, + /// + /// + [Map("LOANS_ASSET_REDEMPTION")] + LoansAssetRedemption, } } diff --git a/ByBit.Net/Objects/Models/V5/BybitInsurance.cs b/ByBit.Net/Objects/Models/V5/BybitInsurance.cs index 065e1116..a1bacb14 100644 --- a/ByBit.Net/Objects/Models/V5/BybitInsurance.cs +++ b/ByBit.Net/Objects/Models/V5/BybitInsurance.cs @@ -13,6 +13,11 @@ public class BybitInsurance [JsonProperty("coin")] public string Asset { get; set; } = string.Empty; /// + /// Symbols + /// + [JsonProperty("symbols")] + public string Symbols { get; set; } = string.Empty; + /// /// Balance /// public decimal Balance { get; set; } From 5591c4550b7d25969f8c0563746fdf63c2d64c1d Mon Sep 17 00:00:00 2001 From: JKorf Date: Sun, 28 Apr 2024 11:30:56 +0200 Subject: [PATCH 6/6] Updated CryptoExchange.Net version --- ByBit.Net/Bybit.Net.csproj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ByBit.Net/Bybit.Net.csproj b/ByBit.Net/Bybit.Net.csproj index 91634618..b249f497 100644 --- a/ByBit.Net/Bybit.Net.csproj +++ b/ByBit.Net/Bybit.Net.csproj @@ -52,8 +52,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + \ No newline at end of file