From dd9f7a6310015e0a63eb6c92b8582e317d7caed7 Mon Sep 17 00:00:00 2001 From: Wouter Date: Sat, 3 Feb 2024 16:52:17 +0100 Subject: [PATCH] Added a custom NextFundingRateConverter that adds the offset to the current time instead --- .../NextFundingRateTimeConverter.cs | 41 +++++++++++++++++++ Kucoin.Net/Kucoin.Net.xml | 11 ++++- .../Objects/Models/Futures/KucoinContract.cs | 9 ++-- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Kucoin.Net/Converters/NextFundingRateTimeConverter.cs diff --git a/Kucoin.Net/Converters/NextFundingRateTimeConverter.cs b/Kucoin.Net/Converters/NextFundingRateTimeConverter.cs new file mode 100644 index 00000000..4271ef21 --- /dev/null +++ b/Kucoin.Net/Converters/NextFundingRateTimeConverter.cs @@ -0,0 +1,41 @@ +using System; +using Newtonsoft.Json; + +namespace Kucoin.Net.Converters +{ + /// + /// Does basically the same thing as the shared DateTimeConverter, with a slight difference. + /// Instead of adding the time to Jan 01 1970, it adds it to the current time. This isn't ideal since + /// it produces inaccuracies, but it's the format the kucoin api returns for the next funding rate time. + /// + internal class NextFundingRateTimeConverter : JsonConverter + { + public override DateTime? ReadJson( + JsonReader reader, + Type objectType, + DateTime? existingValue, + bool hasExistingValue, + JsonSerializer serializer + ) + { + if (reader.Value == null) + { + return null; + } + + var value = (long)reader.Value; + var now = DateTime.UtcNow; + var offset = TimeSpan.FromMilliseconds(value); + return now + offset; + } + + public override void WriteJson( + JsonWriter writer, + DateTime? value, + JsonSerializer serializer + ) + { + throw new NotSupportedException("This converter only supports reading"); + } + } +} diff --git a/Kucoin.Net/Kucoin.Net.xml b/Kucoin.Net/Kucoin.Net.xml index 511e0d15..fffe8a56 100644 --- a/Kucoin.Net/Kucoin.Net.xml +++ b/Kucoin.Net/Kucoin.Net.xml @@ -739,6 +739,13 @@ + + + Does basically the same thing as the shared DateTimeConverter, with a slight difference. + Instead of adding the time to Jan 01 1970, it adds it to the current time. This isn't ideal since + it produces inaccuracies, but it's the format the kucoin api returns for the next funding rate time. + + Direction @@ -4254,7 +4261,9 @@ - Next funding rate time + Next funding rate time. This time may not be accurate up to a couple of seconds. + This is due to the fact that the API returns this value as an offset from the current time, + but we have no way of knowing the exact time the API returned this value. diff --git a/Kucoin.Net/Objects/Models/Futures/KucoinContract.cs b/Kucoin.Net/Objects/Models/Futures/KucoinContract.cs index 14eca7f8..55a4fd74 100644 --- a/Kucoin.Net/Objects/Models/Futures/KucoinContract.cs +++ b/Kucoin.Net/Objects/Models/Futures/KucoinContract.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using CryptoExchange.Net.Converters; +using Kucoin.Net.Converters; using Newtonsoft.Json; namespace Kucoin.Net.Objects.Models.Futures @@ -206,9 +207,11 @@ public class KucoinContract /// public decimal? PredictedFundingFeeRate { get; set; } /// - /// Next funding rate time + /// Next funding rate time. This time may not be accurate up to a couple of seconds. + /// This is due to the fact that the API returns this value as an offset from the current time, + /// but we have no way of knowing the exact time the API returned this value. /// - [JsonConverter(typeof(DateTimeConverter))] + [JsonConverter(typeof(NextFundingRateTimeConverter))] public DateTime? NextFundingRateTime { get; set; } /// /// Source exchanges @@ -243,4 +246,4 @@ public class KucoinContract /// public string? FundingQuoteSymbol1M { get; set; } } -} +} \ No newline at end of file