From 641e8ce016b8bbb6e42e96fe79c171cd66f5edea Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 5 Mar 2024 21:34:26 +0100 Subject: [PATCH 01/10] wip --- .../JsonToObjectComparer.cs | 1 + Bitfinex.Net/Bitfinex.Net.csproj | 6 ++-- Bitfinex.Net/Bitfinex.Net.xml | 4 +-- .../BitfinexRestClientGeneralApi.cs | 31 +++++++++++-------- .../SpotApi/BitfinexRestClientSpotApi.cs | 31 +++++++++++-------- Bitfinex.Net/Usings.cs | 1 + 6 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 Bitfinex.Net/Usings.cs diff --git a/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs b/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs index a7ede63..1f1b98b 100644 --- a/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs +++ b/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs @@ -1,5 +1,6 @@ using Bitfinex.Net.UnitTests.TestImplementations; using CryptoExchange.Net.Converters; +using CryptoExchange.Net.Converters.JsonNet; using CryptoExchange.Net.Objects; using Newtonsoft.Json; using Newtonsoft.Json.Linq; diff --git a/Bitfinex.Net/Bitfinex.Net.csproj b/Bitfinex.Net/Bitfinex.Net.csproj index f1e67ba..88fb7bd 100644 --- a/Bitfinex.Net/Bitfinex.Net.csproj +++ b/Bitfinex.Net/Bitfinex.Net.csproj @@ -2,7 +2,7 @@ netstandard2.0;netstandard2.1 enable - 8.0 + 10.0 Bitfinex.Net @@ -51,10 +51,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - all runtime; build; native; contentfiles; analyzers; buildtransitive + + + \ No newline at end of file diff --git a/Bitfinex.Net/Bitfinex.Net.xml b/Bitfinex.Net/Bitfinex.Net.xml index d19a2c9..00f68e5 100644 --- a/Bitfinex.Net/Bitfinex.Net.xml +++ b/Bitfinex.Net/Bitfinex.Net.xml @@ -111,7 +111,7 @@ - + @@ -216,7 +216,7 @@ - + diff --git a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs index 865f445..7a39fa1 100644 --- a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs +++ b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs @@ -4,6 +4,8 @@ using CryptoExchange.Net; using CryptoExchange.Net.Authentication; using CryptoExchange.Net.Objects; +using CryptoExchange.Net.Sockets.MessageParsing; +using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; @@ -59,24 +61,27 @@ internal Uri GetUrl(string endpoint, string version) } /// - protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable>> responseHeaders, string data) + protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable>> responseHeaders, IMessageAccessor accessor) { - var errorData = ValidateJson(data); - if (!errorData) - return new ServerError(data); + if (!accessor.IsJson) + return new ServerError(accessor.GetOriginalString()); - if (!(errorData.Data is JArray)) + if (accessor.GetNodeType() != NodeType.Array) { - if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) - return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); - if (errorData.Data["message"] != null) - return new ServerError(errorData.Data["message"]!.ToString()); - else - return new ServerError(errorData.Data.ToString()); + + //if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) + // return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); + //if (errorData.Data["message"] != null) + // return new ServerError(errorData.Data["message"]!.ToString()); + //else + // return new ServerError(errorData.Data.ToString()); } - var error = errorData.Data.ToObject(); - return new ServerError(error!.ErrorCode, error.ErrorMessage); + var result = accessor.Deserialize(); + if (!result) + return new ServerError(accessor.GetOriginalString()); + + return new ServerError(result.Data.ErrorCode!, result.Data.ErrorMessage!); } /// diff --git a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs index e3281f0..3abb271 100644 --- a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs +++ b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs @@ -7,6 +7,8 @@ using CryptoExchange.Net.CommonObjects; using CryptoExchange.Net.Interfaces.CommonClients; using CryptoExchange.Net.Objects; +using CryptoExchange.Net.Sockets.MessageParsing; +using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; @@ -419,24 +421,27 @@ async Task>> IBaseRestClient.GetBalancesAsync #endregion /// - protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable>> responseHeaders, string data) + protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable>> responseHeaders, IMessageAccessor accessor) { - var errorData = ValidateJson(data); - if (!errorData) - return new ServerError(data); + if (!accessor.IsJson) + return new ServerError(accessor.GetOriginalString()); - if (!(errorData.Data is JArray)) + if (accessor.GetNodeType() != NodeType.Array) { - if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) - return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); - if (errorData.Data["message"] != null) - return new ServerError(errorData.Data["message"]!.ToString()); - else - return new ServerError(errorData.Data.ToString()); + + //if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) + // return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); + //if (errorData.Data["message"] != null) + // return new ServerError(errorData.Data["message"]!.ToString()); + //else + // return new ServerError(errorData.Data.ToString()); } - var error = errorData.Data.ToObject(); - return new ServerError(error!.ErrorCode, error.ErrorMessage); + var result = accessor.Deserialize(); + if (!result) + return new ServerError(accessor.GetOriginalString()); + + return new ServerError(result.Data.ErrorCode!, result.Data.ErrorMessage!); } } } diff --git a/Bitfinex.Net/Usings.cs b/Bitfinex.Net/Usings.cs new file mode 100644 index 0000000..7ef0a6f --- /dev/null +++ b/Bitfinex.Net/Usings.cs @@ -0,0 +1 @@ +global using CryptoExchange.Net.Converters.JsonNet; \ No newline at end of file From 9de01d87481bd3e62e23d952d06f1aa82c766b65 Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 6 Mar 2024 16:51:39 +0100 Subject: [PATCH 02/10] wip --- .../BitfinexRestClientGeneralApi.cs | 26 ++++++++++++------- .../SpotApi/BitfinexRestClientSpotApi.cs | 26 ++++++++++++------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs index 7a39fa1..982cc6b 100644 --- a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs +++ b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs @@ -68,20 +68,28 @@ protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable(MessagePath.Get().Property("error")); + var errorCode = accessor.GetValue(MessagePath.Get().Property("code")); + var errorDesc = accessor.GetValue(MessagePath.Get().Property("error_description")); + if (error != null && errorCode != null && errorDesc != null) + return new ServerError(errorCode.Value, $"{error}: {errorDesc}"); - //if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) - // return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); - //if (errorData.Data["message"] != null) - // return new ServerError(errorData.Data["message"]!.ToString()); - //else - // return new ServerError(errorData.Data.ToString()); + var message = accessor.GetValue(MessagePath.Get().Property("message")); + if (message != null) + return new ServerError(message); + + return new ServerError(accessor.GetOriginalString()); } - var result = accessor.Deserialize(); - if (!result) + var code = accessor.GetValue(MessagePath.Get().Index(1)); + var msg = accessor.GetValue(MessagePath.Get().Index(2)); + if (msg == null) return new ServerError(accessor.GetOriginalString()); - return new ServerError(result.Data.ErrorCode!, result.Data.ErrorMessage!); + if (code == null) + return new ServerError(msg); + + return new ServerError(code.Value, msg); } /// diff --git a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs index 3abb271..2cfc4ce 100644 --- a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs +++ b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs @@ -428,20 +428,28 @@ protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable(MessagePath.Get().Property("error")); + var errorCode = accessor.GetValue(MessagePath.Get().Property("code")); + var errorDesc = accessor.GetValue(MessagePath.Get().Property("error_description")); + if (error != null && errorCode != null && errorDesc != null) + return new ServerError(errorCode.Value, $"{error}: {errorDesc}"); + + var message = accessor.GetValue(MessagePath.Get().Property("message")); + if (message != null) + return new ServerError(message); - //if (errorData.Data["error"] != null && errorData.Data["code"] != null && errorData.Data["error_description"] != null) - // return new ServerError((int)errorData.Data["code"]!, errorData.Data["error"] + ": " + errorData.Data["error_description"]); - //if (errorData.Data["message"] != null) - // return new ServerError(errorData.Data["message"]!.ToString()); - //else - // return new ServerError(errorData.Data.ToString()); + return new ServerError(accessor.GetOriginalString()); } - var result = accessor.Deserialize(); - if (!result) + var code = accessor.GetValue(MessagePath.Get().Index(1)); + var msg = accessor.GetValue(MessagePath.Get().Index(2)); + if (msg == null) return new ServerError(accessor.GetOriginalString()); - return new ServerError(result.Data.ErrorCode!, result.Data.ErrorMessage!); + if (code == null) + return new ServerError(msg); + + return new ServerError(code.Value, msg); } } } From c5d533d583ee5bce21446542fdc98dcb98e3bb0f Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 6 Mar 2024 21:45:57 +0100 Subject: [PATCH 03/10] wip --- Bitfinex.Net/Bitfinex.Net.xml | 10 +++++----- Bitfinex.Net/BitfinexAuthenticationProvider.cs | 1 + Bitfinex.Net/Clients/BitfinexRestClient.cs | 4 ++-- Bitfinex.Net/Clients/BitfinexSocketClient.cs | 4 ++-- .../Clients/GeneralApi/BitfinexRestClientGeneralApi.cs | 5 +++-- .../Clients/SpotApi/BitfinexRestClientSpotApi.cs | 5 +++-- .../Clients/SpotApi/BitfinexSocketClientSpotApi.cs | 5 +++-- Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs | 4 ++-- .../Sockets/Subscriptions/BitfinexSubscription.cs | 4 ++-- .../Sockets/Subscriptions/BitfinexUserSubscription.cs | 4 ++-- 10 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Bitfinex.Net/Bitfinex.Net.xml b/Bitfinex.Net/Bitfinex.Net.xml index 00f68e5..05b36f9 100644 --- a/Bitfinex.Net/Bitfinex.Net.xml +++ b/Bitfinex.Net/Bitfinex.Net.xml @@ -111,7 +111,7 @@ - + @@ -216,7 +216,7 @@ - + @@ -522,7 +522,7 @@ - + @@ -5356,10 +5356,10 @@ Spot API options - + - + diff --git a/Bitfinex.Net/BitfinexAuthenticationProvider.cs b/Bitfinex.Net/BitfinexAuthenticationProvider.cs index 7f4a3aa..7b42186 100644 --- a/Bitfinex.Net/BitfinexAuthenticationProvider.cs +++ b/Bitfinex.Net/BitfinexAuthenticationProvider.cs @@ -6,6 +6,7 @@ using Bitfinex.Net.Objects.Internal; using CryptoExchange.Net; using CryptoExchange.Net.Authentication; +using CryptoExchange.Net.Clients; using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; using Newtonsoft.Json; diff --git a/Bitfinex.Net/Clients/BitfinexRestClient.cs b/Bitfinex.Net/Clients/BitfinexRestClient.cs index b551086..1fbe54d 100644 --- a/Bitfinex.Net/Clients/BitfinexRestClient.cs +++ b/Bitfinex.Net/Clients/BitfinexRestClient.cs @@ -1,5 +1,4 @@ -using CryptoExchange.Net; -using System; +using System; using Bitfinex.Net.Interfaces.Clients; using Bitfinex.Net.Interfaces.Clients.SpotApi; using Bitfinex.Net.Interfaces.Clients.GeneralApi; @@ -10,6 +9,7 @@ using System.Net.Http; using Bitfinex.Net.Objects.Options; using Microsoft.Extensions.DependencyInjection; +using CryptoExchange.Net.Clients; namespace Bitfinex.Net.Clients { diff --git a/Bitfinex.Net/Clients/BitfinexSocketClient.cs b/Bitfinex.Net/Clients/BitfinexSocketClient.cs index 547e82a..c891e03 100644 --- a/Bitfinex.Net/Clients/BitfinexSocketClient.cs +++ b/Bitfinex.Net/Clients/BitfinexSocketClient.cs @@ -1,11 +1,11 @@ -using CryptoExchange.Net; -using System; +using System; using Microsoft.Extensions.Logging; using Bitfinex.Net.Interfaces.Clients; using Bitfinex.Net.Interfaces.Clients.SpotApi; using Bitfinex.Net.Clients.SpotApi; using CryptoExchange.Net.Authentication; using Bitfinex.Net.Objects.Options; +using CryptoExchange.Net.Clients; namespace Bitfinex.Net.Clients { diff --git a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs index 982cc6b..b9216d7 100644 --- a/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs +++ b/Bitfinex.Net/Clients/GeneralApi/BitfinexRestClientGeneralApi.cs @@ -3,9 +3,10 @@ using Bitfinex.Net.Objects.Options; using CryptoExchange.Net; using CryptoExchange.Net.Authentication; +using CryptoExchange.Net.Clients; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; diff --git a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs index 2cfc4ce..80d611c 100644 --- a/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs +++ b/Bitfinex.Net/Clients/SpotApi/BitfinexRestClientSpotApi.cs @@ -4,11 +4,12 @@ using Bitfinex.Net.Objects.Options; using CryptoExchange.Net; using CryptoExchange.Net.Authentication; +using CryptoExchange.Net.Clients; using CryptoExchange.Net.CommonObjects; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Interfaces.CommonClients; using CryptoExchange.Net.Objects; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; diff --git a/Bitfinex.Net/Clients/SpotApi/BitfinexSocketClientSpotApi.cs b/Bitfinex.Net/Clients/SpotApi/BitfinexSocketClientSpotApi.cs index 32961ac..20b16d7 100644 --- a/Bitfinex.Net/Clients/SpotApi/BitfinexSocketClientSpotApi.cs +++ b/Bitfinex.Net/Clients/SpotApi/BitfinexSocketClientSpotApi.cs @@ -21,9 +21,10 @@ using CryptoExchange.Net.Sockets; using System.Globalization; using Bitfinex.Net.Objects.Sockets.Queries; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Bitfinex.Net.ExtensionMethods; +using CryptoExchange.Net.Interfaces; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Clients; namespace Bitfinex.Net.Clients.SpotApi { diff --git a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs index 4845f72..765374f 100644 --- a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs +++ b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs @@ -1,10 +1,10 @@ using Bitfinex.Net.Objects.Internal; using Bitfinex.Net.Objects.Models.Socket; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; using CryptoExchange.Net.Objects.Sockets; using CryptoExchange.Net.Sockets; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs index 6573e96..d93039c 100644 --- a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs +++ b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs @@ -2,11 +2,11 @@ using Bitfinex.Net.Enums; using Bitfinex.Net.Objects.Internal; using Bitfinex.Net.Objects.Sockets.Queries; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; using CryptoExchange.Net.Objects.Sockets; using CryptoExchange.Net.Sockets; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; diff --git a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs index c04c30c..3a0004a 100644 --- a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs +++ b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs @@ -1,10 +1,10 @@ using Bitfinex.Net.Objects.Models; using Bitfinex.Net.Objects.Models.Socket; +using CryptoExchange.Net.Converters.MessageParsing; +using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; using CryptoExchange.Net.Objects.Sockets; using CryptoExchange.Net.Sockets; -using CryptoExchange.Net.Sockets.MessageParsing; -using CryptoExchange.Net.Sockets.MessageParsing.Interfaces; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; From 1b5655b102f986c12147d32d008db4cea72f2902 Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 13 Mar 2024 21:55:37 +0100 Subject: [PATCH 04/10] wip --- Bitfinex.Net/Objects/Sockets/Queries/BitfinexAuthQuery.cs | 6 +++--- Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs | 6 +++--- Bitfinex.Net/Objects/Sockets/Queries/BitfinexSubQuery.cs | 6 +++--- .../Sockets/Subscriptions/BitfinexInfoSubscription.cs | 6 +++--- .../Objects/Sockets/Subscriptions/BitfinexSubscription.cs | 4 ++-- .../Sockets/Subscriptions/BitfinexUserSubscription.cs | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexAuthQuery.cs b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexAuthQuery.cs index 65722c3..e521312 100644 --- a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexAuthQuery.cs +++ b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexAuthQuery.cs @@ -15,12 +15,12 @@ public BitfinexAuthQuery(BitfinexAuthentication authRequest) : base(authRequest, { } - public override Task> HandleMessageAsync(SocketConnection connection, DataEvent message) + public override CallResult HandleMessage(SocketConnection connection, DataEvent message) { if (message.Data.Status != "OK") - return Task.FromResult(new CallResult(new ServerError(message.Data.Code!.Value, message.Data.Message!))); + return new CallResult(new ServerError(message.Data.Code!.Value, message.Data.Message!)); - return Task.FromResult(new CallResult(message.Data, message.OriginalData, null)); + return new CallResult(message.Data, message.OriginalData, null); } } } diff --git a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs index 765374f..d2612ec 100644 --- a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs +++ b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexQuery.cs @@ -28,12 +28,12 @@ public BitfinexQuery(BitfinexSocketQuery request) : base(request, true, 1) return typeof(BitfinexSocketEvent>); } - public override Task>>> HandleMessageAsync(SocketConnection connection, DataEvent>> message) + public override CallResult>> HandleMessage(SocketConnection connection, DataEvent>> message) { if (message.Data.Data.Result != "SUCCESS") - return Task.FromResult(new CallResult>>(new ServerError(message.Data.Data.ErrorMessage!))); + return new CallResult>>(new ServerError(message.Data.Data.ErrorMessage!)); - return Task.FromResult(new CallResult>>(message.Data)); + return new CallResult>>(message.Data); } } } diff --git a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexSubQuery.cs b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexSubQuery.cs index 4e27a17..a80c1a3 100644 --- a/Bitfinex.Net/Objects/Sockets/Queries/BitfinexSubQuery.cs +++ b/Bitfinex.Net/Objects/Sockets/Queries/BitfinexSubQuery.cs @@ -31,12 +31,12 @@ public BitfinexSubQuery(string evnt, string channel, string? symbol, string? pre }; } - public override Task> HandleMessageAsync(SocketConnection connection, DataEvent message) + public override CallResult HandleMessage(SocketConnection connection, DataEvent message) { if (message.Data.Event == "error") - return Task.FromResult(new CallResult(new ServerError(message.Data.Message!))); + return new CallResult(new ServerError(message.Data.Message!)); - return Task.FromResult(new CallResult(message.Data)); + return new CallResult(message.Data); } } } diff --git a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexInfoSubscription.cs b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexInfoSubscription.cs index 0047b7b..e03576f 100644 --- a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexInfoSubscription.cs +++ b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexInfoSubscription.cs @@ -16,13 +16,13 @@ public BitfinexInfoSubscription(ILogger logger) : base(logger, false) { } - public override Task HandleMessageAsync(SocketConnection connection, DataEvent message) + public override CallResult HandleMessage(SocketConnection connection, DataEvent message) { if (message.Data.Code == null) { // welcome event, send a config message for receiving checsum updates for order book subscriptions _ = connection.SendAndWaitQueryAsync(new BitfinexConfQuery(131072)); - return Task.FromResult(new CallResult(null)); + return new CallResult(null); } var code = message.Data.Code; @@ -46,7 +46,7 @@ public override Task HandleMessageAsync(SocketConnection connection, break; } - return Task.FromResult(new CallResult(null)); + return new CallResult(null); } } } diff --git a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs index d93039c..68ea1f7 100644 --- a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs +++ b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexSubscription.cs @@ -93,7 +93,7 @@ public override void HandleSubQueryResponse(BitfinexResponse message) return new BitfinexUnsubQuery(_channelId); } - public override Task DoHandleMessageAsync(SocketConnection connection, DataEvent message) + public override CallResult DoHandleMessage(SocketConnection connection, DataEvent message) { if (message.Data is BitfinexChecksum checksum) _checksumHandler?.Invoke(message.As(checksum.Checksum, _symbol)); @@ -107,7 +107,7 @@ public override Task DoHandleMessageAsync(SocketConnection connectio _handler?.Invoke(message.As>(new[] { single3Update.Data }, _symbol, _firstUpdate ? SocketUpdateType.Snapshot : SocketUpdateType.Update)); _firstUpdate = false; - return Task.FromResult(new CallResult(null)); + return new CallResult(null); } } } diff --git a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs index 3a0004a..0ca8d86 100644 --- a/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs +++ b/Bitfinex.Net/Objects/Sockets/Subscriptions/BitfinexUserSubscription.cs @@ -129,7 +129,7 @@ public BitfinexUserSubscription(ILogger logger, public override Query? GetUnsubQuery() => null; - public override Task DoHandleMessageAsync(SocketConnection connection, DataEvent message) + public override CallResult DoHandleMessage(SocketConnection connection, DataEvent message) { if (message.Data is BitfinexSocketEvent> positionSnapshot) _positionHandler?.Invoke(message.As>(positionSnapshot.Data)); @@ -177,7 +177,7 @@ public override Task DoHandleMessageAsync(SocketConnection connectio else if (message.Data is BitfinexSocketEvent marginSymbolUpdate) _marginSymbolHandler?.Invoke(message.As(marginSymbolUpdate.Data)); - return Task.FromResult(new CallResult(null)); + return new CallResult(null); } } } From 9234426193fed8bcd85c22634fe865f4a0d9195f Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 14 Mar 2024 16:39:14 +0100 Subject: [PATCH 05/10] wip --- .../BitfinexSocketClientTests.cs | 58 +++++++++---------- .../TestImplementations/TestSocket.cs | 12 ++-- .../BitfinexAuthenticationProvider.cs | 2 +- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs index 8eca51f..478b860 100644 --- a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs +++ b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs @@ -56,7 +56,7 @@ public async Task SubscribingToBookUpdates_Should_SubscribeSuccessfully(Precisio }; // act - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); var taskResult = await subTask.ConfigureAwait(false); @@ -93,12 +93,12 @@ public async Task SubscribingToBookUpdates_Should_TriggerWithBookUpdate(Precisio Precision = JsonConvert.SerializeObject(prec, new PrecisionConverter(false)), Symbol = "tBTCUSD" }; - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); BitfinexOrderBookEntry[] expected = new[] { new BitfinexOrderBookEntry() { RawPrice = "1", RawQuantity = "2", Count = 3, Price = 1, Quantity = 2 } }; // act - await socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); + socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); @@ -133,7 +133,7 @@ public async Task SubscribingToCandleUpdates_Should_SubscribeSuccessfully(KlineI }; // act - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); @@ -169,12 +169,12 @@ public async Task SubscribingToCandleUpdates_Should_TriggerWithCandleUpdate(Klin ChannelId = 1, Key = "trade:" + JsonConvert.SerializeObject(timeframe, new KlineIntervalConverter(false)) + ":tBTCUSD" }; - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); BitfinexKline[] expected = new[] { new BitfinexKline() }; // act - await socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); + socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); @@ -200,7 +200,7 @@ public async Task SubscribingToTickerUpdates_Should_SubscribeSuccessfully() }; // act - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); @@ -227,12 +227,12 @@ public async Task SubscribingToTickerUpdates_Should_TriggerWithTickerUpdate() Symbol = "tBTCUSD", Pair = "BTCUSD" }; - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); BitfinexStreamTicker expected = new BitfinexStreamTicker(); // act - await socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); + socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert Assert.IsTrue(TestHelpers.AreEqual(result, expected)); @@ -261,7 +261,7 @@ public async Task SubscribingToRawBookUpdates_Should_SubscribeSuccessfully() }; // act - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); @@ -291,12 +291,12 @@ public async Task SubscribingToRawBookUpdates_Should_TriggerWithRawBookUpdate() Precision = "R0", Length = 10 }; - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); BitfinexRawOrderBookEntry[] expected = new []{ new BitfinexRawOrderBookEntry()}; // act - await socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); + socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); @@ -322,7 +322,7 @@ public async Task SubscribingToTradeUpdates_Should_SubscribeSuccessfully() }; // act - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); @@ -349,12 +349,12 @@ public async Task SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() Symbol = "BTCUSD", Pair = "BTCUSD" }; - await socket.InvokeMessage(subResponse); + socket.InvokeMessage(subResponse); subTask.Wait(5000); BitfinexTradeSimple[] expected = new[] { new BitfinexTradeSimple() }; // act - await socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); + socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); @@ -580,9 +580,9 @@ public async Task PlacingAnOrder_Should_SucceedIfSuccessResponse() // act var placeTask = client.SpotApi.PlaceOrderAsync(OrderSide.Buy, OrderType.ExchangeLimit, "tBTCUSD", 1, price: 1, clientOrderId: 1234); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); Thread.Sleep(100); - await socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); + socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); var result = placeTask.Result; // assert @@ -601,9 +601,9 @@ public async Task PlacingAnOrder_Should_FailIfErrorResponse() // act var placeTask = client.SpotApi.PlaceOrderAsync(OrderSide.Buy, OrderType.ExchangeLimit, "tBTCUSD", 1, price: 1, clientOrderId: 123); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); Thread.Sleep(100); - await socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(order)}, 0, \"error\", \"order placing failed\"]]"); + socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(order)}, 0, \"error\", \"order placing failed\"]]"); var result = placeTask.Result; // assert @@ -625,7 +625,7 @@ public async Task PlacingAnOrder_Should_FailIfNoResponse() // act var placeTask = client.SpotApi.PlaceOrderAsync(OrderSide.Buy, OrderType.ExchangeLimit, "tBTCUSD", 1, price: 1, clientOrderId: 123); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Message = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Message = "OK" }); var result = placeTask.Result; // assert @@ -652,9 +652,9 @@ public async Task PlacingAnMarketOrder_Should_SucceedIfSuccessResponse() // act var placeTask = client.SpotApi.PlaceOrderAsync(OrderSide.Buy, OrderType.ExchangeMarket, "tBTCUSD", 1, price: 1, clientOrderId: 1234); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); Thread.Sleep(100); - await socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); + socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); var result = placeTask.Result; // assert @@ -682,9 +682,9 @@ public async Task PlacingAnFOKOrder_Should_SucceedIfSuccessResponse() // act var placeTask = client.SpotApi.PlaceOrderAsync(OrderSide.Buy, OrderType.ExchangeFillOrKill, "tBTCUSD", 1, price: 1, clientOrderId: 1234); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); Thread.Sleep(100); - await socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); + socket.InvokeMessage($"[0, \"n\", [0, \"on-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); var result = placeTask.Result; // assert @@ -712,9 +712,9 @@ public async Task PlacingAnFundingOffer_Should_SucceedIfSuccessResponse() // act var placeTask = client.SpotApi.SubmitFundingOfferAsync(FundingOfferType.Limit, "fUSD", 1, 1, 1); - await socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); + socket.InvokeMessage(new BitfinexResponse() { Event = "auth", Status = "OK" }); Thread.Sleep(100); - await socket.InvokeMessage($"[0, \"n\", [0, \"fon-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); + socket.InvokeMessage($"[0, \"n\", [0, \"fon-req\", 0, 0, {JsonConvert.SerializeObject(expected)}, 0, \"SUCCESS\", \"Submitted\"]]"); var result = placeTask.Result; // assert @@ -737,7 +737,7 @@ public async Task ReceivingAReconnectMessage_Should_ReconnectWebsocket() var rstEvent = new ManualResetEvent(false); var subTask = client.SpotApi.SubscribeToKlineUpdatesAsync("tBTCUSD", KlineInterval.FiveMinutes, data => { }); - await socket.InvokeMessage(new CandleSubscriptionResponse() + socket.InvokeMessage(new CandleSubscriptionResponse() { Channel = "candles", Event = "subscribed", @@ -749,9 +749,9 @@ await socket.InvokeMessage(new CandleSubscriptionResponse() subResult.Data.ConnectionRestored += (t) => rstEvent.Set(); // act - await socket.InvokeMessage("{\"event\":\"info\", \"code\": 20051}"); + socket.InvokeMessage("{\"event\":\"info\", \"code\": 20051}"); Thread.Sleep(100); - await socket.InvokeMessage(new CandleSubscriptionResponse() + socket.InvokeMessage(new CandleSubscriptionResponse() { Channel = "candles", Event = "subscribed", diff --git a/Bitfinex.Net.UnitTests/TestImplementations/TestSocket.cs b/Bitfinex.Net.UnitTests/TestImplementations/TestSocket.cs index 4495d57..90227a7 100644 --- a/Bitfinex.Net.UnitTests/TestImplementations/TestSocket.cs +++ b/Bitfinex.Net.UnitTests/TestImplementations/TestSocket.cs @@ -21,7 +21,7 @@ public class TestSocket: IWebsocket public event Func OnReconnecting; #pragma warning restore 0067 public event Func OnRequestSent; - public event Func OnStreamMessage; + public event Action> OnStreamMessage; public event Func OnError; public event Func OnOpen; @@ -100,16 +100,14 @@ public void InvokeOpen() OnOpen?.Invoke(); } - public async Task InvokeMessage(string data) + public void InvokeMessage(string data) { - var stream = new MemoryStream(Encoding.UTF8.GetBytes(data)); - await OnStreamMessage?.Invoke(WebSocketMessageType.Text, stream); + OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory(Encoding.UTF8.GetBytes(data))); } - public async Task InvokeMessage(T data) + public void InvokeMessage(T data) { - var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))); - await OnStreamMessage?.Invoke(WebSocketMessageType.Text, stream); + OnStreamMessage?.Invoke(WebSocketMessageType.Text, new ReadOnlyMemory(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)))); } public void InvokeError(Exception error) diff --git a/Bitfinex.Net/BitfinexAuthenticationProvider.cs b/Bitfinex.Net/BitfinexAuthenticationProvider.cs index 7b42186..adeb310 100644 --- a/Bitfinex.Net/BitfinexAuthenticationProvider.cs +++ b/Bitfinex.Net/BitfinexAuthenticationProvider.cs @@ -28,7 +28,7 @@ public BitfinexAuthenticationProvider(ApiCredentials credentials, INonceProvider _nonceProvider = nonceProvider ?? new BitfinexNonceProvider(); } - public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary uriParameters, out SortedDictionary bodyParameters, out Dictionary headers) + public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat, out SortedDictionary uriParameters, out SortedDictionary bodyParameters, out Dictionary headers) { uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary(providedParameters) : new SortedDictionary(); bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary(providedParameters) : new SortedDictionary(); From 5b42c7b6565d6a656c4384f070af675ac7294838 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 15 Mar 2024 14:37:04 +0100 Subject: [PATCH 06/10] Fixed test warnings --- .../BitfinexSocketClientTests.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs index 478b860..7f9310c 100644 --- a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs +++ b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs @@ -72,7 +72,7 @@ public async Task SubscribingToBookUpdates_Should_SubscribeSuccessfully(Precisio [TestCase(Precision.PrecisionLevel1, Frequency.TwoSeconds)] [TestCase(Precision.PrecisionLevel2, Frequency.TwoSeconds)] [TestCase(Precision.PrecisionLevel3, Frequency.TwoSeconds)] - public async Task SubscribingToBookUpdates_Should_TriggerWithBookUpdate(Precision prec, Frequency freq) + public void SubscribingToBookUpdates_Should_TriggerWithBookUpdate(Precision prec, Frequency freq) { // arrange var socket = new TestSocket(); @@ -115,7 +115,7 @@ public async Task SubscribingToBookUpdates_Should_TriggerWithBookUpdate(Precisio [TestCase(KlineInterval.OneDay)] [TestCase(KlineInterval.SevenDays)] [TestCase(KlineInterval.FourteenDays)] - public async Task SubscribingToCandleUpdates_Should_SubscribeSuccessfully(KlineInterval timeframe) + public void SubscribingToCandleUpdates_Should_SubscribeSuccessfully(KlineInterval timeframe) { // arrange var socket = new TestSocket(); @@ -152,7 +152,7 @@ public async Task SubscribingToCandleUpdates_Should_SubscribeSuccessfully(KlineI [TestCase(KlineInterval.OneDay)] [TestCase(KlineInterval.SevenDays)] [TestCase(KlineInterval.FourteenDays)] - public async Task SubscribingToCandleUpdates_Should_TriggerWithCandleUpdate(KlineInterval timeframe) + public void SubscribingToCandleUpdates_Should_TriggerWithCandleUpdate(KlineInterval timeframe) { // arrange var socket = new TestSocket(); @@ -181,7 +181,7 @@ public async Task SubscribingToCandleUpdates_Should_TriggerWithCandleUpdate(Klin } [Test] - public async Task SubscribingToTickerUpdates_Should_SubscribeSuccessfully() + public void SubscribingToTickerUpdates_Should_SubscribeSuccessfully() { // arrange var socket = new TestSocket(); @@ -209,7 +209,7 @@ public async Task SubscribingToTickerUpdates_Should_SubscribeSuccessfully() } [Test] - public async Task SubscribingToTickerUpdates_Should_TriggerWithTickerUpdate() + public void SubscribingToTickerUpdates_Should_TriggerWithTickerUpdate() { // arrange var socket = new TestSocket(); @@ -239,7 +239,7 @@ public async Task SubscribingToTickerUpdates_Should_TriggerWithTickerUpdate() } [Test] - public async Task SubscribingToRawBookUpdates_Should_SubscribeSuccessfully() + public void SubscribingToRawBookUpdates_Should_SubscribeSuccessfully() { // arrange var socket = new TestSocket(); @@ -270,7 +270,7 @@ public async Task SubscribingToRawBookUpdates_Should_SubscribeSuccessfully() } [Test] - public async Task SubscribingToRawBookUpdates_Should_TriggerWithRawBookUpdate() + public void SubscribingToRawBookUpdates_Should_TriggerWithRawBookUpdate() { // arrange var socket = new TestSocket(); @@ -303,7 +303,7 @@ public async Task SubscribingToRawBookUpdates_Should_TriggerWithRawBookUpdate() } [Test] - public async Task SubscribingToTradeUpdates_Should_SubscribeSuccessfully() + public void SubscribingToTradeUpdates_Should_SubscribeSuccessfully() { // arrange var socket = new TestSocket(); @@ -331,7 +331,7 @@ public async Task SubscribingToTradeUpdates_Should_SubscribeSuccessfully() } [Test] - public async Task SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() + public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() { // arrange var socket = new TestSocket(); @@ -561,7 +561,7 @@ public async Task SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() //} [Test] - public async Task PlacingAnOrder_Should_SucceedIfSuccessResponse() + public void PlacingAnOrder_Should_SucceedIfSuccessResponse() { // arrange var socket = new TestSocket(); @@ -591,7 +591,7 @@ public async Task PlacingAnOrder_Should_SucceedIfSuccessResponse() } [Test] - public async Task PlacingAnOrder_Should_FailIfErrorResponse() + public void PlacingAnOrder_Should_FailIfErrorResponse() { // arrange var socket = new TestSocket(); @@ -612,7 +612,7 @@ public async Task PlacingAnOrder_Should_FailIfErrorResponse() } [Test] - public async Task PlacingAnOrder_Should_FailIfNoResponse() + public void PlacingAnOrder_Should_FailIfNoResponse() { // arrange var socket = new TestSocket(); @@ -633,7 +633,7 @@ public async Task PlacingAnOrder_Should_FailIfNoResponse() } [Test] - public async Task PlacingAnMarketOrder_Should_SucceedIfSuccessResponse() + public void PlacingAnMarketOrder_Should_SucceedIfSuccessResponse() { // arrange var socket = new TestSocket(); @@ -663,7 +663,7 @@ public async Task PlacingAnMarketOrder_Should_SucceedIfSuccessResponse() } [Test] - public async Task PlacingAnFOKOrder_Should_SucceedIfSuccessResponse() + public void PlacingAnFOKOrder_Should_SucceedIfSuccessResponse() { // arrange var socket = new TestSocket(); @@ -693,7 +693,7 @@ public async Task PlacingAnFOKOrder_Should_SucceedIfSuccessResponse() } [Test] - public async Task PlacingAnFundingOffer_Should_SucceedIfSuccessResponse() + public void PlacingAnFundingOffer_Should_SucceedIfSuccessResponse() { // arrange var socket = new TestSocket(); From 38a9429c62d1ac9fc1a683c97d82bd5fcc566898 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 15 Mar 2024 21:53:30 +0100 Subject: [PATCH 07/10] Updated unit test packages --- .../Bitfinex.Net.UnitTests.csproj | 8 +-- Bitfinex.Net.UnitTests/BitfinexClientTests.cs | 15 ++--- .../BitfinexSocketClientTests.cs | 61 ++++++++++--------- .../JsonToObjectComparer.cs | 13 ++-- Bitfinex.Net/Bitfinex.Net.csproj | 2 +- 5 files changed, 52 insertions(+), 47 deletions(-) diff --git a/Bitfinex.Net.UnitTests/Bitfinex.Net.UnitTests.csproj b/Bitfinex.Net.UnitTests/Bitfinex.Net.UnitTests.csproj index 4857d6e..1e7d9ae 100644 --- a/Bitfinex.Net.UnitTests/Bitfinex.Net.UnitTests.csproj +++ b/Bitfinex.Net.UnitTests/Bitfinex.Net.UnitTests.csproj @@ -7,10 +7,10 @@ - - - - + + + + diff --git a/Bitfinex.Net.UnitTests/BitfinexClientTests.cs b/Bitfinex.Net.UnitTests/BitfinexClientTests.cs index 2e52db5..493107a 100644 --- a/Bitfinex.Net.UnitTests/BitfinexClientTests.cs +++ b/Bitfinex.Net.UnitTests/BitfinexClientTests.cs @@ -15,6 +15,7 @@ using Bitfinex.Net.Clients; using Bitfinex.Net.ExtensionMethods; using CryptoExchange.Net.Objects.Sockets; +using NUnit.Framework.Legacy; namespace Bitfinex.Net.UnitTests { @@ -31,9 +32,9 @@ public async Task ReceivingHttpError_Should_ResultInErrorResult() var result = await client.SpotApi.ExchangeData.GetAssetFullNamesAsync(); // assert - Assert.AreEqual(false, result.Success); - Assert.IsTrue(result.Error.ToString().Contains("Error message")); - Assert.AreEqual(HttpStatusCode.BadRequest, result.ResponseStatusCode); + Assert.ReferenceEquals(false, result.Success); + Assert.That(result.Error.ToString().Contains("Error message")); + Assert.ReferenceEquals(HttpStatusCode.BadRequest, result.ResponseStatusCode); } @@ -45,7 +46,7 @@ public void ProvidingApiCredentials_Should_SaveApiCredentials() var authProvider = new BitfinexAuthenticationProvider(new ApiCredentials("TestKey", "TestSecret"), null); // assert - Assert.AreEqual(authProvider.GetApiKey(), "TestKey"); + Assert.ReferenceEquals(authProvider.GetApiKey(), "TestKey"); } [Test] @@ -58,7 +59,7 @@ public void SigningString_Should_ReturnCorrectString() string signed = authProvider.Sign("SomeTestString"); // assert - Assert.AreEqual(signed, "9052C73092B21B945BC5859CADBA6A5658E142F021FCB092A72F68E8A0D5E6351CFEBAE52DB9067D4360F796CB520960"); + Assert.ReferenceEquals(signed, "9052C73092B21B945BC5859CADBA6A5658E142F021FCB092A72F68E8A0D5E6351CFEBAE52DB9067D4360F796CB520960"); } [Test] @@ -112,7 +113,7 @@ public void CheckRestInterfaces() foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task)))) { var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray()); - Assert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}"); + ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}"); methods++; } Debug.WriteLine($"{clientInterface.Name} {methods} methods validated"); @@ -132,7 +133,7 @@ public void CheckSocketInterfaces() foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task>)))) { var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray()); - Assert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}"); + ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}"); methods++; } Debug.WriteLine($"{clientInterface.Name} {methods} methods validated"); diff --git a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs index 7f9310c..3f889c1 100644 --- a/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs +++ b/Bitfinex.Net.UnitTests/BitfinexSocketClientTests.cs @@ -19,6 +19,7 @@ using Bitfinex.Net.Objects.Models.Socket; using System.Diagnostics; using Bitfinex.Net.Objects.Sockets; +using NUnit.Framework.Legacy; namespace Bitfinex.Net.UnitTests { @@ -61,7 +62,7 @@ public async Task SubscribingToBookUpdates_Should_SubscribeSuccessfully(Precisio var taskResult = await subTask.ConfigureAwait(false); // assert - Assert.IsTrue(taskResult.Success); + Assert.That(taskResult.Success); } [TestCase(Precision.PrecisionLevel0, Frequency.Realtime)] @@ -101,7 +102,7 @@ public void SubscribingToBookUpdates_Should_TriggerWithBookUpdate(Precision prec socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert - Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); + Assert.That(TestHelpers.AreEqual(result[0], expected[0])); } [TestCase(KlineInterval.OneMinute)] @@ -138,7 +139,7 @@ public void SubscribingToCandleUpdates_Should_SubscribeSuccessfully(KlineInterva subTask.Wait(5000); // assert - Assert.IsTrue(subTask.Result.Success); + Assert.That(subTask.Result.Success); } [TestCase(KlineInterval.OneMinute)] @@ -177,7 +178,7 @@ public void SubscribingToCandleUpdates_Should_TriggerWithCandleUpdate(KlineInter socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert - Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); + Assert.That(TestHelpers.AreEqual(result[0], expected[0])); } [Test] @@ -205,7 +206,7 @@ public void SubscribingToTickerUpdates_Should_SubscribeSuccessfully() subTask.Wait(5000); // assert - Assert.IsTrue(subTask.Result.Success); + Assert.That(subTask.Result.Success); } [Test] @@ -235,7 +236,7 @@ public void SubscribingToTickerUpdates_Should_TriggerWithTickerUpdate() socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert - Assert.IsTrue(TestHelpers.AreEqual(result, expected)); + Assert.That(TestHelpers.AreEqual(result, expected)); } [Test] @@ -266,7 +267,7 @@ public void SubscribingToRawBookUpdates_Should_SubscribeSuccessfully() subTask.Wait(5000); // assert - Assert.IsTrue(subTask.Result.Success); + Assert.That(subTask.Result.Success); } [Test] @@ -299,7 +300,7 @@ public void SubscribingToRawBookUpdates_Should_TriggerWithRawBookUpdate() socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert - Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); + Assert.That(TestHelpers.AreEqual(result[0], expected[0])); } [Test] @@ -327,7 +328,7 @@ public void SubscribingToTradeUpdates_Should_SubscribeSuccessfully() subTask.Wait(5000); // assert - Assert.IsTrue(subTask.Result.Success); + Assert.That(subTask.Result.Success); } [Test] @@ -357,7 +358,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() socket.InvokeMessage($"[1, {JsonConvert.SerializeObject(expected)}]"); // assert - Assert.IsTrue(TestHelpers.AreEqual(result[0], expected[0])); + Assert.That(TestHelpers.AreEqual(result[0], expected[0])); } //[TestCase("ou", BitfinexEventType.OrderUpdate)] @@ -386,7 +387,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); //} //[TestCase("te", BitfinexEventType.TradeExecuted)] @@ -414,7 +415,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); //} //[TestCase("ws", BitfinexEventType.WalletSnapshot, false)] @@ -441,7 +442,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data.First())); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data.First())); //} //[TestCase("pn", BitfinexEventType.PositionNew)] @@ -470,7 +471,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data.First())); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data.First())); //} //[TestCase("fcn", BitfinexEventType.FundingCreditsNew)] @@ -499,7 +500,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); //} //[TestCase("fln", BitfinexEventType.FundingLoanNew)] @@ -528,7 +529,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); //} //[TestCase("fon", BitfinexEventType.FundingOfferNew)] @@ -557,7 +558,7 @@ public void SubscribingToTradeUpdates_Should_TriggerWithTradeUpdate() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); + // Assert.That(TestHelpers.AreEqual(result.Data.First(), expected.Data[0])); //} [Test] @@ -586,8 +587,8 @@ public void PlacingAnOrder_Should_SucceedIfSuccessResponse() var result = placeTask.Result; // assert - Assert.IsTrue(result.Success); - Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data)); + Assert.That(result.Success); + Assert.That(TestHelpers.AreEqual(expected, result.Data)); } [Test] @@ -607,8 +608,8 @@ public void PlacingAnOrder_Should_FailIfErrorResponse() var result = placeTask.Result; // assert - Assert.IsFalse(result.Success); - Assert.IsTrue(result.Error.Message.Contains("order placing failed")); + ClassicAssert.IsFalse(result.Success); + Assert.That(result.Error.Message.Contains("order placing failed")); } [Test] @@ -629,7 +630,7 @@ public void PlacingAnOrder_Should_FailIfNoResponse() var result = placeTask.Result; // assert - Assert.IsFalse(result.Success); + ClassicAssert.IsFalse(result.Success); } [Test] @@ -658,8 +659,8 @@ public void PlacingAnMarketOrder_Should_SucceedIfSuccessResponse() var result = placeTask.Result; // assert - Assert.IsTrue(result.Success); - Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data)); + Assert.That(result.Success); + Assert.That(TestHelpers.AreEqual(expected, result.Data)); } [Test] @@ -688,8 +689,8 @@ public void PlacingAnFOKOrder_Should_SucceedIfSuccessResponse() var result = placeTask.Result; // assert - Assert.IsTrue(result.Success); - Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data)); + Assert.That(result.Success); + Assert.That(TestHelpers.AreEqual(expected, result.Data)); } [Test] @@ -718,8 +719,8 @@ public void PlacingAnFundingOffer_Should_SucceedIfSuccessResponse() var result = placeTask.Result; // assert - Assert.IsTrue(result.Success); - Assert.IsTrue(TestHelpers.AreEqual(expected, result.Data)); + Assert.That(result.Success); + Assert.That(TestHelpers.AreEqual(expected, result.Data)); } @@ -762,7 +763,7 @@ public async Task ReceivingAReconnectMessage_Should_ReconnectWebsocket() var triggered = rstEvent.WaitOne(1000); // assert - Assert.IsTrue(triggered); + Assert.That(triggered); } // TODO FIX TIMING ISSUE @@ -811,7 +812,7 @@ public async Task ReceivingAReconnectMessage_Should_ReconnectWebsocket() // rstEvent.WaitOne(1000); // // assert - // Assert.IsTrue(subResultWhenPaused?.Error?.Message.Contains("Socket is paused")); + // Assert.That(subResultWhenPaused?.Error?.Message.Contains("Socket is paused")); //} } } diff --git a/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs b/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs index 1f1b98b..995324d 100644 --- a/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs +++ b/Bitfinex.Net.UnitTests/JsonToObjectComparer.cs @@ -5,6 +5,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NUnit.Framework; +using NUnit.Framework.Legacy; using System; using System.Collections; using System.Collections.Generic; @@ -83,7 +84,7 @@ public async Task ProcessSubject( var result = (CallResult)await TestHelpers.InvokeAsync(method, getSubject(client), input.ToArray()); // asset - Assert.Null(result.Error, method.Name); + ClassicAssert.Null(result.Error, method.Name); var resultData = result.GetType().GetProperty("Data", BindingFlags.Public | BindingFlags.Instance).GetValue(result); ProcessData(method.Name, resultData, json, parametersToSetNull, useNestedJsonPropertyForCompare, ignoreProperties, takeFirstItemForCompare); @@ -217,10 +218,8 @@ private static void CheckObject(string method, JProperty prop, object obj, Dicti // Property has a value var property = resultProperties.SingleOrDefault(p => p.Item2?.PropertyName == prop.Name).p; - if (property is null) - property = resultProperties.SingleOrDefault(p => p.p.Name == prop.Name).p; - if (property is null) - property = resultProperties.SingleOrDefault(p => p.p.Name.ToUpperInvariant() == prop.Name.ToUpperInvariant()).p; + property ??= resultProperties.SingleOrDefault(p => p.p.Name == prop.Name).p; + property ??= resultProperties.SingleOrDefault(p => p.p.Name.ToUpperInvariant() == prop.Name.ToUpperInvariant()).p; if (property is null) { @@ -332,7 +331,9 @@ private static void CheckPropertyValue(string method, JToken propValue, object p { if (info.GetCustomAttribute(true) == null && info.GetCustomAttribute(true)?.ItemConverterType == null) + { CheckValues(method, propertyName, (JValue)propValue, propertyValue); + } } } } @@ -371,7 +372,9 @@ private static void CheckValues(string method, string property, JValue jsonValue // timestamp, hard to check.. } else if (jsonValue.Value().ToLowerInvariant() != objectValue.ToString().ToLowerInvariant()) + { throw new Exception($"{method}: {property} not equal: {jsonValue.Value()} vs {objectValue.ToString()}"); + } } else if (jsonValue.Type == JTokenType.Integer) { diff --git a/Bitfinex.Net/Bitfinex.Net.csproj b/Bitfinex.Net/Bitfinex.Net.csproj index 88fb7bd..7947e44 100644 --- a/Bitfinex.Net/Bitfinex.Net.csproj +++ b/Bitfinex.Net/Bitfinex.Net.csproj @@ -47,7 +47,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From bdb6f394e32d453106143fd19ead333716b46b49 Mon Sep 17 00:00:00 2001 From: JKorf Date: Fri, 15 Mar 2024 22:15:16 +0100 Subject: [PATCH 08/10] Tests fixes --- Bitfinex.Net.UnitTests/BitfinexClientTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Bitfinex.Net.UnitTests/BitfinexClientTests.cs b/Bitfinex.Net.UnitTests/BitfinexClientTests.cs index 493107a..67527f0 100644 --- a/Bitfinex.Net.UnitTests/BitfinexClientTests.cs +++ b/Bitfinex.Net.UnitTests/BitfinexClientTests.cs @@ -32,9 +32,9 @@ public async Task ReceivingHttpError_Should_ResultInErrorResult() var result = await client.SpotApi.ExchangeData.GetAssetFullNamesAsync(); // assert - Assert.ReferenceEquals(false, result.Success); + Assert.That(false == result.Success); Assert.That(result.Error.ToString().Contains("Error message")); - Assert.ReferenceEquals(HttpStatusCode.BadRequest, result.ResponseStatusCode); + Assert.That(HttpStatusCode.BadRequest == result.ResponseStatusCode); } @@ -46,7 +46,7 @@ public void ProvidingApiCredentials_Should_SaveApiCredentials() var authProvider = new BitfinexAuthenticationProvider(new ApiCredentials("TestKey", "TestSecret"), null); // assert - Assert.ReferenceEquals(authProvider.GetApiKey(), "TestKey"); + Assert.That(authProvider.GetApiKey() == "TestKey"); } [Test] @@ -59,7 +59,7 @@ public void SigningString_Should_ReturnCorrectString() string signed = authProvider.Sign("SomeTestString"); // assert - Assert.ReferenceEquals(signed, "9052C73092B21B945BC5859CADBA6A5658E142F021FCB092A72F68E8A0D5E6351CFEBAE52DB9067D4360F796CB520960"); + Assert.That(signed == "9052C73092B21B945BC5859CADBA6A5658E142F021FCB092A72F68E8A0D5E6351CFEBAE52DB9067D4360F796CB520960"); } [Test] From 7b1b81a9cde983a260c2d832f26c239687a7528c Mon Sep 17 00:00:00 2001 From: JKorf Date: Sat, 16 Mar 2024 15:43:13 +0100 Subject: [PATCH 09/10] Updated CryptoExchange.Net --- Bitfinex.Net/Bitfinex.Net.csproj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Bitfinex.Net/Bitfinex.Net.csproj b/Bitfinex.Net/Bitfinex.Net.csproj index 7947e44..1983fc9 100644 --- a/Bitfinex.Net/Bitfinex.Net.csproj +++ b/Bitfinex.Net/Bitfinex.Net.csproj @@ -55,8 +55,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + \ No newline at end of file From f470a7c2a038b9dbfdf61b0fae1ca5b397ed2a41 Mon Sep 17 00:00:00 2001 From: JKorf Date: Sat, 16 Mar 2024 15:49:17 +0100 Subject: [PATCH 10/10] Updated version --- Bitfinex.Net/Bitfinex.Net.csproj | 6 +++--- README.md | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Bitfinex.Net/Bitfinex.Net.csproj b/Bitfinex.Net/Bitfinex.Net.csproj index 1983fc9..f0a2b6a 100644 --- a/Bitfinex.Net/Bitfinex.Net.csproj +++ b/Bitfinex.Net/Bitfinex.Net.csproj @@ -7,9 +7,9 @@ Bitfinex.Net JKorf - 7.1.0 - 7.1.0 - 7.1.0 + 7.2.0 + 7.2.0 + 7.2.0 Bitfinex.Net is a client library for accessing the Bitfinex REST and Websocket API. All data is mapped to readable models and enum values. Additional features include an implementation for maintaining a client side order book, easy integration with other exchange client libraries and more. false Bitfinex;Bitfinex.Net;Bitfinex Client;Bitfinex API;CryptoCurrency;CryptoCurrency Exchange diff --git a/README.md b/README.md index 00649ce..49bee99 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,10 @@ 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 7.2.0 - 16 Mar 2024 + * Updated CryptoExchange.Net to 7.1.0, see https://github.com/JKorf/CryptoExchange.Net?tab=readme-ov-file#release-notes for release notes + * Updated unit test package dependencies and updated tests accordingly + * Version 7.1.0 - 25 Feb 2024 * Updated CryptoExchange.Net and implemented reworked websocket message handling. For release notes for the CryptoExchange.Net base library see: https://github.com/JKorf/CryptoExchange.Net?tab=readme-ov-file#release-notes * Combined multiple private websocket subscriptions into single subscription