async def get_sources(ledger: BaseLedger, payment_address: str): """Retrieve sources for this payment address and asynchrounsly generate.""" # We need to use ledger._submit # pylint: disable=protected-access with IndyErrorHandler('Failed to retrieve payment address sources'): next_seqno = -1 while True: get_sources_json, method = \ await payment.build_get_payment_sources_with_from_request( ledger.wallet.handle, None, payment_address, next_seqno ) resp = await ledger._submit(get_sources_json, sign=False) source_list, next_seqno = \ await payment.parse_get_payment_sources_with_from_response( method, resp ) sources = json.loads(source_list) for source in sources: yield source if next_seqno == -1: break
async def fetch_transfer_auth(ledger: BaseLedger): """Retrieve token transfer fee.""" # We need to use ledger._submit # pylint: disable=protected-access with IndyErrorHandler('Failed to retrieve transfer auth'): req = await payment.build_get_txn_fees_req( ledger.wallet.handle, None, SOV_METHOD ) xfer_fee_resp = await ledger._submit(req, sign=False) parse_xfer_fee = await payment.parse_get_txn_fees_response( SOV_METHOD, xfer_fee_resp ) req = await indy_ledger.build_get_auth_rule_request( None, "10001", "ADD", "*", None, "*" ) auth_rule_resp = await ledger._submit(req, sign=False) xfer_auth_fee = json.loads( await payment.get_request_info( auth_rule_resp, json.dumps({'sig_count': 1}), parse_xfer_fee ) ) if ledger.cache: await ledger.cache.set( ['admin-payments::xfer_auth'], xfer_auth_fee, ledger.cache_duration, ) return xfer_auth_fee
async def make_payment( ledger: BaseLedger, inputs: Sequence[str], outputs: Sequence[Dict], extra: Dict = None ) -> Dict: """Make a payment. Arguments: inputs (Sequence[str]): Input sources for the payment i.e. ["txo:sov:12345...", "txo:sov:abcde..."] Use prepare_payment to construct this based off a desired amount to transfer, source, and destintation address. outputs (Sequence[Dict]): Output objects for the payment i.e. [ { "recipient": "pay:sov:1234abcd...", "amount": 100000 }, { "recipient": "pay:sov:abcd1234...", "amount": 200000 }, ] Use prepare_payment to construct this based off a desired amount to transfer, source, and destintation address. extra (Dict): Optional extra information for the payment transaction. """ # We need to use ledger._submit # pylint: disable=protected-access with IndyErrorHandler('Payment failed'): extras = await prepare_extra(ledger, extra) payment_req, payment_method = await payment.build_payment_req( ledger.wallet.handle, None, json.dumps(inputs), json.dumps(outputs), extras ) payment_resp = await ledger._submit( payment_req, sign=False ) receipts = await payment.parse_payment_response( payment_method, payment_resp ) return json.loads(receipts)
async def prepare_extra(ledger: BaseLedger, extra: Dict = None): """Prepare extras field for submission of payment request.""" extra_json = json.dumps(extra or {}) acceptance = await ledger.get_latest_txn_author_acceptance() if acceptance: with IndyErrorHandler( 'Failed to append txn author acceptance to extras'): extra_json = await ( indy_ledger.append_txn_author_agreement_acceptance_to_request( extra_json, acceptance["text"], acceptance["version"], acceptance["digest"], acceptance["mechanism"], acceptance["time"], )) return extra_json
def test_error_handler(self): with self.assertRaises(LedgerTransactionError): with IndyErrorHandler("message", LedgerTransactionError): raise IndyError(error_code=1)