Exemplo n.º 1
0
    def send_signed_transaction(
        self, api: LedgerApi, message: LedgerApiMessage, dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'send_signed_tx'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        transaction_digest = api.send_signed_transaction(
            message.signed_transaction.body
        )
        if transaction_digest is None:  # pragma: nocover
            response = self.get_error_message(
                ValueError("No transaction_digest returned"), api, message, dialogue
            )
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.TRANSACTION_DIGEST,
                    target_message=message,
                    transaction_digest=TransactionDigest(
                        message.signed_transaction.ledger_id, transaction_digest
                    ),
                ),
            )
        return response
Exemplo n.º 2
0
    def get_raw_transaction(
        self, api: LedgerApi, message: LedgerApiMessage, dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'get_raw_transaction'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        raw_transaction = api.get_transfer_transaction(
            sender_address=message.terms.sender_address,
            destination_address=message.terms.counterparty_address,
            amount=message.terms.sender_payable_amount,
            tx_fee=message.terms.fee,
            tx_nonce=message.terms.nonce,
            **message.terms.kwargs,
        )
        if raw_transaction is None:
            response = self.get_error_message(
                ValueError("No raw transaction returned"), api, message, dialogue
            )
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.RAW_TRANSACTION,
                    target_message=message,
                    raw_transaction=RawTransaction(
                        message.terms.ledger_id, raw_transaction
                    ),
                ),
            )
        return response
Exemplo n.º 3
0
    def get_balance(
        self, api: LedgerApi, message: LedgerApiMessage, dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'get_balance'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        balance = api.get_balance(message.address)
        if balance is None:
            response = self.get_error_message(
                ValueError("No balance returned"), api, message, dialogue
            )
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.BALANCE,
                    target_message=message,
                    balance=balance,
                    ledger_id=message.ledger_id,
                ),
            )
        return response
    def get_state(
        self,
        api: LedgerApi,
        message: LedgerApiMessage,
        dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'get_state'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        result = api.get_state(message.callable, *message.args,
                               **message.kwargs.body)
        if result is None:  # pragma: nocover
            response = self.get_error_message(
                ValueError("Failed to get state"), api, message, dialogue)
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.STATE,
                    target_message=message,
                    state=State(message.ledger_id, result),
                    ledger_id=message.ledger_id,
                ),
            )
        return response
    def get_transaction_receipt(
        self,
        api: LedgerApi,
        message: LedgerApiMessage,
        dialogue: LedgerApiDialogue,
    ) -> LedgerApiMessage:
        """
        Send the request 'get_transaction_receipt'.

        :param api: the API object.
        :param message: the Ledger API message
        :return: None
        """
        is_settled = False
        attempts = 0
        while (not is_settled and attempts < self.MAX_ATTEMPTS
               and self.connection_state.get() == ConnectionStates.connected):
            time.sleep(self.TIMEOUT)
            transaction_receipt = api.get_transaction_receipt(
                message.transaction_digest.body)
            if transaction_receipt is not None:
                is_settled = api.is_transaction_settled(transaction_receipt)
            attempts += 1
        attempts = 0
        transaction = api.get_transaction(message.transaction_digest.body)
        while (transaction is None and attempts < self.MAX_ATTEMPTS
               and self.connection_state.get() == ConnectionStates.connected):
            time.sleep(self.TIMEOUT)
            transaction = api.get_transaction(message.transaction_digest.body)
            attempts += 1
        if not is_settled:  # pragma: nocover
            response = self.get_error_message(
                ValueError("Transaction not settled within timeout"),
                api,
                message,
                dialogue,
            )
        elif transaction_receipt is None:  # pragma: nocover
            response = self.get_error_message(
                ValueError("No transaction_receipt returned"), api, message,
                dialogue)
        elif transaction is None:  # pragma: nocover
            response = self.get_error_message(
                ValueError("No transaction returned"), api, message, dialogue)
        else:
            response = cast(
                LedgerApiMessage,
                dialogue.reply(
                    performative=LedgerApiMessage.Performative.
                    TRANSACTION_RECEIPT,
                    target_message=message,
                    transaction_receipt=TransactionReceipt(
                        message.transaction_digest.ledger_id,
                        transaction_receipt,
                        transaction,
                    ),
                ),
            )
        return response