Skip to content

Commit

Permalink
Common endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Apr 5, 2024
1 parent 16383dc commit 0eda275
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 7 deletions.
11 changes: 9 additions & 2 deletions CoinEx.Net/Clients/FuturesApi/CoinExRestClientFuturesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace CoinEx.Net.Clients.FuturesApi
public class CoinExRestClientFuturesApi : RestApiClient, ICoinExRestClientFuturesApi
{
#region fields
internal TimeSyncState _timeSyncState = new TimeSyncState("CoinEx V2 API");

/// <inheritdoc />
public new CoinExRestOptions ClientOptions => (CoinExRestOptions)base.ClientOptions;
#endregion
Expand Down Expand Up @@ -132,9 +134,14 @@ internal async Task<WebCallResult<CoinExPaginated<T>>> ExecutePaginatedAsync<T>(
}

/// <inheritdoc />
public override TimeSyncInfo? GetTimeSyncInfo() => null;
protected override async Task<WebCallResult<DateTime>> GetServerTimestampAsync() => await ExchangeData.GetServerTimeAsync().ConfigureAwait(false);

/// <inheritdoc />
public override TimeSyncInfo? GetTimeSyncInfo()
=> new TimeSyncInfo(_logger, (ApiOptions.AutoTimestamp ?? ClientOptions.AutoTimestamp), (ApiOptions.TimestampRecalculationInterval ?? ClientOptions.TimestampRecalculationInterval), _timeSyncState);

/// <inheritdoc />
public override TimeSpan? GetTimeOffset() => null;
public override TimeSpan? GetTimeOffset()
=> _timeSyncState.TimeOffset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ internal CoinExRestClientFuturesApiExchangeData(CoinExRestClientFuturesApi baseC
_baseClient = baseClient;
}

/// <inheritdoc />
public async Task<WebCallResult<DateTime>> GetServerTimeAsync(CancellationToken ct = default)
{
var result = await _baseClient.ExecuteAsync<CoinExServerTime>(_baseClient.GetUri("v2/time"), HttpMethod.Get, ct).ConfigureAwait(false);
return result.As(result.Data?.ServerTime ?? default);
}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExFuturesSymbol>>> GetSymbolsAsync(IEnumerable<string>? symbols = null, CancellationToken ct = default)
{
Expand Down
11 changes: 9 additions & 2 deletions CoinEx.Net/Clients/SpotApiV2/CoinExRestClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace CoinEx.Net.Clients.SpotApiV2
public class CoinExRestClientSpotApi : RestApiClient, ICoinExRestClientSpotApi, ISpotClient
{
#region fields
internal TimeSyncState _timeSyncState = new TimeSyncState("CoinEx V2 API");

/// <inheritdoc />
public new CoinExRestOptions ClientOptions => (CoinExRestOptions)base.ClientOptions;

Expand Down Expand Up @@ -459,10 +461,15 @@ private static KlineInterval GetKlineIntervalFromTimespan(TimeSpan timeSpan)
}

/// <inheritdoc />
public override TimeSyncInfo? GetTimeSyncInfo() => null;
protected override async Task<WebCallResult<DateTime>> GetServerTimestampAsync() => await ExchangeData.GetServerTimeAsync().ConfigureAwait(false);

/// <inheritdoc />
public override TimeSyncInfo? GetTimeSyncInfo()
=> new TimeSyncInfo(_logger, (ApiOptions.AutoTimestamp ?? ClientOptions.AutoTimestamp), (ApiOptions.TimestampRecalculationInterval ?? ClientOptions.TimestampRecalculationInterval), _timeSyncState);

/// <inheritdoc />
public override TimeSpan? GetTimeOffset() => null;
public override TimeSpan? GetTimeOffset()
=> _timeSyncState.TimeOffset;

/// <inheritdoc />
public ISpotClient CommonSpotClient => this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CoinEx.Net.Objects.Models.V2;
using CoinEx.Net.Enums;
using CoinEx.Net.Interfaces.Clients.SpotApiV2;
using System;

namespace CoinEx.Net.Clients.SpotApiV2
{
Expand All @@ -20,6 +21,20 @@ internal CoinExRestClientSpotApiExchangeData(CoinExRestClientSpotApi baseClient)
}


/// <inheritdoc />
public async Task<WebCallResult<DateTime>> GetServerTimeAsync(CancellationToken ct = default)
{
var result = await _baseClient.ExecuteAsync<CoinExServerTime>(_baseClient.GetUri("v2/time"), HttpMethod.Get, ct).ConfigureAwait(false);
return result.As(result.Data?.ServerTime ?? default);
}

// Doesn't seem to exist on the url specified in the docs
///// <inheritdoc />
//public async Task<WebCallResult<IEnumerable<CoinExMaintenance>>> GetMaintenanceInfoAsync(CancellationToken ct = default)
//{
// return await _baseClient.ExecuteAsync<IEnumerable<CoinExMaintenance>>(_baseClient.GetUri("v2/maintain-info"), HttpMethod.Get, ct).ConfigureAwait(false);
//}

/// <inheritdoc />
public async Task<WebCallResult<IEnumerable<CoinExSymbol>>> GetSymbolsAsync(CancellationToken ct = default)
{
Expand Down
10 changes: 10 additions & 0 deletions CoinEx.Net/Clients/SpotApiV2/CoinExSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public override ReadOnlyMemory<byte> PreprocessStreamMessage(WebSocketMessageTyp

#region public

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToSystemNoticeUpdatesAsync(Action<DataEvent<IEnumerable<CoinExMaintenance>>> onMessage, CancellationToken ct = default)
{
var subscription = new CoinExSubscription<IEnumerable<CoinExMaintenance>>(_logger, "notice", null, new Dictionary<string, object>
{
{ "channels", new object[] { 101 } }
}, onMessage, true);
return await SubscribeAsync(BaseAddress.AppendPath("v2/spot"), subscription, ct).ConfigureAwait(false);
}

/// <inheritdoc />
public async Task<CallResult<UpdateSubscription>> SubscribeToTickerUpdatesAsync(Action<DataEvent<IEnumerable<CoinExTicker>>> onMessage, CancellationToken ct = default)
{
Expand Down
4 changes: 1 addition & 3 deletions CoinEx.Net/CoinEx.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="CryptoExchange.Net" Version="7.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CryptoExchange.Net\CryptoExchange.Net\CryptoExchange.Net.csproj" />
</ItemGroup>
</Project>
69 changes: 69 additions & 0 deletions CoinEx.Net/CoinEx.Net.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ namespace CoinEx.Net.Interfaces.Clients.FuturesApi
/// </summary>
public interface ICoinExRestClientFuturesApiExchangeData
{
/// <summary>
/// Get server time
/// <para><a href="https://docs.coinex.com/api/v2/common/http/time" /></para>
/// </summary>
/// <param name="ct">Cancelation token</param>
/// <returns></returns>
Task<WebCallResult<DateTime>> GetServerTimeAsync(CancellationToken ct = default);

/// <summary>
/// Get list of support symbols
/// <para><a href="https://docs.coinex.com/api/v2/futures/market/http/list-market" /></para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using CoinEx.Net.Objects.Models.V2;
using System;

namespace CoinEx.Net.Interfaces.Clients.SpotApiV2
{
Expand All @@ -12,6 +13,23 @@ namespace CoinEx.Net.Interfaces.Clients.SpotApiV2
/// </summary>
public interface ICoinExRestClientSpotApiExchangeData
{
/// <summary>
/// Get server time
/// </summary>
/// <para><a href="https://docs.coinex.com/api/v2/common/http/time" /></para>
/// <param name="ct">Cancelation token</param>
/// <returns></returns>
Task<WebCallResult<DateTime>> GetServerTimeAsync(CancellationToken ct = default);

// Doesn't seem to exist on the url specified in the docs
///// <summary>
///// Get maintenance info
///// </summary>
///// <para><a href="https://docs.coinex.com/api/v2/common/http/maintain" /></para>
///// <param name="ct">Cancelation token</param>
///// <returns></returns>
//Task<WebCallResult<IEnumerable<CoinExMaintenance>>> GetMaintenanceInfoAsync(CancellationToken ct = default);

/// <summary>
/// Get symbol information
/// <para><a href="https://docs.coinex.com/api/v2/spot/market/http/list-market" /></para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ namespace CoinEx.Net.Interfaces.Clients.SpotApiV2
/// </summary>
public interface ICoinExSocketClientSpotApi : ISocketApiClient, IDisposable
{
/// <summary>
/// Subscribe to system notification updates
/// </summary>
/// <param name="onMessage">Data handler</param>
/// <param name="ct">Cancellation token for closing this subscription</param>
/// <returns>A stream subscription. This stream subscription can be used to be notified when the socket is disconnected/reconnected</returns>
Task<CallResult<UpdateSubscription>> SubscribeToSystemNoticeUpdatesAsync(Action<DataEvent<IEnumerable<CoinExMaintenance>>> onMessage, CancellationToken ct = default);

/// <summary>
/// Subscribe to symbol ticker updates for the specified symbols. Note that only one ticker subscription can be active at the same time; new ticker subscription will override the old subscriptions.
/// <para><a href="https://docs.coinex.com/api/v2/spot/market/ws/market" /></para>
Expand Down
38 changes: 38 additions & 0 deletions CoinEx.Net/Objects/Models/V2/CoinExMaintenance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace CoinEx.Net.Objects.Models.V2
{
/// <summary>
/// Maintenance info
/// </summary>
public record CoinExMaintenance
{
/// <summary>
/// Start time of the maintenance
/// </summary>
[JsonPropertyName("started_at")]
public DateTime? StartTime { get; set; }
/// <summary>
/// End time of the maintenance
/// </summary>
[JsonPropertyName("ended_at")]
public DateTime? EndTime { get; set; }
/// <summary>
/// Scope that's impacted
/// </summary>
[JsonPropertyName("scope")]
public IEnumerable<string> Scope { get; set; } = Array.Empty<string>();
/// <summary>
/// Protection period start time. The protection period refers to a continuous period following the system maintenance (It's an optional configuration, and may or may not be set). During the protection period, you can cancel orders, place orders (limited to Maker Only Limit Orders), and adjust (add or reduce) margins.
/// </summary>
[JsonPropertyName("protect_duration_start")]
public DateTime? ProtectDurationStart { get; set; }
/// <summary>
/// Protection period end time. The protection period refers to a continuous period following the system maintenance (It's an optional configuration, and may or may not be set). During the protection period, you can cancel orders, place orders (limited to Maker Only Limit Orders), and adjust (add or reduce) margins.
/// </summary>
[JsonPropertyName("protect_duration_end")]
public DateTime? ProtectDurationEnd { get; set; }
}
}
13 changes: 13 additions & 0 deletions CoinEx.Net/Objects/Models/V2/CoinExServerTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace CoinEx.Net.Objects.Models.V2
{
internal record CoinExServerTime
{
[JsonPropertyName("timestamp")]
public DateTime ServerTime { get; set; }
}
}

0 comments on commit 0eda275

Please sign in to comment.