mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 18:38:09 +01:00
api: Add validateamount command
This commit is contained in:
@@ -632,6 +632,33 @@ def js_automationstrategies(self, url_split, post_string: str, is_json: bool) ->
|
||||
return bytes(json.dumps(rv), 'UTF-8')
|
||||
|
||||
|
||||
def js_validateamount(self, url_split, post_string: str, is_json: bool) -> bytes:
|
||||
swap_client = self.server.swap_client
|
||||
swap_client.checkSystemStatus()
|
||||
|
||||
post_data = getFormData(post_string, is_json)
|
||||
|
||||
ticker_str = post_data['coin']
|
||||
amount = post_data['amount']
|
||||
round_method = post_data.get('method', 'none')
|
||||
|
||||
valid_round_methods = ('roundoff', 'rounddown', 'none')
|
||||
if round_method not in valid_round_methods:
|
||||
raise ValueError(f'Unknown rounding method, must be one of {valid_round_methods}')
|
||||
|
||||
coin_type = tickerToCoinId(ticker_str)
|
||||
ci = swap_client.ci(coin_type)
|
||||
|
||||
r = 0
|
||||
if round_method == 'roundoff':
|
||||
r = 1
|
||||
elif round_method == 'rounddown':
|
||||
r = -1
|
||||
|
||||
output_amount = ci.format_amount(amount, conv_int=True, r=r)
|
||||
return bytes(json.dumps(output_amount), 'UTF-8')
|
||||
|
||||
|
||||
def js_vacuumdb(self, url_split, post_string, is_json) -> bytes:
|
||||
swap_client = self.server.swap_client
|
||||
swap_client.checkSystemStatus()
|
||||
@@ -747,6 +774,7 @@ pages = {
|
||||
'notifications': js_notifications,
|
||||
'identities': js_identities,
|
||||
'automationstrategies': js_automationstrategies,
|
||||
'validateamount': js_validateamount,
|
||||
'vacuumdb': js_vacuumdb,
|
||||
'getcoinseed': js_getcoinseed,
|
||||
'setpassword': js_setpassword,
|
||||
|
||||
@@ -250,7 +250,9 @@ def parseOfferFormData(swap_client, form_data, page_data, options={}):
|
||||
page_data['to_fee_override'] = ci_to.format_amount(ci_to.make_int(to_fee_override, r=1))
|
||||
parsed_data['to_fee_override'] = page_data['to_fee_override']
|
||||
except Exception as e:
|
||||
print('Error setting fee', str(e)) # Expected if missing fields
|
||||
# Error is expected if missing fields
|
||||
if swap_client.debug is True:
|
||||
swap_client.log.warning(f'parseOfferFormData failed to set fee: Error {e}')
|
||||
|
||||
return parsed_data, errors
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ def float_to_str(f: float) -> str:
|
||||
return format(d1, 'f')
|
||||
|
||||
|
||||
def make_int(v, scale: int = 8, r: int = 0) -> int: # r = 0, no rounding, fail, r > 0 round up, r < 0 floor
|
||||
def make_int(v, scale: int = 8, r: int = 0) -> int: # r = 0, no rounding (fail), r > 0 round off, r < 0 floor
|
||||
if isinstance(v, float):
|
||||
v = float_to_str(v)
|
||||
elif isinstance(v, int):
|
||||
@@ -132,7 +132,7 @@ def make_int(v, scale: int = 8, r: int = 0) -> int: # r = 0, no rounding, fail,
|
||||
if r == 0:
|
||||
raise ValueError('Mantissa too long')
|
||||
if r > 0:
|
||||
# Round up
|
||||
# Round off
|
||||
if int(c) > 4:
|
||||
rv += 1
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user