From 2a3d89b1121a55210778872127ab030f78bd4bb2 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sun, 18 Dec 2022 16:45:07 +0200 Subject: [PATCH] ui: Add swap type. --- basicswap/__init__.py | 2 +- basicswap/basicswap_util.py | 16 +++++ basicswap/js_server.py | 1 + basicswap/templates/offer.html | 4 ++ basicswap/templates/offer_confirm.html | 35 +++++++++- basicswap/templates/offer_new_1.html | 95 +++++++++++++++++++++++--- basicswap/templates/offer_new_2.html | 39 ++++++++++- basicswap/templates/offers.html | 2 + basicswap/ui/page_offers.py | 62 +++++++++++------ 9 files changed, 223 insertions(+), 33 deletions(-) diff --git a/basicswap/__init__.py b/basicswap/__init__.py index 924ead4..002f394 100644 --- a/basicswap/__init__.py +++ b/basicswap/__init__.py @@ -1,3 +1,3 @@ name = "basicswap" -__version__ = "0.11.55" +__version__ = "0.11.56" diff --git a/basicswap/basicswap_util.py b/basicswap/basicswap_util.py index 8ce931f..f8cb7da 100644 --- a/basicswap/basicswap_util.py +++ b/basicswap/basicswap_util.py @@ -458,3 +458,19 @@ def isActiveBidState(state): if state == BidStates.XMR_SWAP_FAILED: return True return False + + +def strSwapType(swap_type): + if swap_type == SwapTypes.SELLER_FIRST: + return 'seller_first' + if swap_type == SwapTypes.XMR_SWAP: + return 'xmr_swap' + return None + + +def strSwapDesc(swap_type): + if swap_type == SwapTypes.SELLER_FIRST: + return 'Secret Hash' + if swap_type == SwapTypes.XMR_SWAP: + return 'Adaptor Sig' + return None diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 05fe6a5..e3f3d25 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -174,6 +174,7 @@ def js_offers(self, url_split, post_string, is_json, sent=False): ci_from = swap_client.ci(o.coin_from) ci_to = swap_client.ci(o.coin_to) rv.append({ + 'swap_type': o.swap_type, 'addr_from': o.addr_from, 'addr_to': o.addr_to, 'offer_id': o.offer_id.hex(), diff --git a/basicswap/templates/offer.html b/basicswap/templates/offer.html index df08a35..74efcbc 100644 --- a/basicswap/templates/offer.html +++ b/basicswap/templates/offer.html @@ -106,6 +106,10 @@ Offer State {{ data.state }} + + Swap Type + {{ data.swap_type }} + {% if data.sent %}You Send{% else %}You Get{% endif %} diff --git a/basicswap/templates/offer_confirm.html b/basicswap/templates/offer_confirm.html index 259745d..f471cd0 100644 --- a/basicswap/templates/offer_confirm.html +++ b/basicswap/templates/offer_confirm.html @@ -161,6 +161,38 @@ + +
+
+
+
+

Swap Type

+
+
+
+ + + +
+ + + + + + + + +
+ +
+
+
+
+
@@ -598,6 +630,7 @@ + @@ -620,4 +653,4 @@
{% include 'footer.html' %} - \ No newline at end of file + diff --git a/basicswap/templates/offer_new_1.html b/basicswap/templates/offer_new_1.html index 953a8b4..420eaf2 100644 --- a/basicswap/templates/offer_new_1.html +++ b/basicswap/templates/offer_new_1.html @@ -80,7 +80,7 @@
-
+
@@ -146,6 +146,39 @@
+ +
+
+
+
+

Swap Type

+
+
+
+ + + +
+ + + + + + + + +
+ +
+
+
+
+
+
@@ -267,6 +300,10 @@
+
+ + +
@@ -280,10 +317,6 @@

Options

-
- - -
@@ -438,6 +471,41 @@ xhr_rates_table.send(); } + function set_swap_type_enabled(coin_from, coin_to, swap_type) { + let make_hidden = false; + if (coin_to == '6' /* XMR */ || coin_to == '8' /* PART_ANON */) { + swap_type.disabled = true; + swap_type.value = 'xmr_swap'; + make_hidden = true; + } else + if (coin_from == '11' /* PIVX */ || coin_from == '12' /* DASH */ || coin_from == '13' /* FIRO */) { + swap_type.disabled = true; + swap_type.value = 'seller_first'; + make_hidden = true; + } else { + swap_type.disabled = false; + } + let swap_type_hidden = document.getElementById('swap_type_hidden'); + console.log('make_hidden', make_hidden); + console.log('swap_type_hidden', swap_type_hidden); + console.log('swap_type.value', swap_type.value); + if (make_hidden) { + if (!swap_type_hidden) { + console.log('createElement'); + swap_type_hidden = document.createElement('input'); + swap_type_hidden.setAttribute('id', 'swap_type_hidden'); + swap_type_hidden.setAttribute('type', 'hidden'); + swap_type_hidden.setAttribute('name', 'swap_type'); + document.getElementById('form').appendChild(swap_type_hidden) + } + swap_type_hidden.setAttribute('value', swap_type.value); + } else + if (swap_type_hidden) { + console.log('remove element'); + swap_type_hidden.parentNode.removeChild(swap_type_hidden); + } + } + function set_rate(value_changed) { const coin_from = document.getElementById('coin_from').value; const coin_to = document.getElementById('coin_to').value; @@ -445,6 +513,10 @@ const amt_to = document.getElementById('amt_to').value; const rate = document.getElementById('rate').value; const lock_rate = rate == '' ? false : document.getElementById('rate_lock').checked; + + const swap_type = document.getElementById('swap_type'); + set_swap_type_enabled(coin_from, coin_to, swap_type); + if (coin_from == '-1' || coin_to == '-1') { return; } @@ -454,15 +526,13 @@ return; } params += '&rate=' + rate + '&amt_from=' + amt_from; - } - else + } else if (lock_rate && value_changed == 'amt_to') { if (amt_to == '' || rate == '') { return; } params += '&amt_to=' + amt_to + '&rate=' + rate; - } - else { + } else { if (amt_from == '' || amt_to == '') { return; } @@ -472,6 +542,13 @@ xhr_rate.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr_rate.send(params); } + + document.addEventListener("DOMContentLoaded", function() { + const coin_from = document.getElementById('coin_from').value; + const coin_to = document.getElementById('coin_to').value; + const swap_type = document.getElementById('swap_type'); + set_swap_type_enabled(coin_from, coin_to, swap_type); + });
diff --git a/basicswap/templates/offer_new_2.html b/basicswap/templates/offer_new_2.html index 574210d..1c2f3c8 100644 --- a/basicswap/templates/offer_new_2.html +++ b/basicswap/templates/offer_new_2.html @@ -112,7 +112,7 @@
+ {% for a in swap_types %} + {{ a[1] }} + {% endfor %} + + + + + + +
@@ -516,6 +550,7 @@ + {% if data.amt_var==true %} {% endif %} {% if data.rate_var==true %} @@ -528,4 +563,4 @@
{% include 'footer.html' %} - \ No newline at end of file + diff --git a/basicswap/templates/offers.html b/basicswap/templates/offers.html index 7132714..bb60f38 100644 --- a/basicswap/templates/offers.html +++ b/basicswap/templates/offers.html @@ -374,6 +374,7 @@ Network Recipient + Swap Type You Get You Send @@ -402,6 +403,7 @@ {{ o[7] }} {% if o[12]==2 %}(Revoked){% elif o[11]==true %}(Not Active){% else %}(Active){% endif %} {{ o[8] }} + {{ o[13] }} {% if o[9]==true %} {{ o[3] }} diff --git a/basicswap/ui/page_offers.py b/basicswap/ui/page_offers.py index cbbf18b..a50fba3 100644 --- a/basicswap/ui/page_offers.py +++ b/basicswap/ui/page_offers.py @@ -32,6 +32,8 @@ from basicswap.basicswap_util import ( DebugTypes, getLockName, strBidState, + strSwapDesc, + strSwapType, TxLockTypes, strOfferState, ) @@ -55,6 +57,15 @@ def decode_offer_id(v): raise ValueError('Bad offer ID') +def swap_type_from_string(str_swap_type: str) -> SwapTypes: + if str_swap_type == 'seller_first': + return SwapTypes.SELLER_FIRST + elif str_swap_type == 'xmr_swap': + return SwapTypes.XMR_SWAP + else: + raise ValueError('Unknown swap type') + + def parseOfferFormData(swap_client, form_data, page_data, options={}): errors = [] parsed_data = {} @@ -91,11 +102,6 @@ def parseOfferFormData(swap_client, form_data, page_data, options={}): except Exception: errors.append('Unknown Coin To') - if parsed_data['coin_to'] in (Coins.XMR, Coins.PART_ANON): - page_data['swap_style'] = 'xmr' - else: - page_data['swap_style'] = 'atomic' - try: page_data['amt_from'] = get_data_entry(form_data, 'amt_from') parsed_data['amt_from'] = inputAmount(page_data['amt_from'], ci_from) @@ -132,6 +138,29 @@ def parseOfferFormData(swap_client, form_data, page_data, options={}): page_data['rate_var'] = True if have_data_entry(form_data, 'rate_var') else False parsed_data['rate_var'] = page_data['rate_var'] + page_data['automation_strat_id'] = int(get_data_entry_or(form_data, 'automation_strat_id', -1)) + parsed_data['automation_strat_id'] = page_data['automation_strat_id'] + swap_type = -1 + if have_data_entry(form_data, 'swap_type'): + page_data['swap_type'] = get_data_entry(form_data, 'swap_type') + parsed_data['swap_type'] = page_data['swap_type'] + swap_type = swap_type_from_string(parsed_data['swap_type']) + if have_data_entry(form_data, 'subfee'): + parsed_data['subfee'] = True + + if parsed_data['coin_to'] in (Coins.XMR, Coins.PART_ANON) or swap_type == SwapTypes.XMR_SWAP: + page_data['swap_style'] = 'xmr' + parsed_data['swap_type'] = strSwapType(SwapTypes.XMR_SWAP) + else: + page_data['swap_style'] = 'atomic' + parsed_data['swap_type'] = strSwapType(SwapTypes.SELLER_FIRST) + + if 'swap_type' in parsed_data: + try: + swap_client.validateSwapType(coin_from, coin_to, swap_type) + except Exception as e: + errors.append(f'Invalid Swap type {e}') + if have_data_entry(form_data, 'step1'): if len(errors) == 0 and have_data_entry(form_data, 'continue'): page_data['step2'] = True @@ -175,13 +204,6 @@ def parseOfferFormData(swap_client, form_data, page_data, options={}): elif have_data_entry(form_data, 'valid_for_seconds'): parsed_data['valid_for_seconds'] = int(get_data_entry(form_data, 'valid_for_seconds')) - page_data['automation_strat_id'] = int(get_data_entry_or(form_data, 'automation_strat_id', -1)) - parsed_data['automation_strat_id'] = page_data['automation_strat_id'] - if have_data_entry(form_data, 'swap_type'): - parsed_data['swap_type'] = get_data_entry(form_data, 'swap_type') - if have_data_entry(form_data, 'subfee'): - parsed_data['subfee'] = True - try: if len(errors) == 0 and page_data['swap_style'] == 'xmr': if have_data_entry(form_data, 'fee_rate_from'): @@ -220,12 +242,7 @@ def postNewOfferFromParsed(swap_client, parsed_data): if 'swap_type' in parsed_data: str_swap_type = parsed_data['swap_type'].lower() - if str_swap_type == 'seller_first': - swap_type = SwapTypes.SELLER_FIRST - elif str_swap_type == 'xmr_swap': - swap_type = SwapTypes.XMR_SWAP - else: - raise ValueError('Unknown swap type') + swap_type = swap_type_from_string(str_swap_type) elif parsed_data['coin_to'] in (Coins.XMR, Coins.PART_ANON): swap_type = SwapTypes.XMR_SWAP @@ -319,6 +336,7 @@ def offer_to_post_string(self, swap_client, offer_id): 'rate': ci_to.format_amount(offer.rate), 'amt_to': ci_to.format_amount((offer.amount_from * offer.rate) // ci_from.COIN()), 'validhrs': offer.time_valid // (60 * 60), + 'swap_type': strSwapType(offer.swap_type), } if offer.amount_negotiable: @@ -359,6 +377,7 @@ def page_newoffer(self, url_split, post_string): 'debug_ui': swap_client.debug_ui, 'automation_strat_id': -1, 'amt_bid_min': format_amount(1000, 8), + 'swap_type': strSwapType(SwapTypes.SELLER_FIRST), } post_data = parse.parse_qs(post_string) @@ -416,6 +435,7 @@ def page_newoffer(self, url_split, post_string): 'data': page_data, 'automation_strategies': automation_strategies, 'summary': summary, + 'swap_types': [(strSwapType(x), strSwapDesc(x)) for x in SwapTypes if strSwapType(x)], }) @@ -532,7 +552,8 @@ def page_offer(self, url_split, post_string): 'debug_ui': swap_client.debug_ui, 'automation_strat_id': -1, 'is_expired': offer.expire_at <= now, - 'active_ind': offer.active_ind + 'active_ind': offer.active_ind, + 'swap_type': strSwapDesc(offer.swap_type) } data.update(extend_data) @@ -660,7 +681,8 @@ def page_offers(self, url_split, post_string, sent=False): o.was_sent, ci_from.format_amount(completed_amount), is_expired, - o.active_ind)) + o.active_ind, + strSwapDesc(o.swap_type))) coins_from, coins_to = listAvailableCoins(swap_client, split_from=True)