Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
def validate_order(sender, token_address, num_tokens, tx_id, did, service_id):
    dt_contract = DataToken(token_address)

    try:
        amount = to_base_18(num_tokens)
        tx, order_event, transfer_event = dt_contract.verify_order_tx(
            Web3Provider.get_web3(), tx_id, did, service_id, amount, sender)
        return tx, order_event, transfer_event
    except AssertionError:
        raise
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
def validate_order(sender, token_address, num_tokens, tx_id, did, service_id):
    dt_contract = DataToken(token_address)

    amount = to_base_18(num_tokens)
    num_tries = 3
    i = 0
    while i < num_tries:
        i += 1
        try:
            tx, order_event, transfer_event = dt_contract.verify_order_tx(
                Web3Provider.get_web3(), tx_id, did, service_id, amount,
                sender)
            return tx, order_event, transfer_event
        except ConnectionClosed:
            if i == num_tries:
                raise