Exemple #1
0
        private static AssetBalanceSummaryDetails[] Add(AssetBalanceSummaryDetails[] a, AssetBalanceSummaryDetails[] b)
        {
            if (a == null && b == null)
            {
                return(null);
            }
            if (a != null && b == null)
            {
                return(a);
            }
            if (a == null && b != null)
            {
                return(b);
            }
            List <AssetBalanceSummaryDetails> result = new List <AssetBalanceSummaryDetails>();

            foreach (var group in a.Concat(b).GroupBy(_ => _.Asset))
            {
                AssetBalanceSummaryDetails details = new AssetBalanceSummaryDetails();
                details.Quantity = group.Sum(o => o.Quantity);
                details.Received = group.Sum(o => o.Received);
                details.Asset    = group.Key;
                result.Add(details);
            }
            return(result.ToArray());
        }
Exemple #2
0
        private static AssetBalanceSummaryDetails[] Minus(AssetBalanceSummaryDetails[] a)
        {
            if (a == null)
            {
                return(null);
            }
            List <AssetBalanceSummaryDetails> result = new List <AssetBalanceSummaryDetails>();

            foreach (var detail in a)
            {
                var balance = new AssetBalanceSummaryDetails();
                balance.Quantity = -detail.Quantity;
                balance.Received = -detail.Received;
                result.Add(balance);
            }
            return(result.ToArray());
        }
Exemple #3
0
        internal static BalanceSummaryDetails CreateFrom(List <OrderedBalanceChange> changes, Network network, bool colored)
        {
            var details = new BalanceSummaryDetails
            {
                Amount           = CalculateAmount(changes, c => c is Coin),
                TransactionCount = changes.Count,
                Received         = changes.Select(_ => _.Amount < Money.Zero ? Money.Zero : _.Amount).Sum(),
            };

            if (colored)
            {
                Dictionary <AssetId, AssetBalanceSummaryDetails> coloredDetails = new Dictionary <AssetId, AssetBalanceSummaryDetails>();
                foreach (var change in changes)
                {
                    foreach (var coin in change.ReceivedCoins.OfType <ColoredCoin>())
                    {
                        AssetBalanceSummaryDetails coloredDetail = null;
                        if (!coloredDetails.TryGetValue(coin.AssetId, out coloredDetail))
                        {
                            coloredDetail       = new AssetBalanceSummaryDetails();
                            coloredDetail.Asset = coin.AssetId.GetWif(network);
                            coloredDetails.Add(coin.AssetId, coloredDetail);
                        }
                        coloredDetail.Quantity += (long)coin.Asset.Quantity;
                        coloredDetail.Received += (long)coin.Asset.Quantity;
                    }
                    foreach (var coin in change.SpentCoins.OfType <ColoredCoin>())
                    {
                        AssetBalanceSummaryDetails coloredDetail = null;
                        if (!coloredDetails.TryGetValue(coin.AssetId, out coloredDetail))
                        {
                            coloredDetail       = new AssetBalanceSummaryDetails();
                            coloredDetail.Asset = coin.AssetId.GetWif(network);
                            coloredDetails.Add(coin.AssetId, coloredDetail);
                        }
                        coloredDetail.Quantity -= (long)coin.Asset.Quantity;
                    }
                }
                details.Assets = coloredDetails.Values.ToArray();
            }
            return(details);
        }