From 80f0098a3d63f0e8c18caa566478e64ee32e3065 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Thu, 4 Aug 2022 11:47:27 +0200 Subject: [PATCH] ui: Rates table example --- basicswap/basicswap.py | 11 +++++- basicswap/js_server.py | 5 ++- basicswap/templates/offer_new_1.html | 54 ++++++++++++++++++++++++++++ tests/basicswap/test_run.py | 2 +- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index a88c41b..c86250d 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5993,7 +5993,16 @@ class BasicSwap(BaseApp): if 'bittrex' in rv: js = rv['bittrex'] rate = js['rate_last'] if 'rate_last' in js else js['rate_inferred'] - rv_array.append(('bittrex.com', ticker_from, ticker_to, '', '', js['from_btc'], js['to_btc'], format_float(float(rate)))) + rv_array.append(( + 'bittrex.com', + ticker_from, + ticker_to, + '', + '', + format_float(float(js['from_btc'])), + format_float(float(js['to_btc'])), + format_float(float(rate)) + )) return rv_array return rv diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 73e1ada..e370427 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -17,7 +17,6 @@ from .basicswap_util import ( from .chainparams import ( Coins, chainparams, - getCoinIdFromTicker, ) from .ui.util import ( PAGE_LIMIT, @@ -344,8 +343,8 @@ def js_rates_list(self, url_split, query_string, is_json): get_data = urllib.parse.parse_qs(query_string) sc = self.server.swap_client - coin_from = getCoinIdFromTicker(get_data['from'][0]) - coin_to = getCoinIdFromTicker(get_data['to'][0]) + coin_from = getCoinType(get_data['from'][0]) + coin_to = getCoinType(get_data['to'][0]) return bytes(json.dumps(sc.lookupRates(coin_from, coin_to, True)), 'UTF-8') diff --git a/basicswap/templates/offer_new_1.html b/basicswap/templates/offer_new_1.html index b5f79f5..906f15d 100644 --- a/basicswap/templates/offer_new_1.html +++ b/basicswap/templates/offer_new_1.html @@ -48,6 +48,7 @@ + @@ -83,6 +84,43 @@ xhr_rate.onload = () => { } } +const xhr_rates_table = new XMLHttpRequest(); +xhr_rates_table.onload = () => { + if (xhr_rates_table.status == 200) { + const list = JSON.parse(xhr_rates_table.response); + + headings = ['Source', 'Coin From', 'Coin To', 'Coin From USD Rate', 'Coin To USD Rate', 'Coin From BTC Rate', 'Coin To BTC Rate', 'Relative Rate']; + table = document.createElement('table'); + headings_row = document.createElement('tr'); + for (let i = 0; i < headings.length; i++) { + column = document.createElement('th'); + column.textContent = headings[i]; + headings_row.appendChild(column); + } + table.appendChild(headings_row); + + for (let i = 0; i < list.length; i++) { + data_row = document.createElement('tr'); + for (let j = 0; j < list[i].length; j++) { + column = document.createElement('td'); + column.textContent = list[i][j]; + data_row.appendChild(column); + } + table.appendChild(data_row); + } + // Clear existing + const display_node = document.getElementById("rates_display"); + while (display_node.lastElementChild) { + display_node.removeChild(display_node.lastElementChild); + } + + heading = document.createElement('h4'); + heading.textContent = 'Rates' + display_node.appendChild(heading); + display_node.appendChild(table); + } +} + function lookup_rates() { const coin_from = document.getElementById('coin_from').value; const coin_to = document.getElementById('coin_to').value; @@ -100,6 +138,22 @@ function lookup_rates() { xhr_rates.send('coin_from='+coin_from+'&coin_to='+coin_to); } +function lookup_rates_table() { + const coin_from = document.getElementById('coin_from').value; + const coin_to = document.getElementById('coin_to').value; + + if (coin_from == '-1' || coin_to == '-1') { + alert('Coins from and to must be set first.'); + return; + } + + inner_html = '

Rates

Updating...

'; + document.getElementById('rates_display').innerHTML = inner_html; + + xhr_rates_table.open('GET', '/json/rateslist?from='+coin_from+'&to='+coin_to); + xhr_rates_table.send(); +} + function set_rate(value_changed) { const coin_from = document.getElementById('coin_from').value; const coin_to = document.getElementById('coin_to').value; diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index 9148821..585659a 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -111,7 +111,7 @@ class Test(BaseTest): assert ('bittrex' in rv) rv = read_json_api(1800, 'rateslist?from=PART&to=BTC') - assert(len(rv) == 2) + assert len(rv) == 2 def test_01_verifyrawtransaction(self): txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000'