コード例 #1
0
    def _request_token_create_transaction(self) -> None:
        """
        Request token create transaction

        :return: None
        """
        strategy = cast(Strategy, self.context.strategy)
        strategy.is_behaviour_active = False
        contract_api_dialogues = cast(ContractApiDialogues,
                                      self.context.contract_api_dialogues)
        contract_api_msg = ContractApiMessage(
            performative=ContractApiMessage.Performative.GET_RAW_TRANSACTION,
            dialogue_reference=contract_api_dialogues.
            new_self_initiated_dialogue_reference(),
            ledger_id=strategy.ledger_id,
            contract_id="fetchai/erc1155:0.6.0",
            contract_address=strategy.contract_address,
            callable="get_create_batch_transaction",
            kwargs=ContractApiMessage.Kwargs({
                "deployer_address": self.context.agent_address,
                "token_ids": strategy.token_ids,
            }),
        )
        contract_api_msg.counterparty = LEDGER_API_ADDRESS
        contract_api_dialogue = cast(
            Optional[ContractApiDialogue],
            contract_api_dialogues.update(contract_api_msg),
        )
        assert contract_api_dialogue is not None, "ContractApiDialogue not generated"
        contract_api_dialogue.terms = strategy.get_create_token_terms()
        self.context.outbox.put_message(message=contract_api_msg)
        self.context.logger.info(
            "[{}]: Requesting create batch transaction...".format(
                self.context.agent_name))
コード例 #2
0
    def _handle_propose(self, fipa_msg: FipaMessage,
                        fipa_dialogue: FipaDialogue) -> None:
        """
        Handle the CFP.

        If the CFP matches the supplied services then send a PROPOSE, otherwise send a DECLINE.

        :param fipa_msg: the message
        :param fipa_dialogue: the dialogue object
        :return: None
        """
        if all(key in [
                "contract_address",
                "from_supply",
                "to_supply",
                "value",
                "trade_nonce",
                "token_id",
        ] for key in fipa_msg.proposal.values.keys()):
            # accept any proposal with the correct keys
            self.context.logger.info(
                "[{}]: received valid PROPOSE from sender={}: proposal={}".
                format(
                    self.context.agent_name,
                    fipa_msg.counterparty[-5:],
                    fipa_msg.proposal.values,
                ))
            strategy = cast(Strategy, self.context.strategy)
            contract_api_dialogues = cast(ContractApiDialogues,
                                          self.context.contract_api_dialogues)
            contract_api_msg = ContractApiMessage(
                performative=ContractApiMessage.Performative.GET_RAW_MESSAGE,
                dialogue_reference=contract_api_dialogues.
                new_self_initiated_dialogue_reference(),
                ledger_id=strategy.ledger_id,
                contract_id="fetchai/erc1155:0.6.0",
                contract_address=fipa_msg.proposal.values["contract_address"],
                callable="get_hash_single",
                kwargs=ContractApiMessage.Kwargs({
                    "from_address":
                    fipa_msg.counterparty,
                    "to_address":
                    self.context.agent_address,
                    "token_id":
                    int(fipa_msg.proposal.values["token_id"]),
                    "from_supply":
                    int(fipa_msg.proposal.values["from_supply"]),
                    "to_supply":
                    int(fipa_msg.proposal.values["to_supply"]),
                    "value":
                    int(fipa_msg.proposal.values["value"]),
                    "trade_nonce":
                    int(fipa_msg.proposal.values["trade_nonce"]),
                }),
            )
            terms = Terms(
                ledger_id=strategy.ledger_id,
                sender_address=self.context.agent_address,
                counterparty_address=fipa_msg.counterparty,
                amount_by_currency_id={},
                quantities_by_good_id={
                    str(fipa_msg.proposal.values["token_id"]):
                    int(fipa_msg.proposal.values["from_supply"]) -
                    int(fipa_msg.proposal.values["to_supply"])
                },
                is_sender_payable_tx_fee=False,
                nonce=str(fipa_msg.proposal.values["trade_nonce"]),
            )
            contract_api_msg.counterparty = LEDGER_API_ADDRESS
            contract_api_dialogue = cast(
                Optional[ContractApiDialogue],
                contract_api_dialogues.update(contract_api_msg),
            )
            assert (contract_api_dialogue
                    is not None), "Error when creating contract api dialogue."
            contract_api_dialogue.terms = terms
            contract_api_dialogue.associated_fipa_dialogue = fipa_dialogue
            self.context.outbox.put_message(message=contract_api_msg)
            self.context.logger.info(
                "[{}]: requesting single hash message from contract api...".
                format(self.context.agent_name))
        else:
            self.context.logger.info(
                "[{}]: received invalid PROPOSE from sender={}: proposal={}".
                format(
                    self.context.agent_name,
                    fipa_msg.counterparty[-5:],
                    fipa_msg.proposal.values,
                ))
