2018-04-14 15:35:52 +02:00
|
|
|
|
using System;
|
2018-04-15 14:18:51 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-04-14 15:35:52 +02: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 14:18:51 +02:00
|
|
|
|
using BTCPayServer.Logging;
|
2018-04-18 09:07:16 +02:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2018-04-23 09:09:18 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2018-04-14 15:35:52 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.HostedServices
|
|
|
|
|
{
|
2018-04-24 00:40:36 +02:00
|
|
|
|
public class RatesHostedService : BaseAsyncService
|
2018-04-14 15:35:52 +02:00
|
|
|
|
{
|
|
|
|
|
private SettingsRepository _SettingsRepository;
|
2018-04-15 14:18:51 +02:00
|
|
|
|
private IRateProviderFactory _RateProviderFactory;
|
2018-04-18 09:07:16 +02:00
|
|
|
|
private CoinAverageSettings _coinAverageSettings;
|
|
|
|
|
public RatesHostedService(SettingsRepository repo,
|
|
|
|
|
CoinAverageSettings coinAverageSettings,
|
2018-04-15 14:18:51 +02:00
|
|
|
|
IRateProviderFactory rateProviderFactory)
|
2018-04-14 15:35:52 +02:00
|
|
|
|
{
|
|
|
|
|
this._SettingsRepository = repo;
|
2018-04-15 14:18:51 +02:00
|
|
|
|
_RateProviderFactory = rateProviderFactory;
|
2018-04-18 09:07:16 +02:00
|
|
|
|
_coinAverageSettings = coinAverageSettings;
|
2018-04-14 15:35:52 +02:00
|
|
|
|
}
|
2018-04-18 09:07:16 +02:00
|
|
|
|
|
2018-04-24 00:40:36 +02:00
|
|
|
|
internal override Task[] initializeTasks()
|
2018-04-15 14:18:51 +02:00
|
|
|
|
{
|
2018-04-24 00:40:36 +02:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
createLoopTask(RefreshCoinAverageSupportedExchanges),
|
|
|
|
|
createLoopTask(RefreshCoinAverageSettings)
|
|
|
|
|
};
|
2018-04-15 14:18:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 00:40:36 +02:00
|
|
|
|
async Task RefreshCoinAverageSupportedExchanges()
|
2018-04-14 15:35:52 +02:00
|
|
|
|
{
|
2018-04-19 09:54:25 +02:00
|
|
|
|
await new SynchronizationContextRemover();
|
2018-04-24 00:40:36 +02:00
|
|
|
|
var tickers = await new CoinAverageRateProvider("BTC") { Authenticator = _coinAverageSettings }.GetExchangeTickersAsync();
|
|
|
|
|
_coinAverageSettings.AvailableExchanges = tickers
|
|
|
|
|
.Exchanges
|
|
|
|
|
.Select(c => (c.DisplayName, c.Name))
|
|
|
|
|
.ToArray();
|
2018-04-27 04:44:21 +02:00
|
|
|
|
await Task.Delay(TimeSpan.FromHours(5), _SyncToken);
|
2018-04-18 09:07:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 00:40:36 +02:00
|
|
|
|
async Task RefreshCoinAverageSettings()
|
2018-04-18 09:07:16 +02:00
|
|
|
|
{
|
2018-04-24 00:40:36 +02: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 09:07:16 +02:00
|
|
|
|
{
|
2018-04-24 00:40:36 +02:00
|
|
|
|
_coinAverageSettings.KeyPair = (rates.PublicKey, rates.PrivateKey);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_coinAverageSettings.KeyPair = null;
|
|
|
|
|
}
|
2018-04-27 04:44:21 +02:00
|
|
|
|
await _SettingsRepository.WaitSettingsChanged<RatesSetting>(_SyncToken);
|
2018-04-14 15:35:52 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|