Skip to content

Commit

Permalink
Added a custom NextFundingRateConverter that adds the offset to the c…
Browse files Browse the repository at this point in the history
…urrent time instead (#212)
  • Loading branch information
Wtiben authored Apr 17, 2024
1 parent bf2fa2a commit f87758c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
41 changes: 41 additions & 0 deletions Kucoin.Net/Converters/NextFundingRateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Newtonsoft.Json;

namespace Kucoin.Net.Converters
{
/// <summary>
/// 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.
/// </summary>
internal class NextFundingRateTimeConverter : JsonConverter<DateTime?>
{
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");
}
}
}
11 changes: 10 additions & 1 deletion Kucoin.Net/Kucoin.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Kucoin.Net/Objects/Models/Futures/KucoinContract.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -206,9 +207,11 @@ public class KucoinContract
/// </summary>
public decimal? PredictedFundingFeeRate { get; set; }
/// <summary>
/// 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.
/// </summary>
[JsonConverter(typeof(DateTimeConverter))]
[JsonConverter(typeof(NextFundingRateTimeConverter))]
public DateTime? NextFundingRateTime { get; set; }
/// <summary>
/// Source exchanges
Expand Down Expand Up @@ -243,4 +246,4 @@ public class KucoinContract
/// </summary>
public string? FundingQuoteSymbol1M { get; set; }
}
}
}

0 comments on commit f87758c

Please sign in to comment.