public IEnumerable <ICoin> Select(IEnumerable <ICoin> coins, IMoney target)
        {
            var    result = new List <ICoin>();
            IMoney total  = Money.Zero;

            if (target.CompareTo(Money.Zero) == 0)
            {
                return(result);
            }

            foreach (ICoin coin in coins)
            {
                // Build up our ongoing total
                total = total.Add(coin.Amount);
                result.Add(coin);

                // If we make the target, return the current set.
                if (total.CompareTo(target) >= 0)
                {
                    return(result);
                }
            }

            // If we are here we didn't have enough funds.
            return(null);
        }
Exemple #2
0
        public IEnumerable <ICoin> Select(IEnumerable <ICoin> coins, IMoney target)
        {
            var    result = new List <ICoin>();
            IMoney total  = Money.Zero;

            if (target.CompareTo(Money.Zero) == 0)
            {
                return(result);
            }


            var orderedCoinGroups = coins.GroupBy(c => new Key().ScriptPubKey)
                                    .Select(scriptPubKeyCoins => new
            {
                Amount = scriptPubKeyCoins.Select(c => c.Amount).Sum(Money.Zero),
                Coins  = scriptPubKeyCoins.ToList()
            }).OrderBy(c => c.Amount);

            //If any of your UTXO² matches the Target¹ it will be used.
            var targetCoin = orderedCoinGroups.FirstOrDefault(c => c.Amount.CompareTo(target) == 0);

            if (targetCoin != null)
            {
                return(targetCoin.Coins);
            }

            foreach (var coinGroup in orderedCoinGroups)
            {
                // If this UTXO is greater than the target, just use it.
                if (coinGroup.Amount.CompareTo(target) == 1)
                {
                    return(coinGroup.Coins);
                }

                // Build up our ongoing total
                total = total.Add(coinGroup.Amount);
                result.AddRange(coinGroup.Coins);

                // If we go over the total, return the current set.
                if (total.CompareTo(target) == 1)
                {
                    return(result);
                }
            }

            // If we didn't have enough funds, return null
            if (total.CompareTo(target) == -1)
            {
                return(null);
            }

            return(result);
        }
Exemple #3
0
		public static IMoney Sum(this IEnumerable<IMoney> moneys, IMoney zero)
		{
			if(moneys == null)
				throw new ArgumentNullException("moneys");
			if(zero == null)
				throw new ArgumentNullException("zero");
			IMoney result = zero;
			foreach(var money in moneys)
			{
				result = result.Add(money);
			}
			return result;
		}
Exemple #4
0
        private void AppendMoney(Money aMoney)
        {
            IMoney old = FindMoney(aMoney.Currency);

            if (old == null)
            {
                fMonies.Add(aMoney);
                return;
            }
            fMonies.Remove(old);
            IMoney sum = old.Add(aMoney);

            if (sum.IsZero)
            {
                return;
            }
            fMonies.Add(sum);
        }
Exemple #5
0
        public static IMoney Sum(this IEnumerable <IMoney> moneys, IMoney zero)
        {
            if (moneys == null)
            {
                throw new ArgumentNullException(nameof(moneys));
            }
            if (zero == null)
            {
                throw new ArgumentNullException(nameof(zero));
            }
            IMoney result = zero;

            foreach (var money in moneys)
            {
                result = result.Add(money);
            }
            return(result);
        }