1 from PyQt4.QtGui import *
2 from PyQt4.QtCore import *
11 from decimal import Decimal
12 from electrum.plugins import BasePlugin
13 from electrum.i18n import _
14 from electrum_gui.qt.util import *
15 from electrum_gui.qt.amountedit import AmountEdit
18 EXCHANGES = ["BitcoinAverage",
32 class Exchanger(threading.Thread):
34 def __init__(self, parent):
35 threading.Thread.__init__(self)
38 self.quote_currencies = None
39 self.lock = threading.Lock()
40 self.query_rates = threading.Event()
41 self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
42 self.parent.exchanges = EXCHANGES
43 self.parent.currencies = ["EUR","GBP","USD","PLN"]
44 self.parent.win.emit(SIGNAL("refresh_exchanges_combo()"))
45 self.parent.win.emit(SIGNAL("refresh_currencies_combo()"))
46 self.is_running = False
48 def get_json(self, site, get_string):
50 connection = httplib.HTTPSConnection(site)
51 connection.request("GET", get_string)
54 resp = connection.getresponse()
55 if resp.reason == httplib.responses[httplib.NOT_FOUND]:
58 json_resp = json.loads(resp.read())
64 def exchange(self, btc_amount, quote_currency):
66 if self.quote_currencies is None:
68 quote_currencies = self.quote_currencies.copy()
69 if quote_currency not in quote_currencies:
71 if self.use_exchange == "CoinDesk":
73 resp_rate = self.get_json('api.coindesk.com', "/v1/bpi/currentprice/" + str(quote_currency) + ".json")
76 return btc_amount * decimal.Decimal(str(resp_rate["bpi"][str(quote_currency)]["rate_float"]))
77 return btc_amount * decimal.Decimal(str(quote_currencies[quote_currency]))
80 self.is_running = False
82 def update_rate(self):
83 self.use_exchange = self.parent.config.get('use_exchange', "Blockchain")
85 "BitcoinAverage": self.update_ba,
86 "BitcoinVenezuela": self.update_bv,
87 "Bitcurex": self.update_bx,
88 "Bitmarket": self.update_bm,
89 "BitPay": self.update_bp,
90 "Blockchain": self.update_bc,
91 "BTCChina": self.update_CNY,
92 "CaVirtEx": self.update_cv,
93 "CoinDesk": self.update_cd,
94 "Coinbase": self.update_cb,
95 "LocalBitcoins": self.update_lb,
96 "Winkdex": self.update_wd,
99 update_rates[self.use_exchange]()
104 self.is_running = True
105 while self.is_running:
106 self.query_rates.clear()
108 self.query_rates.wait(150)
113 resp_currencies = self.get_json('api.coindesk.com', "/v1/bpi/supported-currencies.json")
117 quote_currencies = {}
118 for cur in resp_currencies:
119 quote_currencies[str(cur["currency"])] = 0.0
121 self.quote_currencies = quote_currencies
122 self.parent.set_currencies(quote_currencies)
126 winkresp = self.get_json('winkdex.com', "/static/data/0_600_288.json")
127 ####could need nonce value in GET, no Docs available
130 quote_currencies = {"USD": 0.0}
131 ####get y of highest x in "prices"
132 lenprices = len(winkresp["prices"])
133 usdprice = winkresp["prices"][lenprices-1]["y"]
135 quote_currencies["USD"] = decimal.Decimal(str(usdprice))
137 self.quote_currencies = quote_currencies
140 self.parent.set_currencies(quote_currencies)
144 jsonresp = self.get_json('www.cavirtex.com', "/api/CAD/ticker.json")
147 quote_currencies = {"CAD": 0.0}
148 cadprice = jsonresp["last"]
150 quote_currencies["CAD"] = decimal.Decimal(str(cadprice))
152 self.quote_currencies = quote_currencies
155 self.parent.set_currencies(quote_currencies)
159 jsonresp = self.get_json('www.bitmarket.pl', "/json/BTCPLN/ticker.json")
162 quote_currencies = {"PLN": 0.0}
163 pln_price = jsonresp["last"]
165 quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
167 self.quote_currencies = quote_currencies
170 self.parent.set_currencies(quote_currencies)
174 jsonresp = self.get_json('pln.bitcurex.com', "/data/ticker.json")
177 quote_currencies = {"PLN": 0.0}
178 pln_price = jsonresp["last"]
180 quote_currencies["PLN"] = decimal.Decimal(str(pln_price))
182 self.quote_currencies = quote_currencies
185 self.parent.set_currencies(quote_currencies)
187 def update_CNY(self):
189 jsonresp = self.get_json('data.btcchina.com', "/data/ticker")
192 quote_currencies = {"CNY": 0.0}
193 cnyprice = jsonresp["ticker"]["last"]
195 quote_currencies["CNY"] = decimal.Decimal(str(cnyprice))
197 self.quote_currencies = quote_currencies
200 self.parent.set_currencies(quote_currencies)
204 jsonresp = self.get_json('bitpay.com', "/api/rates")
207 quote_currencies = {}
210 quote_currencies[str(r["code"])] = decimal.Decimal(r["rate"])
212 self.quote_currencies = quote_currencies
215 self.parent.set_currencies(quote_currencies)
219 jsonresp = self.get_json('coinbase.com', "/api/v1/currencies/exchange_rates")
223 quote_currencies = {}
226 if r[:7] == "btc_to_":
227 quote_currencies[r[7:].upper()] = self._lookup_rate_cb(jsonresp, r)
229 self.quote_currencies = quote_currencies
232 self.parent.set_currencies(quote_currencies)
237 jsonresp = self.get_json('blockchain.info', "/ticker")
240 quote_currencies = {}
243 quote_currencies[r] = self._lookup_rate(jsonresp, r)
245 self.quote_currencies = quote_currencies
248 self.parent.set_currencies(quote_currencies)
249 # print "updating exchange rate", self.quote_currencies["USD"]
253 jsonresp = self.get_json('localbitcoins.com', "/bitcoinaverage/ticker-all-currencies/")
256 quote_currencies = {}
259 quote_currencies[r] = self._lookup_rate_lb(jsonresp, r)
261 self.quote_currencies = quote_currencies
264 self.parent.set_currencies(quote_currencies)
269 jsonresp = self.get_json('api.bitcoinvenezuela.com', "/")
272 quote_currencies = {}
274 for r in jsonresp["BTC"]:
275 quote_currencies[r] = Decimal(jsonresp["BTC"][r])
277 self.quote_currencies = quote_currencies
280 self.parent.set_currencies(quote_currencies)
285 jsonresp = self.get_json('api.bitcoinaverage.com', "/ticker/global/all")
288 quote_currencies = {}
291 if not r == "timestamp":
292 quote_currencies[r] = self._lookup_rate_ba(jsonresp, r)
294 self.quote_currencies = quote_currencies
297 self.parent.set_currencies(quote_currencies)
300 def get_currencies(self):
301 return [] if self.quote_currencies == None else sorted(self.quote_currencies.keys())
303 def _lookup_rate(self, response, quote_id):
304 return decimal.Decimal(str(response[str(quote_id)]["15m"]))
305 def _lookup_rate_cb(self, response, quote_id):
306 return decimal.Decimal(str(response[str(quote_id)]))
307 def _lookup_rate_ba(self, response, quote_id):
308 return decimal.Decimal(response[str(quote_id)]["last"])
309 def _lookup_rate_lb(self, response, quote_id):
310 return decimal.Decimal(response[str(quote_id)]["rates"]["last"])
313 class Plugin(BasePlugin):
316 return "Exchange rates"
318 def description(self):
319 return """exchange rates, retrieved from blockchain.info, CoinDesk, or Coinbase"""
322 def __init__(self,a,b):
323 BasePlugin.__init__(self,a,b)
324 self.currencies = [self.config.get('currency', "EUR")]
325 self.exchanges = [self.config.get('use_exchange', "Blockchain")]
328 self.win = self.gui.main_window
329 self.win.connect(self.win, SIGNAL("refresh_currencies()"), self.win.update_status)
330 self.btc_rate = Decimal("0.0")
332 self.exchanger = Exchanger(self)
333 self.exchanger.start()
334 self.gui.exchanger = self.exchanger #
336 def set_currencies(self, currency_options):
337 self.currencies = sorted(currency_options)
338 self.win.emit(SIGNAL("refresh_currencies()"))
339 self.win.emit(SIGNAL("refresh_currencies_combo()"))
341 def get_fiat_balance_text(self, btc_balance, r):
342 # return balance as: 1.23 USD
343 r[0] = self.create_fiat_balance_text(Decimal(btc_balance) / 100000000)
345 def get_fiat_price_text(self, r):
346 # return BTC price as: 123.45 USD
347 r[0] = self.create_fiat_balance_text(1)
352 def get_fiat_status_text(self, btc_balance, r2):
353 # return status as: (1.23 USD) 1 BTC~123.45 USD
356 self.get_fiat_price_text(r)
359 price_text = "1 BTC~%s"%quote
360 fiat_currency = quote[-3:]
361 btc_price = quote[:-4]
362 fiat_balance = Decimal(btc_price) * (Decimal(btc_balance)/100000000)
363 balance_text = "(%.2f %s)" % (fiat_balance,fiat_currency)
364 text = " " + balance_text + " " + price_text + " "
367 def create_fiat_balance_text(self, btc_balance):
368 quote_currency = self.config.get("currency", "EUR")
369 self.exchanger.use_exchange = self.config.get("use_exchange", "Blockchain")
370 cur_rate = self.exchanger.exchange(Decimal("1.0"), quote_currency)
374 quote_balance = btc_balance * Decimal(cur_rate)
375 self.btc_rate = cur_rate
376 quote_text = "%.2f %s" % (quote_balance, quote_currency)
379 def load_wallet(self, wallet):
382 for item in self.wallet.get_tx_history(self.wallet.storage.get("current_account", None)):
383 tx_hash, conf, is_mine, value, fee, balance, timestamp = item
384 tx_list[tx_hash] = {'value': value, 'timestamp': timestamp, 'balance': balance}
386 self.tx_list = tx_list
389 def requires_settings(self):
394 out = BasePlugin.toggle(self)
395 self.win.update_status()
396 if self.config.get('use_exchange_rate'):
400 self.gui.main_window.show_message(_("To see fiat amount when sending bitcoin, please restart Electrum to activate the new GUI settings."))
405 self.exchanger.stop()
407 def history_tab_update(self):
408 if self.config.get('history_rates', 'unchecked') == "checked":
409 cur_exchange = self.config.get('use_exchange', "Blockchain")
411 tx_list = self.tx_list
416 mintimestr = datetime.datetime.fromtimestamp(int(min(tx_list.items(), key=lambda x: x[1]['timestamp'])[1]['timestamp'])).strftime('%Y-%m-%d')
419 maxtimestr = datetime.datetime.now().strftime('%Y-%m-%d')
421 if cur_exchange == "CoinDesk":
423 resp_hist = self.exchanger.get_json('api.coindesk.com', "/v1/bpi/historical/close.json?start=" + mintimestr + "&end=" + maxtimestr)
426 elif cur_exchange == "Winkdex":
428 resp_hist = self.exchanger.get_json('winkdex.com', "/static/data/0_86400_730.json")['prices']
431 elif cur_exchange == "BitcoinVenezuela":
432 cur_currency = self.config.get('currency', "EUR")
433 if cur_currency == "VEF":
435 resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['VEF_BTC']
438 elif cur_currency == "ARS":
440 resp_hist = self.exchanger.get_json('api.bitcoinvenezuela.com', "/historical/index.php")['ARS_BTC']
446 self.gui.main_window.is_edit = True
447 self.gui.main_window.history_list.setColumnCount(6)
448 self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance'), _('Fiat Amount')] )
449 root = self.gui.main_window.history_list.invisibleRootItem()
450 childcount = root.childCount()
451 for i in range(childcount):
454 tx_info = tx_list[str(item.data(0, Qt.UserRole).toPyObject())]
456 newtx = self.wallet.get_tx_history()
457 v = newtx[[x[0] for x in newtx].index(str(item.data(0, Qt.UserRole).toPyObject()))][3]
459 tx_info = {'timestamp':int(time.time()), 'value': v }
461 tx_time = int(tx_info['timestamp'])
462 if cur_exchange == "CoinDesk":
463 tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
465 tx_USD_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(resp_hist['bpi'][tx_time_str]), "USD")
467 tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(str(tx_info['value']))/100000000 , "USD")
468 elif cur_exchange == "Winkdex":
469 tx_time_str = int(tx_time) - (int(tx_time) % (60 * 60 * 24))
471 tx_rate = resp_hist[[x['x'] for x in resp_hist].index(tx_time_str)]['y']
472 tx_USD_val = "%.2f %s" % (Decimal(tx_info['value']) / 100000000 * Decimal(tx_rate), "USD")
474 tx_USD_val = "%.2f %s" % (self.btc_rate * Decimal(tx_info['value'])/100000000 , "USD")
475 elif cur_exchange == "BitcoinVenezuela":
476 tx_time_str = datetime.datetime.fromtimestamp(tx_time).strftime('%Y-%m-%d')
478 num = resp_hist[tx_time_str].replace(',','')
479 tx_BTCVEN_val = "%.2f %s" % (Decimal(str(tx_info['value'])) / 100000000 * Decimal(num), cur_currency)
481 tx_BTCVEN_val = _("No data")
483 if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
484 item.setText(5, tx_USD_val)
485 elif cur_exchange == "BitcoinVenezuela":
486 item.setText(5, tx_BTCVEN_val)
487 if Decimal(str(tx_info['value'])) < 0:
488 item.setForeground(5, QBrush(QColor("#BC1E1E")))
490 for i, width in enumerate(self.gui.main_window.column_widths['history']):
491 self.gui.main_window.history_list.setColumnWidth(i, width)
492 self.gui.main_window.history_list.setColumnWidth(4, 140)
493 self.gui.main_window.history_list.setColumnWidth(5, 120)
494 self.gui.main_window.is_edit = False
497 def settings_widget(self, window):
498 return EnterButton(_('Settings'), self.settings_dialog)
500 def settings_dialog(self):
502 d.setWindowTitle("Settings")
503 layout = QGridLayout(d)
504 layout.addWidget(QLabel(_('Exchange rate API: ')), 0, 0)
505 layout.addWidget(QLabel(_('Currency: ')), 1, 0)
506 layout.addWidget(QLabel(_('History Rates: ')), 2, 0)
508 combo_ex = QComboBox()
509 hist_checkbox = QCheckBox()
510 hist_checkbox.setEnabled(False)
511 if self.config.get('history_rates', 'unchecked') == 'unchecked':
512 hist_checkbox.setChecked(False)
514 hist_checkbox.setChecked(True)
515 ok_button = QPushButton(_("OK"))
519 cur_request = str(self.currencies[x])
522 if cur_request != self.config.get('currency', "EUR"):
523 self.config.set_key('currency', cur_request, True)
524 cur_exchange = self.config.get('use_exchange', "Blockchain")
525 if cur_request == "USD" and (cur_exchange == "CoinDesk" or cur_exchange == "Winkdex"):
526 hist_checkbox.setEnabled(True)
527 elif cur_request == "VEF" and (cur_exchange == "BitcoinVenezuela"):
528 hist_checkbox.setEnabled(True)
529 elif cur_request == "ARS" and (cur_exchange == "BitcoinVenezuela"):
530 hist_checkbox.setEnabled(True)
532 hist_checkbox.setChecked(False)
533 hist_checkbox.setEnabled(False)
534 self.win.update_status()
540 self.fiat_button.setText(cur_request)
543 hist_checkbox.setChecked(False)
544 hist_checkbox.setEnabled(False)
547 cur_request = str(self.exchanges[x])
548 if cur_request != self.config.get('use_exchange', "Blockchain"):
549 self.config.set_key('use_exchange', cur_request, True)
552 self.exchanger.query_rates.set()
553 cur_currency = self.config.get('currency', "EUR")
554 if cur_request == "CoinDesk" or cur_request == "Winkdex":
555 if cur_currency == "USD":
556 hist_checkbox.setEnabled(True)
559 elif cur_request == "BitcoinVenezuela":
560 if cur_currency == "VEF" or cur_currency == "ARS":
561 hist_checkbox.setEnabled(True)
566 set_currencies(combo)
567 self.win.update_status()
569 def on_change_hist(checked):
571 self.config.set_key('history_rates', 'checked')
572 self.history_tab_update()
574 self.config.set_key('history_rates', 'unchecked')
575 self.gui.main_window.history_list.setHeaderLabels( [ '', _('Date'), _('Description') , _('Amount'), _('Balance')] )
576 self.gui.main_window.history_list.setColumnCount(5)
577 for i,width in enumerate(self.gui.main_window.column_widths['history']):
578 self.gui.main_window.history_list.setColumnWidth(i, width)
580 def set_hist_check(hist_checkbox):
581 cur_exchange = self.config.get('use_exchange', "Blockchain")
582 if cur_exchange == "CoinDesk" or cur_exchange == "Winkdex":
583 hist_checkbox.setEnabled(True)
584 elif cur_exchange == "BitcoinVenezuela":
585 hist_checkbox.setEnabled(True)
587 hist_checkbox.setEnabled(False)
589 def set_currencies(combo):
590 current_currency = self.config.get('currency', "EUR")
595 combo.addItems(self.currencies)
597 index = self.currencies.index(current_currency)
600 combo.setCurrentIndex(index)
602 def set_exchanges(combo_ex):
607 combo_ex.addItems(self.exchanges)
609 index = self.exchanges.index(self.config.get('use_exchange', "Blockchain"))
612 combo_ex.setCurrentIndex(index)
617 set_exchanges(combo_ex)
618 set_currencies(combo)
619 set_hist_check(hist_checkbox)
620 combo.currentIndexChanged.connect(on_change)
621 combo_ex.currentIndexChanged.connect(on_change_ex)
622 hist_checkbox.stateChanged.connect(on_change_hist)
623 combo.connect(self.win, SIGNAL('refresh_currencies_combo()'), lambda: set_currencies(combo))
624 combo_ex.connect(d, SIGNAL('refresh_exchanges_combo()'), lambda: set_exchanges(combo_ex))
625 ok_button.clicked.connect(lambda: ok_clicked())
626 layout.addWidget(combo,1,1)
627 layout.addWidget(combo_ex,0,1)
628 layout.addWidget(hist_checkbox,2,1)
629 layout.addWidget(ok_button,3,1)
637 quote_currency = self.config.get("currency", "???")
638 return quote_currency
640 def fiat_dialog(self):
641 if not self.config.get('use_exchange_rate'):
642 self.gui.main_window.show_message(_("To use this feature, first enable the exchange rate plugin."))
645 if not self.gui.main_window.network.is_connected():
646 self.gui.main_window.show_message(_("To use this feature, you must have a network connection."))
649 quote_currency = self.fiat_unit()
651 d = QDialog(self.gui.main_window)
652 d.setWindowTitle("Fiat")
653 vbox = QVBoxLayout(d)
654 text = "Amount to Send in " + quote_currency
655 vbox.addWidget(QLabel(_(text)+':'))
658 fiat_e = AmountEdit(self.fiat_unit)
659 grid.addWidget(fiat_e, 1, 0)
662 self.get_fiat_price_text(r)
665 text = "1 BTC~%s"%quote
666 grid.addWidget(QLabel(_(text)), 4, 0, 3, 0)
668 self.gui.main_window.show_message(_("Exchange rate not available. Please check your network connection."))
672 vbox.addLayout(ok_cancel_buttons(d))
677 fiat = str(fiat_e.text())
679 if str(fiat) == "" or str(fiat) == ".":
683 btcamount = Decimal(fiat) / Decimal(quote)
684 if str(self.gui.main_window.base_unit()) == "mBTC":
685 btcamount = btcamount * 1000
686 quote = "%.8f"%btcamount
687 self.gui.main_window.amount_e.setText( quote )
689 def exchange_rate_button(self, grid):
690 quote_currency = self.config.get("currency", "EUR")
691 self.fiat_button = EnterButton(_(quote_currency), self.fiat_dialog)
692 grid.addWidget(self.fiat_button, 4, 3, Qt.AlignHCenter)