Skip to content

Commit

Permalink
Minor fixes for BTC wallet and GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
devos50 committed Mar 16, 2018
1 parent f8bf5a9 commit 95c1fc5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Tribler/Core/Modules/restapi/wallets_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ def render_POST(self, request):
"""
.. http:get:: /wallets/(string:wallet identifier)/transfer
A GET request to this endpoint will return past transactions of a specific wallet.
A POST request to this endpoint will transfer some units from a wallet to another address.
**Example request**:
.. sourcecode:: none
curl -X GET http://localhost:8085/wallets/BTC/transfer
curl -X POST http://localhost:8085/wallets/BTC/transfer
--data "amount=0.3&destination=mpC1DDgSP4PKc5HxJzQ5w9q6CGLBEQuLsN"
**Example response**:
Expand Down
38 changes: 37 additions & 1 deletion Tribler/Test/Community/Market/Wallet/test_btc_wallet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from jsonrpclib import ProtocolError
from twisted.internet.defer import inlineCallbacks, succeed, Deferred

from Tribler.Test.Core.base_test import MockObject
Expand Down Expand Up @@ -143,8 +144,43 @@ def test_get_transactions(self):
wallet = BitcoinWallet(self.session_base_dir)
mock_daemon = MockObject()
mock_server = MockObject()
transactions = [{'value': -1, 'txid': 'a', 'timestamp': 1}, {'value': 1, 'txid': 'a', 'timestamp': 1}]
transactions = [{
'value': -1,
'txid': 'a',
'timestamp': 1,
'input_addresses': ['a', 'b'],
'output_addresses': ['c', 'd'],
'confirmations': 3
}, {
'value': 1,
'txid': 'b',
'timestamp': False, # In Electrum, this means that the transaction has not been confirmed yet
'input_addresses': ['a', 'b'],
'output_addresses': ['c', 'd'],
'confirmations': 0
}]
mock_server.run_cmdline = lambda _: transactions
mock_daemon.get_server = lambda _: mock_server
wallet.get_daemon = lambda: mock_daemon
return wallet.get_transactions()

@deferred(timeout=10)
def test_get_transactions_error(self):
"""
Test whether no transactions are returned when there's a protocol in the JSON RPC protocol
"""
wallet = BitcoinWallet(self.session_base_dir)
mock_daemon = MockObject()
mock_server = MockObject()

def failing_run_cmdline(*_):
raise ProtocolError()

mock_server.run_cmdline = failing_run_cmdline
mock_daemon.get_server = lambda _: mock_server
wallet.get_daemon = lambda: mock_daemon

def verify_transactions(transactions):
self.assertFalse(transactions)

return wallet.get_transactions().addCallback(verify_transactions)
14 changes: 13 additions & 1 deletion Tribler/Test/Community/Market/test_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def test_to_network(self):
# Test for to network
self.assertEquals(NotImplemented, self.trade.to_network())


class ProposedTradeTestSuite(unittest.TestCase):
"""Proposed trade test cases."""

Expand Down Expand Up @@ -61,6 +60,19 @@ def test_from_network(self):
self.assertEquals(Quantity(30, 'MC'), data.quantity)
self.assertEquals(Timestamp(1462224447.117), data.timestamp)

def test_has_acceptable_price(self):
"""
Test the acceptable price method
"""
self.assertTrue(self.proposed_trade.has_acceptable_price(True, Price(63000, 'BTC')))
self.assertFalse(self.proposed_trade.has_acceptable_price(False, Price(63000, 'BTC')))
self.assertTrue(self.proposed_trade.has_acceptable_price(False, Price(64000, 'BTC')))
self.assertFalse(self.proposed_trade.has_acceptable_price(True, Price(64000, 'BTC')))

# Test a price close to the proposed price
self.assertTrue(self.proposed_trade.has_acceptable_price(False, Price(63400.0 - 1e-07, 'BTC')))
self.assertTrue(self.proposed_trade.has_acceptable_price(True, Price(63400.0 - 1e-07, 'BTC')))


class DeclinedTradeTestSuite(unittest.TestCase):
"""Declined trade test cases."""
Expand Down
4 changes: 2 additions & 2 deletions Tribler/community/market/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,10 +1033,10 @@ def received_proposed_trade(self, _, data):
declined_trade = Trade.decline(self.message_repository.next_identity(),
Timestamp.now(), proposed_trade, decline_reason)
self.logger.debug("Declined trade made with id: %s for proposed trade with id: %s "
"(valid? %s, available quantity of order: %s, reserved: %s, traded: %s)",
"(valid? %s, available quantity of order: %s, reserved: %s, traded: %s), reason: %s",
str(declined_trade.message_id), str(proposed_trade.message_id),
order.is_valid(), order.available_quantity, order.reserved_quantity,
order.traded_quantity)
order.traded_quantity, decline_reason)
self.send_declined_trade(declined_trade)
else:
self.logger.debug("Proposed trade received with id: %s for order with id: %s",
Expand Down
8 changes: 7 additions & 1 deletion Tribler/community/market/core/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,13 @@ def has_acceptable_price(self, is_ask, order_price):
Return whether this trade proposal has an acceptable price.
:rtype: bool
"""
return (is_ask and self.price >= order_price) or (not is_ask and self.price <= order_price)
def isclose(price_a, price_b):
price_a = float(price_a)
price_b = float(price_b)
return abs(price_a - price_b) <= 1e-06

return (is_ask and (self.price >= order_price or isclose(self.price, order_price))) or \
(not is_ask and (self.price <= order_price or isclose(self.price, order_price)))

