ui: Change Subtract Fee to Sweep All for XMR.

This commit is contained in:
tecnovert 2024-02-10 19:05:48 +02:00
parent e9986148d7
commit 9be4bd28fd
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
6 changed files with 104 additions and 50 deletions

View file

@ -1910,6 +1910,9 @@ class BasicSwap(BaseApp):
def withdrawCoin(self, coin_type, value, addr_to, subfee: bool) -> str:
ci = self.ci(coin_type)
if subfee and coin_type == Coins.XMR:
self.log.info('withdrawCoin sweep all {} to {}'.format(ci.ticker(), addr_to))
else:
self.log.info('withdrawCoin {} {} to {} {}'.format(value, ci.ticker(), addr_to, ' subfee' if subfee else ''))
txid = ci.withdrawCoin(value, addr_to, subfee)

View file

@ -473,25 +473,24 @@ class XMRInterface(CoinInterface):
return bytes.fromhex(rv['tx_hash_list'][0])
def withdrawCoin(self, value: int, addr_to: str, subfee: bool) -> str:
def withdrawCoin(self, value: int, addr_to: str, sweepall: bool) -> str:
with self._mx_wallet:
value_sats = make_int(value, self.exp())
self.openWallet(self._wallet_filename)
self.rpc_wallet('refresh')
if subfee:
if sweepall:
balance = self.rpc_wallet('get_balance')
diff = balance['unlocked_balance'] - value_sats
if diff >= 0 and diff <= 10:
self._log.info('subfee enabled and value close to total, using sweep_all.')
if balance['balance'] != balance['unlocked_balance']:
raise ValueError('Balance must be fully confirmed to use sweep all.')
self._log.info('XMR withdraw sweep_all.')
self._log.debug('XMR balance: {}'.format(balance['balance']))
params = {'address': addr_to}
if self._fee_priority > 0:
params['priority'] = self._fee_priority
rv = self.rpc_wallet('sweep_all', params)
return rv['tx_hash_list'][0]
raise ValueError('Withdraw value must be close to total to use subfee/sweep_all.')
value_sats: int = make_int(value, self.exp())
params = {'destinations': [{'amount': value_sats, 'address': addr_to}]}
if self._fee_priority > 0:
params['priority'] = self._fee_priority

View file

@ -52,9 +52,17 @@ def getFormData(post_string: str, is_json: bool):
def withdraw_coin(swap_client, coin_type, post_string, is_json):
post_data = getFormData(post_string, is_json)
value = get_data_entry(post_data, 'value')
address = get_data_entry(post_data, 'address')
if coin_type == Coins.XMR:
value = None
sweepall = get_data_entry(post_data, 'sweepall')
if not isinstance(sweepall, bool):
sweepall = toBool(sweepall)
if not sweepall:
value = get_data_entry(post_data, 'value')
else:
value = get_data_entry(post_data, 'value')
subfee = get_data_entry(post_data, 'subfee')
if not isinstance(subfee, bool):
subfee = toBool(subfee)
@ -66,6 +74,8 @@ def withdraw_coin(swap_client, coin_type, post_string, is_json):
elif coin_type == Coins.LTC:
type_from = get_data_entry_or(post_data, 'type_from', 'plain')
txid_hex = swap_client.withdrawLTC(type_from, value, address, subfee)
elif coin_type == Coins.XMR:
txid_hex = swap_client.withdrawCoin(coin_type, value, address, sweepall)
else:
txid_hex = swap_client.withdrawCoin(coin_type, value, address, subfee)

View file

