Skip to content

BL3P: added Trade stream (websocket) #479

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

Merged
merged 2 commits into from
Nov 9, 2019
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
3 changes: 1 addition & 2 deletions ExchangeSharp/API/Exchanges/BL3P/BL3PException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Runtime.Serialization;
using ExchangeSharp.API.Exchanges.BL3P.Models;

namespace ExchangeSharp.API.Exchanges.BL3P
namespace ExchangeSharp.BL3P
{
[Serializable]
internal class BL3PException : Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using ExchangeSharp.API.Exchanges.BL3P.Models;
using ExchangeSharp.Dependencies.Converters;
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Converters
namespace ExchangeSharp.BL3P
{
internal class BL3PResponseConverter<TSuccess> : JsonComplexObjectConverter<BL3PResponsePayload>
where TSuccess : BL3PResponsePayload, new()
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Enums/BL3PCurrencyFee.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
namespace ExchangeSharp.BL3P
{
public enum BL3PCurrencyFee : byte
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Enums/BL3POrderStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
namespace ExchangeSharp.BL3P
{
internal enum BL3POrderStatus
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Enums/BL3POrderType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
namespace ExchangeSharp.BL3P
{
internal enum BL3POrderType
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Enums/BL3PResponseType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
namespace ExchangeSharp.BL3P
{
internal enum BL3PResponseType
{
Expand Down
40 changes: 30 additions & 10 deletions ExchangeSharp/API/Exchanges/BL3P/ExchangeBL3PAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using ExchangeSharp.API.Exchanges.BL3P;
using ExchangeSharp.API.Exchanges.BL3P.Enums;
using ExchangeSharp.API.Exchanges.BL3P.Extensions;
using ExchangeSharp.API.Exchanges.BL3P.Models;
using ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Add;
using ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Result;
using ExchangeSharp.BL3P;
using ExchangeSharp.Utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -127,10 +122,10 @@ protected override async Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(
params string[] marketSymbols
)
{
if (marketSymbols == null)
throw new ArgumentNullException(nameof(marketSymbols));
if (marketSymbols.Length == 0)
throw new ArgumentException("Value cannot be an empty collection.", nameof(marketSymbols));
if (marketSymbols == null || marketSymbols.Length == 0)
{
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
}

Task MessageCallback(IWebSocket _, byte[] msg)
{
Expand Down Expand Up @@ -170,6 +165,31 @@ await Task.WhenAll(
);
}

protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols)
{
if (marketSymbols == null || marketSymbols.Length == 0)
{
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
}
Task MessageCallback(IWebSocket _, byte[] msg)
{ // {{ "date": 1573255932, "marketplace": "BTCEUR", "price_int": 802466000, "type": "buy", "amount_int": 6193344 } }
JToken token = JToken.Parse(msg.ToStringFromUTF8());
var symbol = token["marketplace"].ToStringInvariant();
ExchangeTrade trade = token.ParseTrade(amountKey: "amount_int", priceKey: "price_int", typeKey: "type", timestampKey: "date",
timestampType: TimestampType.UnixSeconds,
idKey: null, // + TODO: add Id Key when BL3P starts providing this info
typeKeyIsBuyValue: "buy");
callback(new KeyValuePair<string, ExchangeTrade>(symbol, trade));
return Task.CompletedTask;
}

return new MultiWebsocketWrapper(
await Task.WhenAll(
marketSymbols.Select(ms => ConnectWebSocketAsync($"{ms}/trades", MessageCallback))
).ConfigureAwait(false)
);
}

protected override bool CanMakeAuthenticatedRequest(IReadOnlyDictionary<string, object> payload)
{
return !(PublicApiKey is null) && !(PrivateApiKey is null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using ExchangeSharp.API.Exchanges.BL3P.Enums;
using ExchangeSharp.API.Exchanges.BL3P.Models;

namespace ExchangeSharp.API.Exchanges.BL3P.Extensions
namespace ExchangeSharp.BL3P
{
internal static class BL3PExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Models/BL3PAmount.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
internal class BL3PAmount
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/Models/BL3POrderBook.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
// ReSharper disable once InconsistentNaming
internal class BL3POrderBook
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
// ReSharper disable once InconsistentNaming
internal class BL3POrderRequest
Expand Down
3 changes: 1 addition & 2 deletions ExchangeSharp/API/Exchanges/BL3P/Models/BL3PResponse.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExchangeSharp.API.Exchanges.BL3P.Enums;
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
internal class BL3PEmptyResponse
: BL3PResponse<BL3PResponsePayload, BL3PResponsePayloadError>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
internal class BL3PResponsePayload
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models
namespace ExchangeSharp.BL3P
{
internal class BL3PResponsePayloadError : BL3PResponsePayload
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExchangeSharp.API.Exchanges.BL3P.Converters;
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Add
namespace ExchangeSharp.BL3P
{
internal class BL3POrderAddResponse : BL3PResponse<BL3POrderAddSuccess>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Add
namespace ExchangeSharp.BL3P
{
internal class BL3POrderAddSuccess : BL3PResponsePayload
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ExchangeSharp.API.Exchanges.BL3P.Converters;
using Newtonsoft.Json;

namespace ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Result
namespace ExchangeSharp.BL3P
{
internal class BL3POrderResultResponse : BL3PResponse<BL3POrderResultSuccess>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using ExchangeSharp.API.Exchanges.BL3P.Enums;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Result
namespace ExchangeSharp.BL3P
{
internal class BL3POrderResultSuccess : BL3PResponsePayload
{
Expand Down
2 changes: 1 addition & 1 deletion ExchangeSharp/API/Exchanges/BL3P/MultiWebsocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace ExchangeSharp.API.Exchanges.BL3P
namespace ExchangeSharp.BL3P
{
internal sealed class MultiWebsocketWrapper : IWebSocket
{
Expand Down
4 changes: 2 additions & 2 deletions ExchangeSharp/API/Exchanges/_Base/ExchangeAPIExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ internal static async Task<ExchangeTicker> ParseTickerAsync(this ExchangeAPI api
/// <param name="typeKeyIsBuyValue">Type key buy value</param>
/// <returns>Trade</returns>
internal static ExchangeTrade ParseTrade(this JToken token, object amountKey, object priceKey, object typeKey,
object timestampKey, TimestampType timestampType, object idKey, string typeKeyIsBuyValue = "buy")
object timestampKey, TimestampType timestampType, object? idKey, string typeKeyIsBuyValue = "buy")
{
return ParseTradeComponents<ExchangeTrade>(token, amountKey, priceKey, typeKey,
timestampKey, timestampType, idKey, typeKeyIsBuyValue);
Expand Down Expand Up @@ -610,7 +610,7 @@ internal static ExchangeTrade ParseTradeNDAX(this JToken token, object amountKey
}

internal static T ParseTradeComponents<T>(this JToken token, object amountKey, object priceKey, object typeKey,
object timestampKey, TimestampType timestampType, object idKey, string typeKeyIsBuyValue = "buy")
object timestampKey, TimestampType timestampType, object? idKey, string typeKeyIsBuyValue = "buy")
where T : ExchangeTrade, new()
{
var isBuy = token[typeKey].ToStringInvariant().EqualsWithOption(typeKeyIsBuyValue);
Expand Down