Skip to content

Commit

Permalink
Started implementing MarketInfo.cs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonesoul committed Oct 24, 2014
1 parent a3b9aaf commit 843b131
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/CoiniumServ/CoiniumServ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
<Compile Include="Markets\IMarketData.cs" />
<Compile Include="Markets\MarketData.cs" />
<Compile Include="Persistance\Layers\Hybrid\Migrations\M003FixDefaults.cs" />
<Compile Include="Pools\IMarketInfo.cs" />
<Compile Include="Pools\MarketInfo.cs" />
<Compile Include="Server\Web\Modules\TosModule.cs" />
<Compile Include="Utils\Extensions\LinqExtensions.cs" />
<Compile Include="Utils\Helpers\Humanize.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/CoiniumServ/Container/Registries/ClassRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void RegisterInstances()
_applicationContext.Container.Register<IJobTracker, JobTracker>().AsMultiInstance();
_applicationContext.Container.Register<IBlockProcessor, BlockProcessor>().AsMultiInstance();
_applicationContext.Container.Register<INetworkInfo, NetworkInfo>().AsMultiInstance();
_applicationContext.Container.Register<IMarketInfo, MarketInfo>().AsMultiInstance();
_applicationContext.Container.Register<IBlockRepository, BlocksRepository>().AsMultiInstance();
_applicationContext.Container.Register<IPaymentRepository, PaymentRepository>().AsMultiInstance();
_applicationContext.Container.Register<IAccountManager, AccountManager>().AsMultiInstance();
Expand Down
2 changes: 2 additions & 0 deletions src/CoiniumServ/Factories/IObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public interface IObjectFactory

INetworkInfo GetNetworkInfo(IDaemonClient daemonClient, IHashAlgorithm hashAlgorithm, IPoolConfig poolConfig);

IMarketInfo GetMarketInfo(IPoolConfig poolConfig);

IBlockRepository GetBlockRepository(IStorageLayer storageLayer);

IMiningServer GetMiningServer(string type, IPoolConfig poolConfig, IPool pool, IMinerManager minerManager, IJobManager jobManager,IBanManager banManager);
Expand Down
10 changes: 10 additions & 0 deletions src/CoiniumServ/Factories/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ public INetworkInfo GetNetworkInfo(IDaemonClient daemonClient, IHashAlgorithm ha
return _applicationContext.Container.Resolve<INetworkInfo>(@params);
}

public IMarketInfo GetMarketInfo(IPoolConfig poolConfig)
{
var @params = new NamedParameterOverloads
{
{"poolConfig", poolConfig}
};

return _applicationContext.Container.Resolve<IMarketInfo>(@params);
}

public IBlockRepository GetBlockRepository(IStorageLayer storageLayer)
{
var @params = new NamedParameterOverloads
Expand Down
8 changes: 7 additions & 1 deletion src/CoiniumServ/Markets/IMarketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
//
#endregion

using CoiniumServ.Pools;
using System;
using System.Collections.Generic;
using CoiniumServ.Utils.Repository;

namespace CoiniumServ.Markets
{
public interface IMarketManager : IRepository<IMarketData>
{
event EventHandler Update;

IEnumerable<IMarketData> GetMarketsFor(string marketCurrency, string baseCurrency);

IMarketData GetBestMarketFor(string marketCurrency, string baseCurrency);
}
}
27 changes: 18 additions & 9 deletions src/CoiniumServ/Markets/MarketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public class MarketManager : IMarketManager

private readonly IList<IExchangeClient> _exchanges;

private HashSet<IMarketData> _storage;
private HashSet<IMarketData> _storage;

public event EventHandler Update;

public int Count { get { return _storage.Count; } }

public MarketManager(IBittrexClient bittrexClient, IPoloniexClient poloniexClient, ICryptsyClient cryptsyClient)
{
Expand All @@ -57,10 +61,7 @@ public MarketManager(IBittrexClient bittrexClient, IPoloniexClient poloniexClien
};

// update the data initially
_timer = new Timer(Run, null, Timeout.Infinite, Timeout.Infinite); // create the timer as disabled.
Run(null);

var ltc = GetMarketsFor("LTC", "BTC");
_timer = new Timer(Run, null, 1, Timeout.Infinite); // schedule the timer for the first run.
}

private void Run(object state)
Expand All @@ -87,11 +88,21 @@ private void UpdateMarkets()
}

