public async Task <IEnumerable <FundDTO> > GetAllFunds() { var funds = _cryptoContext.Funds; var dtos = _mapper.Map <IEnumerable <FundDTO> >(funds); var fundDtos = dtos as FundDTO[] ?? dtos.ToArray(); foreach (var fundDto in fundDtos) { var exchange = await _cryptoContext.Exchanges.FindAsync(fundDto.ExchangeId); fundDto.ExchangeName = exchange.ExchangeId.ToString(); fundDto.CurrentFiatRate = await _marketData.GetCurrentRate("CHF", fundDto.Currency); fundDto.WorthFiat = fundDto.Amount * fundDto.CurrentFiatRate; fundDto.CoinMeta = await _marketData.GetCoinInfo(fundDto.Currency); } return(fundDtos); }
public async Task <AggrInvestmentDTO> Get() { var aggregation = new AggrInvestmentDTO(); var trades = _cryptoContext.Transactions.Where(t => t.Type == TransactionType.Trade) .OrderByDescending(t => t.DateTime); aggregation.Investments = new List <InvestmentDTO>(); foreach (var trade in trades) { var currentRate = await _marketData.GetCurrentRate("CHF", trade.BuyCurrency); var dto = _mapper.Map <InvestmentDTO>(trade); dto.CurrentFiatRate = currentRate; dto.CurrentFiatValue = dto.BuyAmount * currentRate; aggregation.Investments.Add(dto); aggregation.TotalTradeInvest += dto.BuyFiatAmount; aggregation.TotalWorth += dto.CurrentFiatValue; } // This calculation is usless since it needs to be respected when a token is sold var grouped = aggregation.Investments.GroupBy(dto => dto.BuyCurrency) .Select(g => new { Key = g.Key, Profit = g.Sum(v => v.CurrentFiatValue - v.BuyFiatAmount) }); aggregation.TokenProfits = grouped.ToDictionary(arg => arg.Key, arg => arg.Profit); aggregation.Profit = aggregation.TotalWorth - aggregation.TotalTradeInvest; return(aggregation); }