1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 16:36:15 +00:00

Fixed api credentials getting disposed, fixed DateTimeConverter losing precision

This commit is contained in:
Jkorf 2021-12-13 12:57:31 +01:00
parent 5c665ad54c
commit 8ba0ded16d
3 changed files with 19 additions and 12 deletions

View File

@ -25,6 +25,8 @@ namespace CryptoExchange.Net
{ {
private readonly ApiCredentials? _apiCredentials; private readonly ApiCredentials? _apiCredentials;
private AuthenticationProvider? _authenticationProvider; private AuthenticationProvider? _authenticationProvider;
private bool _created;
/// <summary> /// <summary>
/// The authentication provider for this API client. (null if no credentials are set) /// The authentication provider for this API client. (null if no credentials are set)
/// </summary> /// </summary>
@ -32,8 +34,11 @@ namespace CryptoExchange.Net
{ {
get get
{ {
if (_authenticationProvider == null && _apiCredentials != null) if (!_created && _apiCredentials != null)
{
_authenticationProvider = CreateAuthenticationProvider(_apiCredentials); _authenticationProvider = CreateAuthenticationProvider(_apiCredentials);
_created = true;
}
return _authenticationProvider; return _authenticationProvider;
} }
@ -51,7 +56,7 @@ namespace CryptoExchange.Net
/// <param name="apiOptions">Api client options</param> /// <param name="apiOptions">Api client options</param>
protected BaseApiClient(BaseClientOptions options, ApiClientOptions apiOptions) protected BaseApiClient(BaseClientOptions options, ApiClientOptions apiOptions)
{ {
_apiCredentials = apiOptions.ApiCredentials ?? options.ApiCredentials; _apiCredentials = apiOptions.ApiCredentials?.Copy() ?? options.ApiCredentials?.Copy();
BaseAddress = apiOptions.BaseAddress; BaseAddress = apiOptions.BaseAddress;
} }

View File

@ -12,6 +12,7 @@ namespace CryptoExchange.Net.Converters
public class DateTimeConverter: JsonConverter public class DateTimeConverter: JsonConverter
{ {
private static readonly DateTime _epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private static readonly DateTime _epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private const long ticksPerSecond = TimeSpan.TicksPerMillisecond * 1000;
private const decimal ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000; private const decimal ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
private const decimal ticksPerNanosecond = TimeSpan.TicksPerMillisecond / 1000m / 1000; private const decimal ticksPerNanosecond = TimeSpan.TicksPerMillisecond / 1000m / 1000;
@ -127,7 +128,7 @@ namespace CryptoExchange.Net.Converters
/// </summary> /// </summary>
/// <param name="seconds"></param> /// <param name="seconds"></param>
/// <returns></returns> /// <returns></returns>
public static DateTime ConvertFromSeconds(double seconds) => _epoch.AddSeconds(seconds); public static DateTime ConvertFromSeconds(double seconds) => _epoch.AddTicks((long)Math.Round(seconds * ticksPerSecond));
/// <summary> /// <summary>
/// Convert a milliseconds since epoch (01-01-1970) value to DateTime /// Convert a milliseconds since epoch (01-01-1970) value to DateTime
/// </summary> /// </summary>

View File

@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Interfaces;
@ -493,15 +494,15 @@ namespace CryptoExchange.Net.OrderBook
/// <returns></returns> /// <returns></returns>
public string ToString(int numberOfEntries) public string ToString(int numberOfEntries)
{ {
var result = string.Empty; var stringBuilder = new StringBuilder();
result += $"Asks ({AskCount}): {Environment.NewLine}"; stringBuilder.AppendLine($" Ask quantity Ask price | Bid price Bid quantity");
foreach (var entry in Asks.Take(numberOfEntries).Reverse()) for(var i = 0; i < numberOfEntries; i++)
result += $" {entry.Price.ToString(CultureInfo.InvariantCulture).PadLeft(8)} | {entry.Quantity.ToString(CultureInfo.InvariantCulture).PadRight(8)}{Environment.NewLine}"; {
var ask = asks.Count > i ? asks.ElementAt(i).Value: null;
result += $"Bids ({BidCount}): {Environment.NewLine}"; var bid = bids.Count > i ? bids.ElementAt(i).Value: null;
foreach (var entry in Bids.Take(numberOfEntries)) stringBuilder.AppendLine($"[{ask?.Quantity.ToString(CultureInfo.InvariantCulture),14}] {ask?.Price.ToString(CultureInfo.InvariantCulture),14} | {bid?.Price.ToString(CultureInfo.InvariantCulture),-14} [{bid?.Quantity.ToString(CultureInfo.InvariantCulture),-14}]");
result += $" {entry.Price.ToString(CultureInfo.InvariantCulture).PadLeft(8)} | {entry.Quantity.ToString(CultureInfo.InvariantCulture).PadRight(8)}{Environment.NewLine}"; }
return result; return stringBuilder.ToString();
} }
private void CheckBestOffersChanged(ISymbolOrderBookEntry prevBestBid, ISymbolOrderBookEntry prevBestAsk) private void CheckBestOffersChanged(ISymbolOrderBookEntry prevBestBid, ISymbolOrderBookEntry prevBestAsk)