FreeMoney.BitcoinExchangeRateService.LookupRate C# (CSharp) Method

LookupRate() public method

public LookupRate ( string currency_code ) : decimal
currency_code string
return decimal
        public decimal LookupRate(string currency_code)
        {
            //string useragent = "Bitcoin payment module for OpenSim - https://github.com/edmundedgar/Mod-Bitcoin";
            string url = m_config["bitcoin_exchange_rate_service_1_url"];
            HttpWebRequest httpWebRequest=(HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse ();
            string response;
            using (StreamReader streamReader = new StreamReader (httpWebResponse.GetResponseStream ())) {
                response = streamReader.ReadToEnd ();
                streamReader.Close ();
            }

            if (httpWebResponse.StatusCode != HttpStatusCode.OK) {
                return 0.0m;
            }

            JObject jo = JObject.Parse(response);
            JObject currency_rates = (JObject)jo[currency_code];
            string currency_rate = (string)currency_rates["24h"];
            return Decimal.Parse(currency_rate);
        }

Usage Example

コード例 #1
0
        decimal ToBTC(decimal amount, string currency_code)
        {
            m_log.Info("[FreeMoney] Trying to convert " + amount.ToString() + " " + currency_code + " to Bitcoins.");

            // If we have a hard-coded exchange rate, use that.
            // If not, try to use a dynamic service

            // TODO: With the dynamic service, cache the result.
            decimal exchange_rate = Decimal.Parse(m_config["bitcoin_exchange_rate"]);

            if (exchange_rate == 0m)
            {
                m_log.Info("[FreeMoney] Looking up the exchange rate.");
                BitcoinExchangeRateService serv = new BitcoinExchangeRateService(m_config);
                exchange_rate = serv.LookupRate(currency_code);
            }

            decimal btc_amount = Decimal.Round((amount / exchange_rate), 4);

            m_log.Info("[FreeMoney] " + amount.ToString() + " " + currency_code + " equals " + btc_amount.ToString() + " Bitcoins.");

            return(btc_amount);
        }
All Usage Examples Of FreeMoney.BitcoinExchangeRateService::LookupRate