· 6 years ago · Feb 26, 2020, 12:58 PM
1using System;
2using System.Diagnostics;
3using System.Text;
4using IO.Swagger.Api;
5using IO.Swagger.Client;
6using IO.Swagger.Model;
7using System.Security.Cryptography;
8
9
10namespace ByBit
11{
12 public class Program
13 {
14 private static string SignRequest(OrderSide orderSide, OrderSymbol orderSymbol, OrderType orderType, int? orderQuantity, int? orderPrice, OrderTimeInForce orderTimeInForce)
15 {
16 var parameters = new string[]
17 {
18 $"api_key={Configuration.Default.ApiKey["api_key"]}",
19 $"order_type={orderType}",
20 $"{(orderQuantity.HasValue ? $"qty={orderQuantity.Value}" : string.Empty)}",
21 $"{(orderPrice.HasValue ? $"price={orderQuantity.Value}" : string.Empty)}",
22 $"side={orderSide}",
23 $"symbol={orderSymbol}",
24 $"time_in_force={orderTimeInForce}",
25 $"timestamp={Configuration.Default.ApiKey["timestamp"]}"
26 };
27
28 return RequestEncryptor.CreateSignature(Configuration.Default.ApiKey["secret"], parameters);
29 }
30 public enum OrderSide { Buy, Sell }
31 public enum OrderSymbol { BTCUSD, XRPUSD, ETHUSD, EOSUSD };
32 public enum OrderTimeInForce { GoodTillCancel, ImmediateOrCancel };
33 public enum OrderType { Market, Limit }
34
35
36
37 [STAThread]
38 static void Main(string[] args)
39 {
40 var timestamp = (long)((DateTime.UtcNow) - new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalMilliseconds;
41
42 // Configure API key authorization: apiKey
43 Configuration.Default.ApiKey.Add("api_key", "API_KEY");
44 Configuration.Default.ApiKey.Add("secret", "API_SECRET");
45 Configuration.Default.ApiKey.Add("timestamp", timestamp.ToString());
46 Configuration.Default.ApiKey.Add("sign", SignRequest());
47
48 //var apiInstance1 = new APIkeyApi();
49
50 //try
51 //{
52 // // Get account api-key information.
53 // Object result = apiInstance1.APIkeyInfo();
54 // Debug.WriteLine(result);
55 //}
56 //catch (Exception e)
57 //{
58 // Debug.Print("Exception when calling APIkeyApi.APIkeyInfo: " + e.Message);
59 //}
60
61 //var apiInstance = new WalletApi();
62 //var startDate = startDate_example; // string | Start point for result (optional)
63 //var endDate = endDate_example; // string | End point for result (optional)
64 //var currency = currency_example; // string | Currency type (optional)
65 //var walletFundType = walletFundType_example; // string | wallet fund type (optional)
66 //var page = page_example; // string | Page. Default getting first page data (optional)
67 //var limit = limit_example; // string | Limit for data size per page, max size is 50. Default as showing 20 pieces of data per page (optional)
68
69 try
70 {
71 // Get wallet fund records
72 //Object result = apiInstance.WalletGetRecords();
73 //Debug.WriteLine(result);
74 }
75 catch (Exception e)
76 {
77 Debug.Print("Exception when calling WalletApi.WalletGetRecords: " + e.Message);
78 }
79
80 var apiOrders = new OrderApi();
81
82 try
83 {
84 //Object result = apiOrders.OrderNewV2("long", "BTCUSD", "Limit", 200, "GoodTillCancel");
85 SignRequest(OrderSide.Buy, OrderSymbol.BTCUSD, OrderType.Limit, 500, 9000, OrderTimeInForce.GoodTillCancel);
86
87 //apiOrders.OrderCancelAll("BTCUSD");
88 //Object result=apiOrders.OrderCancelAllAsyncWithHttpInfo("BTCUSD");
89 }
90 catch (Exception e)
91 { Debug.Print("Exception when calling OrderApi: " + e.Message); }
92 }
93
94 private static string SignRequest()
95 {
96 return RequestEncryptor.CreateSignature(
97 Configuration.Default.ApiKey["secret"], new string[]
98 {
99 $"api_key={Configuration.Default.ApiKey["api_key"]}",
100 $"timestamp={Configuration.Default.ApiKey["timestamp"]}"
101 });
102
103 }
104 }
105
106
107 /// <summary>
108 ///
109 /// </summary>
110 public static class RequestEncryptor
111 {
112
113 /// <summary>
114 ///
115 /// </summary>
116 /// <param name="secret"></param>
117 /// <param name="message"></param>
118 /// <returns></returns>
119 public static string CreateSignature(string secret, string[] parameters)
120 {
121 var parameterString = string.Join("&", parameters);
122 var signatureBytes = Hmacsha256(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(parameterString));
123
124 return ByteArrayToString(signatureBytes);
125 }
126
127 private static byte[] Hmacsha256(byte[] keyByte, byte[] messageBytes)
128 {
129 using (var hash = new HMACSHA256(keyByte))
130 {
131 return hash.ComputeHash(messageBytes);
132 }
133 }
134
135 public static string ByteArrayToString(byte[] ba)
136 {
137 var hex = new StringBuilder(ba.Length * 2);
138
139 foreach (var b in ba)
140 {
141 hex.AppendFormat("{0:x2}", b);
142 }
143 return hex.ToString();
144 }
145 }
146}