From 18a444b0714d41c986b86333fb652d243b03be5f Mon Sep 17 00:00:00 2001 From: tecnovert Date: Mon, 25 Jul 2022 12:54:58 +0200 Subject: [PATCH] ui: Add json endpoint to list all coin types. --- basicswap/http_server.py | 4 +++- basicswap/js_server.py | 22 ++++++++++++++++++++++ tests/basicswap/test_run.py | 12 ++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 894f517..26348c9 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -33,6 +33,7 @@ from .basicswap_util import ( strAddressType, ) from .js_server import ( + js_coins, js_error, js_wallets, js_offers, @@ -678,7 +679,8 @@ class HttpHandler(BaseHTTPRequestHandler): self.putHeaders(status_code, 'text/plain') func = js_index if len(url_split) > 2: - func = {'wallets': js_wallets, + func = {'coins': js_coins, + 'wallets': js_wallets, 'offers': js_offers, 'sentoffers': js_sentoffers, 'bids': js_bids, diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 8a101c6..6ee00e5 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -16,6 +16,7 @@ from .basicswap_util import ( ) from .chainparams import ( Coins, + chainparams, ) from .ui.util import ( PAGE_LIMIT, @@ -61,6 +62,27 @@ def withdraw_coin(swap_client, coin_type, post_string, is_json): return {'txid': txid_hex} +def js_coins(self, url_split, post_string, is_json): + swap_client = self.server.swap_client + + coins = [] + for coin in Coins: + cc = swap_client.coin_clients[coin] + entry = { + 'id': int(coin), + 'ticker': chainparams[cc['coin']]['ticker'], + 'name': cc['name'].capitalize(), + 'active': False if cc['connection_type'] == 'none' else True, + } + if coin == Coins.PART_ANON: + entry['variant'] = 'Anon' + elif coin == Coins.PART_BLIND: + entry['variant'] = 'Blind' + coins.append(entry) + + return bytes(json.dumps(coins), 'UTF-8') + + def js_wallets(self, url_split, post_string, is_json): swap_client = self.server.swap_client if len(url_split) > 3: diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index 352dcf5..f8ed63e 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -85,6 +85,18 @@ class Test(BaseTest): ci = self.swap_clients[0].ci(coin_type) return ci.make_int(float(js_wallets[str(int(coin_type))]['balance']) + float(js_wallets[str(int(coin_type))]['unconfirmed'])) + def test_001_js_coins(self): + js_coins = read_json_api(1800, 'coins') + + for c in Coins: + coin = next((x for x in js_coins if x['id'] == int(c)), None) + if c in (Coins.PART, Coins.BTC, Coins.LTC, Coins.PART_ANON, Coins.PART_BLIND): + assert(coin['active'] is True) + else: + assert(coin['active'] is False) + if c in (Coins.PART_ANON, Coins.PART_BLIND): + assert(coin['ticker'] == 'PART') + def test_01_verifyrawtransaction(self): txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000' prevout = {