From 3ed6eca95f7bb568dc993bab7f4c73c8c411376d Mon Sep 17 00:00:00 2001 From: tecnovert Date: Wed, 15 Jun 2022 00:25:17 +0200 Subject: [PATCH] ui: Show bids that can be accepted. --- basicswap/basicswap.py | 37 ++++++++++++------- basicswap/basicswap_util.py | 2 +- basicswap/http_server.py | 29 ++++++++++++++- basicswap/interface_xmr.py | 17 ++++----- basicswap/templates/bids.html | 13 +++++++ basicswap/templates/index.html | 1 + .../basicswap/extended/test_xmr_persistent.py | 4 +- tests/basicswap/test_xmr.py | 2 +- 8 files changed, 78 insertions(+), 27 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index bf1b555..711b029 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5210,21 +5210,24 @@ class BasicSwap(BaseApp): continue num_watched_outputs += len(v['watched_outputs']) - bids_sent = 0 - bids_received = 0 - q = self.engine.execute('SELECT was_sent, was_received, COUNT(*) FROM bids GROUP BY was_sent, was_received ') - for r in q: - if r[0]: - bids_sent += r[2] - if r[1]: - bids_received += r[2] - now = int(time.time()) - q = self.engine.execute('SELECT COUNT(*) FROM offers WHERE active_ind = 1 AND expire_at > {}'.format(now)).first() + q_str = '''SELECT + COUNT(CASE WHEN was_sent THEN 1 ELSE NULL END) AS count_sent, + COUNT(CASE WHEN was_received THEN 1 ELSE NULL END) AS count_received, + COUNT(CASE WHEN was_received AND state = {} AND expire_at > {} THEN 1 ELSE NULL END) AS count_available + FROM bids WHERE active_ind = 1'''.format(BidStates.BID_RECEIVED, now) + q = self.engine.execute(q_str).first() + bids_sent = q[0] + bids_received = q[1] + bids_available = q[2] + + q_str = '''SELECT + COUNT(CASE WHEN expire_at > {} THEN 1 ELSE NULL END) AS count_active, + COUNT(CASE WHEN was_sent THEN 1 ELSE NULL END) AS count_sent + FROM offers WHERE active_ind = 1'''.format(now) + q = self.engine.execute(q_str).first() num_offers = q[0] - - q = self.engine.execute('SELECT COUNT(*) FROM offers WHERE was_sent = 1').first() - num_sent_offers = q[0] + num_sent_offers = q[1] rv = { 'network': self.chain, @@ -5233,6 +5236,7 @@ class BasicSwap(BaseApp): 'num_sent_offers': num_sent_offers, 'num_recv_bids': bids_received, 'num_sent_bids': bids_sent, + 'num_available_bids': bids_available, 'num_watched_outputs': num_watched_outputs, } return rv @@ -5455,6 +5459,13 @@ class BasicSwap(BaseApp): else: query_str += 'AND bids.was_received = 1 ' + bid_state_ind = filters.get('bid_state_ind', -1) + if bid_state_ind != -1: + query_str += 'AND bids.state = {} '.format(bid_state_ind) + with_expired = filters.get('with_expired', True) + if with_expired is not True: + query_str += 'AND bids.expire_at > {} '.format(now) + sort_dir = filters.get('sort_dir', 'DESC').upper() sort_by = filters.get('sort_by', 'created_at') query_str += f' ORDER BY bids.{sort_by} {sort_dir}' diff --git a/basicswap/basicswap_util.py b/basicswap/basicswap_util.py index 7a980c3..3bf1739 100644 --- a/basicswap/basicswap_util.py +++ b/basicswap/basicswap_util.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2021 tecnovert +# Copyright (c) 2021-2022 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. diff --git a/basicswap/http_server.py b/basicswap/http_server.py index f03d606..b75e3db 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -18,6 +18,7 @@ from . import __version__ from .util import ( dumpj, ensure, + toBool, format_timestamp, ) from .chainparams import ( @@ -26,6 +27,7 @@ from .chainparams import ( getCoinIdFromTicker, ) from .basicswap_util import ( + BidStates, SwapTypes, DebugTypes, strBidState, @@ -50,6 +52,7 @@ from .ui.util import ( PAGE_LIMIT, describeBid, getCoinName, + listBidStates, get_data_entry, have_data_entry, get_data_entry_or, @@ -716,15 +719,21 @@ class HttpHandler(BaseHTTPRequestHandler): old_states=old_states, ), 'UTF-8') - def page_bids(self, url_split, post_string, sent=False): + def page_bids(self, url_split, post_string, sent=False, available=False): swap_client = self.server.swap_client filters = { 'page_no': 1, + 'bid_state_ind': -1, + 'with_expired': True, 'limit': PAGE_LIMIT, 'sort_by': 'created_at', 'sort_dir': 'desc', } + if available: + filters['bid_state_ind'] = BidStates.BID_RECEIVED + filters['with_expired'] = False + messages = [] form_data = self.checkForm(post_string, 'bids', messages) if form_data and have_data_entry(form_data, 'applyfilters'): @@ -736,11 +745,26 @@ class HttpHandler(BaseHTTPRequestHandler): sort_dir = get_data_entry(form_data, 'sort_dir') ensure(sort_dir in ['asc', 'desc'], 'Invalid sort dir') filters['sort_dir'] = sort_dir + if have_data_entry(form_data, 'state'): + state_ind = get_data_entry(form_data, 'state') + if state_ind != -1: + try: + state = BidStates(state_ind) + except Exception: + raise ValueError('Invalid state') + filters['bid_state_ind'] = state_ind + if have_data_entry(form_data, 'with_expired'): + with_expired = toBool(get_data_entry(form_data, 'with_expired')) + filters['with_expired'] = with_expired set_pagination_filters(form_data, filters) bids = swap_client.listBids(sent=sent, filters=filters) + page_data = { + 'bid_states': listBidStates() + } + template = env.get_template('bids.html') return bytes(template.render( title=self.server.title, @@ -748,6 +772,7 @@ class HttpHandler(BaseHTTPRequestHandler): page_type='Sent' if sent else 'Received', messages=messages, filters=filters, + data=page_data, bids=[(format_timestamp(b[0]), b[2].hex(), b[3].hex(), strBidState(b[5]), strTxState(b[7]), strTxState(b[8]), b[11]) for b in bids], form_id=os.urandom(8).hex(), @@ -1003,6 +1028,8 @@ class HttpHandler(BaseHTTPRequestHandler): return self.page_bids(url_split, post_string) if url_split[1] == 'sentbids': return self.page_bids(url_split, post_string, sent=True) + if url_split[1] == 'availablebids': + return self.page_bids(url_split, post_string, available=True) if url_split[1] == 'watched': return self.page_watched(url_split, post_string) if url_split[1] == 'smsgaddresses': diff --git a/basicswap/interface_xmr.py b/basicswap/interface_xmr.py index 097852e..5364f64 100644 --- a/basicswap/interface_xmr.py +++ b/basicswap/interface_xmr.py @@ -12,19 +12,21 @@ import basicswap.contrib.ed25519_fast as edf import basicswap.ed25519_fast_util as edu import basicswap.util_xmr as xmr_util from coincurve.ed25519 import ( + ed25519_add, ed25519_get_pubkey, ed25519_scalar_add, - ed25519_add) +) from coincurve.keys import PrivateKey from coincurve.dleag import ( - verify_ed25519_point, - dleag_proof_len, + dleag_prove, dleag_verify, - dleag_prove) + dleag_proof_len, + verify_ed25519_point, +) from .util import ( - ensure, dumpj, + ensure, make_int, TemporaryError) from .rpc_xmr import ( @@ -33,9 +35,7 @@ from .rpc_xmr import ( make_xmr_wallet_rpc_func) from .util import ( b2i, b2h) -from .chainparams import CoinInterface, Coins - -XMR_COIN = 10 ** 12 +from .chainparams import XMR_COIN, CoinInterface, Coins class XMRInterface(CoinInterface): @@ -155,7 +155,6 @@ class XMRInterface(CoinInterface): def getNewAddress(self, placeholder): with self._mx_wallet: - self._log.warning('TODO - subaddress?') self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename}) return self.rpc_wallet_cb('create_address', {'account_index': 0})['address'] diff --git a/basicswap/templates/bids.html b/basicswap/templates/bids.html index 392e7ab..2b6bc36 100644 --- a/basicswap/templates/bids.html +++ b/basicswap/templates/bids.html @@ -20,6 +20,19 @@ +State + +Include Expired + + Page: {{ filters.page_no }} diff --git a/basicswap/templates/index.html b/basicswap/templates/index.html index 74db5c1..1841b9a 100644 --- a/basicswap/templates/index.html +++ b/basicswap/templates/index.html @@ -16,6 +16,7 @@ Version: {{ version }} Swaps in Progress: {{ summary.num_swapping }}
Network Offers: {{ summary.num_network_offers }}
Sent Offers: {{ summary.num_sent_offers }}
+Available Bids: {{ summary.num_available_bids }}
Received Bids: {{ summary.num_recv_bids }}
Sent Bids: {{ summary.num_sent_bids }}
Watched Outputs: {{ summary.num_watched_outputs }}
diff --git a/tests/basicswap/extended/test_xmr_persistent.py b/tests/basicswap/extended/test_xmr_persistent.py index 0461304..17ee83d 100644 --- a/tests/basicswap/extended/test_xmr_persistent.py +++ b/tests/basicswap/extended/test_xmr_persistent.py @@ -9,8 +9,8 @@ export RESET_TEST=true export TEST_PATH=/tmp/test_persistent mkdir -p ${TEST_PATH}/bin/{particl,monero,bitcoin} -cp ~/tmp/particl-0.21.2.9-x86_64-linux-gnu.tar.gz ${TEST_PATH}/bin/particl -cp ~/tmp/bitcoin-0.21.1-x86_64-linux-gnu.tar.gz ${TEST_PATH}/bin/bitcoin +cp ~/tmp/particl-0.21.2.9-x86_64-linux-gnu_nousb.tar.gz ${TEST_PATH}/bin/particl +cp ~/tmp/bitcoin-22.0-x86_64-linux-gnu.tar.gz ${TEST_PATH}/bin/bitcoin cp ~/tmp/monero-linux-x64-v0.17.3.0.tar.bz2 ${TEST_PATH}/bin/monero/monero-0.17.3.0-x86_64-linux-gnu.tar.bz2 export PYTHONPATH=$(pwd) python tests/basicswap/extended/test_xmr_persistent.py diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index d51d3c1..3906efd 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -46,8 +46,8 @@ from basicswap.rpc import ( waitForRPC, ) from basicswap.rpc_xmr import ( - callrpc_xmr_na, callrpc_xmr, + callrpc_xmr_na, ) from basicswap.interface_xmr import ( XMR_COIN,