diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 07e5be9..7ddbd7a 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -53,20 +53,20 @@ from .ui.util import ( PAGE_LIMIT, inputAmount, describeBid, + getCoinName, getCoinType, - setCoinFilter, get_data_entry, have_data_entry, get_data_entry_or, + listAvailableCoins, ) from .ui.page_tor import page_tor +from .ui.page_offers import page_offers env = Environment(loader=PackageLoader('basicswap', 'templates')) env.filters['formatts'] = format_timestamp -invalid_coins_from = (Coins.XMR, Coins.PART_ANON) - def value_or_none(v): if v == -1 or v == '-1': @@ -74,34 +74,6 @@ def value_or_none(v): return v -def getCoinName(c): - if c == Coins.PART_ANON: - return chainparams[Coins.PART]['name'].capitalize() + 'Anon' - if c == Coins.PART_BLIND: - return chainparams[Coins.PART]['name'].capitalize() + 'Blind' - return chainparams[c]['name'].capitalize() - - -def listAvailableCoins(swap_client, with_variants=True, split_from=False): - coins_from = [] - coins = [] - for k, v in swap_client.coin_clients.items(): - if k not in chainparams: - continue - if v['connection_type'] == 'rpc': - coins.append((int(k), getCoinName(k))) - if split_from and k not in invalid_coins_from: - coins_from.append(coins[-1]) - if with_variants and k == Coins.PART: - for v in (Coins.PART_ANON, Coins.PART_BLIND): - coins.append((int(v), getCoinName(v))) - if split_from and v not in invalid_coins_from: - coins_from.append(coins[-1]) - if split_from: - return coins_from, coins - return coins - - def validateTextInput(text, name, messages, max_length=None): if max_length is not None and len(text) > max_length: messages.append(f'Error: {name} is too long') @@ -1066,69 +1038,6 @@ class HttpHandler(BaseHTTPRequestHandler): form_id=os.urandom(8).hex(), ), 'UTF-8') - def page_offers(self, url_split, post_string, sent=False): - swap_client = self.server.swap_client - - filters = { - 'coin_from': -1, - 'coin_to': -1, - 'page_no': 1, - 'limit': PAGE_LIMIT, - 'sort_by': 'created_at', - 'sort_dir': 'desc', - } - messages = [] - form_data = self.checkForm(post_string, 'offers', messages) - if form_data and have_data_entry(form_data, 'applyfilters'): - filters['coin_from'] = setCoinFilter(form_data, 'coin_from') - filters['coin_to'] = setCoinFilter(form_data, 'coin_to') - - if have_data_entry(form_data, 'sort_by'): - sort_by = get_data_entry(form_data, 'sort_by') - ensure(sort_by in ['created_at', 'rate'], 'Invalid sort by') - filters['sort_by'] = sort_by - if have_data_entry(form_data, 'sort_dir'): - sort_dir = get_data_entry(form_data, 'sort_dir') - ensure(sort_dir in ['asc', 'desc'], 'Invalid sort dir') - filters['sort_dir'] = sort_dir - - if form_data and have_data_entry(form_data, 'pageback'): - filters['page_no'] = int(form_data[b'pageno'][0]) - 1 - if filters['page_no'] < 1: - filters['page_no'] = 1 - elif form_data and have_data_entry(form_data, 'pageforwards'): - filters['page_no'] = int(form_data[b'pageno'][0]) + 1 - - if filters['page_no'] > 1: - filters['offset'] = (filters['page_no'] - 1) * PAGE_LIMIT - - offers = swap_client.listOffers(sent, filters) - - formatted_offers = [] - for o in offers: - ci_from = swap_client.ci(Coins(o.coin_from)) - ci_to = swap_client.ci(Coins(o.coin_to)) - formatted_offers.append(( - format_timestamp(o.created_at), - o.offer_id.hex(), - ci_from.coin_name(), ci_to.coin_name(), - ci_from.format_amount(o.amount_from), - ci_to.format_amount((o.amount_from * o.rate) // ci_from.COIN()), - ci_to.format_amount(o.rate), - 'Public' if o.addr_to == swap_client.network_addr else o.addr_to)) - - template = env.get_template('offers.html') - return bytes(template.render( - title=self.server.title, - h2=self.server.title, - page_type='Sent' if sent else 'Received', - coins=listAvailableCoins(swap_client), - messages=messages, - filters=filters, - offers=formatted_offers, - form_id=os.urandom(8).hex(), - ), 'UTF-8') - def page_bid(self, url_split, post_string): ensure(len(url_split) > 2, 'Bid ID not specified') try: @@ -1504,11 +1413,11 @@ class HttpHandler(BaseHTTPRequestHandler): if url_split[1] == 'offer': return self.page_offer(url_split, post_string) if url_split[1] == 'offers': - return self.page_offers(url_split, post_string) + return page_offers(self, url_split, post_string) if url_split[1] == 'newoffer': return self.page_newoffer(url_split, post_string) if url_split[1] == 'sentoffers': - return self.page_offers(url_split, post_string, sent=True) + return page_offers(self, url_split, post_string, sent=True) if url_split[1] == 'bid': return self.page_bid(url_split, post_string) if url_split[1] == 'bids': diff --git a/basicswap/templates/offers.html b/basicswap/templates/offers.html index 51ce728..1f26cdb 100644 --- a/basicswap/templates/offers.html +++ b/basicswap/templates/offers.html @@ -45,9 +45,9 @@ - + {% for o in offers %} - + {% endfor %}
AtRecipientOffer IDCoin FromCoin ToAmount FromAmount ToRate
AtFromRecipientOffer IDCoin FromCoin ToAmount FromAmount ToRate
{{ o[0] }}{{ o[7] }}{{ o[1] }}{{ o[2] }}{{ o[3] }}{{ o[4] }}{{ o[5] }}{{ o[6] }}
{{ o[0] }}{{ o[8]|truncate(12, True) }}{% if o[9]==true %} Sent{% endif %}{{ o[7] }}{{ o[1] }}{{ o[2] }}{{ o[3] }}{{ o[4] }}{{ o[5] }}{{ o[6] }}
diff --git a/basicswap/ui/page_offers.py b/basicswap/ui/page_offers.py new file mode 100644 index 0000000..8f4bb7c --- /dev/null +++ b/basicswap/ui/page_offers.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2022 tecnovert +# Distributed under the MIT software license, see the accompanying +# file LICENSE or http://www.opensource.org/licenses/mit-license.php. + +import os + +from .util import ( + PAGE_LIMIT, + setCoinFilter, + get_data_entry, + have_data_entry, + listAvailableCoins, +) +from basicswap.util import ( + ensure, + format_timestamp, +) +from basicswap.chainparams import ( + Coins, +) + + +def page_offers(self, url_split, post_string, sent=False): + swap_client = self.server.swap_client + + filters = { + 'coin_from': -1, + 'coin_to': -1, + 'page_no': 1, + 'limit': PAGE_LIMIT, + 'sort_by': 'created_at', + 'sort_dir': 'desc', + } + messages = [] + form_data = self.checkForm(post_string, 'offers', messages) + if form_data and have_data_entry(form_data, 'applyfilters'): + filters['coin_from'] = setCoinFilter(form_data, 'coin_from') + filters['coin_to'] = setCoinFilter(form_data, 'coin_to') + + if have_data_entry(form_data, 'sort_by'): + sort_by = get_data_entry(form_data, 'sort_by') + ensure(sort_by in ['created_at', 'rate'], 'Invalid sort by') + filters['sort_by'] = sort_by + if have_data_entry(form_data, 'sort_dir'): + sort_dir = get_data_entry(form_data, 'sort_dir') + ensure(sort_dir in ['asc', 'desc'], 'Invalid sort dir') + filters['sort_dir'] = sort_dir + + if form_data and have_data_entry(form_data, 'pageback'): + filters['page_no'] = int(form_data[b'pageno'][0]) - 1 + if filters['page_no'] < 1: + filters['page_no'] = 1 + elif form_data and have_data_entry(form_data, 'pageforwards'): + filters['page_no'] = int(form_data[b'pageno'][0]) + 1 + + if filters['page_no'] > 1: + filters['offset'] = (filters['page_no'] - 1) * PAGE_LIMIT + + offers = swap_client.listOffers(sent, filters) + + formatted_offers = [] + for o in offers: + ci_from = swap_client.ci(Coins(o.coin_from)) + ci_to = swap_client.ci(Coins(o.coin_to)) + formatted_offers.append(( + format_timestamp(o.created_at), + o.offer_id.hex(), + ci_from.coin_name(), ci_to.coin_name(), + ci_from.format_amount(o.amount_from), + ci_to.format_amount((o.amount_from * o.rate) // ci_from.COIN()), + ci_to.format_amount(o.rate), + 'Public' if o.addr_to == swap_client.network_addr else o.addr_to, + o.addr_from, + o.was_sent)) + + template = self.server.env.get_template('offers.html') + return bytes(template.render( + title=self.server.title, + h2=self.server.title, + page_type='Sent' if sent else 'Received', + coins=listAvailableCoins(swap_client), + messages=messages, + filters=filters, + offers=formatted_offers, + form_id=os.urandom(8).hex(), + ), 'UTF-8') diff --git a/basicswap/ui/page_tor.py b/basicswap/ui/page_tor.py index 50dfb2b..b9d93e1 100644 --- a/basicswap/ui/page_tor.py +++ b/basicswap/ui/page_tor.py @@ -20,7 +20,6 @@ def extract_data(bytes_in): def page_tor(self, url_split, post_string): - template = self.server.env.get_template('tor.html') swap_client = self.server.swap_client @@ -37,6 +36,7 @@ def page_tor(self, url_split, post_string): messages = [] + template = self.server.env.get_template('tor.html') return bytes(template.render( title=self.server.title, h2=self.server.title, diff --git a/basicswap/ui/util.py b/basicswap/ui/util.py index 34053c6..55deeb7 100644 --- a/basicswap/ui/util.py +++ b/basicswap/ui/util.py @@ -12,6 +12,7 @@ from basicswap.util import ( ) from basicswap.chainparams import ( Coins, + chainparams, ) from basicswap.basicswap_util import ( TxTypes, @@ -29,6 +30,7 @@ from basicswap.basicswap_util import ( from basicswap.protocols.xmr_swap_1 import getChainBSplitKey PAGE_LIMIT = 50 +invalid_coins_from = (Coins.XMR, Coins.PART_ANON) def tickerToCoinId(ticker): @@ -343,3 +345,31 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b data['view_tx_desc'] = json.dumps(ci_from.describeTx(data['view_tx_hex']), indent=4) return data + + +def getCoinName(c): + if c == Coins.PART_ANON: + return chainparams[Coins.PART]['name'].capitalize() + 'Anon' + if c == Coins.PART_BLIND: + return chainparams[Coins.PART]['name'].capitalize() + 'Blind' + return chainparams[c]['name'].capitalize() + + +def listAvailableCoins(swap_client, with_variants=True, split_from=False): + coins_from = [] + coins = [] + for k, v in swap_client.coin_clients.items(): + if k not in chainparams: + continue + if v['connection_type'] == 'rpc': + coins.append((int(k), getCoinName(k))) + if split_from and k not in invalid_coins_from: + coins_from.append(coins[-1]) + if with_variants and k == Coins.PART: + for v in (Coins.PART_ANON, Coins.PART_BLIND): + coins.append((int(v), getCoinName(v))) + if split_from and v not in invalid_coins_from: + coins_from.append(coins[-1]) + if split_from: + return coins_from, coins + return coins