Esempio n. 1
0
 def _set_game(self, parameters: Parameters, ledger_api: LedgerApi,
               contract: ERC1155Contract) -> None:
     """Set the contract and configuration based on provided parameters."""
     game = cast(Game, self.context.game)
     game.phase = Phase.CONTRACT_DEPLOYED
     self.context.logger.info("Setting up the game")
     configuration = Configuration(
         parameters.version_id,
         parameters.tx_fee,
     )
     configuration.good_id_to_name = generate_good_id_to_name(
         parameters.good_ids)
     configuration.currency_id_to_name = generate_currency_id_to_name(
         parameters.currency_ids)
     configuration.contract_address = parameters.contract_address
     game.conf = configuration
Esempio n. 2
0
    def act(self) -> None:
        """
        Implement the act.

        :return: None
        """
        game = cast(Game, self.context.game)
        parameters = cast(Parameters, self.context.parameters)
        contract = cast(ERC1155Contract, self.context.contracts.erc1155)
        ledger_api = self.context.ledger_apis.get_api(parameters.ledger)
        if game.phase.value == Phase.CONTRACT_DEPLOYING.value:
            tx_receipt = ledger_api.get_transaction_receipt(
                tx_digest=game.contract_manager.deploy_tx_digest
            )
            if tx_receipt is None:
                self.context.logger.info(
                    "[{}]: Cannot verify whether contract deployment was successful. Retrying...".format(
                        self.context.agent_name
                    )
                )
            elif tx_receipt.status != 1:
                self.context.is_active = False
                self.context.warning(
                    "[{}]: The contract did not deployed successfully. Transaction hash: {}. Aborting!".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
            else:
                self.context.logger.info(
                    "[{}]: The contract was successfully deployed. Contract address: {}. Transaction hash: {}".format(
                        self.context.agent_name,
                        tx_receipt.contractAddress,
                        tx_receipt.transactionHash.hex(),
                    )
                )
                contract.set_address(ledger_api, tx_receipt.contractAddress)
                configuration = Configuration(parameters.version_id, parameters.tx_fee,)
                currency_ids = generate_currency_ids(parameters.nb_currencies, contract)
                configuration.currency_id_to_name = generate_currency_id_to_name(
                    currency_ids
                )
                good_ids = generate_good_ids(parameters.nb_goods, contract)
                configuration.good_id_to_name = generate_good_id_to_name(good_ids)
                configuration.contract_address = tx_receipt.contractAddress
                game.conf = configuration
                game.phase = Phase.CONTRACT_DEPLOYED
        elif game.phase.value == Phase.TOKENS_CREATING.value:
            tx_receipt = ledger_api.get_transaction_receipt(
                tx_digest=game.contract_manager.create_tokens_tx_digest
            )
            if tx_receipt is None:
                self.context.logger.info(
                    "[{}]: Cannot verify whether token creation was successful. Retrying...".format(
                        self.context.agent_name
                    )
                )
            elif tx_receipt.status != 1:
                self.context.is_active = False
                self.context.warning(
                    "[{}]: The token creation wasn't successful. Transaction hash: {}. Aborting!".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
            else:
                self.context.logger.info(
                    "[{}]: Successfully created the tokens. Transaction hash: {}".format(
                        self.context.agent_name, tx_receipt.transactionHash.hex()
                    )
                )
                game.phase = Phase.TOKENS_CREATED
        elif game.phase.value == Phase.TOKENS_MINTING.value:
            for (
                agent_addr,
                tx_digest,
            ) in game.contract_manager.mint_tokens_tx_digests.items():
                if agent_addr in game.contract_manager.confirmed_mint_tokens_agents:
                    continue
                tx_receipt = ledger_api.get_transaction_receipt(tx_digest=tx_digest)
                if tx_receipt is None:
                    self.context.logger.info(
                        "[{}]: Cannot verify whether token minting for agent_addr={} was successful. Retrying...".format(
                            self.context.agent_name, agent_addr
                        )
                    )
                elif tx_receipt.status != 1:
                    self.context.is_active = False
                    self.context.logger.warning(
                        "[{}]: The token minting for agent_addr={} wasn't successful. Transaction hash: {}. Aborting!".format(
                            self.context.agent_name,
                            agent_addr,
                            tx_receipt.transactionHash.hex(),
                        )
                    )
                else:
                    self.context.logger.info(
                        "[{}]: Successfully minted the tokens for agent_addr={}. Transaction hash: {}".format(
                            self.context.agent_name,
                            agent_addr,
                            tx_receipt.transactionHash.hex(),
                        )
                    )
                    game.contract_manager.add_confirmed_mint_tokens_agents(agent_addr)
                    if len(game.contract_manager.confirmed_mint_tokens_agents) == len(
                        game.initial_agent_states
                    ):
                        self.context.logger.info("All tokens minted!")
                        game.phase = Phase.TOKENS_MINTED