1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Initial commit

This commit is contained in:
JKorf 2018-02-28 13:53:29 +01:00
parent e2af98f2c9
commit 2bf860947a
23 changed files with 837 additions and 0 deletions

25
ApiProxy.cs Normal file
View File

@ -0,0 +1,25 @@
using System;
namespace CryptoExchange.Net
{
public class ApiProxy
{
/// <summary>
/// The host address of the proxy
/// </summary>
public string Host { get; }
/// <summary>
/// The port of the proxy
/// </summary>
public int Port { get; }
public ApiProxy(string host, int port)
{
if(string.IsNullOrEmpty(host) || port <= 0)
throw new ArgumentException("Proxy host or port not filled");
Host = host;
Port = port;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
namespace CryptoExchange.Net.Authentication
{
public class ApiCredentials
{
/// <summary>
/// The api key
/// </summary>
public string Key { get; }
/// <summary>
/// The api secret
/// </summary>
public string Secret { get; }
public ApiCredentials() { }
public ApiCredentials(string key, string secret)
{
if(string.IsNullOrEmpty(key) || string.IsNullOrEmpty(secret))
throw new ArgumentException("Apikey or apisecret not provided");
Key = key;
Secret = secret;
}
}
}

View File

@ -0,0 +1,25 @@
using CryptoExchange.Net.Interfaces;
namespace CryptoExchange.Net.Authentication
{
public abstract class AuthenticationProvider
{
protected ApiCredentials credentials;
protected AuthenticationProvider(ApiCredentials credentials)
{
this.credentials = credentials;
}
public abstract string AddAuthenticationToUriString(string uri);
public abstract IRequest AddAuthenticationToRequest(IRequest request);
protected string ByteToString(byte[] buff)
{
var sbinary = "";
foreach (byte t in buff)
sbinary += t.ToString("X2"); /* hex format */
return sbinary;
}
}
}

36
BaseConverter.cs Normal file
View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace CryptoExchange.Net
{
public abstract class BaseConverter<T>: JsonConverter
{
protected abstract Dictionary<T, string> Mapping { get; }
private readonly bool quotes;
protected BaseConverter(bool useQuotes)
{
quotes = useQuotes;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (quotes)
writer.WriteValue(Mapping[(T)value]);
else
writer.WriteRawValue(Mapping[(T)value]);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return Mapping.Single(v => v.Value.ToLower() == reader.Value.ToString().ToLower()).Key;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
}
}

24
CallResult.cs Normal file
View File

@ -0,0 +1,24 @@
namespace CryptoExchange.Net
{
public class CallResult<T>
{
/// <summary>
/// The data returned by the call
/// </summary>
public T Data { get; internal set; }
/// <summary>
/// An error if the call didn't succeed
/// </summary>
public Error Error { get; internal set; }
/// <summary>
/// Whether the call was successful
/// </summary>
public bool Success => Error == null;
public CallResult(T data, Error error)
{
Data = data;
Error = error;
}
}
}

22
CryptoExchange.Net.csproj Normal file
View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<PackageId>CryptoExchange.Net</PackageId>
<Authors>JKorf</Authors>
<PackageVersion>0.0.1</PackageVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageProjectUrl>https://github.com/JKorf/CryptoExchange.Net</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/JKorf/CryptoExchange.Net/blob/master/LICENSE</PackageLicenseUrl>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
</ItemGroup>
</Project>

49
Error.cs Normal file
View File

@ -0,0 +1,49 @@
namespace CryptoExchange.Net
{
public abstract class Error
{
public int Code { get; set; }
public string Message { get; set; }
protected Error(int code, string message)
{
Code = code;
Message = message;
}
public override string ToString()
{
return $"{Code}: {Message}";
}
}
public class CantConnectError : Error
{
public CantConnectError() : base(1, "Can't connect to the server") { }
}
public class NoApiCredentialsError : Error
{
public NoApiCredentialsError() : base(2, "No credentials provided for private endpoint") { }
}
public class ServerError: Error
{
public ServerError(string message) : base(3, "Server error: " + message) { }
}
public class WebError : Error
{
public WebError(string message) : base(3, "Web error: " + message) { }
}
public class DeserializeError : Error
{
public DeserializeError(string message) : base(4, "Error deserializing data: " + message) { }
}
public class UnknownError : Error
{
public UnknownError(string message) : base(5, "Unknown error occured " + message) { }
}
}

240
ExchangeClient.cs Normal file
View File

@ -0,0 +1,240 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Logging;
using CryptoExchange.Net.RateLimiter;
using CryptoExchange.Net.Requests;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CryptoExchange.Net
{
public abstract class ExchangeClient: IDisposable
{
public IRequestFactory RequestFactory { get; set; } = new RequestFactory();
protected Log log;
protected ApiProxy apiProxy;
private AuthenticationProvider authProvider;
private readonly List<IRateLimiter> rateLimiters = new List<IRateLimiter>();
protected ExchangeClient(ExchangeOptions exchangeOptions, AuthenticationProvider authentictationProvider)
{
log = new Log();
authProvider = authentictationProvider;
Configure(exchangeOptions);
}
/// <summary>
/// Configure the client using the provided options
/// </summary>
/// <param name="exchangeOptions">Options</param>
protected void Configure(ExchangeOptions exchangeOptions)
{
log.Level = exchangeOptions.LogVerbosity;
apiProxy = exchangeOptions.Proxy;
foreach (var rateLimiter in exchangeOptions.RateLimiters)
rateLimiters.Add(rateLimiter);
}
/// <summary>
/// Adds a rate limiter to the client. There are 2 choices, the <see cref="RateLimiterTotal"/> and the <see cref="RateLimiterPerEndpoint"/>.
/// </summary>
/// <param name="limiter">The limiter to add</param>
public void AddRateLimiter(IRateLimiter limiter)
{
rateLimiters.Add(limiter);
}
/// <summary>
/// Removes all rate limiters from this client
/// </summary>
public void RemoveRateLimiters()
{
rateLimiters.Clear();
}
/// <summary>
/// Set the authentication provider
/// </summary>
/// <param name="authentictationProvider"></param>
protected void SetAuthenticationProvider(AuthenticationProvider authentictationProvider)
{
authProvider = authentictationProvider;
}
protected async Task<CallResult<T>> ExecuteRequest<T>(Uri uri, string method = "GET", Dictionary<string, object> parameters = null, bool signed = false) where T : class
{
if(signed && authProvider == null)
return new CallResult<T>(null, new NoApiCredentialsError());
var uriString = uri.ToString();
if (parameters != null)
{
if (!uriString.EndsWith("?"))
uriString += "?";
uriString += $"{string.Join("&", parameters.Select(s => $"{s.Key}={s.Value}"))}";
}
if (signed)
uriString = authProvider.AddAuthenticationToUriString(uriString);
var request = RequestFactory.Create(uriString);
request.Method = method;
if (apiProxy != null)
request.SetProxy(apiProxy.Host, apiProxy.Port);
if (signed)
request = authProvider.AddAuthenticationToRequest(request);
foreach (var limiter in rateLimiters)
{
double limitedBy = limiter.LimitRequest(uri.AbsolutePath);
if (limitedBy > 0)
log.Write(LogVerbosity.Debug, $"Request {uri.AbsolutePath} was limited by {limitedBy}ms by {limiter.GetType().Name}");
}
log.Write(LogVerbosity.Debug, $"Sending request to {uriString}");
var result = await ExecuteRequest(request);
if (result.Error != null)
return new CallResult<T>(null, result.Error);
return Deserialize<T>(result.Data);
}
private async Task<CallResult<string>> ExecuteRequest(IRequest request)
{
string returnedData = "";
try
{
var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
returnedData = await reader.ReadToEndAsync().ConfigureAwait(false);
return new CallResult<string>(returnedData, null);
}
}
catch (WebException we)
{
var response = (HttpWebResponse)we.Response;
string infoMessage = response == null ? "No response from server" : $"Status: {response.StatusCode}-{response.StatusDescription}, Message: {we.Message}";
return new CallResult<string>(null, new WebError(infoMessage));
}
catch (Exception e)
{
return new CallResult<string>(null, new UnknownError(e.Message + ", data: " + returnedData));
}
}
private CallResult<T> Deserialize<T>(string data) where T: class
{
try
{
var obj = JToken.Parse(data);
if (log.Level == LogVerbosity.Debug)
{
if (obj is JObject o)
CheckObject(typeof(T), o);
else
CheckObject(typeof(T), (JObject) ((JArray) obj)[0]);
}
return new CallResult<T>(obj.ToObject<T>(), null);
}
catch (JsonReaderException jre)
{
return new CallResult<T>(null, new DeserializeError($"Error occured at Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}. Received data: {data}"));
}
catch (JsonSerializationException jse)
{
return new CallResult<T>(null, new DeserializeError($"Message: {jse.Message}. Received data: {data}"));
}
}
private void CheckObject(Type type, JObject obj)
{
var properties = new List<string>();
var props = type.GetProperties();
foreach (var prop in props)
{
var attr = prop.GetCustomAttributes(typeof(JsonPropertyAttribute), false).FirstOrDefault();
var ignore = prop.GetCustomAttributes(typeof(JsonIgnoreAttribute), false).FirstOrDefault();
if (ignore != null)
continue;
properties.Add(attr == null ? prop.Name.ToLower() : ((JsonPropertyAttribute) attr).PropertyName.ToLower());
}
foreach (var token in obj)
{
var d = properties.SingleOrDefault(p => p == token.Key.ToLower());
if (d == null)
log.Write(LogVerbosity.Warning, $"Didn't find property `{token.Key}` in object of type `{type.Name}`");
else
{
properties.Remove(d);
var propType = GetProperty(d, props)?.PropertyType;
if (propType == null)
continue;
if (!IsSimple(propType) && propType != typeof(DateTime))
{
if(propType.IsArray && token.Value.HasValues && ((JArray)token.Value).Any() && ((JArray)token.Value)[0] is JObject)
CheckObject(propType.GetElementType(), (JObject)token.Value[0]);
else if(token.Value is JObject)
CheckObject(propType, (JObject)token.Value);
}
}
}
foreach(var prop in properties)
log.Write(LogVerbosity.Warning, $"Didn't find key `{prop}` in returned data object of type `{type.Name}`");
}
private PropertyInfo GetProperty(string name, PropertyInfo[] props)
{
foreach (var prop in props)
{
var attr = prop.GetCustomAttributes(typeof(JsonPropertyAttribute), false).FirstOrDefault();
if (attr == null)
{
if (prop.Name.ToLower() == name.ToLower())
return prop;
}
else
{
if (((JsonPropertyAttribute) attr).PropertyName.ToLower() == name)
return prop;
}
}
return null;
}
private bool IsSimple(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
// nullable type, check if the nested type is simple.
return IsSimple(type.GetGenericArguments()[0]);
}
return type.IsPrimitive
|| type.IsEnum
|| type == typeof(string)
|| type == typeof(decimal);
}
public virtual void Dispose()
{
}
}
}

40
ExchangeOptions.cs Normal file
View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.IO;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;
using CryptoExchange.Net.RateLimiter;
namespace CryptoExchange.Net
{
/// <summary>
/// Options
/// </summary>
public class ExchangeOptions
{
/// <summary>
/// The api credentials
/// </summary>
public ApiCredentials ApiCredentials { get; set; }
/// <summary>
/// Proxy to use
/// </summary>
public ApiProxy Proxy { get; set; }
/// <summary>
/// The log verbosity
/// </summary>
public LogVerbosity LogVerbosity { get; set; } = LogVerbosity.Warning;
/// <summary>
/// The log writer
/// </summary>
public TextWriter LogWriter { get; set; } = new DebugTextWriter();
/// <summary>
/// List of ratelimiters to use
/// </summary>
public List<IRateLimiter> RateLimiters { get; set; } = new List<IRateLimiter>();
}
}

29
ExtensionMethods.cs Normal file
View File

@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace CryptoExchange.Net
{
public static class ExtensionMethods
{
public static void AddParameter(this Dictionary<string, object> parameters, string key, string value)
{
parameters.Add(key, value);
}
public static void AddParameter(this Dictionary<string, object> parameters, string key, object value)
{
parameters.Add(key, value);
}
public static void AddOptionalParameter(this Dictionary<string, object> parameters, string key, object value)
{
if(value != null)
parameters.Add(key, value);
}
public static void AddOptionalParameter(this Dictionary<string, string> parameters, string key, string value)
{
if (value != null)
parameters.Add(key, value);
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using CryptoExchange.Net.RateLimiter;
namespace CryptoExchange.Net.Interfaces
{
public interface IExchangeClient
{
IRequestFactory RequestFactory { get; set; }
void AddRateLimiter(IRateLimiter limiter);
void RemoveRateLimiters();
}
}

15
Interfaces/IRequest.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Net;
namespace CryptoExchange.Net.Interfaces
{
public interface IRequest
{
Uri Uri { get; }
WebHeaderCollection Headers { get; set; }
string Method { get; set; }
void SetProxy(string host, int port);
IResponse GetResponse();
}
}

View File

@ -0,0 +1,8 @@

namespace CryptoExchange.Net.Interfaces
{
public interface IRequestFactory
{
IRequest Create(string uri);
}
}

9
Interfaces/IResponse.cs Normal file
View File

@ -0,0 +1,9 @@
using System.IO;
namespace CryptoExchange.Net.Interfaces
{
public interface IResponse
{
Stream GetResponseStream();
}
}

View File

@ -0,0 +1,16 @@
using System.Diagnostics;
using System.IO;
using System.Text;
namespace CryptoExchange.Net.Logging
{
public class DebugTextWriter: TextWriter
{
public override Encoding Encoding => Encoding.ASCII;
public override void WriteLine(string value)
{
Debug.WriteLine(value);
}
}
}

25
Logging/Log.cs Normal file
View File

@ -0,0 +1,25 @@
using System;
using System.IO;
namespace CryptoExchange.Net.Logging
{
public class Log
{
public TextWriter TextWriter { get; internal set; } = new DebugTextWriter();
public LogVerbosity Level { get; internal set; } = LogVerbosity.Warning;
public void Write(LogVerbosity logType, string message)
{
if ((int)logType >= (int)Level)
TextWriter.WriteLine($"{DateTime.Now:hh:mm:ss:fff} | {logType} | {message}");
}
}
public enum LogVerbosity
{
Debug,
Warning,
Error,
None
}
}

View File

@ -0,0 +1,7 @@
namespace CryptoExchange.Net.RateLimiter
{
public interface IRateLimiter
{
double LimitRequest(string url);
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace CryptoExchange.Net.RateLimiter
{
public class RateLimitObject
{
public object LockObject { get; }
private List<DateTime> Times { get; }
public RateLimitObject()
{
LockObject = new object();
Times = new List<DateTime>();
}
public double GetWaitTime(DateTime time, int limit, TimeSpan perTimePeriod)
{
Times.RemoveAll(d => d < time - perTimePeriod);
if (Times.Count >= limit)
return (Times.First() - (time - perTimePeriod)).TotalMilliseconds;
return 0;
}
public void Add(DateTime time)
{
Times.Add(time);
Times.Sort();
}
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace CryptoExchange.Net.RateLimiter
{
/// <summary>
/// Limits the amount of requests per time period to a certain limit, counts the request per endpoint.
/// </summary>
public class RateLimiterPerEndpoint: IRateLimiter
{
internal Dictionary<string, RateLimitObject> history = new Dictionary<string, RateLimitObject>();
private int limitPerEndpoint;
private TimeSpan perTimePeriod;
private object historyLock = new object();
/// <summary>
/// Create a new RateLimiterPerEndpoint. This rate limiter limits the amount of requests per time period to a certain limit, counts the request per endpoint.
/// </summary>
/// <param name="limitPerEndpoint">The amount to limit to</param>
/// <param name="perTimePeriod">The time period over which the limit counts</param>
public RateLimiterPerEndpoint(int limitPerEndpoint, TimeSpan perTimePeriod)
{
this.limitPerEndpoint = limitPerEndpoint;
this.perTimePeriod = perTimePeriod;
}
public double LimitRequest(string url)
{
double waitTime;
RateLimitObject rlo;
lock (historyLock)
{
if (history.ContainsKey(url))
rlo = history[url];
else
{
rlo = new RateLimitObject();
history.Add(url, rlo);
}
}
var sw = Stopwatch.StartNew();
lock (rlo.LockObject)
{
sw.Stop();
waitTime = rlo.GetWaitTime(DateTime.UtcNow, limitPerEndpoint, perTimePeriod);
if (waitTime != 0)
{
Thread.Sleep(Convert.ToInt32(waitTime));
waitTime += sw.ElapsedMilliseconds;
}
rlo.Add(DateTime.UtcNow);
}
return waitTime;
}
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace CryptoExchange.Net.RateLimiter
{
/// <summary>
/// Limits the amount of requests per time period to a certain limit, counts the total amount of requests.
/// </summary>
public class RateLimiterTotal: IRateLimiter
{
internal List<DateTime> history = new List<DateTime>();
private int limit;
private TimeSpan perTimePeriod;
private object requestLock = new object();
/// <summary>
/// Create a new RateLimiterTotal. This rate limiter limits the amount of requests per time period to a certain limit, counts the total amount of requests.
/// </summary>
/// <param name="limit">The amount to limit to</param>
/// <param name="perTimePeriod">The time period over which the limit counts</param>
public RateLimiterTotal(int limit, TimeSpan perTimePeriod)
{
this.limit = limit;
this.perTimePeriod = perTimePeriod;
}
public double LimitRequest(string url)
{
var sw = Stopwatch.StartNew();
lock (requestLock)
{
sw.Stop();
double waitTime = 0;
var checkTime = DateTime.UtcNow;
history.RemoveAll(d => d < checkTime - perTimePeriod);
if (history.Count >= limit)
{
waitTime = (history.First() - (checkTime - perTimePeriod)).TotalMilliseconds;
if (waitTime > 0)
{
Thread.Sleep(Convert.ToInt32(waitTime));
waitTime += sw.ElapsedMilliseconds;
}
}
history.Add(DateTime.UtcNow);
history.Sort();
return waitTime;
}
}
}
}

41
Requests/Request.cs Normal file
View File

@ -0,0 +1,41 @@
using System;
using System.Net;
using CryptoExchange.Net.Interfaces;
namespace CryptoExchange.Net.Requests
{
public class Request : IRequest
{
private readonly WebRequest request;
public Request(WebRequest request)
{
this.request = request;
}
public WebHeaderCollection Headers
{
get => request.Headers;
set => request.Headers = value;
}
public string Method
{
get => request.Method;
set => request.Method = value;
}
public Uri Uri
{
get => request.RequestUri;
}
public void SetProxy(string host, int port)
{
request.Proxy = new WebProxy(host, port); ;
}
public IResponse GetResponse()
{
return new Response(request.GetResponse());
}
}
}

View File

@ -0,0 +1,13 @@
using System.Net;
using CryptoExchange.Net.Interfaces;
namespace CryptoExchange.Net.Requests
{
public class RequestFactory : IRequestFactory
{
public IRequest Create(string uri)
{
return new Request(WebRequest.Create(uri));
}
}
}

21
Requests/Response.cs Normal file
View File

@ -0,0 +1,21 @@
using System.IO;
using System.Net;
using CryptoExchange.Net.Interfaces;
namespace CryptoExchange.Net.Requests
{
public class Response : IResponse
{
private readonly WebResponse response;
public Response(WebResponse response)
{
this.response = response;
}
public Stream GetResponseStream()
{
return response.GetResponseStream();
}
}
}