コード例 #3
0
ファイル: handlers.py プロジェクト: vishalbelsare/agents-aea
    def _handle_accept_w_inform(self, fipa_msg: FipaMessage,
                                fipa_dialogue: FipaDialogue) -> None:
        """
        Handle the ACCEPT_W_INFORM.

        If the ACCEPT_W_INFORM message contains the signed transaction, sign it too, otherwise do nothing.

        :param fipa_msg: the message
        :param fipa_dialogue: the dialogue object
        :return: None
        """
        tx_signature = fipa_msg.info.get("tx_signature", None)
        if tx_signature is not None:
            self.context.logger.info(
                "[{}]: received ACCEPT_W_INFORM from sender={}: tx_signature={}"
                .format(self.context.agent_name, fipa_msg.counterparty[-5:],
                        tx_signature))
            strategy = cast(Strategy, self.context.strategy)
            contract_api_dialogues = cast(ContractApiDialogues,
                                          self.context.contract_api_dialogues)
            contract_api_msg = ContractApiMessage(
                performative=ContractApiMessage.Performative.
                GET_RAW_TRANSACTION,
                dialogue_reference=contract_api_dialogues.
                new_self_initiated_dialogue_reference(),
                ledger_id=strategy.ledger_id,
                contract_id="fetchai/erc1155:0.6.0",
                contract_address=strategy.contract_address,
                callable="get_atomic_swap_single_transaction",
                kwargs=ContractApiMessage.Kwargs({
                    "from_address":
                    self.context.agent_address,
                    "to_address":
                    fipa_msg.counterparty,
                    "token_id":
                    int(fipa_dialogue.proposal.values["token_id"]),
                    "from_supply":
                    int(fipa_dialogue.proposal.values["from_supply"]),
                    "to_supply":
                    int(fipa_dialogue.proposal.values["to_supply"]),
                    "value":
                    int(fipa_dialogue.proposal.values["value"]),
                    "trade_nonce":
                    int(fipa_dialogue.proposal.values["trade_nonce"]),
                    "signature":
                    tx_signature,
                }),
            )
            contract_api_msg.counterparty = LEDGER_API_ADDRESS
            contract_api_dialogue = cast(
                Optional[ContractApiDialogue],
                contract_api_dialogues.update(contract_api_msg),
            )
            assert (contract_api_dialogue
                    is not None), "Contract api dialogue not created."
            contract_api_dialogue.terms = strategy.get_single_swap_terms(
                fipa_dialogue.proposal, fipa_msg.counterparty)
            self.context.outbox.put_message(message=contract_api_msg)
            self.context.logger.info(
                "[{}]: Requesting single atomic swap transaction...".format(
                    self.context.agent_name))
        else:
            self.context.logger.info(
                "[{}]: received ACCEPT_W_INFORM from sender={} with no signature."
                .format(self.context.agent_name, fipa_msg.counterparty[-5:]))