def pay_for_service( amount: float, token_address: str, did: str, service_id: int, fee_receiver: str, from_wallet: Wallet, consumer: str, ) -> str: """ Submits the payment for chosen service in DataTokens. :param amount: :param token_address: :param did: :param service_id: :param fee_receiver: :param from_wallet: Wallet instance :param consumer: str the address of consumer of the service, defaults to the payer (the `from_wallet` address) :return: hex str id of transfer transaction """ amount_base = to_base_18(amount) dt = DataToken(token_address) balance = dt.balanceOf(from_wallet.address) if balance < amount_base: raise AssertionError( f"Your token balance {balance} is not sufficient " f"to execute the requested service. This service " f"requires {amount_base} number of tokens.") if did.startswith("did:"): did = add_0x_prefix(did_to_id(did)) if fee_receiver is None: fee_receiver = ZERO_ADDRESS if consumer is None: consumer = from_wallet.address tx_hash = dt.startOrder(consumer, amount_base, service_id, fee_receiver, from_wallet) try: dt.verify_order_tx( Web3Provider.get_web3(), tx_hash, did, service_id, amount_base, from_wallet.address, ) return tx_hash except (AssertionError, Exception) as e: msg = ( f"Downloading asset files failed. The problem is related to " f"the transfer of the data tokens required for the download " f"service: {e}") logger.error(msg) raise AssertionError(msg)
def pay_for_service( web3: Web3, amount: int, token_address: str, did: str, service_id: int, fee_receiver: str, from_wallet: Wallet, consumer: str, ) -> str: """ Submits the payment for chosen service in DataTokens. :param amount: :param token_address: :param did: :param service_id: :param fee_receiver: :param from_wallet: Wallet instance :param consumer: str the address of consumer of the service :return: hex str id of transfer transaction """ dt = DataToken(web3, token_address) balance = dt.balanceOf(from_wallet.address) if balance < amount: raise InsufficientBalance( f"Your token balance {pretty_ether_and_wei(balance, dt.symbol())} is not sufficient " f"to execute the requested service. This service " f"requires {pretty_ether_and_wei(amount, dt.symbol())}." ) if did.startswith("did:"): did = add_0x_prefix(did_to_id(did)) if fee_receiver is None: fee_receiver = ZERO_ADDRESS tx_hash = dt.startOrder(consumer, amount, service_id, fee_receiver, from_wallet) try: dt.verify_order_tx(tx_hash, did, service_id, amount, from_wallet.address) return tx_hash except (AssertionError, Exception) as e: msg = ( f"Downloading asset files failed. The problem is related to " f"the transfer of the data tokens required for the download " f"service: {e}" ) logger.error(msg) raise AssertionError(msg)
def pay_for_service(amount: float, token_address: str, did: str, service_id: int, fee_receiver: str, from_wallet: Wallet) -> str: """ Submits the payment for chosen service in DataTokens. :param amount: :param token_address: :param did: :param service_id: :param fee_receiver: :param from_wallet: Wallet instance :return: hex str id of transfer transaction """ amount_base = to_base_18(amount) dt = DataToken(token_address) balance = dt.balanceOf(from_wallet.address) if balance < amount_base: raise AssertionError( f'Your token balance {balance} is not sufficient ' f'to execute the requested service. This service ' f'requires {amount_base} number of tokens.') if did.startswith('did:'): did = add_0x_prefix(did_to_id(did)) tx_hash = dt.startOrder(from_wallet.address, amount_base, service_id, fee_receiver, from_wallet) try: dt.verify_order_tx(Web3Provider.get_web3(), tx_hash, did, service_id, amount_base, from_wallet.address) return tx_hash except (AssertionError, Exception) as e: msg = ( f'Downloading asset files failed. The problem is related to ' f'the transfer of the data tokens required for the download ' f'service: {e}') logger.error(msg) raise AssertionError(msg)