public async Task <IReadOnlyCollection <AssetBalanceModel> > GetLykkeAsync() { IReadOnlyCollection <Balance> balances = await _balanceService.GetAsync(ExchangeNames.Lykke); IReadOnlyCollection <Credit> credits = await _creditService.GetAllAsync(); string[] assets = balances.Select(o => o.AssetId) .Union(credits.Select(o => o.AssetId)) .ToArray(); var model = new List <AssetBalanceModel>(); foreach (string assetId in assets) { Balance balance = balances.SingleOrDefault(o => o.AssetId == assetId); Credit credit = credits.SingleOrDefault(o => o.AssetId == assetId); decimal balanceAmount = balance?.Amount ?? decimal.Zero; decimal creditAmount = credit?.Amount ?? decimal.Zero; model.Add(new AssetBalanceModel { AssetId = assetId, Amount = balanceAmount, CreditAmount = creditAmount, Disbalance = balanceAmount - creditAmount }); } return(model); }
public async Task <IReadOnlyCollection <BalanceReport> > GetAsync() { IReadOnlyCollection <Balance> lykkeBalances = await _balanceService.GetAsync(ExchangeNames.Lykke); IReadOnlyCollection <Balance> externalBalances = await _balanceService.GetAsync(ExchangeNames.External); IReadOnlyCollection <Credit> credits = await _creditService.GetAllAsync(); IReadOnlyCollection <Domain.AssetSettings> assetsSettings = await _assetSettingsService.GetAllAsync(); string[] assets = externalBalances .Select(o => o.AssetId) .Union(lykkeBalances.Select(o => o.AssetId).Union(credits.Select(o => o.AssetId)) .Select(o => assetsSettings.SingleOrDefault(p => p.LykkeAssetId == o)?.AssetId ?? o)) .ToArray(); var reports = new List <BalanceReport>(); foreach (string assetId in assets) { Domain.AssetSettings assetSettings = assetsSettings.SingleOrDefault(o => o.AssetId == assetId); string lykkeAssetId = assetSettings?.LykkeAssetId ?? assetId; Balance lykkeBalance = lykkeBalances.SingleOrDefault(o => o.AssetId == lykkeAssetId); Balance externalBalance = externalBalances.SingleOrDefault(o => o.AssetId == assetId); Credit credit = credits.SingleOrDefault(o => o.AssetId == lykkeAssetId); decimal lykkeDisbalance = (lykkeBalance?.Amount ?? 0) - (credit?.Amount ?? 0); decimal totalAmount = lykkeDisbalance + (externalBalance?.Amount ?? 0); if (totalAmount != 0) { (decimal? TotalAmountInUsd, decimal? Rate)tuple = (null, null); if (assetId == AssetConsts.UsdAssetId) { tuple = (totalAmount, 1); } else if (assetSettings != null) { Quote quote = await _quoteService .GetAsync(assetSettings.QuoteSource, assetSettings.ExternalAssetPairId); if (quote != null) { tuple = Calculator.CalculateCrossMidPrice(totalAmount, quote, assetSettings.IsInverse); } } reports.Add(new BalanceReport { Asset = assetId, LykkeAssetId = lykkeAssetId, ExternalAssetId = assetId, LykkeAmount = lykkeBalance?.Amount, LykkeCreditAmount = credit?.Amount, LykkeDisbalance = lykkeDisbalance, ExternalAmount = externalBalance?.Amount, TotalAmount = totalAmount, TotalAmountInUsd = tuple.TotalAmountInUsd, CrossRate = tuple.Rate }); } } return(reports); }