Exemple #1
0
 def _set_contract(
     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 the address of the deployed contract")
     contract.set_deployed_instance(
         ledger_api=ledger_api, contract_address=parameters.contract_address,
     )
     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
Exemple #2
0
 def _deploy_contract(
     self, ledger_api: LedgerApi, contract: ERC1155Contract
 ) -> None:
     """Send deploy contract tx msg to decision maker."""
     game = cast(Game, self.context.game)
     game.phase = Phase.CONTRACT_DEPLOYMENT_PROPOSAL
     self.context.logger.info(
         "[{}]: Sending deploy transaction to decision maker.".format(
             self.context.agent_name
         )
     )
     contract.set_instance(ledger_api)
     transaction_message = contract.get_deploy_transaction_msg(
         deployer_address=self.context.agent_address,
         ledger_api=ledger_api,
         skill_callback_id=self.context.skill_id,
     )
     self.context.decision_maker_message_queue.put_nowait(transaction_message)
Exemple #3
0
def generate_currency_ids(nb_currencies: int) -> List[int]:
    """
    Generate currency ids.

    :param nb_currencies: the number of currencies.
    :param contract: the instance of the contract.
    """
    currency_ids = ERC1155Contract.generate_token_ids(FT_ID, nb_currencies)
    assert len(currency_ids) == nb_currencies
    return currency_ids
Exemple #4
0
def generate_good_ids(nb_goods: int) -> List[int]:
    """
    Generate ids for things.

    :param nb_goods: the number of things.
    :param contract: the instance of the contract
    """
    good_ids = ERC1155Contract.generate_token_ids(FT_ID, nb_goods)
    assert len(good_ids) == nb_goods
    return good_ids
Exemple #5
0
def generate_good_ids(nb_goods: int, starting_index: int) -> List[int]:
    """
    Generate ids for things.

    :param nb_goods: the number of things.
    :param starting_index: the index to start creating from.
    """
    good_ids = ERC1155Contract.generate_token_ids(FT_ID, nb_goods, starting_index)
    enforce(
        len(good_ids) == nb_goods, "Length of good ids and number of goods must match."
    )
    return good_ids
Exemple #6
0
def generate_good_ids(nb_goods: int) -> List[int]:
    """
    Generate ids for things.

    :param nb_goods: the number of things.
    :param contract: the instance of the contract
    """
    good_ids = ERC1155Contract.generate_token_ids(FT_ID, nb_goods)
    enforce(
        len(good_ids) == nb_goods,
        "Length of good ids and number of goods must match.")
    return good_ids
Exemple #7
0
def generate_currency_ids(nb_currencies: int) -> List[int]:
    """
    Generate currency ids.

    :param nb_currencies: the number of currencies.
    :param contract: the instance of the contract.
    """
    currency_ids = ERC1155Contract.generate_token_ids(FT_ID, nb_currencies)
    enforce(
        len(currency_ids) == nb_currencies,
        "Length of currency ids and number of currencies must match.",
    )
    return currency_ids
Exemple #8
0
    def __init__(self, **kwargs) -> None:
        """Initialize the strategy of the agent."""
        self._ledger_id = kwargs.pop("ledger_id", DEFAULT_LEDGER_ID)
        self._token_type = kwargs.pop("token_type", DEFAULT_TOKEN_TYPE)
        assert self._token_type in [1,
                                    2], "Token type must be 1 (NFT) or 2 (FT)"
        self._nb_tokens = kwargs.pop("nb_tokens", DEFAULT_NB_TOKENS)
        self._token_ids = kwargs.pop("token_ids", None)
        self._mint_quantities = kwargs.pop("mint_quantities",
                                           DEFAULT_MINT_QUANTITIES)
        assert (len(self._mint_quantities) == self._nb_tokens
                ), "Number of tokens must match mint quantities array size."
        if self._token_type == 1:
            assert all(quantity == 1 for quantity in
                       self._mint_quantities), "NFTs must have a quantity of 1"
        self._contract_address = kwargs.pop("contract_address", None)
        assert (
            self._token_ids is None and self._contract_address is None
        ) or (
            self._token_ids is not None and self._contract_address is not None
        ), "Either provide contract address and token ids or provide neither."

        self.from_supply = kwargs.pop("from_supply", DEFAULT_FROM_SUPPLY)
        self.to_supply = kwargs.pop("to_supply", DEFAULT_TO_SUPPLY)
        self.value = kwargs.pop("value", DEFAULT_VALUE)

        location = kwargs.pop("location", DEFAULT_LOCATION)
        self._agent_location = {
            "location": Location(location["longitude"], location["latitude"])
        }
        self._set_service_data = kwargs.pop("service_data",
                                            DEFAULT_SERVICE_DATA)
        assert (len(self._set_service_data) == 2
                and "key" in self._set_service_data
                and "value" in self._set_service_data
                ), "service_data must contain keys `key` and `value`"
        self._remove_service_data = {"key": self._set_service_data["key"]}
        self._simple_service_data = {
            self._set_service_data["key"]: self._set_service_data["value"]
        }

        super().__init__(**kwargs)

        self.is_behaviour_active = True
        self._is_contract_deployed = self._contract_address is not None
        self._is_tokens_created = self._token_ids is not None
        self._is_tokens_minted = self._token_ids is not None
        if self._token_ids is None:
            self._token_ids = ERC1155Contract.generate_token_ids(
                token_type=self._token_type, nb_tokens=self._nb_tokens)
Exemple #9
0
def generate_currency_ids(nb_currencies: int, starting_index: int) -> List[int]:
    """
    Generate currency ids.

    :param nb_currencies: the number of currencies.
    :param starting_index: the index to start creating from.
    """
    currency_ids = ERC1155Contract.generate_token_ids(
        FT_ID, nb_currencies, starting_index
    )
    enforce(
        len(currency_ids) == nb_currencies,
        "Length of currency ids and number of currencies must match.",
    )
    return currency_ids
Exemple #10
0
 def _get_create_items_tx_msg(
     self,
     configuration: Configuration,
     ledger_api: LedgerApi,
     contract: ERC1155Contract,
 ) -> TransactionMessage:
     token_ids = [
         int(good_id) for good_id in configuration.good_id_to_name.keys()
     ] + [
         int(currency_id) for currency_id in configuration.currency_id_to_name.keys()
     ]
     tx_msg = contract.get_create_batch_transaction_msg(
         deployer_address=self.context.agent_address,
         ledger_api=ledger_api,
         skill_callback_id=self.context.skill_id,
         token_ids=token_ids,
     )
     return tx_msg
Exemple #11
0
 def _get_mint_goods_and_currency_tx_msg(
     self, agent_state: AgentState, ledger_api: LedgerApi, contract: ERC1155Contract,
 ) -> TransactionMessage:
     token_ids = []  # type: List[int]
     mint_quantities = []  # type: List[int]
     for good_id, quantity in agent_state.quantities_by_good_id.items():
         token_ids.append(int(good_id))
         mint_quantities.append(quantity)
     for currency_id, amount in agent_state.amount_by_currency_id.items():
         token_ids.append(int(currency_id))
         mint_quantities.append(amount)
     tx_msg = contract.get_mint_batch_transaction_msg(
         deployer_address=self.context.agent_address,
         recipient_address=agent_state.agent_address,
         mint_quantities=mint_quantities,
         ledger_api=ledger_api,
         skill_callback_id=self.context.skill_id,
         token_ids=token_ids,
     )
     return tx_msg
Exemple #12
0
    def __init__(self, **kwargs) -> None:
        """Initialize the strategy of the agent."""
        self._ledger_id = kwargs.pop("ledger_id", DEFAULT_LEDGER_ID)
        self._token_type = kwargs.pop("token_type", DEFAULT_TOKEN_TYPE)
        assert self._token_type in [1,
                                    2], "Token type must be 1 (NFT) or 2 (FT)"
        self._nb_tokens = kwargs.pop("nb_tokens", DEFAULT_NB_TOKENS)
        self._token_ids = kwargs.pop("token_ids", None)
        self._mint_quantities = kwargs.pop("mint_quantities",
                                           DEFAULT_MINT_QUANTITIES)
        assert (len(self._mint_quantities) == self._nb_tokens
                ), "Number of tokens must match mint quantities array size."
        if self._token_type == 1:
            assert all(quantity == 1 for quantity in
                       self._mint_quantities), "NFTs must have a quantity of 1"
        self._contract_address = kwargs.pop("contract_address", None)
        assert (
            self._token_ids is None and self._contract_address is None
        ) or (
            self._token_ids is not None and self._contract_address is not None
        ), "Either provide contract address and token ids or provide neither."

        self.from_supply = kwargs.pop("from_supply", DEFAULT_FROM_SUPPLY)
        self.to_supply = kwargs.pop("to_supply", DEFAULT_TO_SUPPLY)
        self.value = kwargs.pop("value", DEFAULT_VALUE)

        self._service_data = kwargs.pop("service_data", DEFAULT_SERVICE_DATA)
        self._data_model = kwargs.pop("data_model", DEFAULT_DATA_MODEL)
        self._data_model_name = kwargs.pop("data_model_name",
                                           DEFAULT_DATA_MODEL_NAME)

        super().__init__(**kwargs)

        self.is_behaviour_active = True
        self._is_contract_deployed = self._contract_address is not None
        self._is_tokens_created = self._token_ids is not None
        self._is_tokens_minted = self._token_ids is not None
        if self._token_ids is None:
            self._token_ids = ERC1155Contract.generate_token_ids(
                token_type=self._token_type, nb_tokens=self._nb_tokens)