diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 2fecd3a..c3bc5ca 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -37,7 +37,12 @@ from .basicswap import ( ) +def format_timestamp(value): + return time.strftime('%Y-%m-%d %H:%M', time.localtime(value)) + + env = Environment(loader=PackageLoader('basicswap', 'templates')) +env.filters['formatts'] = format_timestamp def getCoinName(c): @@ -324,30 +329,27 @@ class HttpHandler(BaseHTTPRequestHandler): raise ValueError('Bad bid ID') swap_client = self.server.swap_client - content = html_content_start(self.server.title, self.server.title, 30) \ - + '

Bid: ' + bid_id.hex() + '

' \ - + '

Page Refresh: 30 seconds

' - + messages = [] show_txns = False if post_string != '': form_data = urllib.parse.parse_qs(post_string) form_id = form_data[b'formid'][0].decode('utf-8') if self.server.last_form_id.get('bid', None) == form_id: - content += '

Prevented double submit for form {}.

'.format(form_id) + messages.append('Prevented double submit for form {}.'.format(form_id)) else: self.server.last_form_id['bid'] = form_id if b'abandon_bid' in form_data: try: swap_client.abandonBid(bid_id) - content += '

Bid abandoned

' + messages.append('Bid abandoned') except Exception as e: - content += '

Error' + str(e) + '

' + messages.append('Error ' + str(e)) if b'accept_bid' in form_data: try: swap_client.acceptBid(bid_id) - content += '

Bid accepted

' + messages.append('Bid accepted') except Exception as e: - content += '

Error' + str(e) + '

' + messages.append('Error ' + str(e)) if b'show_txns' in form_data: show_txns = True @@ -387,58 +389,63 @@ class HttpHandler(BaseHTTPRequestHandler): else: state_description = '' - tr = '{}{}' - content += '' + data = { + 'amt_from': format8(bid.amount), + 'amt_to': format8((bid.amount * offer.rate) // COIN), + 'ticker_from': ticker_from, + 'ticker_to': ticker_to, + 'bid_state': getBidState(bid.state), + 'state_description': state_description, + 'itx_state': getTxState(bid.initiate_txn_state), + 'ptx_state': getTxState(bid.participate_txn_state), + 'offer_id': bid.offer_id.hex(), + 'addr_from': bid.bid_addr, + 'addr_fund_proof': bid.proof_address, + 'created_at': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(bid.created_at)), + 'expired_at': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(bid.expire_at)), + 'was_sent': 'True' if bid.was_sent else 'False', + 'was_received': 'True' if bid.was_received else 'False', + 'initiate_tx': 'None' if not bid.initiate_txid else (bid.initiate_txid.hex() + ' ' + ticker_from), + 'initiate_conf': 'None' if not bid.initiate_txn_conf else bid.initiate_txn_conf, + 'participate_tx': 'None' if not bid.participate_txid else (bid.participate_txid.hex() + ' ' + ticker_to), + 'participate_conf': 'None' if not bid.participate_txn_conf else bid.participate_txn_conf, + 'show_txns': show_txns, + } - content += tr.format('Swap', format8(bid.amount) + ' ' + ticker_from + ' for ' + format8((bid.amount * offer.rate) // COIN) + ' ' + ticker_to) - content += tr.format('Bid State', getBidState(bid.state)) - content += tr.format('State Description', state_description) - content += tr.format('ITX State', getTxState(bid.initiate_txn_state)) - content += tr.format('PTX State', getTxState(bid.participate_txn_state)) - content += tr.format('Offer', '' + bid.offer_id.hex() + '') - content += tr.format('Address From', bid.bid_addr) - content += tr.format('Proof of Funds', bid.proof_address) - content += tr.format('Created At', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(bid.created_at))) - content += tr.format('Expired At', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(bid.expire_at))) - content += tr.format('Sent', 'True' if bid.was_sent else 'False') - content += tr.format('Received', 'True' if bid.was_received else 'False') - content += tr.format('Initiate Tx', 'None' if not bid.initiate_txid else (bid.initiate_txid.hex() + ' ' + ticker_from)) - content += tr.format('Initiate Conf', 'None' if not bid.initiate_txn_conf else bid.initiate_txn_conf) - content += tr.format('Participate Tx', 'None' if not bid.participate_txid else (bid.participate_txid.hex() + ' ' + ticker_to)) - content += tr.format('Participate Conf', 'None' if not bid.participate_txn_conf else bid.participate_txn_conf) if show_txns: - content += tr.format('Initiate Tx Refund', 'None' if not bid.initiate_txn_refund else bid.initiate_txn_refund.hex()) - content += tr.format('Participate Tx Refund', 'None' if not bid.participate_txn_refund else bid.participate_txn_refund.hex()) - content += tr.format('Initiate Spend Tx', 'None' if not bid.initiate_spend_txid else (bid.initiate_spend_txid.hex() + ' {}'.format(bid.initiate_spend_n))) - content += tr.format('Participate Spend Tx', 'None' if not bid.participate_spend_txid else (bid.participate_spend_txid.hex() + ' {}'.format(bid.participate_spend_n))) - content += '
' + data['initiate_tx_refund'] = 'None' if not bid.initiate_txn_refund else bid.initiate_txn_refund.hex() + data['participate_tx_refund'] = 'None' if not bid.participate_txn_refund else bid.participate_txn_refund.hex() + data['initiate_tx_spend'] = 'None' if not bid.initiate_spend_txid else (bid.initiate_spend_txid.hex() + ' {}'.format(bid.initiate_spend_n)) + data['participate_tx_spend'] = 'None' if not bid.participate_spend_txid else (bid.participate_spend_txid.hex() + ' {}'.format(bid.participate_spend_n)) - content += '
' - if bid.was_received: - content += '
' - content += '' - content += '' - content += '
' - - content += '

