Skip to content

Commit d3484ec

Browse files
authored
BL3P: added Trade stream (websocket) (#479)
* BL3P: added Trade stream (websocket) - simplified BL3P namespace - account for null or empty marketSymbols param in OrderBook and Trades WS - fixed capitalization in BL3PResponsePayloadError.cs * one more namespace
1 parent 208a487 commit d3484ec

20 files changed

+50
-39
lines changed

ExchangeSharp/API/Exchanges/BL3P/BL3PException.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using ExchangeSharp.API.Exchanges.BL3P.Models;
43

5-
namespace ExchangeSharp.API.Exchanges.BL3P
4+
namespace ExchangeSharp.BL3P
65
{
76
[Serializable]
87
internal class BL3PException : Exception

ExchangeSharp/API/Exchanges/BL3P/Converters/BL3PResponseConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using ExchangeSharp.API.Exchanges.BL3P.Models;
21
using ExchangeSharp.Dependencies.Converters;
32
using Newtonsoft.Json;
43

5-
namespace ExchangeSharp.API.Exchanges.BL3P.Converters
4+
namespace ExchangeSharp.BL3P
65
{
76
internal class BL3PResponseConverter<TSuccess> : JsonComplexObjectConverter<BL3PResponsePayload>
87
where TSuccess : BL3PResponsePayload, new()

ExchangeSharp/API/Exchanges/BL3P/Enums/BL3PCurrencyFee.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
1+
namespace ExchangeSharp.BL3P
22
{
33
public enum BL3PCurrencyFee : byte
44
{

ExchangeSharp/API/Exchanges/BL3P/Enums/BL3POrderStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
1+
namespace ExchangeSharp.BL3P
22
{
33
internal enum BL3POrderStatus
44
{

ExchangeSharp/API/Exchanges/BL3P/Enums/BL3POrderType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
1+
namespace ExchangeSharp.BL3P
22
{
33
internal enum BL3POrderType
44
{

ExchangeSharp/API/Exchanges/BL3P/Enums/BL3PResponseType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
1+
namespace ExchangeSharp.BL3P
22
{
33
internal enum BL3PResponseType
44
{

ExchangeSharp/API/Exchanges/BL3P/ExchangeBL3PAPI.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
using System.Security.Cryptography;
66
using System.Text;
77
using System.Threading.Tasks;
8-
using ExchangeSharp.API.Exchanges.BL3P;
9-
using ExchangeSharp.API.Exchanges.BL3P.Enums;
10-
using ExchangeSharp.API.Exchanges.BL3P.Extensions;
11-
using ExchangeSharp.API.Exchanges.BL3P.Models;
12-
using ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Add;
13-
using ExchangeSharp.API.Exchanges.BL3P.Models.Orders.Result;
8+
using ExchangeSharp.BL3P;
149
using ExchangeSharp.Utility;
1510
using Newtonsoft.Json;
1611
using Newtonsoft.Json.Linq;
@@ -127,10 +122,10 @@ protected override async Task<IWebSocket> OnGetDeltaOrderBookWebSocketAsync(
127122
params string[] marketSymbols
128123
)
129124
{
130-
if (marketSymbols == null)
131-
throw new ArgumentNullException(nameof(marketSymbols));
132-
if (marketSymbols.Length == 0)
133-
throw new ArgumentException("Value cannot be an empty collection.", nameof(marketSymbols));
125+
if (marketSymbols == null || marketSymbols.Length == 0)
126+
{
127+
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
128+
}
134129

135130
Task MessageCallback(IWebSocket _, byte[] msg)
136131
{
@@ -170,6 +165,31 @@ await Task.WhenAll(
170165
);
171166
}
172167

168+
protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols)
169+
{
170+
if (marketSymbols == null || marketSymbols.Length == 0)
171+
{
172+
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
173+
}
174+
Task MessageCallback(IWebSocket _, byte[] msg)
175+
{ // {{ "date": 1573255932, "marketplace": "BTCEUR", "price_int": 802466000, "type": "buy", "amount_int": 6193344 } }
176+
JToken token = JToken.Parse(msg.ToStringFromUTF8());
177+
var symbol = token["marketplace"].ToStringInvariant();
178+
ExchangeTrade trade = token.ParseTrade(amountKey: "amount_int", priceKey: "price_int", typeKey: "type", timestampKey: "date",
179+
timestampType: TimestampType.UnixSeconds,
180+
idKey: null, // + TODO: add Id Key when BL3P starts providing this info
181+
typeKeyIsBuyValue: "buy");
182+
callback(new KeyValuePair<string, ExchangeTrade>(symbol, trade));
183+
return Task.CompletedTask;
184+
}
185+
186+
return new MultiWebsocketWrapper(
187+
await Task.WhenAll(
188+
marketSymbols.Select(ms => ConnectWebSocketAsync($"{ms}/trades", MessageCallback))
189+
).ConfigureAwait(false)
190+
);
191+
}
192+
173193
protected override bool CanMakeAuthenticatedRequest(IReadOnlyDictionary<string, object> payload)
174194
{
175195
return !(PublicApiKey is null) && !(PrivateApiKey is null);

ExchangeSharp/API/Exchanges/BL3P/Extensions/BL3PExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using ExchangeSharp.API.Exchanges.BL3P.Enums;
2-
using ExchangeSharp.API.Exchanges.BL3P.Models;
3-
4-
namespace ExchangeSharp.API.Exchanges.BL3P.Extensions
1+
namespace ExchangeSharp.BL3P
52
{
63
internal static class BL3PExtensions
74
{

ExchangeSharp/API/Exchanges/BL3P/Models/BL3PAmount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Newtonsoft.Json;
22

3-
namespace ExchangeSharp.API.Exchanges.BL3P.Models
3+
namespace ExchangeSharp.BL3P
44
{
55
internal class BL3PAmount
66
{

ExchangeSharp/API/Exchanges/BL3P/Models/BL3POrderBook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Newtonsoft.Json;
22

3-
namespace ExchangeSharp.API.Exchanges.BL3P.Models
3+
namespace ExchangeSharp.BL3P
44
{
55
// ReSharper disable once InconsistentNaming
66
internal class BL3POrderBook

0 commit comments

Comments
 (0)