mirror of
https://github.com/basicswap/basicswap.git
synced 2024-12-22 19:49:20 +00:00
api: Add validateamount command
This commit is contained in:
parent
42421321f6
commit
a1bcf8d4b9
6 changed files with 49 additions and 5 deletions
|
@ -632,6 +632,33 @@ def js_automationstrategies(self, url_split, post_string: str, is_json: bool) ->
|
||||||
return bytes(json.dumps(rv), 'UTF-8')
|
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:
|
def js_vacuumdb(self, url_split, post_string, is_json) -> bytes:
|
||||||
swap_client = self.server.swap_client
|
swap_client = self.server.swap_client
|
||||||
swap_client.checkSystemStatus()
|
swap_client.checkSystemStatus()
|
||||||
|
@ -747,6 +774,7 @@ pages = {
|
||||||
'notifications': js_notifications,
|
'notifications': js_notifications,
|
||||||
'identities': js_identities,
|
'identities': js_identities,
|
||||||
'automationstrategies': js_automationstrategies,
|
'automationstrategies': js_automationstrategies,
|
||||||
|
'validateamount': js_validateamount,
|
||||||
'vacuumdb': js_vacuumdb,
|
'vacuumdb': js_vacuumdb,
|
||||||
'getcoinseed': js_getcoinseed,
|
'getcoinseed': js_getcoinseed,
|
||||||
'setpassword': js_setpassword,
|
'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))
|
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']
|
parsed_data['to_fee_override'] = page_data['to_fee_override']
|
||||||
except Exception as e:
|
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
|
return parsed_data, errors
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ def float_to_str(f: float) -> str:
|
||||||
return format(d1, 'f')
|
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):
|
if isinstance(v, float):
|
||||||
v = float_to_str(v)
|
v = float_to_str(v)
|
||||||
elif isinstance(v, int):
|
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:
|
if r == 0:
|
||||||
raise ValueError('Mantissa too long')
|
raise ValueError('Mantissa too long')
|
||||||
if r > 0:
|
if r > 0:
|
||||||
# Round up
|
# Round off
|
||||||
if int(c) > 4:
|
if int(c) > 4:
|
||||||
rv += 1
|
rv += 1
|
||||||
break
|
break
|
||||||
|
|
|
@ -463,6 +463,7 @@ def main():
|
||||||
print(f'Not bidding on offer {offer_id}, too many failed bids ({failed_sent_bids}).')
|
print(f'Not bidding on offer {offer_id}, too many failed bids ({failed_sent_bids}).')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
validateamount: bool = False
|
||||||
max_coin_from_balance = bid_template.get('max_coin_from_balance', -1)
|
max_coin_from_balance = bid_template.get('max_coin_from_balance', -1)
|
||||||
if max_coin_from_balance > 0:
|
if max_coin_from_balance > 0:
|
||||||
wallet_from = read_json_api_wallet('wallets/{}'.format(coin_from_data['ticker']))
|
wallet_from = read_json_api_wallet('wallets/{}'.format(coin_from_data['ticker']))
|
||||||
|
@ -472,6 +473,7 @@ def main():
|
||||||
if total_balance_from + bid_amount > max_coin_from_balance:
|
if total_balance_from + bid_amount > max_coin_from_balance:
|
||||||
if can_adjust_amount and max_coin_from_balance - total_balance_from > min_swap_amount:
|
if can_adjust_amount and max_coin_from_balance - total_balance_from > min_swap_amount:
|
||||||
bid_amount = max_coin_from_balance - total_balance_from
|
bid_amount = max_coin_from_balance - total_balance_from
|
||||||
|
validateamount = True
|
||||||
print(f'Reduced bid amount to {bid_amount}')
|
print(f'Reduced bid amount to {bid_amount}')
|
||||||
else:
|
else:
|
||||||
if args.debug:
|
if args.debug:
|
||||||
|
@ -493,8 +495,9 @@ def main():
|
||||||
adjusted_bid_amount = adjusted_swap_amount_to / offer_rate
|
adjusted_bid_amount = adjusted_swap_amount_to / offer_rate
|
||||||
|
|
||||||
if adjusted_bid_amount > min_swap_amount:
|
if adjusted_bid_amount > min_swap_amount:
|
||||||
print(f'Reduced bid amount to {bid_amount}')
|
|
||||||
bid_amount = adjusted_bid_amount
|
bid_amount = adjusted_bid_amount
|
||||||
|
validateamount = True
|
||||||
|
print(f'Reduced bid amount to {bid_amount}')
|
||||||
swap_amount_to = adjusted_bid_amount * offer_rate
|
swap_amount_to = adjusted_bid_amount * offer_rate
|
||||||
|
|
||||||
if total_balance_to - swap_amount_to < min_coin_to_balance:
|
if total_balance_to - swap_amount_to < min_coin_to_balance:
|
||||||
|
@ -502,6 +505,8 @@ def main():
|
||||||
print(f'Bid amount would exceed minimum coin to wallet total for offer {offer_id}')
|
print(f'Bid amount would exceed minimum coin to wallet total for offer {offer_id}')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if validateamount:
|
||||||
|
bid_amount = read_json_api('validateamount', {'coin': coin_from_data['ticker'], 'amount': bid_amount, 'method': 'rounddown'})
|
||||||
bid_data = {
|
bid_data = {
|
||||||
'offer_id': offer['offer_id'],
|
'offer_id': offer['offer_id'],
|
||||||
'amount_from': bid_amount}
|
'amount_from': bid_amount}
|
||||||
|
|
|
@ -507,7 +507,7 @@ class Test(unittest.TestCase):
|
||||||
possible_bids = get_possible_bids(rv_stdout)
|
possible_bids = get_possible_bids(rv_stdout)
|
||||||
possible_bids = get_possible_bids(rv_stdout)
|
possible_bids = get_possible_bids(rv_stdout)
|
||||||
assert (len(possible_bids) == 1)
|
assert (len(possible_bids) == 1)
|
||||||
assert (float(possible_bids[0]['amount_from'] < 20.0))
|
assert (float(possible_bids[0]['amount_from']) < 20.0)
|
||||||
|
|
||||||
logging.info('Adding mock data to node1 db for tests')
|
logging.info('Adding mock data to node1 db for tests')
|
||||||
rows = []
|
rows = []
|
||||||
|
|
|
@ -185,6 +185,15 @@ class Test(BaseTest):
|
||||||
rv = read_json_api(1800, 'wallets/part')
|
rv = read_json_api(1800, 'wallets/part')
|
||||||
assert ('locked_utxos' in rv)
|
assert ('locked_utxos' in rv)
|
||||||
|
|
||||||
|
rv = read_json_api(1800, 'validateamount', {'coin': 'part', 'amount': 0.000000015})
|
||||||
|
assert ('Mantissa too long' in rv['error'])
|
||||||
|
|
||||||
|
rv = read_json_api(1800, 'validateamount', {'coin': 'part', 'amount': 0.000000015, 'method': 'roundoff'})
|
||||||
|
assert (rv == '0.00000002')
|
||||||
|
|
||||||
|
rv = read_json_api(1800, 'validateamount', {'coin': 'part', 'amount': 0.000000015, 'method': 'rounddown'})
|
||||||
|
assert (rv == '0.00000001')
|
||||||
|
|
||||||
def test_004_validateSwapType(self):
|
def test_004_validateSwapType(self):
|
||||||
logging.info('---------- Test validateSwapType')
|
logging.info('---------- Test validateSwapType')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue