public async Task <ExchangeRateDto?> CreateOrUpdateExchangeRate(string currency) { var exchangeRateFromCoinbase = await _coinbaseConnector.GetExchangeRatesForCurrency(currency); if (exchangeRateFromCoinbase == null) { _logger.LogError("Data from Coinbase for exchange rate {Currency} was null", currency); return(null); } var nokRate = exchangeRateFromCoinbase.Rates[ExchangeRateConstants.NOK]; var usdRate = exchangeRateFromCoinbase.Rates[ExchangeRateConstants.USD]; var eurRate = exchangeRateFromCoinbase.Rates[ExchangeRateConstants.EUR]; var exchangeRates = await _exchangeRateProvider.GetExchangeRates(new ExchangeRateSearchParameters { Currencies = new [] { currency } }); var exchangeRate = exchangeRates.FirstOrDefault(); if (exchangeRate != null) { exchangeRate.NOKRate = nokRate; exchangeRate.USDRate = usdRate; exchangeRate.EURRate = eurRate; _dbRepository.QueueUpdate <ExchangeRate, ExchangeRateDto>(exchangeRate); } else { exchangeRate = new ExchangeRateDto { Currency = currency, NOKRate = nokRate, USDRate = usdRate, EURRate = eurRate }; _dbRepository.QueueAdd <ExchangeRate, ExchangeRateDto>(exchangeRate); } await _dbRepository.ExecuteQueueAsync(); return(exchangeRate); }
public async Task UpdateAccountBalance() { var coinbaseProAccounts = await _dbRepository .WhereAsync <Account, AccountDto>(x => x.Exchange == "CoinbasePro"); var coinbaseProAccountsCount = coinbaseProAccounts.Count; _logger.LogInformation("Updating {Count} CoinbasePro-accounts", coinbaseProAccountsCount); var counter = 0; var timer = new Stopwatch(); timer.Start(); foreach (var account in coinbaseProAccounts) { var now = DateTime.Now; var assetHistoryForCurrentDay = GetAssetHistoryForCurrentDay(account, now); if (assetHistoryForCurrentDay == null) { AddAssetHistory(account); } else { UpdateAssetHistory(assetHistoryForCurrentDay, account); } counter++; } await _dbRepository.ExecuteQueueAsync(); timer.Stop(); _logger.LogInformation("Finished updating CoinbasePro asset history for {Count} of {Total} accounts in {Seconds} seconds", counter, coinbaseProAccountsCount, timer.Elapsed.TotalSeconds); }
public async Task UpdateAccounts() { var accountsInDb = await _dbRepository .WhereAsync <Account, AccountDto>(x => x.Exchange == "Coinbase"); var coinbaseAccounts = await _coinbaseConnector.GetAccounts(); var coinbaseAccountsCount = coinbaseAccounts.Count; var counter = 0; _logger.LogInformation("Updating {Count} Coinbase-accounts", coinbaseAccountsCount); var timer = new Stopwatch(); timer.Start(); foreach (var coinbaseAccount in coinbaseAccounts) { try { await UpdateAccount(coinbaseAccount, accountsInDb); counter++; } catch (Exception e) { _logger.LogError(e, "Failed updating Coinbase-account {Account}", coinbaseAccount.Currency.Name); } } await _dbRepository.ExecuteQueueAsync(); timer.Stop(); _logger.LogInformation("Finished updating {Counter} of {Total} Coinbase-accounts in {Seconds} seconds", counter, coinbaseAccountsCount, timer.Elapsed.TotalSeconds); }