Skip to content

Commit a87fb8b

Browse files
authored
add trade stream for Coincheck (#762)
1 parent e0c2582 commit a87fb8b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace ExchangeSharp
9+
{
10+
public sealed partial class ExchangeCoincheckAPI : ExchangeAPI
11+
{
12+
public override string BaseUrl { get; set; } = "https://coincheck.com";
13+
public override string BaseUrlWebSocket { get; set; } = "wss://ws-api.coincheck.com";
14+
15+
public ExchangeCoincheckAPI()
16+
{
17+
NonceStyle = NonceStyle.UnixSeconds;
18+
NonceOffset = TimeSpan.FromSeconds(0.1);
19+
// WebSocketOrderBookType = not implemented
20+
MarketSymbolSeparator = "_";
21+
MarketSymbolIsUppercase = false;
22+
// ExchangeGlobalCurrencyReplacements[] not implemented
23+
}
24+
25+
protected override async Task<IEnumerable<string>> OnGetMarketSymbolsAsync()
26+
{ // unclear, but appears like this is all they have available, at least for trade stream (from their poor documentation)
27+
return new[] { "btc_jpy", "etc_jpy", "fct_jpy", "mona_jpy", "plt_jpy", };
28+
}
29+
30+
protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValuePair<string, ExchangeTrade>, Task> callback, params string[] marketSymbols)
31+
{
32+
if (marketSymbols == null || marketSymbols.Length == 0)
33+
{
34+
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
35+
}
36+
return await ConnectPublicWebSocketAsync("", async (_socket, msg) =>
37+
{ /*[
38+
2357062, // 0 "ID",
39+
"[pair]", // 1 "Currency pair"
40+
"148638.0", // 2 "Order rate"
41+
"5.0", // 3 "Order amount"
42+
"sell" // 4 "Specify order_type."
43+
]*/
44+
JToken token = JToken.Parse(msg.ToStringFromUTF8());
45+
// no error msgs provided
46+
if (token.Type == JTokenType.Array)
47+
{
48+
var trade = token.ParseTrade(3, 2, 4, null, TimestampType.None, 0);
49+
string marketSymbol = token[1].ToStringInvariant();
50+
await callback(new KeyValuePair<string, ExchangeTrade>(marketSymbol, trade));
51+
}
52+
else Logger.Warn($"Unexpected token type {token.Type}");
53+
}, async (_socket) =>
54+
{ /*{
55+
"type": "subscribe",
56+
"channel": "[pair]-trades"
57+
}*/
58+
foreach (var marketSymbol in marketSymbols)
59+
{
60+
var subscribeRequest = new
61+
{
62+
type = "subscribe",
63+
channel = $"{marketSymbol}-trades",
64+
};
65+
await _socket.SendMessageAsync(subscribeRequest);
66+
}
67+
});
68+
}
69+
}
70+
public partial class ExchangeName { public const string Coincheck = "Coincheck"; }
71+
}

0 commit comments

Comments
 (0)