Fix Swap Type select for Doge + UI update for Get Rate Inferred.

This commit is contained in:
gerlofvanek 2025-01-06 20:37:33 +01:00
parent 3f8012f0d0
commit 80dbbd3d12

View file

@ -284,14 +284,14 @@ if (document.readyState === 'loading') {
</div> </div>
<div class="w-full md:w-1/2 p-3"> <div class="w-full md:w-1/2 p-3">
<p class="mb-1.5 font-medium text-base dark:text-white text-coolGray-800">Rate</p> <p class="mb-1.5 font-medium text-base dark:text-white text-coolGray-800">Rate</p>
<div class="relative"> <div class="flex items-center gap-2">
<div class="relative flex-1">
<div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none"> <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
{{ select_rate_svg | safe }} {{ select_rate_svg | safe }}
</div> </div>
<input class="pl-10 hover:border-blue-500 pl-10 bg-gray-50 text-gray-900 appearance-none pr-10 dark:bg-gray-500 dark:text-white border border-gray-300 dark:border-gray-400 dark:text-gray-50 dark:placeholder-white text-sm rounded-lg outline-none focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 focus:ring-0" type="text" id="rate" name="rate" value="{{ data.rate }}" onchange="set_rate('rate');"> <input class="pl-10 hover:border-blue-500 bg-gray-50 text-gray-900 appearance-none pr-10 dark:bg-gray-500 dark:text-white border border-gray-300 dark:border-gray-400 dark:text-gray-50 dark:placeholder-white text-sm rounded-lg outline-none focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 focus:ring-0" type="text" id="rate" name="rate" value="{{ data.rate }}" onchange="set_rate('rate');">
</div> </div>
<div class="text-sm mt-2"> <button type="button" id="get_rate_inferred_button" class="px-4 py-2.5 text-sm font-medium text-white bg-blue-500 hover:bg-blue-600 rounded-md shadow-sm focus:outline-none">Get Rate Inferred</button>
<a href="" id="get_rate_inferred_button" class="mt-2 dark:text-white bold text-coolGray-800">Get Rate Inferred:</a><span class="dark:text-white" id="rate_inferred_display"></span>
</div> </div>
<div class="flex form-check form-check-inline mt-5"> <div class="flex form-check form-check-inline mt-5">
<div class="flex items-center h-5"> <input class="form-check-input hover:border-blue-500 w-5 h-5 form-check-input text-blue-600 bg-gray-50 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-1 dark:bg-gray-500 dark:border-gray-400" type="checkbox" id="rate_lock" name="rate_lock" value="rl" checked=checked> </div> <div class="flex items-center h-5"> <input class="form-check-input hover:border-blue-500 w-5 h-5 form-check-input text-blue-600 bg-gray-50 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-1 dark:bg-gray-500 dark:border-gray-400" type="checkbox" id="rate_lock" name="rate_lock" value="rl" checked=checked> </div>
@ -306,7 +306,6 @@ if (document.readyState === 'loading') {
</div> </div>
</div> </div>
</div> </div>
{% if debug_mode == true %} {% if debug_mode == true %}
<div class="py-0 border-b items-center justify-between -mx-4 mb-6 pb-3 border-gray-400 border-opacity-20"> <div class="py-0 border-b items-center justify-between -mx-4 mb-6 pb-3 border-gray-400 border-opacity-20">
<div class="w-full md:w-10/12"> <div class="w-full md:w-10/12">
@ -414,32 +413,31 @@ if (document.readyState === 'loading') {
</div> </div>
</div> </div>
</section> </section>
<script> <script>
const xhr_rates = new XMLHttpRequest(); const xhr_rates = new XMLHttpRequest();
xhr_rates.onload = () => { xhr_rates.onload = () => {
if (xhr_rates.status == 200) { if (xhr_rates.status == 200) {
const obj = JSON.parse(xhr_rates.response); const obj = JSON.parse(xhr_rates.response);
inner_html = '<pre><code>' + JSON.stringify(obj, null, ' ') + '</code></pre>'; inner_html = '<pre><code>' + JSON.stringify(obj, null, ' ') + '</code></pre>';
document.getElementById('rates_display').innerHTML = inner_html; document.getElementById('rates_display').innerHTML = inner_html;
} }
} };
const xhr_rate = new XMLHttpRequest();
xhr_rate.onload = () => { const xhr_rate = new XMLHttpRequest();
xhr_rate.onload = () => {
if (xhr_rate.status == 200) { if (xhr_rate.status == 200) {
const obj = JSON.parse(xhr_rate.response); const obj = JSON.parse(xhr_rate.response);
if (obj.hasOwnProperty('rate')) { if (obj.hasOwnProperty('rate')) {
document.getElementById('rate').value = obj['rate']; document.getElementById('rate').value = obj['rate'];
} else } else if (obj.hasOwnProperty('amount_to')) {
if (obj.hasOwnProperty('amount_to')) {
document.getElementById('amt_to').value = obj['amount_to']; document.getElementById('amt_to').value = obj['amount_to'];
} else } else if (obj.hasOwnProperty('amount_from')) {
if (obj.hasOwnProperty('amount_from')) {
document.getElementById('amt_from').value = obj['amount_from']; document.getElementById('amt_from').value = obj['amount_from'];
} }
} }
} };
function lookup_rates() { function lookup_rates() {
const coin_from = document.getElementById('coin_from').value; const coin_from = document.getElementById('coin_from').value;
const coin_to = document.getElementById('coin_to').value; const coin_to = document.getElementById('coin_to').value;
@ -468,9 +466,9 @@ if (document.readyState === 'loading') {
xhr_rates.open('POST', '/json/rates'); xhr_rates.open('POST', '/json/rates');
xhr_rates.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr_rates.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr_rates.send('coin_from=' + selectedCoin + '&coin_to=' + coin_to); xhr_rates.send('coin_from=' + selectedCoin + '&coin_to=' + coin_to);
} }
function getRateInferred(event) { function getRateInferred(event) {
event.preventDefault(); event.preventDefault();
const coin_from = document.getElementById('coin_from').value; const coin_from = document.getElementById('coin_from').value;
@ -480,17 +478,23 @@ if (document.readyState === 'loading') {
const xhr_rates = new XMLHttpRequest(); const xhr_rates = new XMLHttpRequest();
xhr_rates.onreadystatechange = function() { xhr_rates.onreadystatechange = function() {
if (xhr_rates.readyState === XMLHttpRequest.DONE) { if (xhr_rates.readyState === XMLHttpRequest.DONE) {
//console.log(xhr_rates.responseText);
if (xhr_rates.status === 200) { if (xhr_rates.status === 200) {
try { try {
const responseData = JSON.parse(xhr_rates.responseText); const responseData = JSON.parse(xhr_rates.responseText);
if (responseData.coingecko && responseData.coingecko.rate_inferred) {
const rateInferred = responseData.coingecko.rate_inferred; const rateInferred = responseData.coingecko.rate_inferred;
//document.getElementById('rate_inferred_display').innerText = " (" + coin_from + " to " + coin_to + "): " + rateInferred; document.getElementById('rate').value = rateInferred;
document.getElementById('rate_inferred_display').innerText = " " + rateInferred; set_rate('rate');
} else {
document.getElementById('rate').value = 'Error: Rate limit';
console.error('Rate limit reached or invalid response format');
}
} catch (error) { } catch (error) {
document.getElementById('rate').value = 'Error: Rate limit';
console.error('Error parsing response:', error); console.error('Error parsing response:', error);
} }
} else { } else {
document.getElementById('rate').value = 'Error: Rate limit';
console.error('Error fetching data:', xhr_rates.statusText); console.error('Error fetching data:', xhr_rates.statusText);
} }
} }
@ -499,21 +503,33 @@ if (document.readyState === 'loading') {
xhr_rates.open('POST', '/json/rates'); xhr_rates.open('POST', '/json/rates');
xhr_rates.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr_rates.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr_rates.send(params); xhr_rates.send(params);
} }
document.getElementById('get_rate_inferred_button').addEventListener('click', getRateInferred); document.getElementById('get_rate_inferred_button').addEventListener('click', getRateInferred);
function set_swap_type_enabled(coin_from, coin_to, swap_type) {
const adaptor_sig_only_coins = [
'6', /* XMR */
'9', /* WOW */
'8', /* PART_ANON */
'7', /* PART_BLIND */
'13', /* FIRO */
'17', /* BCH */
'18' /* DOGE */
];
const secret_hash_only_coins = [
'11', /* PIVX */
'12' /* DASH */
];
function set_swap_type_enabled(coin_from, coin_to, swap_type) {
const adaptor_sig_only_coins = ['6' /* XMR */,'9' /* WOW */, '8' /* PART_ANON */, '7' /* PART_BLIND */, '13' /* FIRO */, '17' /* BCH */];
const secret_hash_only_coins = ['11' /* PIVX */, '12' /* DASH */];
let make_hidden = false; let make_hidden = false;
if (adaptor_sig_only_coins.includes(coin_from) || adaptor_sig_only_coins.includes(coin_to)) { if (adaptor_sig_only_coins.includes(coin_from) || adaptor_sig_only_coins.includes(coin_to)) {
swap_type.disabled = true; swap_type.disabled = true;
swap_type.value = 'xmr_swap'; swap_type.value = 'xmr_swap';
make_hidden = true; make_hidden = true;
swap_type.classList.add('select-disabled'); swap_type.classList.add('select-disabled');
} else } else if (secret_hash_only_coins.includes(coin_from) && secret_hash_only_coins.includes(coin_to)) {
if (secret_hash_only_coins.includes(coin_from) && secret_hash_only_coins.includes(coin_to)) {
swap_type.disabled = true; swap_type.disabled = true;
swap_type.value = 'seller_first'; swap_type.value = 'seller_first';
make_hidden = true; make_hidden = true;
@ -522,6 +538,7 @@ if (document.readyState === 'loading') {
swap_type.disabled = false; swap_type.disabled = false;
swap_type.classList.remove('select-disabled'); swap_type.classList.remove('select-disabled');
} }
let swap_type_hidden = document.getElementById('swap_type_hidden'); let swap_type_hidden = document.getElementById('swap_type_hidden');
if (make_hidden) { if (make_hidden) {
if (!swap_type_hidden) { if (!swap_type_hidden) {
@ -529,16 +546,15 @@ if (document.readyState === 'loading') {
swap_type_hidden.setAttribute('id', 'swap_type_hidden'); swap_type_hidden.setAttribute('id', 'swap_type_hidden');
swap_type_hidden.setAttribute('type', 'hidden'); swap_type_hidden.setAttribute('type', 'hidden');
swap_type_hidden.setAttribute('name', 'swap_type'); swap_type_hidden.setAttribute('name', 'swap_type');
document.getElementById('form').appendChild(swap_type_hidden) document.getElementById('form').appendChild(swap_type_hidden);
} }
swap_type_hidden.setAttribute('value', swap_type.value); swap_type_hidden.setAttribute('value', swap_type.value);
} else } else if (swap_type_hidden) {
if (swap_type_hidden) {
swap_type_hidden.parentNode.removeChild(swap_type_hidden); swap_type_hidden.parentNode.removeChild(swap_type_hidden);
} }
} }
function set_rate(value_changed) { function set_rate(value_changed) {
const coin_from = document.getElementById('coin_from').value; const coin_from = document.getElementById('coin_from').value;
const coin_to = document.getElementById('coin_to').value; const coin_to = document.getElementById('coin_to').value;
const amt_from = document.getElementById('amt_from').value; const amt_from = document.getElementById('amt_from').value;
@ -546,18 +562,24 @@ if (document.readyState === 'loading') {
const rate = document.getElementById('rate').value; const rate = document.getElementById('rate').value;
const lock_rate = rate == '' ? false : document.getElementById('rate_lock').checked; const lock_rate = rate == '' ? false : document.getElementById('rate_lock').checked;
if (value_changed === 'coin_from' || value_changed === 'coin_to') {
document.getElementById('rate').value = '';
return;
}
const swap_type = document.getElementById('swap_type'); const swap_type = document.getElementById('swap_type');
set_swap_type_enabled(coin_from, coin_to, swap_type); set_swap_type_enabled(coin_from, coin_to, swap_type);
if (coin_from == '-1' || coin_to == '-1') { if (coin_from == '-1' || coin_to == '-1') {
return; return;
} }
params = 'coin_from=' + coin_from + '&coin_to=' + coin_to;
let params = 'coin_from=' + coin_from + '&coin_to=' + coin_to;
if (value_changed == 'rate' || (lock_rate && value_changed == 'amt_from') || (amt_to == '' && value_changed == 'amt_from')) { if (value_changed == 'rate' || (lock_rate && value_changed == 'amt_from') || (amt_to == '' && value_changed == 'amt_from')) {
if (rate == '' || (amt_from == '' && amt_to == '')) { if (rate == '' || (amt_from == '' && amt_to == '')) {
return; return;
} else } else if (amt_from == '' && amt_to != '') {
if (amt_from == '' && amt_to != '') {
if (value_changed == 'amt_from') { if (value_changed == 'amt_from') {
return; return;
} }
@ -565,8 +587,7 @@ if (document.readyState === 'loading') {
} else { } else {
params += '&rate=' + rate + '&amt_from=' + amt_from; params += '&rate=' + rate + '&amt_from=' + amt_from;
} }
} else } else if (lock_rate && value_changed == 'amt_to') {
if (lock_rate && value_changed == 'amt_to') {
if (amt_to == '' || rate == '') { if (amt_to == '' || rate == '') {
return; return;
} }
@ -577,18 +598,19 @@ if (document.readyState === 'loading') {
} }
params += '&amt_from=' + amt_from + '&amt_to=' + amt_to; params += '&amt_from=' + amt_from + '&amt_to=' + amt_to;
} }
xhr_rate.open('POST', '/json/rate'); xhr_rate.open('POST', '/json/rate');
xhr_rate.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr_rate.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr_rate.send(params); xhr_rate.send(params);
} }
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
const coin_from = document.getElementById('coin_from').value; const coin_from = document.getElementById('coin_from').value;
const coin_to = document.getElementById('coin_to').value; const coin_to = document.getElementById('coin_to').value;
const swap_type = document.getElementById('swap_type'); const swap_type = document.getElementById('swap_type');
set_swap_type_enabled(coin_from, coin_to, swap_type); set_swap_type_enabled(coin_from, coin_to, swap_type);
}); });
</script> </script>
</div> </div>
<script src="static/js/new_offer.js"></script> <script src="static/js/new_offer.js"></script>
{% include 'footer.html' %} {% include 'footer.html' %}