using System;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Linq;
namespace ct_api
{
class CoinTrackingAPI
{
readonly string url = "https://cointracking.info/api/v1/";
private readonly string apiKey;
private readonly string apiSecret;
static readonly HttpClient client = new HttpClient();
public CoinTrackingAPI(string key, string secret)
{
apiKey = key;
apiSecret = secret;
}
private FormUrlEncodedContent prepareRequestData(string method, IEnumerable<KeyValuePair<string, string>> data)
{
return new FormUrlEncodedContent(
Enumerable.Concat(
data,
new[] {
new KeyValuePair<string, string>("method", method),
new KeyValuePair<string, string>("nonce", new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString())
}
)
);
}
private async Task signMessage(FormUrlEncodedContent formData)
{
HMACSHA512 hmac = new HMACSHA512(Encoding.ASCII.GetBytes(apiSecret));
byte[] sign = hmac.ComputeHash(await formData.ReadAsByteArrayAsync());
formData.Headers.Add("Key", apiKey);
formData.Headers.Add("Sign", BitConverter.ToString(sign).Replace("-", string.Empty).ToLower());
}
public async Task<string> getTrades(int limit = 0, string order = "ASC", int start = 0, int end = 0, bool tradePrices = false)
{
List<KeyValuePair<string, string>> optionalParams = new List<KeyValuePair<string, string>>();
if (limit > 0) {
optionalParams.Add(new KeyValuePair<string, string>("limit", limit.ToString()));
}
optionalParams.Add(new KeyValuePair<string, string>("order", order));
if (start > 0) {
optionalParams.Add(new KeyValuePair<string, string>("start", start.ToString()));
}
if (end > 0) {
optionalParams.Add(new KeyValuePair<string, string>("end", end.ToString()));
}
if (tradePrices) {
optionalParams.Add(new KeyValuePair<string, string>("trade_prices", "1"));
}
FormUrlEncodedContent formData = prepareRequestData("getTrades", optionalParams);
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> getBalance()
{
FormUrlEncodedContent formData = prepareRequestData("getBalance", new List<KeyValuePair<string, string>>());
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> getHistoricalSummary(bool btc = false, int start = 0, int end = 0)
{
List<KeyValuePair<string, string>> optionalParams = new List<KeyValuePair<string, string>>();
if (btc) {
optionalParams.Add(new KeyValuePair<string, string>("btc", "1"));
}
if (start > 0) {
optionalParams.Add(new KeyValuePair<string, string>("start", start.ToString()));
}
if (end > 0) {
optionalParams.Add(new KeyValuePair<string, string>("end", end.ToString()));
}
FormUrlEncodedContent formData = prepareRequestData("getHistoricalSummary", optionalParams);
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> getHistoricalCurrency(string currency = null, int start = 0, int end = 0)
{
List<KeyValuePair<string, string>> optionalParams = new List<KeyValuePair<string, string>>();
if (!string.IsNullOrEmpty(currency)) {
optionalParams.Add(new KeyValuePair<string, string>("currency", currency));
}
if (start > 0) {
optionalParams.Add(new KeyValuePair<string, string>("start", start.ToString()));
}
if (end > 0) {
optionalParams.Add(new KeyValuePair<string, string>("end", end.ToString()));
}
FormUrlEncodedContent formData = prepareRequestData("getHistoricalCurrency", optionalParams);
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> getGroupedBalance(string group = "exchange", bool excludeDepWith = false, string type = null)
{
List<KeyValuePair<string, string>> optionalParams = new List<KeyValuePair<string, string>>();
if (!string.IsNullOrEmpty(group)) {
optionalParams.Add(new KeyValuePair<string, string>("group", group));
}
if (!string.IsNullOrEmpty(type)) {
optionalParams.Add(new KeyValuePair<string, string>("type", type));
}
if (excludeDepWith) {
optionalParams.Add(new KeyValuePair<string, string>("exclude_dep_with", "1"));
}
FormUrlEncodedContent formData = prepareRequestData("getGroupedBalance", optionalParams);
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
public async Task<string> getGains(string price = null, bool btc = false)
{
List<KeyValuePair<string, string>> optionalParams = new List<KeyValuePair<string, string>>();
if (!string.IsNullOrEmpty(price)) {
optionalParams.Add(new KeyValuePair<string, string>("price", price));
}
if (btc) {
optionalParams.Add(new KeyValuePair<string, string>("btc", "1"));
}
FormUrlEncodedContent formData = prepareRequestData("getGains", optionalParams);
await signMessage(formData);
HttpResponseMessage response = await client.PostAsync(url, formData);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}