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();
            }
        }
Esempio n. 2
0
        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")));
        }
Esempio n. 3
0
        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>());
        }
Esempio n. 4
0
 public static async Task <decimal> GetExchangeRateAsync(Money.CurrencyType c1, Money.CurrencyType c2)
 {
     return(await Protocol.GetExchangeRateAsync(c1, c2));
 }
Esempio n. 5
0
 public static Money Sum(this IEnumerable <Money> source)
 {
     Money.CurrencyType currency = source.Any() ? source.First().Currency : Money.DefaultCurrencyType;
     return(source.Aggregate(new Money(0, currency), (a, b) => a + b));
 }