@ -326,14 +326,21 @@
<input placeholder="{{ w.ticker }} Address" class="hover:border-blue-500 bg-gray-50 text-gray-900 appearance-none pr-10 dark:bg-gray-500 dark:text-white border border-gray-300 dark:border-gray-400 dark:text-gray-50 dark:placeholder-gray-400 text-sm rounded-lg outline-none focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 focus:ring-0" type="text" name="to_{{ w.cid }}" value="{{ w.wd_address }}">
</td>
<td class="py-3 px-6">
<input placeholder="{{ w.ticker }} Amount" class="hover:border-blue-500 bg-gray-50 text-gray-900 appearance-none pr-10 dark:bg-gray-500 dark:text-white border border-gray-300 dark:border-gray-400 dark:text-gray-50 dark:placeholder-gray-400 text-sm rounded-lg outline-none focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 focus:ring-0" type="text" name="amt_{{ w.cid }}" value="{{ w.wd_value }}">
<input placeholder="{{ w.ticker }} Amount" class="hover:border-blue-500 bg-gray-50 text-gray-900 appearance-none pr-10 dark:bg-gray-500 dark:text-white border border-gray-300 dark:border-gray-400 dark:text-gray-50 dark:placeholder-gray-400 text-sm rounded-lg outline-none focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 focus:ring-0" type="text" id="amount" name="amt_{{ w.cid }}" value="{{ w.wd_value }}">
</td>
</tr>
<tr class="opacity-100 text-gray-500 dark:text-gray-100">
{% if w.cid == '6' %} {# XMR #}
<td class="py-3 px-6 bold">Sweep All:</td>
<td class="py-3 px-6">
<input class="hover:border-blue-500 w-5 h-5 form-check-input text-blue-600 bg-gray-50 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-1 dark:bg-gray-500 dark:border-gray-400" type="checkbox" id="sweepall" name="sweepall_{{ w.cid }}" {% if w.wd_sweepall==true %} checked=checked{% endif %}>
</td>
{% else %}
<td class="py-3 px-6 bold">Subtract Fee:</td>
<td class="py-3 px-6">
<input class="hover:border-blue-500 w-5 h-5 form-check-input text-blue-600 bg-gray-50 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-1 dark:bg-gray-500 dark:border-gray-400" type="checkbox" name="subfee_{{ w.cid }}" {% if w.wd_subfee==true %} checked=checked{% endif %}>
</td>
{% endif %}
<td></td>
</tr>
{% if w.cid == '1' %} {# PART #}
@ -623,6 +630,24 @@ document.addEventListener('DOMContentLoaded', () => {
}
calculateTotalUsdValue();
function set_sweep_all(element) {
let input = document.getElementById('amount');
if (element.checked) {
input.disabled = true;
input.style.display = 'none';
} else {
input.disabled = false;
input.style.display = 'block';
}
}
let cb_sweepall = document.getElementById('sweepall');
if (cb_sweepall) {
set_sweep_all(cb_sweepall);
cb_sweepall.addEventListener('change', (event) => {
set_sweep_all(event.currentTarget);
})
}
});
</script>

View file

@ -150,6 +150,13 @@ def page_wallet(self, url_split, post_string):
err_messages.append('Reseed failed ' + str(ex))
swap_client.updateWalletsInfo(True, coin_id)
elif bytes('withdraw_' + cid, 'utf-8') in form_data:
subfee = True if bytes('subfee_' + cid, 'utf-8') in form_data else False
page_data['wd_subfee_' + cid] = subfee
sweepall = True if bytes('sweepall_' + cid, 'utf-8') in form_data else False
page_data['wd_sweepall_' + cid] = sweepall
value = None
if not sweepall:
try:
value = form_data[bytes('amt_' + cid, 'utf-8')][0].decode('utf-8')
page_data['wd_value_' + cid] = value
@ -161,9 +168,6 @@ def page_wallet(self, url_split, post_string):
except Exception as e:
err_messages.append('Missing address')
subfee = True if bytes('subfee_' + cid, 'utf-8') in form_data else False
page_data['wd_subfee_' + cid] = subfee
if coin_id == Coins.PART:
try:
type_from = form_data[bytes('withdraw_type_from_' + cid, 'utf-8')][0].decode('utf-8')
@ -179,7 +183,7 @@ def page_wallet(self, url_split, post_string):
except Exception as e:
err_messages.append('Missing type')
if len(messages) == 0:
if len(err_messages) == 0:
ci = swap_client.ci(coin_id)
ticker = ci.ticker()
try:
@ -189,6 +193,12 @@ def page_wallet(self, url_split, post_string):
elif coin_id == Coins.LTC:
txid = swap_client.withdrawLTC(type_from, value, address, subfee)
messages.append('Withdrew {} {} (from {}) to address {}<br/>In txid: {}'.format(value, ticker, type_from, address, txid))
elif coin_id == Coins.XMR:
txid = swap_client.withdrawCoin(coin_id, value, address, sweepall)
if sweepall:
messages.append('Swept all {} to address {}<br/>In txid: {}'.format(ticker, address, txid))
else:
messages.append('Withdrew {} {} to address {}<br/>In txid: {}'.format(value, ticker, address, txid))
else:
txid = swap_client.withdrawCoin(coin_id, value, address, subfee)
messages.append('Withdrew {} {} to address {}<br/>In txid: {}'.format(value, ticker, address, txid))
@ -261,6 +271,8 @@ def page_wallet(self, url_split, post_string):
wallet_data['wd_address'] = page_data['wd_address_' + cid]
if 'wd_subfee_' + cid in page_data:
wallet_data['wd_subfee'] = page_data['wd_subfee_' + cid]
if 'wd_sweepall_' + cid in page_data:
wallet_data['wd_sweepall'] = page_data['wd_sweepall_' + cid]
if 'utxo_value' in page_data:
wallet_data['utxo_value'] = page_data['utxo_value']

View file

@ -1151,21 +1151,10 @@ class Test(BaseTest):
js_1 = read_json_api(1801, 'wallets')
assert (float(js_1[Coins.XMR.name]['balance']) > 0.0)
post_json = {
'value': 0.001,
'address': address_to,
'subfee': True,
}
rv = read_json_api(1801, 'wallets/xmr/withdraw', post_json)
assert ('Withdraw value must be close to total to use subfee' in rv['error'])
post_json['value'] = 1000000000.0
rv = read_json_api(1801, 'wallets/xmr/withdraw', post_json)
assert ('Withdraw value must be close to total to use subfee' in rv['error'])
post_json = {
'value': 1.1,
'address': address_to,
'subfee': False,
'sweepall': False,
}
rv = read_json_api(1801, 'wallets/xmr/withdraw', post_json)
assert (len(rv['txid']) == 64)
@ -1552,28 +1541,44 @@ class Test(BaseTest):
def test_97_withdraw_all(self):
logging.info('---------- Test XMR withdrawal all')
wait_for_balance(test_delay_event, 'http://127.0.0.1:1800/json/wallets/xmr', 'unconfirmed', 0.0)
wallets0 = read_json_api(TEST_HTTP_PORT + 0, 'wallets')
xmr_total = float(wallets0[Coins.XMR.name]['balance'])
if xmr_total < 10.0:
address_to = read_json_api(1800, 'wallets')[Coins.XMR.name]['deposit_address']
post_json = {
'value': 10.0,
'address': address_to,
'sweepall': False,
}
json_rv = read_json_api(TEST_HTTP_PORT + 1, 'wallets/xmr/withdraw', post_json)
wait_for_balance(test_delay_event, 'http://127.0.0.1:1800/json/wallets/xmr', 'balance', 10.0)
post_json = {
'address': read_json_api(1801, 'wallets')[Coins.XMR.name]['deposit_address'],
'sweepall': True,
}
json_rv = json.loads(post_json_req('http://127.0.0.1:{}/json/wallets/xmr/withdraw'.format(TEST_HTTP_PORT + 0), post_json))
assert (len(json_rv['txid']) == 64)
try:
logging.info('Disabling XMR mining')
pause_event.clear()
js_0 = read_json_api(1800, 'wallets')
address_to = js_0[Coins.XMR.name]['deposit_address']
address_to = read_json_api(1800, 'wallets')[Coins.XMR.name]['deposit_address']
wallets1 = read_json_api(TEST_HTTP_PORT + 1, 'wallets')
xmr_total = float(wallets1[Coins.XMR.name]['balance'])
assert (xmr_total > 10)
post_json = {
'value': 10,
'address': address_to,
'subfee': True,
'sweepall': 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)
assert ('Balance must be fully confirmed to use sweep all' in json_rv['error'])
finally:
logging.info('Restoring XMR mining')
pause_event.set()