public CryptoDataAccounting Calculate() { var cryptoData = ParentEntity.GetCryptoData(); var totalDue = Money.Coins(ParentEntity.ProductInformation.Price / Rate); var paid = Money.Zero; var cryptoPaid = Money.Zero; var paidTxFee = Money.Zero; bool paidEnough = totalDue <= paid; int txCount = 0; var payments = ParentEntity.Payments .Where(p => p.Accounted) .OrderByDescending(p => p.ReceivedTime) .Select(_ => { var txFee = _.GetValue(cryptoData, CryptoCode, cryptoData[_.GetCryptoCode()].TxFee); paid += _.GetValue(cryptoData, CryptoCode); if (!paidEnough) { totalDue += txFee; paidTxFee += txFee; } paidEnough |= totalDue <= paid; if (CryptoCode == _.GetCryptoCode()) { cryptoPaid += _.GetValue(); txCount++; } return(_); }) .ToArray(); if (!paidEnough) { txCount++; totalDue += TxFee; paidTxFee += TxFee; } var accounting = new CryptoDataAccounting(); accounting.TotalDue = totalDue; accounting.Paid = paid; accounting.TxCount = txCount; accounting.CryptoPaid = cryptoPaid; accounting.Due = Money.Max(accounting.TotalDue - accounting.Paid, Money.Zero); accounting.NetworkFee = paidTxFee; return(accounting); }