public override async Task<decimal> GetExchangeRateAsync(Money.CurrencyType c1, Money.CurrencyType c2) {
			await exchangeRateLock.WaitAsync();
			try {
				if(ExchangeRateCache.ContainsKey(c2)) {
					return 1m / (await GetExchangeRateAsync(c2, c1));
				}

				if(!ExchangeRateCache.ContainsKey(c1))
					ExchangeRateCache.Add(c1, new Dictionary<Money.CurrencyType, ExchangeRateInfo>());

				if(ExchangeRateCache[c1].ContainsKey(c2)) {
					if(ExchangeRateCache[c1][c2].Age <= MaxExchangeRateAge)
						return ExchangeRateCache[c1][c2].Rate;

					ExchangeRateCache[c1].Remove(c2);
				}

				decimal rate = await base.GetExchangeRateAsync(c1, c2);
				ExchangeRateCache[c1].Add(c2, new ExchangeRateInfo(rate));
				return rate;
			}
			finally {
				exchangeRateLock.Release();
			}
		}
		static decimal GetExchangeRate(Money.CurrencyType c1, Money.CurrencyType c2, JToken rates) {
			if(c2 != Money.CurrencyType.FindByCode("BTC"))
				throw new NotImplementedException();

			JToken rate = rates[c1.Code];
			if(rate == null)
				throw new Money.UnknownExchangeRateException();

			return rates[c1.Code]["last"].Value<decimal>();
		}
		public static List<Transaction.Output> SelectInpoints(List<Transaction.Output> availableInpoints, Money amountToSend, Money fee, out Money change) {
			var selectedInpoints = new List<Transaction.Output>();
			foreach(var output in availableInpoints) {
				if(selectedInpoints.Select(o => o.Value).Sum() >= amountToSend + fee) break;
				selectedInpoints.Add(output);
			}

			Money selectedTotal = selectedInpoints.Select(o => o.Value).Sum();

			if(selectedTotal < amountToSend + fee)
				throw new InsufficientFundsException();

			change = selectedTotal - (amountToSend + fee);

			return selectedInpoints;
		}
Exemple #4
0
			public virtual Money ExchangeWith(Money money, CurrencyType currency) {
				return Money.Create(money.Currency == currency ? money.Value : money.Value * GetRate(from: money.Currency, to: currency), currency);
			}
			public Delta(Transaction tx, Money value) {
				Transaction = tx;
				Value = value;
			}
Exemple #6
0
		public static async Task<decimal> GetExchangeRateAsync(Money.CurrencyType c1, Money.CurrencyType c2) {
			return await Protocol.GetExchangeRateAsync(c1, c2);
		}
		public async override Task<decimal> GetExchangeRateAsync(Money.CurrencyType c1, Money.CurrencyType c2) {
			if(c2 != Money.CurrencyType.FindByCode("BTC"))
				return await NextProtocol.GetExchangeRateAsync(c1, c2);

			return GetExchangeRate(c1, c2, await GetJsonAsync("/ticker"));
		}
			protected void ReadFromJson(JToken data) {
				Value = Money.Create(Decimal.Parse(data["value"].Value<string>()), "BTC");
//				Console.WriteLine("BTC: {0}\nSAT: {1}", btc, Value);

				ScriptPubKey = Script.FromString(data["scriptPubKey"].Value<string>());
			}
			protected void ReadPayload(BinaryReader reader) {
				Value = new Money(reader.ReadInt64(), "BTC");

				UInt64 scriptLength = VarInt.Read(reader).Value;
				ScriptPubKey = new Script(reader.ReadBytes((int)scriptLength));
			}
			public Output(Script scriptPubKey, Money value, Transaction transaction = null, UInt32 index = 0) {
				ScriptPubKey = scriptPubKey;
				Value = value;
				Transaction = transaction;
				Index = index;
			}
		public static List<Transaction.Output> SelectInpoints(List<Transaction.Output> availableInpoints, Dictionary<Address, Money> destinations, Money fee, out Money change) {
			return SelectInpoints(availableInpoints, destinations.Values.Sum(), fee, out change);
		}
		public static List<Transaction.Output> SelectInpoints(List<Transaction.Output> availableInpoints, Dictionary<Address, Money> destinations, out Money change) {
			long approxNumberOfInputs = SelectInpoints(availableInpoints, destinations, new Money(0, "BTC"), out change).Count + 1;
			return SelectInpoints(availableInpoints, destinations, ApproxFeeForTx(approxNumberOfInputs, destinations.Count + 1), out change);
		}