Ejemplo n.º 1
0
    public async Task <IList <ExchangeRateDto> > GetExchangeRates(ExchangeRateSearchParameters exchangeRateSearchParameters)
    {
        Expression <Func <ExchangeRate, bool> > predicate = exchangeRate =>
                                                            exchangeRateSearchParameters.Currencies == null || exchangeRateSearchParameters.Currencies.Any(currency => exchangeRate.Currency == currency);

        return(await _dbRepository.WhereAsync <ExchangeRate, ExchangeRateDto>(predicate));
    }
Ejemplo n.º 2
0
    public async Task <IList <AccountDto> > GetAccounts(AccountSearchParameters accountSearchParameters)
    {
        Expression <Func <Account, bool> > predicate = account =>
                                                       (accountSearchParameters.Currencies == null || accountSearchParameters.Currencies.Any(currency => account.Currency.Contains(currency))) &&
                                                       (accountSearchParameters.AccountIds == null || accountSearchParameters.AccountIds.Any(accountId => account.Id == accountId)) &&
                                                       (accountSearchParameters.Exchanges == null || accountSearchParameters.Exchanges.Any(bank => account.Exchange.Contains(bank)));

        var accounts = await _dbRepository
                       .WhereAsync <Account, AccountDto>(predicate);

        if (!accountSearchParameters.MergeAccountsWithSameNameFromDifferentExchanges)
        {
            return(accounts);
        }

        var mergedAccounts = new Dictionary <string, AccountDto>();

        foreach (var account in accounts)
        {
            if (mergedAccounts.TryGetValue(account.Currency, out var mergedAccount))
            {
                mergedAccount.Balance      += account.Balance;
                mergedAccount.Exchange     += $", {account.Exchange}";
                mergedAccount.MergedAccount = true;
            }
            else
            {
                mergedAccounts.Add(account.Currency, account);
            }
        }

        return(mergedAccounts.Values.ToList());
    }
Ejemplo n.º 3
0
    public async Task <IList <AssetHistoryDto> > GetAssetHistory(AssetHistorySearchParameters assetHistorySearchParameters)
    {
        Expression <Func <Asset, bool> > predicate = accountBalance =>
                                                     (assetHistorySearchParameters.Currencies == null ||
                                                      assetHistorySearchParameters.Currencies.Any(currency =>
                                                                                                  accountBalance.Account.Currency.Contains(currency))) &&
                                                     (assetHistorySearchParameters.AccountIds == null ||
                                                      assetHistorySearchParameters.AccountIds.Any(accountId => accountBalance.AccountId == accountId)) &&
                                                     (assetHistorySearchParameters.Exchanges == null ||
                                                      assetHistorySearchParameters.Exchanges.Any(exchange =>
                                                                                                 accountBalance.Account.Exchange.Contains(exchange))) &&
                                                     (assetHistorySearchParameters.FromDate == null ||
                                                      accountBalance.CreatedDate >= assetHistorySearchParameters.FromDate.Value) &&
                                                     (assetHistorySearchParameters.ToDate == null ||
                                                      accountBalance.CreatedDate <= assetHistorySearchParameters.ToDate.Value);

        var assets = await _dbRepository
                     .WhereAsync <Asset, AssetHistoryDto>(predicate, queryable => queryable.Include(x => x.Account));

        return(assets);
    }
    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);
    }