Skip to content

Commit

Permalink
Exchange clients now use a common ExchangeApi.cs for api queries.
Browse files Browse the repository at this point in the history
Started implementing poloniex api.
  • Loading branch information
bonesoul committed Oct 22, 2014
1 parent cb22fa4 commit 449d64f
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 154 deletions.
13 changes: 8 additions & 5 deletions src/CoiniumServ/CoiniumServ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,15 @@
<Compile Include="Coin\Config\CoinOptions.cs" />
<Compile Include="Coin\Config\IBlockExplorerOptions.cs" />
<Compile Include="Coin\Config\ICoinOptions.cs" />
<Compile Include="Markets\BittrexClient.cs" />
<Compile Include="Markets\CryptsyClient.cs" />
<Compile Include="Markets\Exchanges\BittrexClient.cs" />
<Compile Include="Markets\Exchanges\ExchangeApi.cs" />
<Compile Include="Markets\Exchanges\CryptsyClient.cs" />
<Compile Include="Markets\Exchange.cs" />
<Compile Include="Markets\IBittrexClient.cs" />
<Compile Include="Markets\ICryptsyClient.cs" />
<Compile Include="Markets\IExchangeClient.cs" />
<Compile Include="Markets\Exchanges\IBittrexClient.cs" />
<Compile Include="Markets\Exchanges\ICryptsyClient.cs" />
<Compile Include="Markets\Exchanges\IExchangeClient.cs" />
<Compile Include="Markets\Exchanges\IPoloniexClient.cs" />
<Compile Include="Markets\Exchanges\PoloniexClient.cs" />
<Compile Include="Markets\IMarketData.cs" />
<Compile Include="Markets\MarketData.cs" />
<Compile Include="Server\Web\Modules\TosModule.cs" />
Expand Down
4 changes: 3 additions & 1 deletion src/CoiniumServ/Container/Registries/ClassRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
using CoiniumServ.Daemon;
using CoiniumServ.Daemon.Config;
using CoiniumServ.Jobs.Tracker;
using CoiniumServ.Markets;
using CoiniumServ.Markets.Exchanges;
using CoiniumServ.Mining.Software;
using CoiniumServ.Payments;
using CoiniumServ.Pools;
Expand Down Expand Up @@ -74,6 +74,8 @@ public void RegisterInstances()

// markets
_applicationContext.Container.Register<IBittrexClient, BittrexClient>().AsSingleton();
_applicationContext.Container.Register<ICryptsyClient, CryptsyClient>().AsSingleton();
_applicationContext.Container.Register<IPoloniexClient, PoloniexClient>().AsSingleton();

// web
_applicationContext.Container.Register<INancyBootstrapper, NancyBootstrapper>().AsSingleton();
Expand Down
5 changes: 5 additions & 0 deletions src/CoiniumServ/Factories/IObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using CoiniumServ.Jobs.Tracker;
using CoiniumServ.Logging;
using CoiniumServ.Markets;
using CoiniumServ.Markets.Exchanges;
using CoiniumServ.Mining;
using CoiniumServ.Mining.Software;
using CoiniumServ.Payments;
Expand Down Expand Up @@ -158,6 +159,10 @@ public interface IObjectFactory

IBittrexClient GetBittrexClient();

ICryptsyClient GetCryptsyClient();

IPoloniexClient GetPoloniexClient();

#endregion

#region mining software
Expand Down
11 changes: 11 additions & 0 deletions src/CoiniumServ/Factories/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using CoiniumServ.Jobs.Tracker;
using CoiniumServ.Logging;
using CoiniumServ.Markets;
using CoiniumServ.Markets.Exchanges;
using CoiniumServ.Mining;
using CoiniumServ.Mining.Software;
using CoiniumServ.Payments;
Expand Down Expand Up @@ -425,6 +426,16 @@ public IBittrexClient GetBittrexClient()
return _applicationContext.Container.Resolve<IBittrexClient>();
}

public ICryptsyClient GetCryptsyClient()
{
return _applicationContext.Container.Resolve<ICryptsyClient>();
}

public IPoloniexClient GetPoloniexClient()
{
return _applicationContext.Container.Resolve<IPoloniexClient>();
}