Old States

' + old_states = [] num_states = len(bid.states) // 12 for i in range(num_states): up = struct.unpack_from(' 0: + old_states.sort(key=lambda x: x[0]) - content += '

home

' - return bytes(content, 'UTF-8') + template = env.get_template('bid.html') + return bytes(template.render( + title=self.server.title, + h2=self.server.title, + bid_id=bid_id.hex(), + messages=messages, + data=data, + form_id=os.urandom(8).hex(), + old_states=old_states, + ), 'UTF-8') def page_bids(self, url_split, post_string, sent=False): swap_client = self.server.swap_client diff --git a/basicswap/templates/bid.html b/basicswap/templates/bid.html new file mode 100644 index 0000000..3dca527 --- /dev/null +++ b/basicswap/templates/bid.html @@ -0,0 +1,56 @@ +{% include 'header.html' %} + +

Bid {{ bid_id }}

+{% if refresh %} +

Page Refresh: {{ refresh }} seconds

+{% endif %} + +{% for m in messages %} +

{{ m }}

+{% endfor %} + +
StateSet At
+ + + + + + + + + + + + + + + + +{% if data.show_txns %} + + + + +{% endif %} +
Swap{{ data.amt_from }} {{ data.ticker_from }} for {{ data.amt_to }} {{ data.ticker_to }}
Bid State{{ data.bid_state }}
StateDescription {{ data.state_description }}
ITX State{{ data.itx_state }}
PTX State{{ data.ptx_state }}
Offer{{ data.offer_id }}
Address From{{ data.addr_from }}
Proof of Funds{{ data.proof_address }}
Created At{{ data.created_at }}
Expired At{{ data.expired_at }}
Sent{{ data.was_sent }}
Received{{ data.was_received }}
Initiate Tx{{ data.initiate_tx }}
Initiate Conf{{ data.initiate_conf }}
Participate Tx{{ data.participate_tx }}
Participate Conf{{ data.participate_conf }}
Initiate Tx Refund{{ data.initiate_tx_refund }}
Participate Tx Refund{{ data.participate_tx_refund }}
Initiate Tx Spend Tx{{ data.initiate_tx_spend }}
Participate Tx Spend Tx{{ data.participate_tx_spend }}
+ +
+{% if data.was_received == 'True' %} +
+{% endif %} + + + +
+ + +

Old States

+ + +{% for s in old_states %} + +{% endfor %} +
StateSet At
{{ s[1] }}{{ s[0] | formatts }}
+ +

home

+