diff --git a/token-distribution/indy_helpers.py b/token-distribution/indy_helpers.py index 54050c5..4ffd2f9 100644 --- a/token-distribution/indy_helpers.py +++ b/token-distribution/indy_helpers.py @@ -4,7 +4,7 @@ from indy.error import IndyError, ErrorCode from indy import wallet, ledger, payment, pool from constants import PAYMENT_METHOD, POOL_NAME, PROTOCOL_VERSION -from utils import run_coroutine, run_array +from utils import run_coroutine def open_wallet(wallet_info) -> int: @@ -116,21 +116,12 @@ def get_payment_sources(pool_handle: int, payment_address: str): handle_payment_error(err) -def verify_payment_on_ledger(pool_handle, receipts): +def verify_payment_on_ledger(pool_handle, receipt): try: - requests = run_array( - [payment.build_verify_payment_req(-1, None, receipt) for receipt in receipts]) - - responses = run_array( - [ledger.submit_request(pool_handle, list(request.result())[0]) for request in requests[0]]) - - results = run_array( - [payment.parse_verify_payment_response(PAYMENT_METHOD, response.result()) for response in responses[0]]) - - for receipt in [result.result() for result in results[0]]: - if len(json.loads(receipt)['sources']) == 0: - raise Exception('Payment failed') - return + request, _ = run_coroutine(payment.build_verify_payment_req(-1, None, receipt)) + response = run_coroutine(ledger.submit_request(pool_handle, request)) + receipts = run_coroutine(payment.parse_verify_payment_response(PAYMENT_METHOD, response)) + return json.loads(receipts) except IndyError as err: handle_payment_error(err) diff --git a/token-distribution/token-distribution.py b/token-distribution/token-distribution.py index a77b277..f28e15d 100644 --- a/token-distribution/token-distribution.py +++ b/token-distribution/token-distribution.py @@ -108,15 +108,17 @@ def ensure_payment_transaction_result(pool_handle, response, targets): load_payment_plugin() receipts = parse_payment_response(response) - to_verifies = [] - for target in targets: - matches = [receipt for receipt in receipts if target["paymentAddress"] == receipt['recipient']] - if len(matches) != 1: - raise Exception('Payment failed for {}'.format(target["paymentAddress"])) - to_verifies.append(matches[0]['receipt']) + if len(receipts) == 0: + raise Exception('Payment failed: There is no any receipts') + + expected = set([target["paymentAddress"] for target in targets]) + actual = set([receipt['recipient'] for receipt in receipts]) + + if expected > actual: + raise Exception('Payment failed for {}'.format(expected - actual)) - verify_payment_on_ledger(pool_handle, to_verifies) + verify_payment_on_ledger(pool_handle, receipts[0]['receipt']) def prepare(args): diff --git a/token-distribution/utils.py b/token-distribution/utils.py index c1a9f91..7800d70 100644 --- a/token-distribution/utils.py +++ b/token-distribution/utils.py @@ -32,10 +32,6 @@ def run_coroutine(coroutine): return loop.run_until_complete(coroutine) -def run_array(array: list): - return run_coroutine(asyncio.wait(array)) - - def read_file(data_file): with open(data_file, newline='') as data_file: return data_file.read()