Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions token-distribution/indy_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 9 additions & 7 deletions token-distribution/token-distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 0 additions & 4 deletions token-distribution/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down