-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProgram.cs
127 lines (104 loc) · 4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Linq;
using System.Threading.Tasks;
using Coinbase.Pro;
using Coinbase.Pro.Models;
using Coinbase.Pro.WebSockets;
using static System.Console;
namespace Examples
{
class Program
{
static async Task Main()
{
WriteLine("Coinbase Example");
var creds = Credentials.ReadCredentials(@"..\..\..\.secrets.txt");
#if NETFRAMEWORK
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
#endif
WriteLine("Making Simple API call.");
await MakeSimpleApiCall(creds);
WriteLine("Subscribing to Websocket Events.");
await SubscribeToWebsocketEvents(creds);
WriteLine("Press enter to exit program.");
ReadLine();
}
public static async Task MakeSimpleApiCall(Credentials creds)
{
using var client = new CoinbaseProClient(new Config
{
ApiKey = creds.ApiKey,
Secret = creds.ApiSecret,
Passphrase = creds.ApiPassphrase,
ApiUrl = "https://api-public.sandbox.pro.coinbase.com"
});
//Uncomment if you want to debug requests to Coinbase with Fiddler
//See more here: https://github.com/bchavez/Coinbase/wiki/Debugging-with-Fiddler
//Uncommenting the line below will only apply to the CoinbaseProClient; not the
//websocket client.
client.EnableFiddlerDebugProxy("http://localhost.:8888");
var products = await client.MarketData.GetProductsAsync();
var productIds = products.Select(p => p.Id).ToArray();
WriteLine(">> Available Product IDs:");
foreach( var productId in productIds )
{
WriteLine($">> {productId}");
}
var data = await client.MarketData.GetStatsAsync("BTC-USD");
WriteLine($">> BTC-USD Volume: {data.Volume}");
}
public static async Task SubscribeToWebsocketEvents(Credentials creds)
{
var socket = new CoinbaseProWebSocket(new WebSocketConfig
{
ApiKey = creds.ApiKey,
Secret = creds.ApiSecret,
Passphrase = creds.ApiPassphrase,
SocketUri = "wss://ws-feed-public.sandbox.pro.coinbase.com"
});
WriteLine(">> Connecting websocket...");
//Uncomment depending on your TFM if you want to debug the websocket
//connection to Coinbase Pro with Fiddler
#if !NETFRAMEWORK
//socket.EnableFiddlerDebugProxy(new HttpConnectProxy(IPEndPoint.Parse("127.0.0.1:8888")));
#else
//socket.EnableFiddlerDebugProxy(new HttpConnectProxy(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888)));
#endif
var result = await socket.ConnectAsync();
if( !result.Success )
throw new Exception("Connect error.");
WriteLine(">> Connected.");
socket.RawSocket.Closed += Websocket_Closed;
socket.RawSocket.Error += Websocket_Error;
socket.RawSocket.MessageReceived += Websocket_MessageReceived;
var sub = new Subscription
{
ProductIds = {"BTC-USD"},
Channels = {"heartbeat"}
};
WriteLine(">> Subscribing to events...");
await socket.SubscribeAsync(sub);
WriteLine(">> Subscribed.");
}
private static void Websocket_Closed(object sender, EventArgs e)
{
WriteLine("!! The websocket closed!");
}
private static void Websocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
{
WriteLine("!! Websocket Error! ");
WriteLine(e);
}
private static void Websocket_MessageReceived(object sender, WebSocket4Net.MessageReceivedEventArgs e)
{
WriteLine("Message received.");
if( WebSocketHelper.TryParse(e.Message, out var msg) )
{
if( msg is HeartbeatEvent hb )
{
WriteLine($"Sequence: {hb.Sequence}, Last Trade Id: {hb.LastTradeId}");
}
}
}
}
}