_storage = results;

OnUpdate(EventArgs.Empty); // notify the listeners about the data update.
}

private void OnUpdate(EventArgs e)
{
var handler = Update;

if (handler != null)
handler(this, e);
}

public IEnumerable<IMarketData> GetMarketsFor(string marketCurrency, string baseCurrency)
{
return _storage.Where(x => x.MarketCurrency == marketCurrency && x.BaseCurrency == baseCurrency);
return _storage.Where(x => x.MarketCurrency == marketCurrency && x.BaseCurrency == baseCurrency).OrderByDescending(x => x.Bid);
}

public IMarketData GetBestMarketFor(string marketCurrency, string baseCurrency)
Expand Down Expand Up @@ -136,8 +147,6 @@ public IEnumerator<IMarketData> GetEnumerator()
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public int Count { get { return _storage.Count; } }
}
}
}
37 changes: 37 additions & 0 deletions src/CoiniumServ/Pools/IMarketInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#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 Newtonsoft.Json;

namespace CoiniumServ.Pools
{
[JsonObject(MemberSerialization.OptIn)]
public interface IMarketInfo
{
[JsonProperty("priceInBtc")]
decimal PriceInBtc { get; }

[JsonProperty("priceInUsd")]
decimal PriceInUsd { get; }
}
}
3 changes: 3 additions & 0 deletions src/CoiniumServ/Pools/IPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public interface IPool : IJsonService
[JsonProperty("network")]
INetworkInfo NetworkInfo { get; }

[JsonProperty("market")]
IMarketInfo MarketInfo { get; }

[JsonProperty("blocks")]
IBlockRepository BlockRepository { get; }

Expand Down
57 changes: 57 additions & 0 deletions src/CoiniumServ/Pools/MarketInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#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 CoiniumServ.Markets;
using Serilog;

namespace CoiniumServ.Pools
{
public class MarketInfo : IMarketInfo
{
private readonly IPoolConfig _poolConfig;

private readonly IMarketManager _marketManager;

private readonly ILogger _logger;

public decimal PriceInBtc { get; private set; }

public decimal PriceInUsd { get; private set; }

public MarketInfo(IMarketManager marketManager, IPoolConfig poolConfig)
{
_logger = Log.ForContext<MarketInfo>().ForContext("Component", poolConfig.Coin.Name);

_poolConfig = poolConfig;
_marketManager = marketManager;
_marketManager.Update += OnMarketUpdate;
}

private void OnMarketUpdate(object sender, EventArgs e)
{
PriceInBtc = Math.Round((decimal) _marketManager.GetBestMarketFor(_poolConfig.Coin.Symbol, "BTC").Bid, 8);
PriceInUsd = Math.Round((decimal) _marketManager.GetBestMarketFor("BTC", "USD").Bid*PriceInBtc, 8);
}
}
}
4 changes: 4 additions & 0 deletions src/CoiniumServ/Pools/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class Pool : IPool

public INetworkInfo NetworkInfo { get; private set; }

public IMarketInfo MarketInfo { get; private set; }

public IBlockRepository BlockRepository { get; private set; }

public IPaymentRepository PaymentRepository { get; private set; }
Expand Down Expand Up @@ -196,6 +198,8 @@ private void InitManagers()
var blockAccounter = _objectFactory.GetBlockAccounter(Config, _storage, AccountManager);
var paymentProcessor = _objectFactory.GetPaymentProcessor(Config, _storage, Daemon, AccountManager);
_objectFactory.GetPaymentManager(Config, blockProcessor, blockAccounter, paymentProcessor);

MarketInfo = _objectFactory.GetMarketInfo(Config);
}
catch (Exception e)
{
Expand Down
6 changes: 3 additions & 3 deletions src/CoiniumServ/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ static void Main(string[] args)

private static void RunGlobalManagers(IObjectFactory objectFactory)
{
// run market manager.
objectFactory.GetMarketManager();

// start pool manager.
objectFactory.GetPoolManager();

Expand All @@ -93,9 +96,6 @@ private static void RunGlobalManagers(IObjectFactory objectFactory)
// run statistics manager.
objectFactory.GetStatisticsManager();

// run market manager.
objectFactory.GetMarketManager();

// run software repository.
objectFactory.GetSoftwareRepository();

Expand Down

0 comments on commit 843b131

Please sign in to comment.