ui: Fix describebid for reverse PART_ANON swaps.

This commit is contained in:
tecnovert 2023-07-14 14:50:29 +02:00
parent 7bc5fc78ba
commit 9888c4ebe1
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
9 changed files with 57 additions and 31 deletions

View File

@ -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)

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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,

View File

@ -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)

View File

@ -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'

View File

@ -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:

View File

@ -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)