mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-05 10:28:10 +01:00
system: Allow preselecting inputs for atomic swaps.
This commit is contained in:
@@ -261,18 +261,32 @@ def waitForNumSwapping(delay_event, port, bids, wait_for=60):
|
||||
raise ValueError('waitForNumSwapping failed')
|
||||
|
||||
|
||||
def wait_for_balance(delay_event, url, balance_key, expect_amount, iterations=20, delay_time=3):
|
||||
def wait_for_balance(delay_event, url, balance_key, expect_amount, iterations=20, delay_time=3) -> None:
|
||||
i = 0
|
||||
while not delay_event.is_set():
|
||||
rv_js = json.loads(urlopen(url).read())
|
||||
if float(rv_js[balance_key]) >= expect_amount:
|
||||
break
|
||||
return
|
||||
delay_event.wait(delay_time)
|
||||
i += 1
|
||||
if i > iterations:
|
||||
raise ValueError('Expect {} {}'.format(balance_key, expect_amount))
|
||||
|
||||
|
||||
def wait_for_unspent(delay_event, ci, expect_amount, iterations=20, delay_time=1) -> None:
|
||||
logging.info(f'Waiting for unspent balance: {expect_amount}')
|
||||
i = 0
|
||||
while not delay_event.is_set():
|
||||
unspent_addr = ci.getUnspentsByAddr()
|
||||
for _, value in unspent_addr.items():
|
||||
if value >= expect_amount:
|
||||
return
|
||||
delay_event.wait(delay_time)
|
||||
i += 1
|
||||
if i > iterations:
|
||||
raise ValueError('wait_for_unspent {}'.format(expect_amount))
|
||||
|
||||
|
||||
def delay_for(delay_event, delay_for=60):
|
||||
logging.info('Delaying for {} seconds.'.format(delay_for))
|
||||
delay_event.wait(delay_for)
|
||||
|
||||
@@ -43,6 +43,7 @@ from tests.basicswap.common import (
|
||||
wait_for_offer,
|
||||
wait_for_bid,
|
||||
wait_for_balance,
|
||||
wait_for_unspent,
|
||||
wait_for_bid_tx_state,
|
||||
wait_for_in_progress,
|
||||
TEST_HTTP_PORT,
|
||||
@@ -496,6 +497,69 @@ class Test(BaseTest):
|
||||
assert (compare_bid_states(offerer_states, self.states_offerer[2]) is True)
|
||||
assert (compare_bid_states(bidder_states, self.states_bidder[2], exact_match=False) is True)
|
||||
|
||||
def test_14_sweep_balance(self):
|
||||
logging.info('---------- Test sweep balance offer')
|
||||
swap_clients = self.swap_clients
|
||||
|
||||
# Disable staking
|
||||
walletsettings = callnoderpc(2, 'walletsettings', ['stakingoptions', ])
|
||||
walletsettings['enabled'] = False
|
||||
walletsettings = callnoderpc(2, 'walletsettings', ['stakingoptions', walletsettings])
|
||||
walletsettings = callnoderpc(2, 'walletsettings', ['stakingoptions', ])
|
||||
assert (walletsettings['stakingoptions']['enabled'] is False)
|
||||
|
||||
# Prepare balance
|
||||
js_w2 = read_json_api(1802, 'wallets')
|
||||
if float(js_w2['PART']['balance']) < 100.0:
|
||||
post_json = {
|
||||
'value': 100,
|
||||
'address': js_w2['PART']['deposit_address'],
|
||||
'subfee': False,
|
||||
}
|
||||
json_rv = read_json_api(TEST_HTTP_PORT + 0, 'wallets/part/withdraw', post_json)
|
||||
assert (len(json_rv['txid']) == 64)
|
||||
wait_for_balance(test_delay_event, 'http://127.0.0.1:1802/json/wallets/part', 'balance', 100.0)
|
||||
|
||||
js_w2 = read_json_api(1802, 'wallets')
|
||||
assert (float(js_w2['PART']['balance']) >= 100.0)
|
||||
|
||||
js_w2 = read_json_api(1802, 'wallets')
|
||||
post_json = {
|
||||
'value': float(js_w2['PART']['balance']),
|
||||
'address': read_json_api(1802, 'wallets/part/nextdepositaddr'),
|
||||
'subfee': True,
|
||||
}
|
||||
json_rv = read_json_api(TEST_HTTP_PORT + 2, 'wallets/part/withdraw', post_json)
|
||||
wait_for_balance(test_delay_event, 'http://127.0.0.1:1802/json/wallets/part', 'balance', 10.0)
|
||||
assert (len(json_rv['txid']) == 64)
|
||||
|
||||
# Create prefunded ITX
|
||||
ci = swap_clients[2].ci(Coins.PART)
|
||||
pi = swap_clients[2].pi(SwapTypes.SELLER_FIRST)
|
||||
js_w2 = read_json_api(1802, 'wallets')
|
||||
swap_value = ci.make_int(js_w2['PART']['balance'])
|
||||
|
||||
itx = pi.getFundedInitiateTxTemplate(ci, swap_value, True)
|
||||
itx_decoded = ci.describeTx(itx.hex())
|
||||
value_after_subfee = ci.make_int(itx_decoded['vout'][0]['value'])
|
||||
assert (value_after_subfee < swap_value)
|
||||
swap_value = value_after_subfee
|
||||
wait_for_unspent(test_delay_event, ci, swap_value)
|
||||
|
||||
# Create swap with prefunded ITX
|
||||
extra_options = {'prefunded_itx': itx}
|
||||
offer_id = swap_clients[2].postOffer(Coins.PART, Coins.BTC, swap_value, 2 * COIN, swap_value, SwapTypes.SELLER_FIRST, extra_options=extra_options)
|
||||
|
||||
wait_for_offer(test_delay_event, swap_clients[1], offer_id)
|
||||
offer = swap_clients[1].getOffer(offer_id)
|
||||
bid_id = swap_clients[1].postBid(offer_id, offer.amount_from)
|
||||
|
||||
wait_for_bid(test_delay_event, swap_clients[2], bid_id)
|
||||
swap_clients[2].acceptBid(bid_id)
|
||||
|
||||
wait_for_bid(test_delay_event, swap_clients[2], bid_id, BidStates.SWAP_COMPLETED, wait_for=60)
|
||||
wait_for_bid(test_delay_event, swap_clients[1], bid_id, BidStates.SWAP_COMPLETED, sent=True, wait_for=60)
|
||||
|
||||
def pass_99_delay(self):
|
||||
logging.info('Delay')
|
||||
for i in range(60 * 10):
|
||||
|
||||
Reference in New Issue
Block a user