def to_network(self):
"""
Expand Down
34 changes: 23 additions & 11 deletions Tribler/community/market/wallet/btc_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import imp
import keyring
from Tribler.Core.Utilities.install_dir import get_base_path
from jsonrpclib import ProtocolError
from twisted.internet.defer import Deferred, succeed, fail, inlineCallbacks
from twisted.internet.task import LoopingCall

Expand Down Expand Up @@ -150,12 +151,23 @@ def get_balance(self):
"""
Return the balance of the wallet.
"""
divider = 100000000
if self.created:
confirmed, unconfirmed, unmatured = self.wallet.get_balance()
options = {'nolnet': False, 'password': None, 'verbose': False, 'cmd': 'getbalance',
'wallet_path': self.wallet_file, 'testnet': self.testnet, 'segwit': False,
'cwd': self.wallet_dir,
'portable': False}
config = SimpleConfig(options)

server = self.get_daemon().get_server(config)
result = server.run_cmdline(options)

confirmed = float(result['confirmed'])
unconfirmed = float(result['unconfirmed']) if 'unconfirmed' in result else 0
unconfirmed += (float(result['unmatured']) if 'unmatured' in result else 0)

return succeed({
"available": float(confirmed) / divider,
"pending": float(unconfirmed + unmatured) / divider,
"available": confirmed,
"pending": unconfirmed,
"currency": 'BTC'
})
else:
Expand Down Expand Up @@ -226,17 +238,17 @@ def get_transactions(self):
config = SimpleConfig(options)

server = self.get_daemon().get_server(config)
result = server.run_cmdline(options)
try:
result = server.run_cmdline(options)
except ProtocolError:
self._logger.error("Unable to fetch transactions from BTC wallet!")
return succeed([])

transactions = []
for transaction in result:
outgoing = transaction['value'] < 0
if outgoing:
from_address = self.get_address()
to_address = ''
else:
from_address = ''
to_address = self.get_address()
from_address = ','.join(transaction['input_addresses'])
to_address = ','.join(transaction['output_addresses'])

transactions.append({
'id': transaction['txid'],
Expand Down
13 changes: 8 additions & 5 deletions TriblerGUI/qt_resources/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>875</width>
<height>768</height>
<height>777</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -7982,8 +7982,8 @@ QTabBar::tab:selected {
<rect>
<x>0</x>
<y>0</y>
<width>673</width>
<height>341</height>
<width>131</width>
<height>260</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout_4">
Expand Down Expand Up @@ -11196,7 +11196,7 @@ QTabBar::tab:selected {
}</string>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tab_4">
<attribute name="title">
Expand Down Expand Up @@ -11273,8 +11273,11 @@ QTabBar::tab:selected {
</property>
<item>
<widget class="QTreeWidget" name="wallet_transactions_list">
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
<property name="indentation">
<number>8</number>
<number>0</number>
</property>
<column>
<property name="text">
Expand Down
2 changes: 1 addition & 1 deletion TriblerGUI/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def timestamp_to_time(timestamp):

diff = today - discovered
if diff.days > 0 or today.day != discovered.day:
return discovered.strftime('%d-%m-%Y')
return discovered.strftime('%d-%m-%Y %H:%M')
return discovered.strftime('Today %H:%M')


Expand Down
1 change: 1 addition & 0 deletions TriblerGUI/widgets/marketpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def on_bid(self, bid):

def on_transaction_complete(self, transaction):
if transaction["mine"]:
transaction = transaction["tx"]
main_text = "Transaction with price %f %s and quantity %f %s completed." \
% (transaction["price"], transaction["price_type"],
transaction["quantity"], transaction["quantity_type"])
Expand Down
8 changes: 5 additions & 3 deletions TriblerGUI/widgets/marketwalletspage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from TriblerGUI.dialogs.confirmationdialog import ConfirmationDialog
from TriblerGUI.tribler_action_menu import TriblerActionMenu
from TriblerGUI.tribler_request_manager import TriblerRequestManager
from TriblerGUI.utilities import get_image_path
from TriblerGUI.utilities import get_image_path, timestamp_to_time


class MarketWalletsPage(QWidget):
Expand Down Expand Up @@ -103,6 +103,7 @@ def initialize_wallet_info(self, wallet_id, pressed_button):
button.setChecked(False)

self.active_wallet = wallet_id
self.window().wallet_info_tabs.setCurrentIndex(0)
self.window().wallet_address_label.setText(self.wallets[wallet_id]['address'])

# Create a QR code of the wallet address
Expand All @@ -126,11 +127,11 @@ def initialize_wallet_info(self, wallet_id, pressed_button):
self.window().wallet_address_qr_label.setText("QR Code functionality not available!")

def load_transactions(self, wallet_id):
self.window().wallet_transactions_list.clear()
self.request_mgr = TriblerRequestManager()
self.request_mgr.perform_request("wallets/%s/transactions" % wallet_id, self.on_transactions)

def on_transactions(self, transactions):
self.window().wallet_transactions_list.clear()
for transaction in transactions["transactions"]:
item = QTreeWidgetItem(self.window().wallet_transactions_list)
item.setText(0, "Sent" if transaction["outgoing"] else "Received")
Expand All @@ -139,7 +140,8 @@ def on_transactions(self, transactions):
item.setText(3, "%g %s" % (transaction["amount"], transaction["currency"]))
item.setText(4, "%g %s" % (transaction["fee_amount"], transaction["currency"]))
item.setText(5, transaction["id"])
item.setText(6, transaction["timestamp"])
timestamp = timestamp_to_time(float(transaction["timestamp"])) if transaction["timestamp"] != "False" else "-"
item.setText(6, timestamp)
self.window().wallet_transactions_list.addTopLevelItem(item)

def on_add_wallet_clicked(self):
Expand Down

0 comments on commit 95c1fc5

Please sign in to comment.