-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPrivateConnection.cs
More file actions
56 lines (45 loc) · 2.89 KB
/
PrivateConnection.cs
File metadata and controls
56 lines (45 loc) · 2.89 KB
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
using System;
using System.Threading;
using System.Threading.Tasks;
using WhalesSecret.ScriptApiLib.Exchanges;
using WhalesSecret.ScriptApiLib.Samples.SharedLib;
using WhalesSecret.TradeScriptLib.API.TradingV1;
using WhalesSecret.TradeScriptLib.Entities;
using WhalesSecret.TradeScriptLib.Exceptions;
namespace WhalesSecret.ScriptApiLib.Samples.BasicSamples.Connections;
/// <summary>
/// Sample that demonstrates how to connect to an exchange market using a private connection. Private connections are necessary when accessing information related to the user's
/// exchange account, or to create orders. These operations require exchange API credentials to be set.
/// </summary>
/// <remarks>IMPORTANT: You have to change the keys and the secrets in <see cref="Credentials"/> to make the sample work.</remarks>
public class PrivateConnection : IScriptApiSample
{
/// <inheritdoc/>
public async Task RunSampleAsync(ExchangeMarket exchangeMarket)
{
using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(2));
await using ScriptApi scriptApi = await ScriptApi.CreateAsync(timeoutCts.Token).ConfigureAwait(false);
// Credentials must be set before we can create a private connection.
#pragma warning disable CA2000 // Dispose objects before losing scope
IApiIdentity apiIdentity = exchangeMarket switch
{
ExchangeMarket.BinanceSpot => Credentials.GetBinanceHmacApiIdentity(),
ExchangeMarket.KucoinSpot => Credentials.GetKucoinApiIdentity(),
ExchangeMarket.KrakenSpot => Credentials.GetKrakenApiIdentity(),
_ => throw new SanityCheckException($"Unsupported exchange market {exchangeMarket} provided."),
};
#pragma warning restore CA2000 // Dispose objects before losing scope
scriptApi.SetCredentials(apiIdentity);
Console.WriteLine($"Connect to {exchangeMarket} exchange with a private connection.");
// Trading connection type means that only a private connection is established. Full-trading would create two connections, public and private.
ConnectionOptions connectionOptions = new(connectionType: ConnectionType.Trading);
await using ITradeApiClient tradeClient = await scriptApi.ConnectAsync(exchangeMarket, connectionOptions).ConfigureAwait(false);
Console.WriteLine($"Private connection to {exchangeMarket} has been established successfully.");
// As the connection is established, we can use the connected client to, for example, query the time of the exchange.
DateTime utcExchangeTime = tradeClient.GetExchangeUtcDateTime();
TimeSpan diff = utcExchangeTime - DateTime.UtcNow;
Console.WriteLine($"Current UTC time of the {exchangeMarket} exchange is {utcExchangeTime}. The difference between the exchange time and the local time is {
diff}.");
Console.WriteLine("Disposing trade API client and script API.");
}
}