diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index fcfc08f..4bbf6ef 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5361,11 +5361,12 @@ class BasicSwap(BaseApp): if c not in chainparams: continue if self.coin_clients[c]['connection_type'] == 'rpc': + key = chainparams[c]['ticker'] if opts.get('ticker_key', False) else c try: - rv[c] = self.getWalletInfo(c) - rv[c].update(self.getBlockchainInfo(c)) + rv[key] = self.getWalletInfo(c) + rv[key].update(self.getBlockchainInfo(c)) except Exception as ex: - rv[c] = {'name': chainparams[c]['name'].capitalize(), 'error': str(ex)} + rv[key] = {'name': chainparams[c]['name'].capitalize(), 'error': str(ex)} return rv def getCachedWalletsInfo(self, opts=None): diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 6ee00e5..669d13c 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -93,12 +93,14 @@ def js_wallets(self, url_split, post_string, is_json): cmd = url_split[4] if cmd == 'withdraw': return bytes(json.dumps(withdraw_coin(swap_client, coin_type, post_string, is_json)), 'UTF-8') + if cmd == 'nextdepositaddr': + return bytes(json.dumps(swap_client.cacheNewAddressForCoin(coin_type)), 'UTF-8') raise ValueError('Unknown command') rv = swap_client.getWalletInfo(coin_type) rv.update(swap_client.getBlockchainInfo(coin_type)) return bytes(json.dumps(rv), 'UTF-8') - return bytes(json.dumps(self.server.swap_client.getWalletsInfo()), 'UTF-8') + return bytes(json.dumps(self.server.swap_client.getWalletsInfo({'ticker_key': True})), 'UTF-8') def js_offers(self, url_split, post_string, is_json, sent=False): diff --git a/tests/basicswap/common.py b/tests/basicswap/common.py index a76a6f6..882c2c1 100644 --- a/tests/basicswap/common.py +++ b/tests/basicswap/common.py @@ -213,7 +213,7 @@ def read_json_api(port, path=None): url = f'http://127.0.0.1:{port}/json' if path is not None: url += '/' + path - return json.loads(urlopen(url).read()) + return json.loads(urlopen(url, timeout=30).read()) def post_json_api(port, path, json_data): diff --git a/tests/basicswap/common_xmr.py b/tests/basicswap/common_xmr.py index 96aa5ad..677665d 100644 --- a/tests/basicswap/common_xmr.py +++ b/tests/basicswap/common_xmr.py @@ -284,7 +284,7 @@ class XmrTestBase(TestBase): raise ValueError('Test stopped.') try: wallets = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read()) - return wallets['6']['main_address'] + return wallets['XMR']['main_address'] except Exception as e: print('Waiting for main address {}'.format(str(e))) self.delay_event.wait(1) @@ -309,7 +309,7 @@ class XmrTestBase(TestBase): raise ValueError('Test stopped.') try: wallets = json.loads(urlopen('http://127.0.0.1:12701/json/wallets').read()) - particl_blocks = wallets['1']['blocks'] + particl_blocks = wallets['PART']['blocks'] print('particl_blocks', particl_blocks) if particl_blocks >= num_blocks: break diff --git a/tests/basicswap/extended/test_wallet_init.py b/tests/basicswap/extended/test_wallet_init.py index e2e3104..f15e67d 100644 --- a/tests/basicswap/extended/test_wallet_init.py +++ b/tests/basicswap/extended/test_wallet_init.py @@ -82,19 +82,19 @@ class Test(unittest.TestCase): waitForServer(self.delay_event, 12700) wallets_0 = read_json_api(12700, 'wallets') - assert(wallets_0['1']['expected_seed'] is True) - assert(wallets_0['6']['expected_seed'] is True) + assert(wallets_0['PART']['expected_seed'] is True) + assert(wallets_0['XMR']['expected_seed'] is True) waitForServer(self.delay_event, 12701) wallets_1 = read_json_api(12701, 'wallets') - assert(wallets_0['1']['expected_seed'] is True) - assert(wallets_1['6']['expected_seed'] is True) + assert(wallets_0['PART']['expected_seed'] is True) + assert(wallets_1['XMR']['expected_seed'] is True) # TODO: Check other coins - assert(wallets_0['1']['deposit_address'] == wallets_1['1']['deposit_address']) - assert(wallets_0['6']['deposit_address'] == wallets_1['6']['deposit_address']) + assert(wallets_0['PART']['deposit_address'] == wallets_1['1']['deposit_address']) + assert(wallets_0['XMR']['deposit_address'] == wallets_1['6']['deposit_address']) except Exception: traceback.print_exc() diff --git a/tests/basicswap/extended/test_wallet_restore.py b/tests/basicswap/extended/test_wallet_restore.py index 25a94ec..41fdb69 100644 --- a/tests/basicswap/extended/test_wallet_restore.py +++ b/tests/basicswap/extended/test_wallet_restore.py @@ -164,12 +164,23 @@ class Test(TestBase): processes[-1].start() waitForServer(self.delay_event, 12703) - # TODO: Try detect past swaps + self.wait_seconds(5) - ltc_orig = read_json_api(12700, 'wallets/ltc') + # TODO: Attempt to detect past swaps + + ltc_original = read_json_api(12700, 'wallets/ltc') ltc_restored = read_json_api(12703, 'wallets/ltc') - assert(float(ltc_orig['balance']) + float(ltc_orig['unconfirmed']) > 0.0) - assert(float(ltc_orig['balance']) + float(ltc_orig['unconfirmed']) == float(ltc_restored['balance']) + float(ltc_restored['unconfirmed'])) + assert(float(ltc_original['balance']) + float(ltc_original['unconfirmed']) > 0.0) + assert(float(ltc_original['balance']) + float(ltc_original['unconfirmed']) == float(ltc_restored['balance']) + float(ltc_restored['unconfirmed'])) + + wallets_original = read_json_api(12700, 'wallets') + # TODO: After restoring a new deposit address should be generated, should be automated + # Swaps should use a new key path, not the external path + next_addr = read_json_api(12703, 'wallets/part/nextdepositaddr') + next_addr = read_json_api(12703, 'wallets/part/nextdepositaddr') + wallets_restored = read_json_api(12703, 'wallets') + for k, w in wallets_original.items(): + assert(w['deposit_address'] == wallets_restored[k]['deposit_address']) logging.info('Test passed.') diff --git a/tests/basicswap/extended/test_xmr_persistent.py b/tests/basicswap/extended/test_xmr_persistent.py index 15f46e6..e7026ba 100644 --- a/tests/basicswap/extended/test_xmr_persistent.py +++ b/tests/basicswap/extended/test_xmr_persistent.py @@ -149,7 +149,7 @@ class Test(unittest.TestCase): wallets = read_json_api(UI_PORT + 1, 'wallets') - self.xmr_addr = wallets['6']['main_address'] + self.xmr_addr = wallets['XMR']['main_address'] num_blocks = 100 if callrpc_xmr_na(XMR_BASE_RPC_PORT + 1, 'get_block_count')['count'] < num_blocks: logging.info('Mining {} Monero blocks to {}.'.format(num_blocks, self.xmr_addr)) diff --git a/tests/basicswap/test_btc_xmr.py b/tests/basicswap/test_btc_xmr.py index 207e5c8..2d80ed2 100644 --- a/tests/basicswap/test_btc_xmr.py +++ b/tests/basicswap/test_btc_xmr.py @@ -51,10 +51,10 @@ class Test(BaseTest): super(Test, cls).tearDownClass() def getBalance(self, js_wallets): - return float(js_wallets[str(int(self.test_coin_from))]['balance']) + float(js_wallets[str(int(self.test_coin_from))]['unconfirmed']) + return float(js_wallets[self.test_coin_from.name]['balance']) + float(js_wallets[self.test_coin_from.name]['unconfirmed']) def getXmrBalance(self, js_wallets): - return float(js_wallets[str(int(Coins.XMR))]['unconfirmed']) + float(js_wallets[str(int(Coins.XMR))]['balance']) + return float(js_wallets[Coins.XMR.name]['unconfirmed']) + float(js_wallets[Coins.XMR.name]['balance']) def test_01_full_swap(self): logging.info('---------- Test {} to XMR'.format(str(self.test_coin_from))) diff --git a/tests/basicswap/test_ltc_xmr.py b/tests/basicswap/test_ltc_xmr.py index 41acd96..5f48e84 100644 --- a/tests/basicswap/test_ltc_xmr.py +++ b/tests/basicswap/test_ltc_xmr.py @@ -72,10 +72,10 @@ class Test(BaseTest): super(Test, cls).tearDownClass() def getBalance(self, js_wallets): - return float(js_wallets[str(int(self.test_coin_from))]['balance']) + float(js_wallets[str(int(self.test_coin_from))]['unconfirmed']) + return float(js_wallets[self.test_coin_from.name]['balance']) + float(js_wallets[self.test_coin_from.name]['unconfirmed']) def getXmrBalance(self, js_wallets): - return float(js_wallets[str(int(Coins.XMR))]['unconfirmed']) + float(js_wallets[str(int(Coins.XMR))]['balance']) + return float(js_wallets[Coins.XMR.name]['unconfirmed']) + float(js_wallets[Coins.XMR.name]['balance']) def test_01_full_swap(self): logging.info('---------- Test {} to XMR'.format(str(self.test_coin_from))) diff --git a/tests/basicswap/test_partblind_xmr.py b/tests/basicswap/test_partblind_xmr.py index 1916b44..96e37dd 100644 --- a/tests/basicswap/test_partblind_xmr.py +++ b/tests/basicswap/test_partblind_xmr.py @@ -64,10 +64,10 @@ class Test(BaseTest): node0_blind_before = js_0['blind_balance'] + js_0['blind_unconfirmed'] def getBalance(self, js_wallets): - return float(js_wallets[str(int(Coins.PART))]['blind_balance']) + float(js_wallets[str(int(Coins.PART))]['blind_unconfirmed']) + return float(js_wallets[Coins.PART.name]['blind_balance']) + float(js_wallets[Coins.PART.name]['blind_unconfirmed']) def getXmrBalance(self, js_wallets): - return float(js_wallets[str(int(Coins.XMR))]['unconfirmed']) + float(js_wallets[str(int(Coins.XMR))]['balance']) + return float(js_wallets[Coins.XMR.name]['unconfirmed']) + float(js_wallets[Coins.XMR.name]['balance']) def test_01_part_xmr(self): logging.info('---------- Test PARTct to XMR') diff --git a/tests/basicswap/test_reload.py b/tests/basicswap/test_reload.py index 8cebf72..b3cca68 100644 --- a/tests/basicswap/test_reload.py +++ b/tests/basicswap/test_reload.py @@ -104,7 +104,7 @@ class Test(unittest.TestCase): data = { 'addr_from': '-1', - 'coin_from': '1', + 'coin_from': 'PART', 'coin_to': '2', 'amt_from': '1', 'amt_to': '1', diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index f8ed63e..e2cfe3b 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -29,6 +29,9 @@ from basicswap.basicswap import ( from basicswap.basicswap_util import ( TxLockTypes, ) +from basicswap.chainparams import ( + chainparams, +) from basicswap.util import ( COIN, make_int, @@ -83,7 +86,8 @@ class Test(BaseTest): def getBalance(self, js_wallets, coin_type): 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'])) + ticker = chainparams[coin_type]['ticker'] + return ci.make_int(float(js_wallets[ticker]['balance']) + float(js_wallets[ticker]['unconfirmed'])) def test_001_js_coins(self): js_coins = read_json_api(1800, 'coins') @@ -395,7 +399,7 @@ class Test(BaseTest): ltc_addr = callnoderpc(0, 'getnewaddress', ['Withdrawal test', 'legacy'], base_rpc_port=LTC_BASE_RPC_PORT) wallets0 = read_json_api(TEST_HTTP_PORT + 0, 'wallets') - assert(float(wallets0['3']['balance']) > 100) + assert(float(wallets0['LTC']['balance']) > 100) post_json = { 'value': 100, diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index 98a7dc9..f45b992 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -555,8 +555,8 @@ class Test(BaseTest): swap_clients = self.swap_clients js_1 = read_json_api(1801, 'wallets') - assert(make_int(js_1[str(int(Coins.XMR))]['balance'], scale=12) > 0) - assert(make_int(js_1[str(int(Coins.XMR))]['unconfirmed'], scale=12) > 0) + assert(make_int(js_1[Coins.XMR.name]['balance'], scale=12) > 0) + assert(make_int(js_1[Coins.XMR.name]['unconfirmed'], scale=12) > 0) offer_id = swap_clients[0].postOffer(Coins.PART, Coins.XMR, 100 * COIN, 0.11 * XMR_COIN, 100 * COIN, SwapTypes.XMR_SWAP) wait_for_offer(test_delay_event, swap_clients[1], offer_id) @@ -577,7 +577,7 @@ class Test(BaseTest): wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True) js_0_end = read_json_api(1800, 'wallets') - end_xmr = float(js_0_end['6']['balance']) + float(js_0_end['6']['unconfirmed']) + end_xmr = float(js_0_end['XMR']['balance']) + float(js_0_end['XMR']['unconfirmed']) assert(end_xmr > 10.9 and end_xmr < 11.0) bid_id_hex = bid_id.hex() @@ -861,7 +861,7 @@ class Test(BaseTest): js_w0_after = read_json_api(1800, 'wallets') js_w1_after = read_json_api(1801, 'wallets') - assert(make_int(js_w1_after['2']['balance'], scale=8, r=1) - (make_int(js_w1_before['2']['balance'], scale=8, r=1) + amt_1) < 1000) + assert(make_int(js_w1_after['BTC']['balance'], scale=8, r=1) - (make_int(js_w1_before['BTC']['balance'], scale=8, r=1) + amt_1) < 1000) def test_07_revoke_offer(self): logging.info('---------- Test offer revocaction') @@ -877,10 +877,10 @@ class Test(BaseTest): logging.info('---------- Test XMR withdrawals') swap_clients = self.swap_clients js_0 = read_json_api(1800, 'wallets') - address_to = js_0[str(int(Coins.XMR))]['deposit_address'] + address_to = js_0[Coins.XMR.name]['deposit_address'] js_1 = read_json_api(1801, 'wallets') - assert(float(js_1[str(int(Coins.XMR))]['balance']) > 0.0) + assert(float(js_1[Coins.XMR.name]['balance']) > 0.0) swap_clients[1].withdrawCoin(Coins.XMR, 1.1, address_to, False) @@ -1113,10 +1113,10 @@ class Test(BaseTest): pause_event.clear() js_0 = read_json_api(1800, 'wallets') - address_to = js_0[str(int(Coins.XMR))]['deposit_address'] + address_to = js_0[Coins.XMR.name]['deposit_address'] wallets1 = read_json_api(TEST_HTTP_PORT + 1, 'wallets') - xmr_total = float(wallets1[str(int(Coins.XMR))]['balance']) + xmr_total = float(wallets1[Coins.XMR.name]['balance']) assert(xmr_total > 10) post_json = { diff --git a/tests/basicswap/test_xmr_bids_offline.py b/tests/basicswap/test_xmr_bids_offline.py index 9c81794..f5a4e82 100644 --- a/tests/basicswap/test_xmr_bids_offline.py +++ b/tests/basicswap/test_xmr_bids_offline.py @@ -50,12 +50,12 @@ class Test(XmrTestBase): waitForServer(self.delay_event, 12700) waitForServer(self.delay_event, 12701) wallets1 = read_json_api(12701, 'wallets') - assert(float(wallets1['6']['balance']) > 0.0) + assert(float(wallets1['XMR']['balance']) > 0.0) offer_data = { 'addr_from': -1, - 'coin_from': 1, - 'coin_to': 6, + 'coin_from': 'PART', + 'coin_to': 'XMR', 'amt_from': 1, 'amt_to': 1, 'lockhrs': 24, @@ -83,7 +83,7 @@ class Test(XmrTestBase): offer0 = offers[0] post_data = { - 'coin_from': '1' + 'coin_from': 'PART' } test_post_offers = json.loads(urlopen('http://127.0.0.1:12701/json/offers', data=parse.urlencode(post_data).encode()).read()) assert(len(test_post_offers) == 2) diff --git a/tests/basicswap/test_xmr_reload.py b/tests/basicswap/test_xmr_reload.py index 84a1a7a..c28e9b5 100644 --- a/tests/basicswap/test_xmr_reload.py +++ b/tests/basicswap/test_xmr_reload.py @@ -46,7 +46,7 @@ class Test(XmrTestBase): waitForServer(self.delay_event, 12700) waitForServer(self.delay_event, 12701) wallets1 = read_json_api(12701, 'wallets') - assert(float(wallets1['6']['balance']) > 0.0) + assert(float(wallets1['XMR']['balance']) > 0.0) data = { 'addr_from': '-1',