XMR: Use sweep_all if trying to withdraw all coin with subfee.

This commit is contained in:
tecnovert 2021-07-29 22:58:14 +02:00
parent 0e59791746
commit 0d0ffe6fe4
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
3 changed files with 47 additions and 3 deletions

View file

@ -404,9 +404,21 @@ class XMRInterface(CoinInterface):
return bytes.fromhex(rv['tx_hash_list'][0])
def withdrawCoin(self, value, addr_to, subfee):
value_sats = make_int(value, self.exp())
self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename})
value_sats = make_int(value, self.exp())
if subfee:
balance = self.rpc_wallet_cb('get_balance')
if balance['unlocked_balance'] - value_sats <= 10:
self._log.info('subfee enabled and value close to total, using sweep_all.')
params = {'address': addr_to}
if self._fee_priority > 0:
params['priority'] = self._fee_priority
rv = self.rpc_wallet_cb('sweep_all', params)
return rv['tx_hash_list'][0]
raise ValueError('Withdraw value must be close to total to use subfee/sweep_all.')
params = {'destinations': [{'amount': value_sats, 'address': addr_to}]}
if self._fee_priority > 0:
params['priority'] = self._fee_priority

View file

@ -652,7 +652,6 @@ class Test(unittest.TestCase):
logging.info('---------- Test LTC withdrawals')
ltc_addr = ltcRpc('getnewaddress "Withdrawal test" legacy')
logging.info('ltc_addr {}'.format(ltc_addr))
wallets0 = json.loads(urlopen('http://127.0.0.1:{}/json/wallets'.format(TEST_HTTP_PORT + 0)).read())
assert(float(wallets0['3']['balance']) > 100)

View file

@ -237,11 +237,16 @@ def callnoderpc(node_id, method, params=[], wallet=None, base_rpc_port=BASE_RPC_
return callrpc(base_rpc_port + node_id, auth, method, params, wallet)
pause_event = threading.Event()
def run_coins_loop(cls):
while not test_delay_event.is_set():
pause_event.wait()
try:
if cls.btc_addr is not None:
btcRpc('generatetoaddress 1 {}'.format(cls.btc_addr))
logging.warning('cls.xmr_addr ' + str(cls.xmr_addr))
if cls.xmr_addr is not None:
callrpc_xmr_na(XMR_BASE_RPC_PORT + 1, 'generateblocks', {'wallet_address': cls.xmr_addr, 'amount_of_blocks': 1})
except Exception as e:
@ -391,6 +396,7 @@ class Test(unittest.TestCase):
cls.update_thread = threading.Thread(target=run_loop, args=(cls,))
cls.update_thread.start()
pause_event.set()
cls.coins_update_thread = threading.Thread(target=run_coins_loop, args=(cls,))
cls.coins_update_thread.start()
except Exception:
@ -644,7 +650,6 @@ class Test(unittest.TestCase):
logging.info('---------- Test XMR withdrawals')
swap_clients = self.swap_clients
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
print('js_0 debug', js_0)
address_to = js_0[str(int(Coins.XMR))]['deposit_address']
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets').read())
@ -765,6 +770,34 @@ class Test(unittest.TestCase):
js_1 = json.loads(urlopen('http://127.0.0.1:1801/json/wallets/part').read())
print('[rm] js_1', js_1)
def test_98_withdraw_all(self):
logging.info('---------- Test XMR withdrawal all')
try:
logging.info('Disabling XMR mining')
pause_event.clear()
js_0 = json.loads(urlopen('http://127.0.0.1:1800/json/wallets').read())
address_to = js_0[str(int(Coins.XMR))]['deposit_address']
wallets1 = json.loads(urlopen('http://127.0.0.1:{}/json/wallets'.format(TEST_HTTP_PORT + 1)).read())
xmr_total = float(wallets1[str(int(Coins.XMR))]['balance'])
assert(xmr_total > 10)
post_json = {
'value': 10,
'address': address_to,
'subfee': True,
}
json_rv = json.loads(post_json_req('http://127.0.0.1:{}/json/wallets/xmr/withdraw'.format(TEST_HTTP_PORT + 1), post_json))
assert(json_rv['error'] == 'Withdraw value must be close to total to use subfee/sweep_all.')
post_json['value'] = xmr_total
json_rv = json.loads(post_json_req('http://127.0.0.1:{}/json/wallets/xmr/withdraw'.format(TEST_HTTP_PORT + 1), post_json))
assert(len(json_rv['txid']) == 64)
finally:
logging.info('Restoring XMR mining')
pause_event.set()
if __name__ == '__main__':
unittest.main()