Fix UnboundLocalError by removing local time import

The local 'import time' inside a function was shadowing the module-level
time import, causing UnboundLocalError in crypto watcher tests. Use the
existing module-level time import instead.
This commit is contained in:
Russell Ballestrini 2025-09-24 10:20:35 -04:00
parent a2d471e4e8
commit d4d1ca4bdd

View file

@ -816,8 +816,6 @@ def process_payment(
if fee_amount > 0 and crypto_payment.shop_sweep_to_address:
try:
# Small delay to ensure refund tx is processed
import time
time.sleep(2)
if crypto_payment.coin_type == "XMR":
@ -992,7 +990,23 @@ def process_payment(
f"Excess refund failed for payment {crypto_payment.id}: {result['error']}"
)
# Note: Auto-sweep is handled separately based on unlocked balance, not confirmation status
# Auto-sweep confirmed payments that haven't been swept yet
if (
client
and crypto_payment.shop_sweep_to_address
and not crypto_payment.is_swept
):
logger.info(f"Payment {crypto_payment.id} confirmed - attempting sweep")
try:
sweep_success = auto_sweep_payment(
client, crypto_payment, env_request.dbsession
)
if sweep_success and crypto_payment.is_swept:
logger.info(f"Payment {crypto_payment.id} swept successfully")
except Exception as e:
logger.error(
f"Auto-sweep failed for confirmed payment {crypto_payment.id}: {e}"
)
else:
if (
crypto_payment.received_amount > 0
@ -1037,39 +1051,7 @@ def process_payment(
f"Refund failed for underpayment {crypto_payment.id}: {result['error']}"
)
# Auto-sweep based on unlocked balance availability, not just confirmation status
if (
crypto_payment.status
in [CryptoPayment.STATUS_CONFIRMED, CryptoPayment.STATUS_CONFIRMED_OVERPAID]
and client
and crypto_payment.shop_sweep_to_address
and not crypto_payment.is_swept
):
logger.info(f"Checking if funds are unlocked for payment {crypto_payment.id}")
try:
# Check if funds are actually unlocked before attempting sweep
result = client._call(
"get_balance", {"account_index": crypto_payment.account_index}
)
unlocked_balance = result.get("unlocked_balance", 0)
if unlocked_balance > 0:
logger.info(
f"Funds unlocked ({unlocked_balance} atomic units) - attempting sweep for payment {crypto_payment.id}"
)
sweep_success = auto_sweep_payment(
client, crypto_payment, env_request.dbsession
)
if sweep_success and crypto_payment.is_swept:
logger.info(f"Payment {crypto_payment.id} swept successfully")
else:
logger.debug(
f"Payment {crypto_payment.id} confirmed but funds not yet unlocked"
)
except Exception as e:
logger.error(
f"Auto-sweep check/attempt failed for payment {crypto_payment.id}: {e}"
)
# Note: Auto-sweep is now handled immediately when payment is confirmed (based on RPC confirmation data)
env_request.dbsession.add(crypto_payment)