Пример #1
0
 def _validate_name_and_references(self, name: str) -> None:
     validate_contract_name(name)
     if name not in self.deployment_data:
         raise KeyError(
             f"Contract deployment: {name} not found in deployment data. "
             f"Available deployments include: {list(sorted(self.deployment_data.keys()))}."
         )
    def _validate_name_and_references(self, name: str) -> None:
        validate_contract_name(name)

        if name not in self.deployment_data:
            raise KeyError(
                "Contract name not found in deployment data. "
                f"Available deployments include: {list(sorted(self.deployment_data.keys()))}."
            )

        contract_type = self.deployment_data[name]["contract_type"]
        if contract_type not in self.contract_factories:
            raise EthPMValidationError(
                f"Contract type: {contract_type} for alias: {name} not found. "
                f"Available contract types include: {list(sorted(self.contract_factories.keys()))}."
            )
Пример #3
0
    def get_contract_factory(self, name: ContractName) -> LinkableContract:
        """
        Return the contract factory for a given contract type, generated from the data vailable
        in ``Package.manifest``. Contract factories are accessible from the package class.

        .. code:: python

           Owned = OwnedPackage.get_contract_factory('owned')

        In cases where a contract uses a library, the contract factory will have
        unlinked bytecode. The ``ethpm`` package ships with its own subclass of
        ``web3.contract.Contract``, ``ethpm.contract.LinkableContract`` with a few extra
        methods and properties related to bytecode linking.

        .. code:: python

           >>> math = owned_package.contract_factories.math
           >>> math.needs_bytecode_linking
           True
           >>> linked_math = math.link_bytecode({'MathLib': '0x1234...'})
           >>> linked_math.needs_bytecode_linking
           False
        """
        validate_contract_name(name)

        if "contractTypes" not in self.manifest:
            raise InsufficientAssetsError(
                "This package does not contain any contract type data."
            )

        try:
            contract_data = self.manifest["contractTypes"][name]
        except KeyError:
            raise InsufficientAssetsError(
                "This package does not contain any package data to generate "
                f"a contract factory for contract type: {name}. Available contract types include: "
                f"{self.contract_types}."
            )

        validate_minimal_contract_factory_data(contract_data)
        contract_kwargs = generate_contract_factory_kwargs(contract_data)
        contract_factory = self.w3.eth.contract(**contract_kwargs)
        return contract_factory
Пример #4
0
 def get_contract_instance(self, name: ContractName,
                           address: Address) -> Contract:
     """
     Will return a ``Web3.contract`` instance generated from the contract type data available
     in ``Package.manifest`` and the provided ``address``. The provided ``address`` must be
     valid on the connected chain available through ``Package.w3``.
     """
     validate_address(address)
     validate_contract_name(name)
     try:
         self.manifest["contract_types"][name]["abi"]
     except KeyError:
         raise InsufficientAssetsError(
             "Package does not have the ABI required to generate a contract instance "
             f"for contract: {name} at address: {address}.")
     contract_kwargs = generate_contract_factory_kwargs(
         self.manifest["contract_types"][name])
     contract_instance = self.w3.eth.contract(address=address,
                                              **contract_kwargs)
     return contract_instance
Пример #5
0
def test_validate_contract_name_invalidates(name):
    with pytest.raises(ValidationError):
        assert validate_contract_name(name)
Пример #6
0
def test_validate_contract_name_validates(name):
    assert validate_contract_name(name) is None