forked from EvgBitWhiskey/BitWhiskey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yobit.cs
117 lines (100 loc) · 3.8 KB
/
Yobit.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
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System.Text;
using System.Windows;
using Newtonsoft.Json;
namespace BitWhiskey
{
class Yobit
{
string key="";
string secret="";
string PUBLIC_API = "https://yobit.net/api/3/";
string KEY_API = "https://yobit.net/tapi/";
public Yobit()
{
}
public string GetResponseForRequest(WebRequest request)
{
using (Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
var jsonResponse = sr.ReadToEnd();
return jsonResponse;
// Console.WriteLine(String.Format("Response: {0}", jsonResponse));
}
}
}
public WebRequest CreateKeyRequest(Cryptor cryptor,string input)
{
string address = KEY_API + "?" + input;
WebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(address);
webRequest.Method = "POST";
webRequest.Timeout = 20000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Add("Key", key);
webRequest.Headers.Add("Sign", cryptor.sign);
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
webRequest.ContentLength = input.Length;
using (var dataStream = webRequest.GetRequestStream())
{
dataStream.Write(inputBytes, 0, input.Length);
}
return webRequest;
}
public WebRequest CreatePublicRequest(string input)
{
string address = PUBLIC_API + "?" + input;
WebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(address);
webRequest.Method = "POST";
webRequest.Timeout = 20000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Clear();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
webRequest.ContentLength = input.Length;
using (var dataStream = webRequest.GetRequestStream())
{
dataStream.Write(inputBytes, 0, input.Length);
}
return webRequest;
}
public string DoKeyRequest(string parameters)
{
Cryptor cryptor = new Cryptor();
cryptor.CalcSign(secret, parameters);
WebRequest webRequest = CreateKeyRequest(cryptor, parameters);
return GetResponseForRequest(webRequest);
}
public string DoPublicRequest(string parameters)
{
WebRequest webRequest = CreatePublicRequest(parameters);
return GetResponseForRequest(webRequest);
}
public int GetNonce()
{
return Helper.ToUnixTimeStamp(DateTime.UtcNow);
//(int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
public void GetInfo()
{
//https://yobit.net/api/3/info;
//https://yobit.net/api/3/ticker/ltc_btc-nmc_btc
string parameters ="method=getInfo&nonce=" + GetNonce();
string response = DoPublicRequest(parameters);
}
public void Trade(string otype,string pair,float rate,float amount)
{
string parameters = "method=Trade&nonce=" + GetNonce();
parameters += "&pair=" + pair;
parameters += "&type=" + otype;
parameters += "&rate=" + rate;
parameters += "&amount=" + amount;
string response = DoKeyRequest(parameters);
}
}
}