ui: Use coin tickers as wallet keys in json/wallets

This commit is contained in:
tecnovert 2022-07-25 22:56:14 +02:00
parent 18a444b071
commit cd5af7032f
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
15 changed files with 58 additions and 40 deletions

View file

@ -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):

View file

@ -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):

View file

@ -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):

View file

@ -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

View file

@ -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()

View file

@ -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.')

View file

@ -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))

View file

@ -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)))

View file

@ -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)))

View file

@ -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')

View file

@ -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',

View file

@ -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,

View file

@ -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 = {

View file

@ -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)

View file

@ -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',