From 9888c4ebe1e2c62a6f4f1e9fb7e882b55caf2c67 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Fri, 14 Jul 2023 14:50:29 +0200 Subject: [PATCH] ui: Fix describebid for reverse PART_ANON swaps. --- basicswap/basicswap.py | 20 ++++++++++++++++---- basicswap/interface/part.py | 2 ++ basicswap/js_server.py | 1 + basicswap/protocols/xmr_swap_1.py | 11 ----------- basicswap/ui/page_bids.py | 2 +- basicswap/ui/page_offers.py | 12 ++---------- basicswap/ui/util.py | 7 ++++--- basicswap/util/__init__.py | 2 +- tests/basicswap/test_btc_xmr.py | 31 ++++++++++++++++++++++++++++++- 9 files changed, 57 insertions(+), 31 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 32f3519..3bee460 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5741,10 +5741,10 @@ class BasicSwap(BaseApp): amount_from: int = bid_data.amount_from amount_to: int = (bid_data.amount_from * bid_data.rate) // ci_to.COIN() - ensure(abs(amount_to - bid_data.amount_to) < 10, 'invalid bid amount_to') # TODO: Tolerance? + ensure(abs(amount_to - bid_data.amount_to) < 20, 'invalid bid amount_to') # TODO: Tolerance? reversed_rate: int = ci_from.make_int(amount_from / bid_data.amount_to, r=1) amount_from_recovered: int = int((amount_to * reversed_rate) // ci_from.COIN()) - ensure(abs(amount_from - amount_from_recovered) < 10, 'invalid bid amount_from') # TODO: Tolerance? + ensure(abs(amount_from - amount_from_recovered) < 20, 'invalid bid amount_from') # TODO: Tolerance? self.validateBidAmount(offer, amount_from, bid_data.rate) @@ -6533,7 +6533,10 @@ class BasicSwap(BaseApp): rv = [] now: int = self.getTime() - query_str = 'SELECT bids.created_at, bids.expire_at, bids.bid_id, bids.offer_id, bids.amount, bids.state, bids.was_received, tx1.state, tx2.state, offers.coin_from, bids.rate, bids.bid_addr FROM bids ' + \ + query_str = 'SELECT ' + \ + 'bids.created_at, bids.expire_at, bids.bid_id, bids.offer_id, bids.amount, bids.state, bids.was_received, ' + \ + 'tx1.state, tx2.state, offers.coin_from, bids.rate, bids.bid_addr, offers.bid_reversed, bids.amount_to, offers.coin_to ' + \ + 'FROM bids ' + \ 'LEFT JOIN offers ON offers.offer_id = bids.offer_id ' + \ 'LEFT JOIN transactions AS tx1 ON tx1.bid_id = bids.bid_id AND tx1.tx_type = {} '.format(TxTypes.ITX) + \ 'LEFT JOIN transactions AS tx2 ON tx2.bid_id = bids.bid_id AND tx2.tx_type = {} '.format(TxTypes.PTX) @@ -6574,7 +6577,16 @@ class BasicSwap(BaseApp): q = session.execute(query_str) for row in q: - rv.append(row) + result = [x for x in row] + if result[12]: # Reversed + coin_from = result[9] + amount_from = result[13] + amount_to = result[4] + result[4] = amount_from + result[13] = amount_to + ci_from = self.ci(coin_from) + result[10] = ci_from.make_int(amount_to / amount_from, r=1) + rv.append(result) return rv finally: self.closeSession(session, commit=False) diff --git a/basicswap/interface/part.py b/basicswap/interface/part.py index 8d9b133..65bb947 100644 --- a/basicswap/interface/part.py +++ b/basicswap/interface/part.py @@ -42,6 +42,8 @@ class BalanceTypes(IntEnum): class PARTInterface(BTCInterface): @staticmethod def coin_type(): + # Returns the base coin type + # ANON and BLIND PART will return Coins.PART return Coins.PART @staticmethod diff --git a/basicswap/js_server.py b/basicswap/js_server.py index 2e4d6fc..5f133fd 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -256,6 +256,7 @@ def formatBids(swap_client, bids, filters) -> bytes: 'expire_at': b[1], 'coin_from': b[9], 'amount_from': swap_client.ci(b[9]).format_amount(b[4]), + 'bid_rate': swap_client.ci(b[14]).format_amount(b[10]), 'bid_state': strBidState(b[5]) } if with_extra_info: diff --git a/basicswap/protocols/xmr_swap_1.py b/basicswap/protocols/xmr_swap_1.py index 1a2592a..b21dbca 100644 --- a/basicswap/protocols/xmr_swap_1.py +++ b/basicswap/protocols/xmr_swap_1.py @@ -111,17 +111,6 @@ def getChainBRemoteSplitKey(swap_client, bid, xmr_swap, offer): return None -def reverseBidAmountAndRate(swap_client, offer, bid_amount: int, bid_rate: int) -> (int, int): - ci_from = swap_client.ci(offer.coin_to) - ci_to = swap_client.ci(offer.coin_from) - use_rate: int = offer.rate if bid_rate is None else bid_rate - amount_from: int = bid_amount - amount_to: int = int((int(amount_from) * use_rate) // ci_from.COIN()) - reversed_rate: int = ci_to.make_int(amount_from / amount_to, r=1) - - return amount_to, reversed_rate - - def setDLEAG(xmr_swap, ci_to, kbsf: bytes) -> None: if ci_to.curve_type() == Curves.ed25519: xmr_swap.kbsf_dleag = ci_to.proveDLEAG(kbsf) diff --git a/basicswap/ui/page_bids.py b/basicswap/ui/page_bids.py index 90a2127..ce7ea0f 100644 --- a/basicswap/ui/page_bids.py +++ b/basicswap/ui/page_bids.py @@ -107,7 +107,7 @@ def page_bid(self, url_split, post_string): if len(data['addr_from_label']) > 0: data['addr_from_label'] = '(' + data['addr_from_label'] + ')' - template = server.env.get_template('bid_xmr.html') if offer.swap_type == SwapTypes.XMR_SWAP else server.env.get_template('bid.html') + template = server.env.get_template('bid_xmr.html' if offer.swap_type == SwapTypes.XMR_SWAP else 'bid.html') return self.render_template(template, { 'bid_id': bid_id.hex(), 'messages': messages, diff --git a/basicswap/ui/page_offers.py b/basicswap/ui/page_offers.py index dd2cfda..7ceb787 100644 --- a/basicswap/ui/page_offers.py +++ b/basicswap/ui/page_offers.py @@ -39,7 +39,6 @@ from basicswap.basicswap_util import ( from basicswap.chainparams import ( Coins, ) -from basicswap.protocols.xmr_swap_1 import reverseBidAmountAndRate def value_or_none(v): @@ -218,7 +217,7 @@ def parseOfferFormData(swap_client, form_data, page_data, options={}): try: if len(errors) == 0 and page_data['swap_style'] == 'xmr': - reverse_bid: bool = ci_from.coin_type() in swap_client.scriptless_coins + reverse_bid: bool = coin_from in swap_client.scriptless_coins ci_leader = ci_to if reverse_bid else ci_from ci_follower = ci_from if reverse_bid else ci_to @@ -485,7 +484,7 @@ def page_offer(self, url_split, post_string): ci_from = swap_client.ci(Coins(offer.coin_from)) ci_to = swap_client.ci(Coins(offer.coin_to)) - reverse_bid: bool = ci_from.coin_type() in swap_client.scriptless_coins + reverse_bid: bool = Coins(offer.coin_from) in swap_client.scriptless_coins # Set defaults debugind = -1 @@ -606,7 +605,6 @@ def page_offer(self, url_split, post_string): data['debug_ind'] = debugind data['debug_options'] = [(int(t), t.name) for t in DebugTypes] - reverse_bid: bool = ci_from.coin_type() in swap_client.scriptless_coins ci_leader = ci_to if reverse_bid else ci_from ci_follower = ci_from if reverse_bid else ci_to @@ -635,12 +633,6 @@ def page_offer(self, url_split, post_string): formatted_bids = [] amt_swapped = 0 for b in bids: - - amount_from = b[4] - rate = b[10] - if reverse_bid: - amount_from, rate = reverseBidAmountAndRate(swap_client, offer, amount_from, rate) - amt_swapped += b[4] formatted_bids.append((b[2].hex(), ci_from.format_amount(amount_from), strBidState(b[5]), ci_to.format_amount(rate), b[11])) data['amt_swapped'] = ci_from.format_amount(amt_swapped) diff --git a/basicswap/ui/util.py b/basicswap/ui/util.py index 0acd798..8fb8f4a 100644 --- a/basicswap/ui/util.py +++ b/basicswap/ui/util.py @@ -28,7 +28,7 @@ from basicswap.basicswap_util import ( getLastBidState, ) -from basicswap.protocols.xmr_swap_1 import getChainBSplitKey, getChainBRemoteSplitKey, reverseBidAmountAndRate +from basicswap.protocols.xmr_swap_1 import getChainBSplitKey, getChainBRemoteSplitKey PAGE_LIMIT = 50 invalid_coins_from = [] @@ -149,7 +149,7 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b ci_from = swap_client.ci(Coins(offer.coin_from)) ci_to = swap_client.ci(Coins(offer.coin_to)) - reverse_bid: bool = ci_from.coin_type() in swap_client.scriptless_coins + reverse_bid: bool = Coins(offer.coin_from) in swap_client.scriptless_coins ci_leader = ci_to if reverse_bid else ci_from ci_follower = ci_from if reverse_bid else ci_to @@ -159,7 +159,8 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b initiator_role: str = 'offerer' # Leader participant_role: str = 'bidder' # Follower if reverse_bid: - bid_amount, bid_rate = reverseBidAmountAndRate(swap_client, offer, bid.amount, bid.rate) + bid_amount = bid.amount_to + bid_rate = ci_from.make_int(bid.amount / bid.amount_to, r=1) initiator_role = 'bidder' participant_role = 'offerer' diff --git a/basicswap/util/__init__.py b/basicswap/util/__init__.py index a28be5b..c905100 100644 --- a/basicswap/util/__init__.py +++ b/basicswap/util/__init__.py @@ -106,7 +106,7 @@ def float_to_str(f: float) -> str: return format(d1, 'f') -def make_int(v, scale=8, r=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 up, r < 0 floor if type(v) == float: v = float_to_str(v) elif type(v) == int: diff --git a/tests/basicswap/test_btc_xmr.py b/tests/basicswap/test_btc_xmr.py index 42615b8..c84eec8 100644 --- a/tests/basicswap/test_btc_xmr.py +++ b/tests/basicswap/test_btc_xmr.py @@ -146,6 +146,35 @@ class TestFunctions(BaseTest): bid_id = swap_clients[1].postXmrBid(offer_id, offer.amount_from) wait_for_bid(test_delay_event, swap_clients[id_offerer], bid_id, BidStates.BID_RECEIVED) + + bid0 = read_json_api(1800, f'bids/{bid_id.hex()}') + bid1 = read_json_api(1801, f'bids/{bid_id.hex()}') + + tolerance = 20 if reverse_bid else 0 + assert (bid0['ticker_from'] == ci_from.ticker()) + assert (bid1['ticker_from'] == ci_from.ticker()) + assert (bid0['ticker_to'] == ci_to.ticker()) + assert (bid1['ticker_to'] == ci_to.ticker()) + assert (abs(ci_from.make_int(bid0['amt_from']) - amt_swap) <= tolerance) + assert (abs(ci_from.make_int(bid1['amt_from']) - amt_swap) <= tolerance) + assert (abs(ci_to.make_int(bid0['bid_rate']) - rate_swap) <= tolerance) + assert (abs(ci_to.make_int(bid1['bid_rate']) - rate_swap) <= tolerance) + assert (bid0['reverse_bid'] == reverse_bid) + assert (bid1['reverse_bid'] == reverse_bid) + + found: bool = False + bids0 = read_json_api(1800, 'bids') + logging.info('bids0 {} '.format(bids0)) + for bid in bids0: + logging.info('bid {} '.format(bid)) + if bid['bid_id'] != bid_id.hex(): + continue + assert (bid['amount_from'] == bid1['amt_from']) + assert (bid['bid_rate'] == bid1['bid_rate']) + found = True + break + assert (found) + swap_clients[id_offerer].acceptBid(bid_id) wait_for_bid(test_delay_event, swap_clients[id_offerer], bid_id, BidStates.SWAP_COMPLETED, wait_for=180) @@ -162,7 +191,7 @@ class TestFunctions(BaseTest): # TODO: Discard block rewards # assert (node0_from_after < node0_from_before - amount_from) - scale_from = 8 + scale_from = ci_from.exp() amount_to = int((amt_swap * rate_swap) // (10 ** scale_from)) amount_to_float = float(ci_to.format_amount(amount_to)) node1_to_after: float = self.getBalance(js_1_after, coin_to)