Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a custom NextFundingRateConverter that adds the offset to the current time instead of unix epoch #212

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
}
Loading