public ISoftwareRepository GetSoftwareRepository()
{
return _applicationContext.Container.Resolve<ISoftwareRepository>();
Expand Down
93 changes: 0 additions & 93 deletions src/CoiniumServ/Markets/CryptsyClient.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/CoiniumServ/Markets/Exchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum Exchange
{
Unknown,
Cryptsy,
Bittrex
Bittrex,
Poloniex
}
}
84 changes: 84 additions & 0 deletions src/CoiniumServ/Markets/Exchanges/BittrexClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#region License
//
// CoiniumServ - Crypto Currency Mining Pool Server Software
// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org
// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For the terms of this license, see licenses/gpl_v3.txt.
//
// Alternatively, you can license this software under a commercial
// license or white-label it as set out in licenses/commercial.txt.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Serilog;

namespace CoiniumServ.Markets.Exchanges
{
public class BittrexClient: ExchangeApi, IBittrexClient
{
private const string ApiBase = "https://bittrex.com/api/v1.1/";

private readonly ILogger _logger;

public BittrexClient()
{
_logger = Log.ForContext<BittrexClient>();
}

public async Task<IList<IMarketData>> GetMarkets()
{
var list = new List<IMarketData>();
var data = await Request(ApiBase, "public/getmarketsummaries");

try
{
foreach (var market in data.@result)
{
try
{
string name = market.MarketName;
var temp = name.Split('-');

var entry = new MarketData
{
Exchange = Exchange.Bittrex,
MarketCurrency = temp.Last(),
BaseCurrency = temp.First(),
Ask = market.Ask,
Bid = market.Bid,
VolumeInMarketCurrency = market.Volume,
VolumeInBaseCurrency = market.BaseVolume
};

list.Add(entry);
}
catch (Exception e)
{
_logger.Error(e.Message);
}
}
}
catch (Exception e)
{
_logger.Error(e.Message);
}

return list;
}
}
}
94 changes: 94 additions & 0 deletions src/CoiniumServ/Markets/Exchanges/CryptsyClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#region License
//
// CoiniumServ - Crypto Currency Mining Pool Server Software
// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org
// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For the terms of this license, see licenses/gpl_v3.txt.
//
// Alternatively, you can license this software under a commercial
// license or white-label it as set out in licenses/commercial.txt.
//
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Converters;
using Serilog;

namespace CoiniumServ.Markets.Exchanges
{
public class CryptsyClient : ExchangeApi, ICryptsyClient
{
private const string PublicApiBase = "http://pubapi.cryptsy.com/";
private const string PublicApiEndpoint = "api.php";
private const string PrivateApiBase = " https://api.cryptsy.com/";
private const string PrivateApiEndpoint = "api";

private readonly ExpandoObjectConverter _converter = new ExpandoObjectConverter();
private readonly ILogger _logger;

public CryptsyClient()
{
_logger = Log.ForContext<BittrexClient>();
}

public async Task<IList<IMarketData>> GetMarkets()
{
var list = new List<IMarketData>();

var @params = new Dictionary<string, string>
{
{"method", "marketdatav2"}
};

var data = await Request(PublicApiBase, PublicApiEndpoint, @params);

try
{
foreach (var kvp in data.@return.markets)
{
try
{
var buyOrders = (IList<dynamic>) kvp.Value.buyorders;
var sellOrders = (IList<dynamic>) kvp.Value.sellorders;

var entry = new MarketData
{
Exchange = Exchange.Cryptsy,
MarketCurrency = kvp.Value.primarycode,
BaseCurrency = kvp.Value.secondarycode,
Ask = double.Parse(sellOrders.First().price, CultureInfo.InvariantCulture),
Bid = double.Parse(buyOrders.First().price, CultureInfo.InvariantCulture),
VolumeInMarketCurrency = double.Parse(kvp.Value.volume, CultureInfo.InvariantCulture),
};
list.Add(entry);
}
catch (Exception e)
{
_logger.Error(e.Message);
}
}
}
catch (Exception e)
{
_logger.Error(e.Message);
}

return list;
}
}
}

0 comments on commit 449d64f

Please sign in to comment.