2018-04-14 22:35:52 +09:00
|
|
|
|
using System;
|
2018-04-15 21:18:51 +09:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Services;
|
|
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2018-04-15 21:18:51 +09:00
|
|
|
|
using BTCPayServer.Logging;
|
2018-04-18 16:07:16 +09:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2018-04-23 16:09:18 +09:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.HostedServices
|
|
|
|
|
{
|
2018-04-23 17:40:36 -05:00
|
|
|
|
public class RatesHostedService : BaseAsyncService
|
2018-04-14 22:35:52 +09:00
|
|
|
|
{
|
|
|
|
|
private SettingsRepository _SettingsRepository;
|
2018-04-18 16:07:16 +09:00
|
|
|
|
private CoinAverageSettings _coinAverageSettings;
|
2018-05-03 03:32:42 +09:00
|
|
|
|
BTCPayRateProviderFactory _RateProviderFactory;
|
2018-04-18 16:07:16 +09:00
|
|
|
|
public RatesHostedService(SettingsRepository repo,
|
2018-05-03 03:32:42 +09:00
|
|
|
|
BTCPayRateProviderFactory rateProviderFactory,
|
|
|
|
|
CoinAverageSettings coinAverageSettings)
|
2018-04-14 22:35:52 +09:00
|
|
|
|
{
|
|
|
|
|
this._SettingsRepository = repo;
|
2018-04-18 16:07:16 +09:00
|
|
|
|
_coinAverageSettings = coinAverageSettings;
|
2018-05-03 03:32:42 +09:00
|
|
|
|
_RateProviderFactory = rateProviderFactory;
|
2018-04-14 22:35:52 +09:00
|
|
|
|
}
|
2018-04-18 16:07:16 +09:00
|
|
|
|
|
2018-04-26 21:52:04 -05:00
|
|
|
|
internal override Task[] InitializeTasks()
|
2018-04-15 21:18:51 +09:00
|
|
|
|
{
|
2018-04-23 17:40:36 -05:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
2018-04-26 21:52:04 -05:00
|
|
|
|
CreateLoopTask(RefreshCoinAverageSupportedExchanges),
|
|
|
|
|
CreateLoopTask(RefreshCoinAverageSettings)
|
2018-04-23 17:40:36 -05:00
|
|
|
|
};
|
2018-04-15 21:18:51 +09:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 17:40:36 -05:00
|
|
|
|
async Task RefreshCoinAverageSupportedExchanges()
|
2018-04-14 22:35:52 +09:00
|
|
|
|
{
|
2018-04-19 16:54:25 +09:00
|
|
|
|
await new SynchronizationContextRemover();
|
2018-05-03 03:32:42 +09:00
|
|
|
|
var tickers = await new CoinAverageRateProvider() { Authenticator = _coinAverageSettings }.GetExchangeTickersAsync();
|
|
|
|
|
var exchanges = new CoinAverageExchanges();
|
|
|
|
|
foreach(var item in tickers
|
2018-04-23 17:40:36 -05:00
|
|
|
|
.Exchanges
|
2018-05-03 03:32:42 +09:00
|
|
|
|
.Select(c => new CoinAverageExchange(c.Name, c.DisplayName)))
|
|
|
|
|
{
|
|
|
|
|
exchanges.Add(item);
|
|
|
|
|
}
|
|
|
|
|
_coinAverageSettings.AvailableExchanges = exchanges;
|
2018-04-26 21:52:04 -05:00
|
|
|
|
await Task.Delay(TimeSpan.FromHours(5), Cancellation);
|
2018-04-18 16:07:16 +09:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-23 17:40:36 -05:00
|
|
|
|
async Task RefreshCoinAverageSettings()
|
2018-04-18 16:07:16 +09:00
|
|
|
|
{
|
2018-04-23 17:40:36 -05:00
|
|
|
|
await new SynchronizationContextRemover();
|
|
|
|
|
var rates = (await _SettingsRepository.GetSettingAsync<RatesSetting>()) ?? new RatesSetting();
|
|
|
|
|
_RateProviderFactory.CacheSpan = TimeSpan.FromMinutes(rates.CacheInMinutes);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(rates.PrivateKey) && !string.IsNullOrWhiteSpace(rates.PublicKey))
|
2018-04-18 16:07:16 +09:00
|
|
|
|
{
|
2018-04-23 17:40:36 -05:00
|
|
|
|
_coinAverageSettings.KeyPair = (rates.PublicKey, rates.PrivateKey);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_coinAverageSettings.KeyPair = null;
|
|
|
|
|
}
|
2018-04-26 21:52:04 -05:00
|
|
|
|
await _SettingsRepository.WaitSettingsChanged<RatesSetting>(Cancellation);
|
2018-04-14 22:35:52 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|