· 5 years ago · Jun 08, 2020, 04:30 AM
1using System;
2using System.Runtime.CompilerServices;
3
4namespace TradingAPI
5{
6 class TradeAPI
7 {
8 static void Main()
9 {
10 string input;
11
12 APICalls calls = new APICalls();
13 TradeAPI main = new TradeAPI();
14
15 input = calls.GetSpecificBalance("btcusd");
16 Console.WriteLine(input);
17 Console.ReadLine();
18 }
19 }
20}
21
22
23using System;
24using RestSharp;
25using System.Collections.Generic;
26using System.Text;
27
28namespace TradingAPI
29{
30 class APICalls
31 {
32 Authentication auth = new Authentication();
33 Credentials c = new Credentials();
34
35 #region || BITSTAMP API CALLS ||
36 public string GetSpecificBalance(string pair1pair2)
37 {
38 c.SetResourceURL($"api/v2/balance/{pair1pair2}/");
39 RestRequest request = new RestRequest($"api/v2/balance/{pair1pair2}/", Method.POST);
40 var response = auth.BitstampAuthentication(request);
41 Console.WriteLine(c.BIT_ResourceURL);
42 return response;
43 }
44 #endregion
45
46 }
47}
48
49
50using System;
51using RestSharp;
52using System.Collections.Generic;
53using System.Text;
54
55namespace TradingAPI
56{
57 class Authentication
58 {
59 readonly Encryption e = new Encryption();
60 readonly Credentials c = new Credentials();
61
62 #region || BITSTAMP API AUTHENTICATION ||
63 public string BitstampAuthentication(RestRequest restRequest)
64 {
65 var signature = e.GetBitstampSignature(c.Nonce, c.BIT_Key, c.BIT_Secret, c.Timestamp, c.BIT_noSSLBaseURL, c.BIT_ResourceURL);
66
67 restRequest.AddHeader("X-Auth", $"BITSTAMP {c.BIT_Key}");
68 restRequest.AddHeader("X-Auth-Signature", signature);
69 restRequest.AddHeader("X-Auth-Nonce", c.Nonce.ToString()); ;
70 restRequest.AddHeader("X-Auth-Timestamp", c.Timestamp.ToString()); ;
71 restRequest.AddHeader("X-Auth-Version", c.BIT_Version);
72
73 RestClient client = new RestClient(c.BIT_BaseURL);
74 IRestResponse response = client.Execute(restRequest);
75
76 return response.Content;
77 }
78 #endregion
79 }
80}
81
82
83using System;
84using System.Security.Cryptography;
85using System.Text;
86
87namespace TradingAPI
88{
89 class Encryption
90 {
91
92 #region || HMAC ENCRYPTION ||
93 public byte[] SignHMACSHA256(String key, byte[] data)
94 {
95 HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
96 return hashMaker.ComputeHash(data);
97 }
98
99 private byte[] StringToByteArray(string str)
100 {
101 return System.Text.Encoding.ASCII.GetBytes(str);
102 }
103
104 private string ByteArrayToString(byte[] hash)
105 {
106 return BitConverter.ToString(hash).Replace("-", "").ToLower();
107 }
108 #endregion
109
110 #region || BITSTAMP SIGNATURE ENCRYPTION ||
111 public string GetBitstampSignature(Guid nonce, string key, string secret, long timer, string baseURL, string resURL)
112 {
113 string msg = $"BITSTAMP {key}POST{baseURL}{resURL}{nonce}{timer}v2";
114 Console.WriteLine(msg);
115 return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
116 }
117 #endregion
118
119 }
120}
121
122
123using System;
124using System.Collections.Generic;
125using System.ComponentModel;
126using System.Runtime.CompilerServices;
127using System.Text;
128using System.Threading;
129
130namespace TradingAPI
131{
132 class Credentials
133 {
134 #region || GENERAL CREDENTIALS ||
135 public long Timestamp { get; } = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
136 public Guid Nonce { get; } = Guid.NewGuid();
137 #endregion
138
139 #region || BITSTAMP CREDENTIALS ||
140 public string BIT_Key { get; } = "x";
141 public string BIT_Secret { get; } = "x";
142 public string BIT_BaseURL { get; } = "https://www.bitstamp.net/";
143 public string BIT_noSSLBaseURL { get; } = "www.bitstamp.net/";
144
145 public string BIT_ResourceURL;
146 public string BIT_Version { get; set; } = "v2";
147 #endregion
148
149 public void SetResourceURL(string resourceURL)
150 {
151 BIT_ResourceURL = resourceURL;
152 }
153 }
154}